Skip to content

Commit

Permalink
Fix for scp cases in sanity
Browse files Browse the repository at this point in the history
    1. currently fab method fab_put_file_to_vm is used
	to copy key to VM which infact fails. Enhance the
	method copy_file_to_vm which can copy from remote
	to VM now and use this to copy keys from config to VM.
    2. if ssh key is not available on config node, generate it.
    3. copy only private key on source VM
    4. copy only public key on destination VM

    Closes-bug: #1758304

Change-Id: I0a82f72a7ad34abf70fcbb7ae06c9128e176b1e9
  • Loading branch information
alokkumar223 committed Mar 27, 2018
1 parent 1a9bdb7 commit d849d47
Showing 1 changed file with 77 additions and 34 deletions.
111 changes: 77 additions & 34 deletions fixtures/vm_test.py
Expand Up @@ -1709,7 +1709,7 @@ def verify_vm_not_in_control_nodes(self):
ri_name = vn_fq_name + ':' + vn_fq_name.split(':')[-1]
for cn in self.get_ctrl_nodes_in_rt_group(vn_fq_name):
# Check for VM route in each control-node
for vm_ip in self.vm_ip_dict[vn_fq_name]:
for vm_ip in self.get_vm_ip_dict()[vn_fq_name]:
cn_routes = self.cn_inspect[cn].get_cn_route_table_entry(
ri_name=ri_name, prefix=vm_ip)
if cn_routes is not None:
Expand Down Expand Up @@ -2071,22 +2071,34 @@ def scp_file_to_vm(self, file, vm_ip, dest_vm_username='ubuntu'):
# end scp_file_to_vm

def put_pub_key_to_vm(self):
'''
Copy pub key from cfgm node to VM
'''
fab_connections.clear()
self.logger.debug('Copying public key to VM %s' % (self.vm_name))
self.orch.put_key_file_to_host(self.vm_node_ip)
auth_file = '.ssh/authorized_keys'
file_name = 'id_rsa.pub'
pub_key = '~/.ssh/%s' % (file_name)
self.run_cmd_on_vm(['mkdir -p ~/.ssh'])
host = self.inputs.host_data[self.vm_node_ip]
with hide('everything'):
with settings(
host_string='%s@%s' % (host['username'], self.vm_node_ip),
password=host['password'],
warn_only=True, abort_on_prompts=False):
fab_put_file_to_vm(host_string='%s@%s' % (
self.vm_username, self.local_ip),
password=self.vm_password,
src='/tmp/id_rsa.pub', dest='/tmp/',
logger=self.logger)
host = self.inputs.host_data[self.inputs.cfgm_ip]
host_string = '%s@%s' % (host['username'], self.inputs.cfgm_ip)
source_file = '%s:%s' % (host_string, pub_key)
try:
self.copy_file_to_vm(source_file, dstdir='/tmp/',
src_password=host['password'])

except IOError as e:
if "Source not found" in str(e):
key_tmp = '/tmp/%s' % (file_name)
source_file = '%s:%s' % (host_string, key_tmp)
self.copy_file_to_vm(source_file, dstdir='/tmp/',
src_password=host['password'])

except Exception, e:
self.logger.exception(
'Exception occured while trying to get the pub key file to the \
VM from config node')

cmds = [
'cat /tmp/id_rsa.pub >> ~/%s' % (auth_file),
'chmod 600 ~/%s' % (auth_file),
Expand Down Expand Up @@ -2156,31 +2168,51 @@ def check_file_transfer(self, dest_vm_fixture, dest_vn_fq_name=None, mode='scp',
# end check_file_transfer

def get_rsa_to_vm(self):
'''Get the rsa file to the VM from the agent
'''Get the rsa file to the VM from cfgm
'''
host = self.inputs.host_data[self.vm_node_ip]
file_name = 'id_rsa'
ssh_dir = '~/.ssh'
key_file = '%s/%s' % (ssh_dir, file_name)
host = self.inputs.host_data[self.inputs.cfgm_ip]
host_string = '%s@%s' % (host['username'], self.inputs.cfgm_ip)
source_file = '%s:%s' % (host_string, key_file)
dst_vm = '%s/' % (ssh_dir)
output = ''
exception_msg = 'Exception occured while trying to get the rsa file' +\
'to the VM from config node'
self.run_cmd_on_vm(['mkdir -p %s' % (ssh_dir)], as_sudo=True)
try:
self.orch.put_key_file_to_host(self.vm_node_ip)
with hide('everything'):
with settings(
host_string='%s@%s' % (
host['username'], self.vm_node_ip),
password=host['password'],
warn_only=True, abort_on_prompts=False):
key_file = self.orch.get_key_file()
fab_put_file_to_vm(host_string='%s@%s' % (
self.vm_username, self.local_ip),
password=self.vm_password,
src=key_file, dest='~/',
logger=self.logger)
self.run_cmd_on_vm(cmds=['chmod 600 id_rsa'])
self.copy_file_to_vm(source_file, dstdir=dst_vm,
src_password=host['password'])

except IOError as e:
if "Source not found" in str(e):
#Try from /tmp
key_tmp = '/tmp/%s' % (file_name)
source_file = '%s:%s' % (host_string, key_tmp)
try:
self.copy_file_to_vm(source_file, dstdir=dst_vm,
src_password=host['password'])
except IOError as e:
if "Source not found" in str(e):
#Generate ssh keys on config node in /tmp and copy to VM
cmd = 'ssh-keygen -f %s -t rsa -N \'\';ls -l %s' % (key_tmp,
key_tmp)
output = remote_cmd(host_string, cmd, with_sudo=True,
password=host['password'], logger=self.logger)
if "No such file or directory" in output:
self.logger.exception('ssh key generation failed')
else:
self.copy_file_to_vm(source_file, dstdir=dst_vm,
src_password=host['password'])
except Exception, e:
self.logger.exception(exception_msg)

except Exception, e:
self.logger.exception(
'Exception occured while trying to get the rsa file to the \
VM from the agent')
self.logger.exception(exception_msg)

self.run_cmd_on_vm(cmds=['chmod 600 %s' % (key_file)])
# end get_rsa_to_vm

def run_cmd_on_vm(self, cmds=[], as_sudo=False, timeout=30,
Expand Down Expand Up @@ -2489,7 +2521,17 @@ def wait_for_ssh_on_vm(self, port='22'):
return False
# end wait_for_ssh_on_vm

def copy_file_to_vm(self, localfile, dstdir=None, force=False):
def copy_file_to_vm(self, localfile, dstdir=None, force=False,
src_password=None):
'''
1. Can copy files from test location(test docker) to VM
2. Can copy files from remote system to VM
Args:
localfile: source file path or in format user@host:file_abs_path
dstdir: dst path on VM
src_password: source system password,required when
copying from remote system
'''
host = self.inputs.get_host_ip(self.vm_node_ip)
# filename = localfile.split('/')[-1]
# if dstdir:
Expand All @@ -2509,7 +2551,8 @@ def copy_file_to_vm(self, localfile, dstdir=None, force=False):
dest_gw_ip = self.vm_node_ip
dest_gw_login = "%s@%s" % (dest_gw_username,dest_gw_ip)
remote_copy(localfile, dstdir, dest_password=self.vm_password,
dest_gw=dest_gw_login, dest_gw_password=dest_gw_password)
dest_gw=dest_gw_login, dest_gw_password=dest_gw_password,
src_password=src_password)
# end copy_file_to_vm

def get_vm_ipv6_addr_from_vm(self, intf='eth0', addr_type='link'):
Expand Down

0 comments on commit d849d47

Please sign in to comment.