bug 608 Database migration recovering with docker relaunch
[osm/openvim.git] / test / lib / ssh.py
1 import subprocess
2 import pexpect
3
4
5 def execute_local(cmd):
6 """
7 Execute a command in locally
8 :param cmd: command to be executed
9 :return:
10 """
11
12 print "execute_local cmd = {}".format(cmd)
13 p = subprocess.Popen(['bash', "-c", cmd], stdout=subprocess.PIPE)
14 out = p.stdout.read()
15 print "execute_local result = {}".format(out)
16 return out
17
18
19 def execute_namespace(vlan, cmd):
20 """
21 Execute a command inside a namespace created by openvim
22 :param vlan: namespace id
23 :param cmd: command to be executed
24 :return:
25 """
26
27 n_cmd = 'sudo ip netns exec {}-qrouter {} '.format(vlan, cmd)
28 return execute_local(n_cmd)
29
30
31 def ping_ok(vlan_id, ip, retries=200):
32 """
33
34 :param vlan_id: namespace id
35 :param ip: vm ip to be pinged
36 :param retries: retries, by default 200
37 :return:
38 """
39 print 'waiting for vm to be active vm with ip = {}'.format(ip)
40 for i in xrange(retries):
41 try:
42 subprocess.check_output(['bash', "-c", "sudo ip netns exec {}-qrouter ping -c 1 {}".format(vlan_id, ip)])
43 return True
44 except Exception, e:
45 pass
46 return False
47
48
49 def ping_ok_btw_2_vms(vlan_id, ip_1, ip_2, retires=8):
50 """
51 Check net connectivity between to VM
52 :param vlan_id: namepsace id
53 :param ip_1: first vm ip
54 :param ip_2: second vm ip
55 :param retires:
56 :return:
57 """
58 for i in xrange(retires):
59
60 try:
61 ns_cmd = 'sudo ip netns exec {}-qrouter '.format(vlan_id)
62 cmd = ns_cmd + ' ssh -oStrictHostKeyChecking=no cirros@{} "ping -c 1 {}"'.format(ip_1, ip_2)
63 child = pexpect.spawn(cmd)
64 child.expect('.*assword*')
65 child.sendline('cubswin:)')
66 child.sendline('cubswin:)')
67
68 cmd = ns_cmd + ' ssh -oStrictHostKeyChecking=no cirros@{} "ping -c 1 {}"'.format(ip_2, ip_1)
69 child = pexpect.spawn(cmd)
70 child.expect('.*assword*')
71 child.sendline('cubswin:)')
72 child.sendline('cubswin:)')
73
74 except EOFError as e:
75 if i == retires:
76 return False
77 pass
78
79 return True
80
81
82 def copy_rsa_keys_into_vm(vlan_id, ip, rsa_key_path):
83 """
84 copy an RSA key given by the user to a vm
85 :param vlan_id:
86 :param ip:
87 :param rsa_key_path:
88 :return:
89 """
90
91 try:
92 execute_local('sudo ssh-keygen -f "/root/.ssh/known_hosts" -R {}'.format(ip))
93 cmd = 'sudo ip netns exec {}-qrouter ssh-copy-id -i {} ' \
94 '-oStrictHostKeyChecking=no -f cirros@{}'.format(vlan_id, rsa_key_path + '.pub', ip)
95
96 print 'copy_rsa_keys_into_vm = ' + cmd
97 child = pexpect.spawn(cmd)
98 child.expect('.*assword*')
99 child.sendline('cubswin:)')
100 child.sendline('cubswin:)')
101 return True
102 except EOFError as e:
103 return False
104
105
106 def execute_check_output(vlan_id, cmd):
107 """
108 Execute a command inside a namespace and raise an expection in case of command fail
109 :param vlan_id: namepsace id
110 :param cmd: command
111 :return:
112 """
113 try:
114 cmd = "sudo ip netns exec {}-qrouter {}".format(vlan_id, cmd)
115 print "execute_check_output = {}".format(cmd)
116 subprocess.check_output(['bash', "-c", cmd])
117 return True
118 except Exception, e:
119 print "error execute_check_output"
120 return False
121
122
123
124
125
126