/
testDispatcher.py
952 lines (840 loc) · 43.6 KB
/
testDispatcher.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
import sys
import urllib.request, urllib.error, urllib.parse
import urllib.request, urllib.parse, urllib.error
import httplib2
import json
import string
import time
from optparse import OptionParser
import traceback
import os as OS
import paramiko
import ipaddress
import subprocess
import boto3
import configparser
from couchbase.cluster import Cluster
from couchbase.cluster import PasswordAuthenticator
from couchbase.exceptions import CouchbaseError
from couchbase.n1ql import N1QLQuery
import get_jenkins_params
import find_rerun_job
# takes an ini template as input, standard out is populated with the server pool
# need a descriptor as a parameter
# need a timeout param
POLL_INTERVAL = 60
SERVER_MANAGER = '172.23.104.162:8081'
ADDL_SERVER_MANAGER = '172.23.104.162:8081'
TEST_SUITE_DB = '172.23.104.162'
SERVER_MANAGER_USER_NAME = 'Administrator'
SERVER_MANAGER_PASSWORD = "esabhcuoc"
TIMEOUT = 60
SSH_NUM_RETRIES = 3
SSH_POLL_INTERVAL = 20
def getNumberOfServers(iniFile):
f = open(iniFile)
contents = f.read()
f.close()
return contents.count('dynamic')
def getNumberOfAddpoolServers(iniFile, addPoolId):
f = open(iniFile)
contents = f.read()
f.close()
try:
return contents.count(addPoolId)
except:
return 0
def get_ssh_username(iniFile):
f = open(iniFile)
contents = f.readlines()
for line in contents:
if "username:" in line:
arr = line.split(":")[1:]
username = arr[0].rstrip()
return username
return ""
def get_ssh_password(iniFile):
f = open(iniFile)
contents = f.readlines()
for line in contents:
if "password:" in line:
arr = line.split(":")[1:]
password = arr[0].rstrip()
return password
return ""
def rreplace(str, pattern, num_replacements):
return str.rsplit(pattern, num_replacements)[0]
def get_available_servers_count(options=None, is_addl_pool=False, os_version="", pool_id=None):
# this bit is Docker/VM dependent
getAvailUrl = 'http://' + SERVER_MANAGER + '/getavailablecount/'
if is_addl_pool:
pool_id = options.addPoolId
if options.serverType.lower() == 'docker':
# may want to add OS at some point
getAvailUrl = getAvailUrl + 'docker?os={0}&poolId={1}'.format(os_version, pool_id)
else:
if is_addl_pool == True:
SM = ADDL_SERVER_MANAGER
else:
SM = SERVER_MANAGER
getAvailUrl = 'http://' + SM + '/getavailablecount/' + '{0}?poolId={1}'.format(os_version,
pool_id)
print("Connecting {}".format(getAvailUrl))
count = 0
response, content = httplib2.Http(timeout=TIMEOUT).request(getAvailUrl, 'GET')
if response.status != 200:
print(time.asctime(time.localtime(time.time())), 'invalid server response', content)
time.sleep(POLL_INTERVAL)
elif int(content) == 0:
print(time.asctime(time.localtime(time.time())), 'no VMs')
time.sleep(POLL_INTERVAL)
else:
count = int(content)
return count
AWS_AMI_MAP = {
"couchbase": {
"amzn2": {
"aarch64": "ami-02eff1b90bf0e886b",
"x86_64": "ami-02c15e47d576743f8"
}
},
"elastic-fts": "ami-0fb83341c1917381c",
"localstack": "ami-06f8ff60aa74ed321"
}
def get_servers_aws(descriptor, how_many, options, os_version, is_addl_pool):
descriptor = urllib.parse.unquote(descriptor)
instance_type = "m4.xlarge"
if is_addl_pool:
image_id = AWS_AMI_MAP[options.addPoolId]
else:
image_id = AWS_AMI_MAP["couchbase"][os_version][options.architecture]
if options.architecture == "aarch64":
instance_type = "t4g.xlarge"
ec2_resource = boto3.resource('ec2')
ec2_client = boto3.client('ec2')
instances = ec2_resource.create_instances(
ImageId=image_id,
MinCount=how_many,
MaxCount=how_many,
InstanceType=instance_type,
LaunchTemplate={
'LaunchTemplateName': 'TestrunnerCouchbase',
'Version': '5'
},
TagSpecifications=[
{
'ResourceType': 'instance',
'Tags': [
{
'Key': 'Name',
'Value': descriptor
},
]
},
],
)
instance_ids = [instance.id for instance in instances]
print("Waiting for instances: ", instance_ids)
ec2_client.get_waiter('instance_status_ok').wait(InstanceIds=instance_ids)
instances = ec2_client.describe_instances(InstanceIds=instance_ids)
ips = []
users_to_try = ["ec2-user", "centos"]
for instance in instances['Reservations'][0]['Instances']:
ip = instance['PublicDnsName']
ips.append(ip)
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
for user in users_to_try:
try:
ssh.connect(ip, username=user, key_filename=OS.environ.get("AWS_SSH_KEY"))
break
except paramiko.AuthenticationException:
continue
else:
raise Exception("Could not connect to %s" % ip)
ssh.exec_command("echo 'couchbase' | sudo passwd --stdin root",)
ssh.exec_command("sudo sed -i '/#PermitRootLogin yes/c\PermitRootLogin yes' /etc/ssh/sshd_config")
ssh.exec_command("sudo sed -i '/PermitRootLogin forced-commands-only/c\#PermitRootLogin forced-commands-only' /etc/ssh/sshd_config")
ssh.exec_command("sudo sed -i '/PasswordAuthentication no/c\PasswordAuthentication yes' /etc/ssh/sshd_config")
ssh.exec_command("sudo service sshd restart")
return ips
def get_servers(options=None, descriptor="", test=None, how_many=0, is_addl_pool=False, os_version="", pool_id=None):
if SERVER_MANAGER == "AWS":
return get_servers_aws(descriptor, how_many, options, os_version, is_addl_pool)
if is_addl_pool:
pool_id = options.addPoolId
if options.serverType.lower() == 'docker':
getServerURL = 'http://' + SERVER_MANAGER + '/getdockers/{0}?count={1}&os={2}&poolId={3}'. \
format(descriptor, how_many, os_version, pool_id)
else:
if is_addl_pool == True:
SM = ADDL_SERVER_MANAGER
else:
SM = SERVER_MANAGER
getServerURL = 'http://' + SM + '/getservers/{0}?count={1}&expiresin={2}&os={3}&poolId={' \
'4}'.format(descriptor, how_many, test['timeLimit'], os_version, pool_id)
print(('getServerURL', getServerURL))
response, content = httplib2.Http(timeout=TIMEOUT).request(getServerURL, 'GET')
print(('response.status', response, content))
if response.status == 499:
time.sleep(POLL_INTERVAL) # some error checking here at some point
return json.loads("[]")
content1 = content.decode()
if '[' not in content1:
content2 = '["'+str(content1)+'"]'
return json.loads(content2)
else:
return json.loads(content1)
def check_servers_via_ssh(servers=[], test=None):
alive_servers = []
bad_servers = []
for server in servers:
if is_vm_alive(server=server, ssh_username=test['ssh_username'], ssh_password=test['ssh_password']):
alive_servers.append(server)
else:
bad_servers.append(server)
return alive_servers, bad_servers
def is_vm_alive(server="", ssh_username="", ssh_password=""):
try:
if '[' in server or ']' in server:
server = server.replace('[', '')
server = server.replace(']','')
ip_version = ipaddress.ip_address(server).version
except ValueError as e:
print("{0} is not recognized as valid IPv4 or IPv6 address.".format(server))
return False
num_retries = 1
while num_retries <= SSH_NUM_RETRIES:
try:
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
#ssh.auth_password()
ssh.connect(hostname=server, username=ssh_username, password=ssh_password)
print("Successfully established test ssh connection to {0}. VM is recognized as valid.".format(server))
return True
except Exception as e:
print("Exception occured while trying to establish ssh connection with {0}: {1}".format(server, str(e)))
num_retries = num_retries +1
time.sleep(SSH_POLL_INTERVAL)
continue
print("{0} is unreachable. Tried to establish ssh connection {1} times".format(server, num_retries-1))
return False
def main():
global SERVER_MANAGER
global ADDL_SERVER_MANAGER
global TIMEOUT
global SSH_NUM_RETRIES
global SSH_POLL_INTERVAL
usage = '%prog -s suitefile -v version -o OS'
parser = OptionParser(usage)
parser.add_option('-v', '--version', dest='version')
parser.add_option('-r', '--run', dest='run') # run is ambiguous but it means 12 hour or weekly
parser.add_option('-o', '--os', dest='os')
parser.add_option('-n', '--noLaunch', action="store_true", dest='noLaunch', default=False)
parser.add_option('-c', '--component', dest='component', default=None)
parser.add_option('-p', '--poolId', dest='poolId', default='12hour')
parser.add_option('-a', '--addPoolId', dest='addPoolId', default=None)
parser.add_option('-t', '--test', dest='test', default=False, action='store_true') # use the test Jenkins
parser.add_option('-s', '--subcomponent', dest='subcomponent', default=None)
parser.add_option('-e', '--extraParameters', dest='extraParameters', default=None)
parser.add_option('-y', '--serverType', dest='serverType', default='VM') # or could be Docker
parser.add_option('-u', '--url', dest='url', default=None)
parser.add_option('-j', '--jenkins', dest='jenkins', default=None)
parser.add_option('-b', '--branch', dest='branch', default='master')
parser.add_option('-g', '--cherrypick', dest='cherrypick', default=None)
# whether to use production version of a test_suite_executor or test version
parser.add_option('-l','--launch_job', dest='launch_job', default='test_suite_executor')
parser.add_option('-f','--jenkins_server_url', dest='jenkins_server_url', default='http://qa.sc.couchbase.com')
parser.add_option('-m','--rerun_params', dest='rerun_params', default='')
parser.add_option('-i','--retries', dest='retries', default='1')
parser.add_option('-q', '--fresh_run', dest='fresh_run',
default=True, action='store_false')
parser.add_option('-k','--include_tests', dest='include_tests', default=None)
parser.add_option('-x','--server_manager', dest='SERVER_MANAGER',
default='172.23.104.162:8081')
parser.add_option('--addl_server_manager', dest='ADDL_SERVER_MANAGER',
default='172.23.104.162:8081')
parser.add_option('-z', '--timeout', dest='TIMEOUT', default = '60')
parser.add_option('-w', '--check_vm', dest='check_vm', default="False")
parser.add_option('--ssh_poll_interval', dest='SSH_POLL_INTERVAL', default="20")
parser.add_option('--ssh_num_retries', dest='SSH_NUM_RETRIES', default="3")
parser.add_option('--job_params', dest='job_params', default=None)
parser.add_option('--architecture', dest='architecture', default="x86_64")
# set of parameters for testing purposes.
#TODO: delete them after successful testing
# dashboardReportedParameters is of the form param1=abc,param2=def
parser.add_option('-d', '--dashboardReportedParameters', dest='dashboardReportedParameters', default=None)
options, args = parser.parse_args()
#Fix the OS for addPoolServers. See CBQE-5609 for details
addPoolServer_os = "centos"
print(('the run is', options.run))
print(('the version is', options.version))
releaseVersion = float('.'.join(options.version.split('.')[:2]))
print(('release version is', releaseVersion))
print(('nolaunch', options.noLaunch))
print(('os', options.os))
# print('url', options.url)
print(('url is', options.url))
print(('cherrypick command is', options.cherrypick))
print(('the reportedParameters are', options.dashboardReportedParameters))
print(('rerun params are', options.rerun_params))
print(('Server Manager is ', options.SERVER_MANAGER))
print(('Timeout is ', options.TIMEOUT))
if options.SERVER_MANAGER:
SERVER_MANAGER=options.SERVER_MANAGER
if options.ADDL_SERVER_MANAGER:
ADDL_SERVER_MANAGER = options.ADDL_SERVER_MANAGER
if options.TIMEOUT:
TIMEOUT=int(options.TIMEOUT)
if options.SSH_POLL_INTERVAL:
SSH_POLL_INTERVAL=int(options.SSH_POLL_INTERVAL)
if options.SSH_NUM_RETRIES:
SSH_NUM_RETRIES=int(options.SSH_NUM_RETRIES)
if options.poolId:
options.poolId = options.poolId.split(",")
# What do we do with any reported parameters?
# 1. Append them to the extra (testrunner) parameters
# 2. Append the right hand of the equals sign to the subcomponent to make a report descriptor
if options.extraParameters is None:
if options.dashboardReportedParameters is None:
runTimeTestRunnerParameters = None
else:
runTimeTestRunnerParameters = options.dashboardReportedParameters
else:
runTimeTestRunnerParameters = options.extraParameters
if options.dashboardReportedParameters is not None:
runTimeTestRunnerParameters = options.extraParameters + ',' + options.dashboardReportedParameters
# f = open(options.suiteFile)
# data = f.readlines()
testsToLaunch = []
# for d in data:
# fields = d.split()
# testsToLaunch.append( {'descriptor':fields[0],'confFile':fields[1],'iniFile':fields[2],
# 'serverCount':int(fields[3]), 'timeLimit':int(fields[4]),
# 'parameters':fields[5]})
cluster = Cluster('couchbase://{}'.format(TEST_SUITE_DB))
authenticator = PasswordAuthenticator(SERVER_MANAGER_USER_NAME,
SERVER_MANAGER_PASSWORD)
cluster.authenticate(authenticator)
cb = cluster.open_bucket('QE-Test-Suites')
if options.run == "12hr_weekly":
suiteString = "('12hour' in partOf or 'weekly' in partOf)"
else:
suiteString = "'" + options.run + "' in partOf"
if options.component is None or options.component == 'None':
queryString = "select * from `QE-Test-Suites` where " + suiteString + " order by component"
else:
if options.subcomponent is None or options.subcomponent == 'None':
splitComponents = options.component.split(',')
componentString = ''
for i in range(len(splitComponents)):
componentString = componentString + "'" + splitComponents[i] + "'"
if i < len(splitComponents) - 1:
componentString = componentString + ','
queryString = "select * from `QE-Test-Suites` where {0} and component in [{1}] order by component;".format(
suiteString, componentString)
else:
# have a subcomponent, assume only 1 component
splitSubcomponents = options.subcomponent.split(',')
subcomponentString = ''
for i in range(len(splitSubcomponents)):
print(('subcomponentString is', subcomponentString))
subcomponentString = subcomponentString + "'" + splitSubcomponents[i] + "'"
if i < len(splitSubcomponents) - 1:
subcomponentString = subcomponentString + ','
queryString = "select * from `QE-Test-Suites` where {0} and component in ['{1}'] and subcomponent in [{2}];". \
format(suiteString, options.component, subcomponentString)
print(('the query is', queryString)) # .format(options.run, componentString)
query = N1QLQuery(queryString)
results = cb.n1ql_query(queryString)
framework = None
for row in results:
try:
data = row['QE-Test-Suites']
data['config'] = data['config'].rstrip() # trailing spaces causes problems opening the files
print(('row', data))
# check any os specific
if 'os' not in data or (data['os'] == options.os) or \
(data['os'] == 'linux' and options.os in {'centos', 'ubuntu'}):
# and also check for which release it is implemented in
if 'implementedIn' not in data or releaseVersion >= float(data['implementedIn']):
if 'jenkins' in data:
# then this is sort of a special case, launch the old style Jenkins job
# not implemented yet
print(('Old style Jenkins', data['jenkins']))
else:
if 'initNodes' in data:
initNodes = data['initNodes'].lower() == 'true'
else:
initNodes = True
if 'installParameters' in data:
installParameters = data['installParameters']
else:
installParameters = 'None'
if 'slave' in data:
slave = data['slave']
else:
slave = 'P0'
if 'owner' in data:
owner = data['owner']
else:
owner = 'QE'
if 'mailing_list' in data:
mailing_list = data['mailing_list']
else:
mailing_list = 'qa@couchbase.com'
if 'mode' in data:
mode = data["mode"]
else:
mode = 'java'
if 'framework' in data:
framework = data["framework"]
else:
framework = 'testrunner'
# checkout the ini file at the specified branch
# raises an exception if the ini file does not exist on that branch
if options.branch != "master":
try:
subprocess.run(["git", "checkout", "origin/" + options.branch, "--", data['config']], check=True)
except Exception:
print('Git error: Did not find {} in {} branch'.format(data['config'], options.branch))
# if there's an additional pool, get the number
# of additional servers needed from the ini
addPoolServerCount = getNumberOfAddpoolServers(
data['config'],
options.addPoolId)
testsToLaunch.append({
'component': data['component'],
'subcomponent': data['subcomponent'],
'confFile': data['confFile'],
'iniFile': data['config'],
'serverCount': getNumberOfServers(data['config']),
'ssh_username': get_ssh_username(data['config']),
'ssh_password': get_ssh_password(data['config']),
'addPoolServerCount': addPoolServerCount,
'timeLimit': data['timeOut'],
'parameters': data['parameters'],
'initNodes': initNodes,
'installParameters': installParameters,
'slave': slave,
'owner': owner,
'mailing_list': mailing_list,
'mode': mode,
'framework': framework
})
else:
print((data['component'], data['subcomponent'], ' is not supported in this release'))
else:
print(('OS does not apply to', data['component'], data['subcomponent']))
except Exception as e:
print('exception in querying tests, possible bad record')
print((traceback.format_exc()))
print(data)
total_req_servercount = 0
total_req_addservercount = 0
for i in testsToLaunch:
total_req_servercount = total_req_servercount + i['serverCount']
total_req_addservercount = total_req_addservercount + i['addPoolServerCount']
print('\n-->Test jobs to launch: {} and required serverCount={}, addPoolServerCount={}'.format(
len(testsToLaunch), total_req_servercount, total_req_addservercount))
print('#component, subcomponent, serverCount, '
'addPoolServerCount --> framework')
for i in testsToLaunch: print('{}, {}, {}, {} --> {}'.format(i['component'],
i['subcomponent'], i['serverCount'],
i['addPoolServerCount'], i['framework']))
print('\n\n')
launchStringBase = str(options.jenkins_server_url) + '/job/' + str(options.launch_job)
# this are VM/Docker dependent - or maybe not
launchString = '/buildWithParameters?token=test_dispatcher&' + \
'version_number={0}&confFile={1}&descriptor={2}&component={3}&subcomponent={4}&' + \
'iniFile={5}¶meters={6}&os={7}&initNodes={' \
'8}&installParameters={9}&branch={10}&slave={' \
'11}&owners={12}&mailing_list={13}&mode={14}&timeout={15}'
if options.rerun_params:
rerun_params = options.rerun_params.strip('\'')
launchString = launchString + '&' + urllib.parse.urlencode({
"rerun_params" : rerun_params})
launchString = launchString + '&retries=' + options.retries
if options.include_tests:
launchString = launchString + '&include_tests='+urllib.parse.quote(options.include_tests.replace("'", " ").strip())
launchString = launchString + '&fresh_run=' + urllib.parse.quote(
str(options.fresh_run).lower())
if options.url is not None:
launchString = launchString + '&url=' + options.url
if options.cherrypick is not None:
launchString = launchString + '&cherrypick=' + urllib.parse.quote(options.cherrypick)
currentDispatcherJobUrl = OS.getenv("BUILD_URL")
currentExecutorParams = get_jenkins_params.get_params(
currentDispatcherJobUrl)
if currentExecutorParams is None:
currentExecutorParams = {}
if OS.environ.get('JOB_URL') is not None:
currentExecutorParams['dispatcher_url'] = OS.getenv('JOB_URL')
currentExecutorParams = json.dumps(currentExecutorParams)
print(currentExecutorParams)
summary = []
servers = []
total_jobs_count = len(testsToLaunch)
job_index = 1
total_servers_being_used = 0
total_addl_servers_being_used = 0
if options.noLaunch:
print("\n -- No launch selected -- \n")
while len(testsToLaunch) > 0:
try:
if not options.noLaunch:
print("\n\n *** Before dispatch, checking for the servers to run a test suite\n")
else:
i = 0
print("\n\n *** Dispatching job#{} of {} with {} servers (total={}) and {} "
"additional "
"servers(total={}) : {}-{} with {}\n".format(job_index, total_jobs_count,
testsToLaunch[i]['serverCount'],
total_servers_being_used,
testsToLaunch[i][
'addPoolServerCount'],
total_addl_servers_being_used,
testsToLaunch[i]['component'],
testsToLaunch[i][
'subcomponent'],
testsToLaunch[i][
'framework'], ))
# sample data as it is a noLaunch to get the general URL
descriptor = "descriptor"
dashboardDescriptor = "dashboardDescriptor"
parameters = "parameters"
url = launchString.format(options.version, testsToLaunch[i]['confFile'], descriptor,
testsToLaunch[i]['component'], dashboardDescriptor,
testsToLaunch[i]['iniFile'],
urllib.parse.quote(parameters), options.os,
testsToLaunch[i]['initNodes'],
testsToLaunch[i]['installParameters'], options.branch,
testsToLaunch[i]['slave'],
urllib.parse.quote(testsToLaunch[i]['owner']),
urllib.parse.quote(testsToLaunch[i]['mailing_list']),
testsToLaunch[i]['mode'], testsToLaunch[i]['timeLimit'])
url = url + '&dispatcher_params=' + urllib.parse.urlencode(
{"parameters": currentExecutorParams})
# optional add [-docker] [-Jenkins extension] - TBD duplicate
launchStringBaseF = launchStringBase
if options.serverType.lower() == 'docker':
launchStringBaseF = launchStringBase + '-docker'
if options.test:
launchStringBaseF = launchStringBase + '-test'
# if options.framework.lower() == "jython":
framework = testsToLaunch[i]['framework']
if framework != 'testrunner':
launchStringBaseF = launchStringBaseF + "-" + framework
elif options.jenkins is not None:
launchStringBaseF = launchStringBaseF + '-' + options.jenkins
url = launchStringBaseF + url
if options.job_params:
url = update_url_with_job_params(url, options.job_params)
print('\n', time.asctime(time.localtime(time.time())), 'launching ', url)
total_servers_being_used += testsToLaunch[0]['serverCount']
total_addl_servers_being_used += testsToLaunch[0]['addPoolServerCount']
testsToLaunch.pop(0)
job_index += 1
continue
# this bit is Docker/VM dependent
# see if we can match a test
haveTestToLaunch = False
i = 0
pool_to_use = options.poolId[0]
for pool_id in options.poolId:
if SERVER_MANAGER == "AWS":
haveTestToLaunch = True
break
serverCount = get_available_servers_count(options=options, os_version=options.os, pool_id=pool_id)
print("Server count={}".format(serverCount))
if not serverCount or serverCount == 0:
continue
print((time.asctime(time.localtime(time.time())), 'there are', serverCount, ' servers available in ', pool_id, ' pool'))
test_index = 0
while not haveTestToLaunch and test_index < len(testsToLaunch):
if testsToLaunch[test_index]['serverCount'] <= serverCount:
if testsToLaunch[test_index]['addPoolServerCount']:
addlServersCount = get_available_servers_count(options=options, is_addl_pool=True, os_version=addPoolServer_os)
if addlServersCount == 0:
print(time.asctime(time.localtime(time.time())), 'no {0} VMs at this time'.format(
options.addPoolId))
test_index = test_index + 1
else:
print(time.asctime(
time.localtime(time.time())), "there are {0} {1} servers available".format(
addlServersCount, options.addPoolId))
haveTestToLaunch = True
else:
haveTestToLaunch = True
else:
test_index = test_index + 1
i = test_index
pool_to_use = pool_id
if haveTestToLaunch:
break
if haveTestToLaunch:
print("\n\n *** Dispatching job#{} of {} with {} servers (total={}) and {} "
"additional "
"servers(total={}) : {}-{} with {}\n".format(job_index, total_jobs_count,
testsToLaunch[i][
'serverCount'],
total_servers_being_used,
testsToLaunch[i][
'addPoolServerCount'],
total_addl_servers_being_used,
testsToLaunch[i][
'component'],
testsToLaunch[i][
'subcomponent'],
testsToLaunch[i][
'framework'], ))
if options.noLaunch:
job_index += 1
testsToLaunch.pop(i)
continue
# build the dashboard descriptor
dashboardDescriptor = urllib.parse.quote(testsToLaunch[i]['subcomponent'])
if options.dashboardReportedParameters is not None:
for o in options.dashboardReportedParameters.split(','):
dashboardDescriptor += '_' + o.split('=')[1]
dispatch_job = True
if not options.fresh_run:
dispatch_job = \
find_rerun_job.should_dispatch_job(
options.os, testsToLaunch[i][
'component'], dashboardDescriptor
, options.version)
# and this is the Jenkins descriptor
descriptor = testsToLaunch[i]['component'] + '-' + testsToLaunch[i]['subcomponent'] + '-' + time.strftime('%b-%d-%X') + '-' + options.version
if SERVER_MANAGER == "AWS":
# A stack name can contain only alphanumeric characters (case-sensitive) and hyphens
descriptor = descriptor.replace("_", "-")
descriptor = "".join(filter(lambda char: str.isalnum(char) or char == "-", descriptor))
descriptor = urllib.parse.quote(descriptor)
# grab the server resources
# this bit is Docker/VM dependent
servers = []
unreachable_servers = []
how_many = testsToLaunch[i]['serverCount'] - len(servers)
if options.check_vm == "True":
while how_many > 0:
unchecked_servers = get_servers(options=options, descriptor=descriptor, test=testsToLaunch[i],
how_many=how_many, os_version=options.os, pool_id=pool_to_use)
checked_servers, bad_servers = check_servers_via_ssh(servers=unchecked_servers,
test=testsToLaunch[i])
for ss in checked_servers:
servers.append(ss)
for ss in bad_servers:
unreachable_servers.append(ss)
how_many = testsToLaunch[i]['serverCount'] - len(servers)
else:
servers = get_servers(options=options, descriptor=descriptor, test=testsToLaunch[i],
how_many=how_many, os_version=options.os, pool_id=pool_to_use)
how_many = testsToLaunch[i]['serverCount'] - len(servers)
if options.serverType.lower() != 'docker':
# sometimes there could be a race, before a dispatcher process acquires vms,
# another waiting dispatcher process could grab them, resulting in lesser vms
# for the second dispatcher process
if len(servers) != testsToLaunch[i]['serverCount']:
print("Received servers count does not match the expected test server count!")
release_servers(descriptor)
continue
# get additional pool servers as needed
addl_servers = []
if SERVER_MANAGER == "AWS":
config = configparser.ConfigParser()
config.read(testsToLaunch[i]["iniFile"])
for section in config.sections():
if section == "cbbackupmgr" and config.has_option(section, "endpoint"):
testsToLaunch[i]['addPoolServerCount'] = 1
options.addPoolId = "localstack"
break
if testsToLaunch[i]['addPoolServerCount']:
how_many_addl = testsToLaunch[i]['addPoolServerCount'] - len(addl_servers)
addl_servers = get_servers(options=options, descriptor=descriptor, test=testsToLaunch[i], how_many=how_many_addl, is_addl_pool=True, os_version=addPoolServer_os)
how_many_addl = testsToLaunch[i]['addPoolServerCount'] - len(addl_servers)
if len(addl_servers) != testsToLaunch[i]['addPoolServerCount']:
print(
"Received additional servers count does not match the expected "
"test additional servers count!")
release_servers(descriptor)
continue
# and send the request to the test executor
# figure out the parameters, there are test suite specific, and added at dispatch time
if runTimeTestRunnerParameters is None:
parameters = testsToLaunch[i]['parameters']
else:
if testsToLaunch[i]['parameters'] == 'None':
parameters = runTimeTestRunnerParameters
else:
parameters = testsToLaunch[i]['parameters'] + ',' + runTimeTestRunnerParameters
url = launchString.format(options.version,
testsToLaunch[i]['confFile'],
descriptor,
testsToLaunch[i]['component'],
dashboardDescriptor,
testsToLaunch[i]['iniFile'],
urllib.parse.quote(parameters),
options.os,
testsToLaunch[i]['initNodes'],
testsToLaunch[i]['installParameters'],
options.branch,
testsToLaunch[i]['slave'],
urllib.parse.quote(testsToLaunch[i]['owner']),
urllib.parse.quote(
testsToLaunch[i]['mailing_list']),
testsToLaunch[i]['mode'],
testsToLaunch[i]['timeLimit'])
url = url + '&dispatcher_params=' + \
urllib.parse.urlencode({"parameters":
currentExecutorParams})
if options.serverType.lower() != 'docker':
servers_str = json.dumps(servers).replace(' ','').replace('[','', 1)
servers_str = rreplace(servers_str, ']', 1)
url = url + '&servers=' + urllib.parse.quote(servers_str)
if testsToLaunch[i]['addPoolServerCount']:
addPoolServers = json.dumps(addl_servers).replace(' ','').replace('[','', 1)
addPoolServers = rreplace(addPoolServers, ']', 1)
url = url + '&addPoolServerId=' +\
options.addPoolId +\
'&addPoolServers=' +\
urllib.parse.quote(addPoolServers)
if len(unreachable_servers) > 0:
print("The following VM(s) are unreachable for ssh connection:")
for s in unreachable_servers:
response, content = httplib2.Http(timeout=TIMEOUT).request('http://' + SERVER_MANAGER + '/releaseip/' + s + '/ssh_failed', 'GET')
print(s)
# optional add [-docker] [-Jenkins extension]
launchStringBaseF = launchStringBase
if options.serverType.lower() == 'docker':
launchStringBaseF = launchStringBase + '-docker'
if options.test:
launchStringBaseF = launchStringBase + '-test'
# if options.framework.lower() == "jython":
framework = testsToLaunch[i]['framework']
if framework != 'testrunner':
launchStringBaseF = launchStringBaseF + "-" + framework
elif options.jenkins is not None:
launchStringBaseF = launchStringBaseF + '-' + options.jenkins
url = launchStringBaseF + url
if options.job_params:
url = update_url_with_job_params(url, options.job_params)
print('\n', time.asctime( time.localtime(time.time()) ), 'launching ', url)
if options.noLaunch or not dispatch_job:
# free the VMs
time.sleep(3)
if options.serverType.lower() == 'docker':
#Docker gets only a single sever from
# get_server. Not hard coding the server
# we get, which as of now is constant,
# but might change in the future.
ipaddr = servers[0]
response, content = httplib2.Http(
timeout=TIMEOUT). \
request(
'http://' + SERVER_MANAGER +
'/releasedockers/' + descriptor +
'?ipaddr=' + ipaddr,
'GET')
print(
'the release response', response, content)
else:
release_servers(descriptor)
else:
response, content = httplib2.Http(timeout=TIMEOUT).request(url, 'GET')
print("Response is: {0}".format(str(response)))
print("Content is: {0}".format(str(content)))
if int(response['status'])>=400:
raise Exception("Unexpected response while dispatching the request!")
total_servers_being_used += testsToLaunch[0]['serverCount']
total_addl_servers_being_used += testsToLaunch[0]['addPoolServerCount']
job_index += 1
testsToLaunch.pop(i)
summary.append( {'test':descriptor, 'time':time.asctime( time.localtime(time.time()) ) } )
if options.noLaunch:
pass # no sleeping necessary
elif options.serverType.lower() == 'docker':
time.sleep(240) # this is due to the docker port allocation race
else:
time.sleep(30)
else:
print('not enough servers at this time')
time.sleep(POLL_INTERVAL)
except Exception as e:
print('have an exception')
print((traceback.format_exc()))
if descriptor:
try:
print('Releasing servers for {} ...'.format(descriptor))
release_servers(descriptor)
except Exception:
pass
time.sleep(POLL_INTERVAL)
# endwhile
if not options.noLaunch:
print('\n\n\ndone, everything is launched')
for i in summary:
print((i['test'], 'was launched at', i['time']))
print("\n\n *** Dispatched total {} jobs using total {} servers and {} additional "
"servers "
"\n".format(total_jobs_count,
total_servers_being_used,
total_addl_servers_being_used ))
else:
print("\n Done!")
return
def update_url_with_job_params(url, job_params):
dict = {}
newurl=''
url_keys_values = url.split('&')
for param_key in url_keys_values:
param_index = param_key.find('=', 1)
rparam_name = param_key[0:param_index]
if param_index < 0:
rparam_value = ''
else:
rparam_value = param_key[param_index + 1:]
dict[rparam_name] = rparam_value
param_keys_values = job_params.split('&')
for param_key in param_keys_values:
param_index = param_key.find('=', 1)
rparam_name = param_key[0:param_index]
if param_index < 0:
rparam_value = ''
else:
rparam_value = param_key[param_index + 1:]
dict[rparam_name] = urllib.parse.urlencode({rparam_name: rparam_value})
print(dict)
for key in dict:
if newurl=='':
newurl = key + "=" + dict[key]
elif dict[key].startswith(key+"="):
newurl += '&' + dict[key]
else:
newurl += '&' + key + "=" + dict[key]
return newurl
def release_servers(descriptor):
if SERVER_MANAGER == "AWS":
descriptor = urllib.parse.unquote(descriptor)
ec2_client = boto3.client('ec2')
instances = ec2_client.describe_instances(
Filters=[
{
'Name': 'tag:Name',
'Values': [
descriptor
]
}
]
)
instance_ids = [instance["InstanceId"] for instance in instances["Reservations"][0]["Instances"]]
ec2_client.terminate_instances(
InstanceIds=instance_ids
)
else:
release_url = "http://{}/releaseservers/{}/available".format(SERVER_MANAGER, descriptor)
print('Release URL: {} '.format(release_url))
response, content = httplib2.Http(timeout=TIMEOUT).request(release_url, 'GET')
print('the release response', response, content)
if __name__ == "__main__":
main()