/
contrail_test_init.py
executable file
·1044 lines (962 loc) · 42 KB
/
contrail_test_init.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import os
import re
import sys
import json
import time
import socket
import getpass
import ConfigParser
import ast
from netaddr import *
import fixtures
from fabric.api import env, run, local
from fabric.operations import get, put, reboot
from fabric.context_managers import settings, hide
from fabric.exceptions import NetworkError
from fabric.contrib.files import exists
from tcutils.util import *
from tcutils.util import custom_dict, read_config_option, get_build_sku
from tcutils.custom_filehandler import *
from tcutils.config.vnc_introspect_utils import VNCApiInspect
from tcutils.config.ds_introspect_utils import VerificationDsSrv
from keystone_tests import KeystoneCommands
from tempfile import NamedTemporaryFile
import re
import subprocess
import ast
from collections import namedtuple
# monkey patch subprocess.check_output cos its not supported in 2.6
if "check_output" not in dir(subprocess): # duck punch it in!
def f(*popenargs, **kwargs):
if 'stdout' in kwargs:
raise ValueError(
'stdout argument not allowed, it will be overridden.')
process = subprocess.Popen(
stdout=subprocess.PIPE,
*popenargs,
**kwargs)
output, unused_err = process.communicate()
retcode = process.poll()
if retcode:
cmd = kwargs.get("args")
if cmd is None:
cmd = popenargs[0]
raise subprocess.CalledProcessError(retcode, cmd)
return output
subprocess.check_output = f
class TestInputs(object):
'''
Class that would populate testbedinfo from parsing the
.ini and .json input files if provided (or)
check the keystone and discovery servers to populate
the same with the certain default value assumptions
'''
__metaclass__ = Singleton
def __init__(self, ini_file=None):
self.api_server_port = '8082'
self.bgp_port = '8083'
self.ds_port = '5998'
self.jenkins_trigger = self.get_os_env('JENKINS_TRIGGERED')
self.os_type = custom_dict(self.get_os_version, 'os_type')
self.config = None
if ini_file:
self.config = ConfigParser.ConfigParser()
self.config.read(ini_file)
self.orchestrator = read_config_option(self.config,
'Basic', 'orchestrator', 'openstack')
self.prov_file = read_config_option(self.config,
'Basic', 'provFile', None)
self.key = read_config_option(self.config,
'Basic', 'key', 'key1')
self.tenant_isolation = read_config_option(self.config,
'Basic',
'tenant_isolation',
True)
# Read admin credentials if any
self.admin_username = read_config_option(self.config,
'Basic',
'adminUser',
os.getenv('OS_USERNAME', None))
self.admin_password = read_config_option(self.config,
'Basic',
'adminPassword',
os.getenv('OS_PASSWORD', None))
self.admin_tenant = read_config_option(self.config,
'Basic',
'adminTenant',
os.getenv('OS_TENANT_NAME', None))
self.stack_user = read_config_option(
self.config,
'Basic',
'stackUser',
self.admin_username)
self.stack_password = read_config_option(
self.config,
'Basic',
'stackPassword',
self.admin_password)
self.stack_tenant = read_config_option(
self.config,
'Basic',
'stackTenant',
self.admin_tenant)
self.stack_domain = read_config_option(
self.config,
'Basic',
'stackDomain',
os.getenv('OS_DOMAIN_NAME', 'default-domain'))
self.endpoint_type = read_config_option(
self.config,
'Basic',
'endpoint_type',
'publicURL')
self.auth_ip = read_config_option(self.config,
'Basic', 'auth_ip', None)
self.auth_port = read_config_option(self.config,
'Basic', 'auth_port', None)
self.multi_tenancy = read_config_option(self.config,
'Basic', 'multiTenancy', False)
self.enable_ceilometer = read_config_option(self.config,
'Basic', 'enable_ceilometer', False)
self.fixture_cleanup = read_config_option(
self.config,
'Basic',
'fixtureCleanup',
'yes')
self.http_proxy = read_config_option(self.config,
'proxy', 'proxy_url', None)
self.ui_config = read_config_option(self.config,
'ui', 'ui_config', None)
self.ui_browser = read_config_option(self.config,
'ui', 'ui_browser', None)
self.verify_webui = read_config_option(self.config,
'ui', 'webui', False)
self.verify_horizon = read_config_option(self.config,
'ui', 'horizon', False)
if not self.ui_browser and (self.verify_webui or self.verify_horizon):
raise ValueError(
"Verification via GUI needs 'browser' details. Please set the same.")
self.devstack = read_config_option(self.config,
'devstack', 'devstack', None)
self.use_devicemanager_for_md5 = read_config_option(
self.config, 'use_devicemanager_for_md5', 'use_devicemanager_for_md5', False)
# router options
self.mx_rt = read_config_option(self.config,
'router', 'route_target', '10003')
self.router_asn = read_config_option(self.config,
'router', 'asn', '64512')
router_info_tuples_string = read_config_option(
self.config,
'router',
'router_info',
'[]')
self.ext_routers = ast.literal_eval(router_info_tuples_string)
self.fip_pool_name = read_config_option(
self.config,
'router',
'fip_pool_name',
'public-pool')
self.fip_pool = read_config_option(self.config,
'router', 'fip_pool', None)
if self.fip_pool:
update_reserve_cidr(self.fip_pool)
self.public_vn = read_config_option(
self.config,
'router',
'public_virtual_network',
'public-network')
self.public_tenant = read_config_option(
self.config,
'router',
'public_tenant_name',
'public-tenant')
# HA setup IPMI username/password
self.ha_setup = read_config_option(self.config, 'HA', 'ha_setup', None)
if self.ha_setup == True:
self.ipmi_username = read_config_option(
self.config,
'HA',
'ipmi_username',
'ADMIN')
self.ipmi_password = read_config_option(
self.config,
'HA',
'ipmi_password',
'ADMIN')
# debug option
self.verify_on_setup = read_config_option(
self.config,
'debug',
'verify_on_setup',
True)
self.stop_on_fail = bool(
read_config_option(
self.config,
'debug',
'stop_on_fail',
None))
self.ha_tmp_list = []
self.tor_agent_data = {}
self.sriov_data = {}
self.mysql_token = None
self.public_host = read_config_option(self.config, 'Basic',
'public_host', '10.204.216.50')
self.prov_file = self.prov_file or self._create_prov_file()
self.prov_data = self.read_prov_file()
#vcenter server
self.vcenter_dc = read_config_option(
self.config, 'vcenter', 'vcenter_dc', None)
self.vcenter_server = read_config_option(
self.config, 'vcenter', 'vcenter_server', None)
self.vcenter_port = read_config_option(
self.config, 'vcenter', 'vcenter_port', None)
self.vcenter_username = read_config_option(
self.config, 'vcenter', 'vcenter_username', None)
self.vcenter_password = read_config_option(
self.config, 'vcenter', 'vcenter_password', None)
self.vcenter_compute = read_config_option(
self.config, 'vcenter', 'vcenter_compute', None)
try:
if 'vcenter_servers' in self.prov_data.keys():
for server in self.prov_data['vcenter_servers']:
for k in server.keys():
self.dv_switch = server[k]['dv_switch']['dv_switch_name']
elif 'vcenter' in self.prov_data.keys():
self.dv_switch = self.prov_data['vcenter'][0]['dv_switch']['dv_switch_name']
except Exception as e:
pass
if self.ha_setup == True:
self.update_etc_hosts_for_vip()
self.username = self.host_data[self.cfgm_ip]['username']
self.password = self.host_data[self.cfgm_ip]['password']
# List of service correspond to each module
self.compute_services = [
'contrail-vrouter-agent',
'supervisor-vrouter',
'contrail-vrouter-nodemgr']
self.control_services = ['contrail-control', 'supervisor-control',
'contrail-control-nodemgr', 'contrail-dns',
'contrail-named']
self.cfgm_services = [
'contrail-api',
'contrail-schema',
'contrail-discovery',
'supervisor-config',
'contrail-config-nodemgr',
'contrail-device-manager']
self.webui_services = ['contrail-webui', 'contrail-webui-middleware',
'supervisor-webui']
self.openstack_services = [
'openstack-cinder-api', 'openstack-cinder-scheduler',
'openstack-cinder-scheduler', 'openstack-glance-api',
'openstack-glance-registry', 'openstack-keystone',
'openstack-nova-api', 'openstack-nova-scheduler',
'openstack-nova-cert']
self.collector_services = [
'contrail-collector', 'contrail-analytics-api',
'contrail-query-engine', 'contrail-analytics-nodemgr',
'supervisor-analytics',
'contrail-snmp-collector', 'contrail-topology']
self.correct_states = ['active', 'backup']
def get_os_env(self, var, default=''):
if var in os.environ:
return os.environ.get(var)
else:
return default
# end get_os_env
def get_os_version(self, host_ip):
'''
Figure out the os type on each node in the cluster
'''
if host_ip in self.os_type:
return self.os_type[host_ip]
username = self.host_data[host_ip]['username']
password = self.host_data[host_ip]['password']
with settings(host_string='%s@%s' % (username, host_ip),
password=password, warn_only=True,
abort_on_prompts=False):
output = run('uname -a')
if 'el6' in output:
self.os_type[host_ip] = 'centos_el6'
elif 'fc17' in output:
self.os_type[host_ip] = 'fc17'
elif 'xen' in output:
self.os_type[host_ip] = 'xenserver'
elif 'Ubuntu' in output:
self.os_type[host_ip] = 'ubuntu'
elif 'el7' in output:
self.os_type[host_ip] = 'redhat'
else:
raise KeyError('Unsupported OS')
return self.os_type[host_ip]
# end get_os_version
def read_prov_file(self):
prov_file = open(self.prov_file, 'r')
prov_data = prov_file.read()
#json_data = json.loads(prov_data)
json_data = ast.literal_eval(prov_data)
self.host_names = []
self.cfgm_ip = ''
self.cfgm_ips = []
self.cfgm_control_ips = []
self.cfgm_names = []
self.collector_ips = []
self.collector_control_ips = []
self.collector_names = []
self.database_ips = []
self.database_names = []
self.database_control_ips = []
self.compute_ips = []
self.compute_names = []
self.compute_control_ips = []
self.compute_info = {}
self.bgp_ips = []
self.bgp_control_ips = []
self.bgp_names = []
self.ds_server_ip = []
self.ds_server_name = []
self.host_ips = []
self.webui_ips = []
self.host_data = {}
self.tor = {}
self.tor_hosts_data = {}
self.physical_routers_data = {}
self.esxi_vm_ips = {}
self.vgw_data = {}
self.vip = {}
for host in json_data['hosts']:
host['name'] = host['name']
self.host_names.append(host['name'])
host_ip = str(IPNetwork(host['ip']).ip)
host_data_ip = str(IPNetwork(host['data-ip']).ip)
host_control_ip = str(IPNetwork(host['control-ip']).ip)
self.host_ips.append(host_ip)
self.host_data[host_ip] = host
self.host_data[host_data_ip] = host
self.host_data[host_control_ip] = host
self.host_data[host['name']] = host
self.host_data[host['name']]['host_ip'] = host_ip
self.host_data[host['name']]['host_data_ip'] = host_data_ip
self.host_data[host['name']]['host_control_ip'] = host_control_ip
roles = host["roles"]
for role in roles:
if role['type'] == 'openstack':
if self.auth_ip:
if self.ha_setup == True:
self.openstack_ip = host_ip
else:
self.openstack_ip = self.auth_ip
else:
self.openstack_ip = host_ip
self.auth_ip = host_ip
if role['type'] == 'cfgm':
self.cfgm_ip = host_ip
self.cfgm_ips.append(host_ip)
self.cfgm_control_ips.append(host_control_ip)
self.cfgm_control_ip = host_control_ip
self.cfgm_names.append(host['name'])
self.ds_server_ip.append(host_ip)
self.ds_server_name.append(host['name'])
self.masterhost = self.cfgm_ip
self.hostname = host['name']
if role['type'] == 'compute':
self.compute_ips.append(host_ip)
self.compute_names.append(host['name'])
self.compute_info[host['name']] = host_ip
self.compute_control_ips.append(host_control_ip)
if role['type'] == 'bgp':
self.bgp_ips.append(host_ip)
self.bgp_control_ips.append(host_control_ip)
self.bgp_names.append(host['name'])
# if role['type'] == 'collector' :
# self.collector_ip= host_ip
if role['type'] == 'webui':
self.webui_ip = host_ip
self.webui_ips.append(host_ip)
if role['type'] == 'collector':
self.collector_ip = host_ip
self.collector_ips.append(host_ip)
self.collector_control_ips.append(host_control_ip)
self.collector_names.append(host['name'])
if role['type'] == 'database':
self.database_ip = host_ip
self.database_ips.append(host_ip)
self.database_names.append(host['name'])
self.database_control_ips.append(host_control_ip)
# end for
# end for
if self.ha_setup == True:
self.vip['keystone'] = self.auth_ip
self.vip['contrail'] = self.auth_ip
if 'vgw' in json_data:
self.vgw_data = json_data['vgw']
if 'tor_agent' in json_data:
self.tor_agent_data = json_data['tor_agent']
if 'sriov' in json_data:
self.sriov_data = json_data['sriov']
if 'tor_hosts' in json_data:
self.tor_hosts_data = json_data['tor_hosts']
if 'physical_routers' in json_data:
self.physical_routers_data = json_data['physical_routers']
self._process_tor_data()
if 'esxi_vms' in json_data:
self.esxi_vm_ips = json_data['esxi_vms']
if 'hosts_ipmi' in json_data:
self.hosts_ipmi = json_data['hosts_ipmi']
return json_data
# end read_prov_file
def _process_tor_data(self):
for (device_name, device_dict) in self.physical_routers_data.iteritems():
device_dict['tor_agents'] = []
device_dict['tor_agent_dicts'] = []
device_dict['tor_tsn_ips'] = []
for (host_str, ta_list) in self.tor_agent_data.iteritems():
for ta in ta_list:
if ta['tor_name'] == device_dict['name']:
ta['tor_agent_host_string'] = host_str
device_dict['tor_ovs_port'] = ta['tor_ovs_port']
device_dict['tor_ovs_protocol'] = ta[
'tor_ovs_protocol']
device_dict['tor_agents'].append('%s:%s' % (host_str,
ta['tor_agent_id']))
device_dict['tor_agent_dicts'].append(ta)
device_dict['tor_tsn_ips'].append(ta['tor_tsn_ip'])
if self.ha_setup == True:
device_dict['controller_ip'] = self.vip['contrail']
else:
device_dict['controller_ip'] = ta['tor_tsn_ip']
# end _process_tor_data
def get_host_ip(self, name):
ip = self.host_data[name]['host_ip']
if ip in self.ha_tmp_list:
ip = self.vip['contrail']
return ip
def get_host_data_ip(self, name):
ip = self.host_data[name]['host_data_ip']
if ip in self.ha_tmp_list:
ip = self.vip['contrail']
return ip
def get_node_name(self, ip):
return self.host_data[ip]['name']
def update_etc_hosts_for_vip(self):
contrail_vip_name = "contrail-vip"
for host in self.host_ips:
cmd = 'if ! grep -Rq "contrail-vip" /etc/hosts; then echo "%s %s" >> /etc/hosts; fi' % (
self.vip['contrail'], contrail_vip_name)
self.run_cmd_on_server(host, cmd)
if self.vip['contrail'] != self.vip['keystone']:
keystone_vip_name = "keystone-vip"
cmd = 'echo "%s %s" >> /etc/hosts' % (
self.vip['keystone'], keystone_vip_name)
self.run_cmd_on_server(host, cmd)
def get_computes(self, cfgm_ip):
kwargs = {'stack_user': self.stack_user,
'stack_password': self.stack_password,
'project_name': self.stack_tenant,
'openstack_ip': self.auth_ip}
api_h = VNCApiInspect(cfgm_ip, args=type('', (), kwargs))
return api_h.get_computes()
def _create_prov_file(self):
''' Creates json data for a single node only.
Optional Env variables:
openstack creds:
* OS_USERNAME (default: admin)
* OS_PASSWORD (default: contrail123)
* OS_TENANT_NAME (default: admin)
* OS_DOMAIN_NAME (default: default-domain)
* OS_AUTH_URL (default: http://127.0.0.1:5000/v2.0)
* OS_INSECURE (default: True)
login creds:
* USERNAME (default: root)
* PASSWORD (default: c0ntrail123)
contrail service:
* DISCOVERY_IP (default: neutron-server ip fetched from keystone endpoint)
'''
pattern = 'http[s]?://(?P<ip>\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}):(?P<port>\d+)'
if self.orchestrator.lower() != 'openstack':
raise Exception('Please specify testbed info in $PARAMS_FILE '
'under "Basic" section, keyword "provFile"')
if self.orchestrator.lower() == 'openstack':
auth_url = os.getenv('OS_AUTH_URL', None) or \
'http://127.0.0.1:5000/v2.0'
insecure = bool(os.getenv('OS_INSECURE', True))
keystone = KeystoneCommands(self.stack_user,
self.stack_password,
self.stack_tenant,
auth_url,
insecure=insecure)
match = re.match(pattern, keystone.get_endpoint('identity')[0])
self.auth_ip = match.group('ip')
self.auth_port = match.group('port')
# Assume contrail-config runs in the same node as neutron-server
discovery = os.getenv('DISCOVERY_IP', None) or \
(keystone and re.match(pattern,
keystone.get_endpoint('network')[0]).group('ip'))
ds_client = VerificationDsSrv(discovery)
services = ds_client.get_ds_services().info
cfgm = database = services['config']
collector = services['analytics']
bgp = services['control-node']
openstack = [self.auth_ip] if self.auth_ip else []
computes = self.get_computes(cfgm[0])
data = {'hosts': list()}
hosts = cfgm + database + collector + bgp + computes + openstack
username = os.getenv('USERNAME', 'root')
password = os.getenv('PASSWORD', 'c0ntrail123')
for host in set(hosts):
with settings(host_string='%s@%s' % (username, host),
password=password, warn_only=True):
hname = run('hostname')
hdict = {'ip': host,
'data-ip': host,
'control-ip': host,
'username': username,
'password': password,
'name': hname,
'roles': [],
}
if host in cfgm:
hdict['roles'].append({'type': 'cfgm'})
if host in collector:
hdict['roles'].append({'type': 'collector'})
if host in database:
hdict['roles'].append({'type': 'database'})
if host in bgp:
hdict['roles'].append({'type': 'bgp'})
if host in computes:
hdict['roles'].append({'type': 'compute'})
if host in openstack:
hdict['roles'].append({'type': 'openstack'})
data['hosts'].append(hdict)
tempfile = NamedTemporaryFile(delete=False)
with open(tempfile.name, 'w') as fd:
json.dump(data, fd)
return tempfile.name
# end _create_prov_data
def get_mysql_token(self):
if self.mysql_token:
return self.mysql_token
if self.orchestrator == 'vcenter':
return None
if self.devstack:
return 'contrail123'
username = self.host_data[self.openstack_ip]['username']
password = self.host_data[self.openstack_ip]['password']
cmd = 'cat /etc/contrail/mysql.token'
with hide('everything'):
with settings(
host_string='%s@%s' % (username, self.openstack_ip),
password=password, warn_only=True, abort_on_prompts=False):
if not exists('/etc/contrail/mysql.token'):
return None
self.mysql_token = self.run_cmd_on_server(
self.openstack_ip,
cmd,
username,
password)
return self.mysql_token
# end get_mysql_token
def get_build_sku(self):
return get_build_sku(self.openstack_ip,
self.host_data[self.openstack_ip]['password'],
self.host_data[self.openstack_ip]['username'])
def run_cmd_on_server(self, server_ip, issue_cmd, username=None,
password=None, pty=True):
if server_ip in self.host_data.keys():
if not username:
username = self.host_data[server_ip]['username']
if not password:
password = self.host_data[server_ip]['password']
with hide('everything'):
with settings(
host_string='%s@%s' % (username, server_ip), password=password,
warn_only=True, abort_on_prompts=False):
output = run('%s' % (issue_cmd), pty=pty)
return output
# end run_cmd_on_server
class ContrailTestInit(object):
def __getattr__(self, attr):
return getattr(self.inputs, attr)
def __init__(
self,
ini_file=None,
stack_user=None,
stack_password=None,
project_fq_name=None,
logger=None):
self.connections = None
self.logger = logger or logging.getLogger(__name__)
self.inputs = TestInputs(ini_file)
self.stack_user = stack_user or self.stack_user
self.stack_password = stack_password or self.stack_password
self.project_fq_name = project_fq_name or \
[self.stack_domain, self.stack_tenant]
self.project_name = self.project_fq_name[1]
self.domain_name = self.project_fq_name[0]
# Possible af values 'v4', 'v6' or 'dual'
# address_family = read_config_option(self.config,
# 'Basic', 'AddressFamily', 'dual')
self.address_family = 'v4'
# end __init__
def set_af(self, af):
self.address_family = af
def get_af(self):
return self.address_family
def verify_thru_gui(self):
'''
Check if GUI based verification is enabled
'''
if self.ui_browser:
return True
return False
def is_gui_based_config(self):
'''
Check if objects have to configured via GUI
'''
if self.ui_config:
return self.ui_config
return False
def verify_state(self):
result = True
for host in self.host_ips:
username = self.host_data[host]['username']
password = self.host_data[host]['password']
if host in self.compute_ips:
for service in self.compute_services:
result = result and self.verify_service_state(
host,
service,
username,
password)
if host in self.bgp_ips:
for service in self.control_services:
result = result and self.verify_service_state(
host,
service,
username,
password)
if host in self.cfgm_ips:
for service in self.cfgm_services:
result = result and self.verify_service_state(
host,
service,
username,
password)
if host in self.collector_ips:
for service in self.collector_services:
result = result and self.verify_service_state(
host,
service,
username,
password)
if host in self.webui_ips:
for service in self.webui_services:
result = result and self.verify_service_state(
host,
service,
username,
password)
# Need to enhance verify_service_state to verify openstack services status as well
# Commenting out openstack service verifcation untill then
# if host == self.openstack_ip:
# for service in self.openstack_services:
# result = result and self.verify_service_state(
# host,
# service,
# username,
# password)
return result
# end verify_state
def get_service_status(self, m, service):
Service = namedtuple('Service', 'name state')
for keys, values in m.items():
values = values[0].rstrip().split()
if service in str(values):
cls = Service(values[0], values[1])
self.logger.info("\n%s:%s" % (cls.name, cls.state))
return cls
return None
def verify_service_state(self, host, service, username, password):
m = None
cls = None
try:
m = self.get_contrail_status(host)
cls = self.get_service_status(m, service)
if (cls.state in self.correct_states):
return True
except Exception as e:
self.logger.exception("Got exception as %s" % (e))
self.logger.exception(
"Service %s not in correct state - its in %s state" %
(cls.name, cls.state))
return False
self.logger.exception(
"Service %s not in correct state - its in %s state" %
(cls.name, cls.state))
return False
def verify_control_connection(self, connections):
discovery = connections.ds_verification_obj
return discovery.verify_bgp_connection()
# end verify_control_connection
def build_compute_to_control_xmpp_connection_dict(self, connections):
agent_to_control_dct = {}
for ip in self.compute_ips:
actual_bgp_peer = []
inspect_h = connections.agent_inspect[ip]
agent_xmpp_status = inspect_h.get_vna_xmpp_connection_status()
for i in xrange(len(agent_xmpp_status)):
actual_bgp_peer.append(agent_xmpp_status[i]['controller_ip'])
agent_to_control_dct[ip] = actual_bgp_peer
return agent_to_control_dct
# end build_compute_to_control_xmpp_connection_dict
def reboot(self, server_ip):
i = socket.gethostbyaddr(server_ip)[0]
print "rebooting %s" % i
if server_ip in self.host_data.keys():
username = self.host_data[server_ip]['username']
password = self.host_data[server_ip]['password']
with hide('everything'):
with settings(
host_string='%s@%s' % (username, server_ip), password=password,
warn_only=True, abort_on_prompts=False):
reboot(wait=300)
run('date')
# end reboot
@retry(delay=10, tries=10)
def confirm_service_active(self, service_name, host):
cmd = 'contrail-status | grep %s | grep " active "' % (service_name)
output = self.run_cmd_on_server(
host, cmd, self.host_data[host]['username'],
self.host_data[host]['password'])
if output is not None:
return True
else:
return False
# end confirm_service_active
def restart_service(
self,
service_name,
host_ips=[],
contrail_service=True):
result = True
if len(host_ips) == 0:
host_ips = self.host_ips
for host in host_ips:
username = self.host_data[host]['username']
password = self.host_data[host]['password']
self.logger.info('Restarting %s.service in %s' %
(service_name, self.host_data[host]['name']))
if contrail_service:
issue_cmd = 'service %s restart' % (service_name)
else:
issue_cmd = 'service %s restart' % (service_name)
self.run_cmd_on_server(
host, issue_cmd, username, password, pty=False)
assert self.confirm_service_active(service_name, host), \
"Service Restart failed for %s" % (service_name)
# end restart_service
def stop_service(self, service_name, host_ips=[], contrail_service=True):
result = True
if len(host_ips) == 0:
host_ips = self.host_ips
for host in host_ips:
username = self.host_data[host]['username']
password = self.host_data[host]['password']
self.logger.info('Stoping %s.service in %s' %
(service_name, self.host_data[host]['name']))
if contrail_service:
issue_cmd = 'service %s stop' % (service_name)
else:
issue_cmd = 'service %s stop' % (service_name)
self.run_cmd_on_server(
host, issue_cmd, username, password, pty=False)
# end stop_service
def start_service(self, service_name, host_ips=[], contrail_service=True):
result = True
if len(host_ips) == 0:
host_ips = self.host_ips
for host in host_ips:
username = self.host_data[host]['username']
password = self.host_data[host]['password']
self.logger.info('Starting %s.service in %s' %
(service_name, self.host_data[host]['name']))
if contrail_service:
issue_cmd = 'service %s start' % (service_name)
else:
issue_cmd = 'service %s start' % (service_name)
self.run_cmd_on_server(
host, issue_cmd, username, password, pty=False)
# end start_service
def _compare_service_state(
self, host, service, state, state_val, active_str1, active_str1_val,
active_str2, active_str2_val):
result = False
if 'xen' in self.os_type[host] or 'centos' in self.os_type[host]:
if active_str2 != active_str2_val:
result = False
self.logger.warn(
'On host %s,Service %s state is (%s) .. NOT Expected !!' %
(host, service, active_str2))
elif 'fc' in self.os_type[host]:
if (state,
active_str1,
active_str2) != (state_val,
active_str1_val,
active_str2_val):
result = False
self.logger.warn(
'On host %s,Service %s states are %s, %s, %s .. NOT Expected !!' %
(host, service, state, active_str1, active_str2))
return result
# end _compare_service_state
def get_contrail_status(self, server_ip, username='root',
password='contrail123'):
cache = self.run_cmd_on_server(server_ip, 'contrail-status')
m = dict([(n, tuple(l.split(';')))
for n, l in enumerate(cache.split('\n'))])
return m
def run_provision_control(
self,
router_asn,
api_server_ip,
api_server_port,
oper):
username = self.host_data[self.cfgm_ip]['username']
password = self.host_data[self.cfgm_ip]['password']
bgp_ips = set(self.bgp_ips)
for host in bgp_ips:
host_name = self.host_data[host]['name']
issue_cmd = "python /opt/contrail/utils/provision_control.py \
--host_name '%s' --host_ip '%s' --router_asn '%s' \
--api_server_ip '%s' --api_server_port '%s' --oper '%s'" % (host_name,
host,
router_asn,
api_server_ip,
api_server_port,
oper)
output = self.run_cmd_on_server(
self.cfgm_ip, issue_cmd, username, password)
if output.return_code != 0:
self.logger.exception('Fail to execute provision_control.py')
return output
# end run_provision_control
def run_provision_mx(
self,
api_server_ip,
api_server_port,
router_name,
router_ip,
router_asn,
oper):
username = self.host_data[self.cfgm_ip]['username']
password = self.host_data[self.cfgm_ip]['password']
issue_cmd = "python /opt/contrail/utils/provision_mx.py \
--api_server_ip '%s' --api_server_port '%s' \
--router_name '%s' --router_ip '%s' \
--router_asn '%s' --oper '%s'" % (
api_server_ip, api_server_port,
router_name, router_ip, router_asn, oper)
output = self.run_cmd_on_server(
self.cfgm_ip, issue_cmd, username, password)
if output.return_code != 0:
self.logger.exception('Fail to execute provision_mx.py')
return output
# end run_provision_mx
def config_route_target(
self,
routing_instance_name,
route_target_number,
router_asn,
api_server_ip,
api_server_port):
username = self.host_data[self.cfgm_ip]['username']
password = self.host_data[self.cfgm_ip]['password']
issue_cmd = "python /opt/contrail/utils/add_route_target.py \
--routing_instance_name '%s' --route_target_number '%s' \
--router_asn '%s' --api_server_ip '%s' --api_server_port '%s'" % (
routing_instance_name, route_target_number,
router_asn, api_server_ip, api_server_port)
output = self.run_cmd_on_server(
self.cfgm_ip, issue_cmd, username, password)
if output.return_code != 0:
self.logger.exception('Fail to execute add_route_target.py')
return output
# end config_route_target
def configure_mx(
self,
tunnel_name,
bgp_group,
cn_ip,
mx_ip,
mx_rt,
mx_as,
mx_user,
mx_password,
ri_name,
intf,
vrf_target,
ri_gateway):
host_ip_with_subnet = "%s/32" % (cn_ip)
# Initializing list of command need to be configured in MX
command_to_push = ['configure']
# Populating the required command
##command_to_push.append("set routing-options dynamic-tunnels tunnel_name source-address %s" %(mx_ip.split('/')[0]))
#command_to_push.append("set routing-options dynamic-tunnels %s source-address %s" %(tunnel_name,mx_ip))
#command_to_push.append("set routing-options dynamic-tunnels %s gre" % (tunnel_name ) )
#command_to_push.append("set routing-options dynamic-tunnels %s destination-networks %s" % (tunnel_name,host_ip_with_subnet))
#command_to_push.append("set protocols bgp group %s type internal" % (bgp_group))
##command_to_push.append("set protocols bgp group %s local-address %s" %(bgp_group,mx_ip.split('/')[0]))
#command_to_push.append("set protocols bgp group %s local-address %s" %(bgp_group,mx_ip))
#command_to_push.append("set protocols bgp group %s family inet-vpn unicast" % (bgp_group))
#command_to_push.append("set protocols bgp group %s neighbor %s" % (bgp_group,cn_ip))
#command_to_push.append("set routing-instances %s instance-type vrf" % (ri_name))
#command_to_push.append("set routing-instances %s interface %s" %(ri_name, intf))
#command_to_push.append("set routing-instances %s vrf-target %s:%s:%s" %(ri_name, vrf_target,mx_as,mx_rt))
#command_to_push.append("set routing-instances %s vrf-table-label" %(ri_name))
#command_to_push.append("set routing-instances %s routing-options static route 0.0.0.0/0 next-hop %s" %(ri_name, ri_gateway))
# command_to_push.append("commit")
print "Final commad will be pushed to MX"
print "%s" % command_to_push
# for command in command_to_push:
# output = self.run_cmd_on_server(mx_ip,command,mx_user,mx_password)
# if output.return_code != 0:
# self.logger.exception('Fail to configure MX')
# return output
command_to_push_string = ";".join(command_to_push)
output = self.run_cmd_on_server(