5 def execute_local(cmd
):
7 Execute a command in locally
8 :param cmd: command to be executed
12 print "execute_local cmd = {}".format(cmd
)
13 p
= subprocess
.Popen(['bash', "-c", cmd
], stdout
=subprocess
.PIPE
)
15 print "execute_local result = {}".format(out
)
19 def execute_namespace(vlan
, cmd
):
21 Execute a command inside a namespace created by openvim
22 :param vlan: namespace id
23 :param cmd: command to be executed
27 n_cmd
= 'sudo ip netns exec {}-qrouter {} '.format(vlan
, cmd
)
28 return execute_local(n_cmd
)
31 def ping_ok(vlan_id
, ip
, retries
=200):
34 :param vlan_id: namespace id
35 :param ip: vm ip to be pinged
36 :param retries: retries, by default 200
39 print 'waiting for vm to be active vm with ip = {}'.format(ip
)
40 for i
in xrange(retries
):
42 subprocess
.check_output(['bash', "-c", "sudo ip netns exec {}-qrouter ping -c 1 {}".format(vlan_id
, ip
)])
49 def ping_ok_btw_2_vms(vlan_id
, ip_1
, ip_2
, retires
=8):
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
58 for i
in xrange(retires
):
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:)')
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:)')
82 def copy_rsa_keys_into_vm(vlan_id
, ip
, rsa_key_path
):
84 copy an RSA key given by the user to a vm
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
)
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:)')
102 except EOFError as e
:
106 def execute_check_output(vlan_id
, cmd
):
108 Execute a command inside a namespace and raise an expection in case of command fail
109 :param vlan_id: namepsace id
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
])
119 print "error execute_check_output"