@@ -14,6 +14,14 @@ import terracumber.cucumber
14
14
import terracumber .junit
15
15
import terracumber .mailer
16
16
import terracumber .utils
17
+ import logging
18
+
19
+ # Set up logger
20
+ logger = logging .getLogger (__name__ )
21
+ logger .setLevel (logging .INFO )
22
+ handler = logging .StreamHandler ()
23
+ handler .setFormatter (logging .Formatter ('%(asctime)s - %(name)s - %(levelname)s - %(message)s' ))
24
+ logger .addHandler (handler )
17
25
18
26
def parse_args ():
19
27
"""Parse arguments"""
@@ -105,18 +113,18 @@ def parse_args():
105
113
args = parser .parse_args ()
106
114
if args .runstep :
107
115
if args .runstep == 'cucumber' and not args .cucumber_cmd :
108
- print ("--runstep cucumber requires --cucumber-cmd" )
116
+ logger . error ("--runstep cucumber requires --cucumber-cmd" )
109
117
return False
110
118
if args .runstep == 'saltshaker' and not args .saltshaker_cmd :
111
- print ("--runstep saltshaker requires --saltshaker-cmd" )
119
+ logger . error ("--runstep saltshaker requires --saltshaker-cmd" )
112
120
return False
113
- if not 'BUILD_TIMESTAMP' in os .environ and not 'BUILD_NUMBER' in os .environ :
114
- print ("--runstep requires BUILD_TIMESTAMP or BUILD_NUMBER variables exported" )
121
+ if 'BUILD_TIMESTAMP' not in os .environ and 'BUILD_NUMBER' not in os .environ :
122
+ logger . error ("--runstep requires BUILD_TIMESTAMP or BUILD_NUMBER variables exported" )
115
123
return False
116
124
elif args .runall :
117
125
pass
118
126
else :
119
- print ("Either --runall or --runstep must be used" )
127
+ logger . error ("Either --runall or --runstep must be used" )
120
128
return False
121
129
return args
122
130
@@ -131,8 +139,7 @@ def read_config(tf_file, tf_variables_description_file, tf_variables_product_fil
131
139
'MAIL_SUBJECT' , 'MAIL_TEMPLATE' , 'MAIL_SUBJECT_ENV_FAIL' ,
132
140
'MAIL_TEMPLATE_ENV_FAIL' , 'MAIL_FROM' , 'MAIL_TO' ]:
133
141
if required not in config :
134
- print ("ERROR: variable %s does not exist at %s and is mandatory!" %
135
- (required , tf_file ))
142
+ logger .error ("Variable %s does not exist in %s and is mandatory!" , required , tf_file )
136
143
return False
137
144
if tf_variables_product_file != '' :
138
145
with open (tf_variables_product_file , 'r' ) as f :
@@ -190,7 +197,7 @@ def get_timestamp(args):
190
197
def create_outputdir (outputdir , timestamp ):
191
198
"""Create an outputdir with a timestamp"""
192
199
if not os .path .isdir (outputdir ):
193
- print ("ERROR: %s folder does not exist" % outputdir )
200
+ logger . error ("ERROR: %s folder does not exist" , outputdir )
194
201
return False
195
202
outputdir = '%s/%s/' % (outputdir .rstrip ('/' ), timestamp )
196
203
existed = True
@@ -220,11 +227,13 @@ def run_terraform(args, tf_vars):
220
227
with open (args .custom_repositories , 'r' ) as repos_file :
221
228
error = terraform .inject_repos (repos_file )
222
229
if error == 1 :
223
- print ("ERROR: %s is not a well-formed JSON file" % args .custom_repositories )
230
+ logger . error ("ERROR: %s is not a well-formed JSON file" , args .custom_repositories )
224
231
return False
225
232
elif error == 2 :
226
- print ("ERROR: make sure to have exactly 1 placeholder for additional repositories in %s" % args .tf )
233
+ logger . error ("ERROR: Make sure to have exactly 1 placeholder for additional repositories in %s" , args .tf )
227
234
return False
235
+ elif error == 3 :
236
+ logger .warning ("WARNING: Proxy or server is missing the placeholder for additional repositories in %s" , args .tf )
228
237
if args .init :
229
238
terraform .init ()
230
239
if args .taint :
@@ -254,13 +263,13 @@ def get_controller_hostname(args, tf_vars):
254
263
def get_bastion_hostname (args , tf_vars ):
255
264
""" Get bastion hostname """
256
265
if args .bastion_hostname :
257
- return args .bastion_hostname
266
+ return args .bastion_hostname
258
267
try :
259
268
terraform = terracumber .terraformer .Terraformer (
260
269
args .gitfolder , args .tf , args .sumaform_backend , tf_vars , args .logfile )
261
270
return terraform .get_hostname ('bastion' )
262
271
except (KeyError , FileNotFoundError ) as error :
263
- print ( error )
272
+ logger . error ( "Error occurred while getting bastion hostname: %s" , error )
264
273
return None
265
274
266
275
@@ -277,15 +286,15 @@ def get_results(args, tf_vars, config, ctl_creds):
277
286
cucumber .get ('%s/%s' % (config ['CUCUMBER_RESULTS' ], copyfile ),
278
287
'%s' % args .outputdir )
279
288
except FileNotFoundError :
280
- print ("Nothing matched %s/%s at %s!" % ( config ['CUCUMBER_RESULTS' ],
281
- copyfile , ctl_creds ['hostname' ]) )
289
+ logger . warning ("Nothing matched %s/%s at %s!" , config ['CUCUMBER_RESULTS' ],
290
+ copyfile , ctl_creds ['hostname' ])
282
291
for copydir in directories :
283
292
try :
284
293
cucumber .get_recursive ('%s/%s' % (config ['CUCUMBER_RESULTS' ], copydir ),
285
294
'%s/%s' % (args .outputdir , copydir ))
286
295
except FileNotFoundError :
287
- print ("Remote directory %s/%s did not exist!" % ( config ['CUCUMBER_RESULTS' ],
288
- copydir ) )
296
+ logger . warning ("Remote directory %s/%s did not exist!" , config ['CUCUMBER_RESULTS' ],
297
+ copydir )
289
298
return True
290
299
291
300
@@ -300,7 +309,7 @@ def get_results_saltshaker(args, tf_vars, config, ctl_creds):
300
309
cucumber .get_recursive ('%s/%s' % (config ['CUCUMBER_RESULTS' ], copydir ),
301
310
'%s/%s' % (args .outputdir , copydir ))
302
311
except FileNotFoundError :
303
- print ("Remote directory %s/%s did not exist!" % (config ['CUCUMBER_RESULTS' ],
312
+ logger . error ("Remote directory %s/%s did not exist!" % (config ['CUCUMBER_RESULTS' ],
304
313
copydir ))
305
314
return True
306
315
@@ -370,7 +379,7 @@ def cucumber_put(args, tf_vars, ctl_creds, src, dest):
370
379
""" Copy a file on the controller """
371
380
ctl = get_controller_hostname (args , tf_vars )
372
381
if ctl is None :
373
- print ("WARNING: not injecting custom repositories to the controller, as it does not exist" )
382
+ logger . warning ("WARNING: not injecting custom repositories to the controller, as it does not exist" )
374
383
else :
375
384
ctl_creds ['hostname' ] = ctl
376
385
ctl_creds ['timeout' ] = 300
@@ -400,9 +409,9 @@ def main():
400
409
(args .outputdir , dir_existed ) = create_outputdir (
401
410
args .outputdir , template_data ['timestamp' ])
402
411
if args .runall and dir_existed :
403
- print ("WARNING: %s directory already exists!" % args .outputdir )
412
+ logger . warning ("WARNING: %s directory already exists!" % args .outputdir )
404
413
if not os .path .isfile (args .tf ):
405
- print ("ERROR: file %s from --tf argument does not exist or is not a file" % args .tf )
414
+ logger . error ("ERROR: file %s from --tf argument does not exist or is not a file" % args .tf )
406
415
sys .exit (1 )
407
416
config = read_config (args .tf , args .tf_variables_description_file , args .tf_variables_product_file )
408
417
if not config :
@@ -420,21 +429,21 @@ def main():
420
429
bastion_creds = {'hostname' : None , 'username' : args .bastion_user ,
421
430
'port' : 22 , 'key_filename' : args .bastion_ssh_key }
422
431
if args .runall or args .runstep == 'gitsync' :
423
- print ("Cloning/Updating repository to/at %s..." % args .gitfolder )
432
+ logger . info ("Cloning/Updating repository to/at %s..." , args .gitfolder )
424
433
try :
425
434
terracumber .git .Git (args .gitrepo , args .gitref ,
426
435
args .gitfolder , auth = git_creds , auto = True )
427
436
results ['git' ] = True
428
437
except Exception as e :
429
- print ("ERROR: %s: %s" % (type (e ).__name__ , e ))
438
+ logger . error ("ERROR: %s: %s" % (type (e ).__name__ , e ))
430
439
results ['git' ] = False
431
440
432
441
if args .runall or args .runstep == 'provision' :
433
- print ("Running terraform..." )
442
+ logger . info ("Running terraform..." )
434
443
results ['terraform' ] = run_terraform (args , tf_vars )
435
444
436
445
if args .runstep == 'saltshaker' :
437
- print ("Running Salt Shaker tests..." )
446
+ logger . info ("Running Salt Shaker tests..." )
438
447
if args .saltshaker_cmd :
439
448
cmd = args .saltshaker_cmd
440
449
else :
@@ -454,28 +463,28 @@ def main():
454
463
455
464
# Only if terraform was successful or step cucumber is called
456
465
if (args .runall and results ['terraform' ]) or args .runstep == 'cucumber' :
457
- print ("Running command..." )
466
+ logger . info ("Running command..." )
458
467
if args .cucumber_cmd :
459
468
cmd = args .cucumber_cmd
460
469
else :
461
470
cmd = config ['CUCUMBER_COMMAND' ]
462
471
results ['output-tests' ] = cucumber_run (args , tf_vars , ctl_creds , cmd )
463
472
464
473
if (args .runall and results ['output-tests' ]) or args .runstep == 'getresults' :
465
- print ("Fetching files from controller to %s..." % args .outputdir )
474
+ logger . info ("Fetching files from controller to %s..." , args .outputdir )
466
475
results ['results' ] = get_results (args , tf_vars , config , ctl_creds )
467
476
468
477
if args .runstep == 'saltshaker_getresults' :
469
- print ("Fetching files from Salt Shaker node to %s..." % args .outputdir )
478
+ logger . info ("Fetching files from Salt Shaker node to %s..." , args .outputdir )
470
479
results ['results' ] = get_results_saltshaker (args , tf_vars , config , ctl_creds )
471
480
472
481
if args .runall or args .runstep == 'mail' :
473
- print ("Preparing and sending email" )
482
+ logger . info ("Preparing and sending email" )
474
483
results ['mail' ] = send_mail (
475
484
args , config , template_data , results ['output-tests' ])
476
485
477
486
if args .runstep == 'saltshaker_mail' :
478
- print ("Preparing and sending email for Salt Shaker" )
487
+ logger . info ("Preparing and sending email for Salt Shaker" )
479
488
results ['mail' ] = send_mail (
480
489
args , config , template_data , results ['output-tests' ], saltshaker = True )
481
490
0 commit comments