155
158
final_path = path_one + '/' + path_two
156
159
return final_path
162
@contextlib.contextmanager
163
def vios_to_vios_auth(source, dest, conn_info):
164
"""Context allowing for SSH between VIOS partitions
166
This context will build an SSH key on the source host, put the key
167
into the authorized_keys on the destination host, and make the
168
private key file name available within the context.
169
The key files and key inserted into authorized_keys will be
170
removed when the context exits.
172
:param source: source IP or DNS name
173
:param dest: destination IP or DNS name
174
:param conn_info: dictionary object with SSH connection
175
information for both hosts
177
KEY_BASE_NAME = "os-%s" % uuid.uuid4().hex
178
keypair_uuid = uuid.uuid4()
179
src_conn_obj = ssh_connect(conn_info)
181
dest_conn_info = Connection(dest, conn_info.username,
183
dest_conn_obj = ssh_connect(dest_conn_info)
185
def run_command(conn_obj, cmd):
186
stdout, stderr = utils.ssh_execute(conn_obj, cmd)
187
return stdout.strip().splitlines()
189
def build_keypair_on_source():
190
mkkey = ('ssh-keygen -f %s -N "" -C %s' %
191
(KEY_BASE_NAME, keypair_uuid.hex))
192
ssh_command_as_root(src_conn_obj, mkkey)
194
chown_key = ('chown %s %s*' % (conn_info.username, KEY_BASE_NAME))
195
ssh_command_as_root(src_conn_obj, chown_key)
197
cat_key = ('cat %s.pub' % KEY_BASE_NAME)
198
pubkey = run_command(src_conn_obj, cat_key)
202
def cleanup_key_on_source():
203
rmkey = 'rm %s*' % KEY_BASE_NAME
204
run_command(src_conn_obj, rmkey)
206
def insert_into_authorized_keys(public_key):
207
echo_key = 'echo "%s" >> .ssh/authorized_keys' % public_key
208
ssh_command_as_root(dest_conn_obj, echo_key)
210
def remove_from_authorized_keys():
211
rmkey = ('sed /%s/d .ssh/authorized_keys > .ssh/authorized_keys' %
213
ssh_command_as_root(dest_conn_obj, rmkey)
215
public_key = build_keypair_on_source()
216
insert_into_authorized_keys(public_key)
221
remove_from_authorized_keys()
222
cleanup_key_on_source()