@@ -14,6 +14,14 @@ import terracumber.cucumber
1414import terracumber .junit
1515import terracumber .mailer
1616import 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 )
1725
1826def parse_args ():
1927 """Parse arguments"""
@@ -105,18 +113,18 @@ def parse_args():
105113 args = parser .parse_args ()
106114 if args .runstep :
107115 if args .runstep == 'cucumber' and not args .cucumber_cmd :
108- print ("--runstep cucumber requires --cucumber-cmd" )
116+ logger . error ("--runstep cucumber requires --cucumber-cmd" )
109117 return False
110118 if args .runstep == 'saltshaker' and not args .saltshaker_cmd :
111- print ("--runstep saltshaker requires --saltshaker-cmd" )
119+ logger . error ("--runstep saltshaker requires --saltshaker-cmd" )
112120 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" )
115123 return False
116124 elif args .runall :
117125 pass
118126 else :
119- print ("Either --runall or --runstep must be used" )
127+ logger . error ("Either --runall or --runstep must be used" )
120128 return False
121129 return args
122130
@@ -131,8 +139,7 @@ def read_config(tf_file, tf_variables_description_file, tf_variables_product_fil
131139 'MAIL_SUBJECT' , 'MAIL_TEMPLATE' , 'MAIL_SUBJECT_ENV_FAIL' ,
132140 'MAIL_TEMPLATE_ENV_FAIL' , 'MAIL_FROM' , 'MAIL_TO' ]:
133141 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 )
136143 return False
137144 if tf_variables_product_file != '' :
138145 with open (tf_variables_product_file , 'r' ) as f :
@@ -190,7 +197,7 @@ def get_timestamp(args):
190197def create_outputdir (outputdir , timestamp ):
191198 """Create an outputdir with a timestamp"""
192199 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 )
194201 return False
195202 outputdir = '%s/%s/' % (outputdir .rstrip ('/' ), timestamp )
196203 existed = True
@@ -220,11 +227,13 @@ def run_terraform(args, tf_vars):
220227 with open (args .custom_repositories , 'r' ) as repos_file :
221228 error = terraform .inject_repos (repos_file )
222229 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 )
224231 return False
225232 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 )
227234 return False
235+ elif error == 3 :
236+ logger .warning ("WARNING: Proxy or server is missing the placeholder for additional repositories in %s" , args .tf )
228237 if args .init :
229238 terraform .init ()
230239 if args .taint :
@@ -254,13 +263,13 @@ def get_controller_hostname(args, tf_vars):
254263def get_bastion_hostname (args , tf_vars ):
255264 """ Get bastion hostname """
256265 if args .bastion_hostname :
257- return args .bastion_hostname
266+ return args .bastion_hostname
258267 try :
259268 terraform = terracumber .terraformer .Terraformer (
260269 args .gitfolder , args .tf , args .sumaform_backend , tf_vars , args .logfile )
261270 return terraform .get_hostname ('bastion' )
262271 except (KeyError , FileNotFoundError ) as error :
263- print ( error )
272+ logger . error ( "Error occurred while getting bastion hostname: %s" , error )
264273 return None
265274
266275
@@ -277,15 +286,15 @@ def get_results(args, tf_vars, config, ctl_creds):
277286 cucumber .get ('%s/%s' % (config ['CUCUMBER_RESULTS' ], copyfile ),
278287 '%s' % args .outputdir )
279288 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' ])
282291 for copydir in directories :
283292 try :
284293 cucumber .get_recursive ('%s/%s' % (config ['CUCUMBER_RESULTS' ], copydir ),
285294 '%s/%s' % (args .outputdir , copydir ))
286295 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 )
289298 return True
290299
291300
@@ -300,7 +309,7 @@ def get_results_saltshaker(args, tf_vars, config, ctl_creds):
300309 cucumber .get_recursive ('%s/%s' % (config ['CUCUMBER_RESULTS' ], copydir ),
301310 '%s/%s' % (args .outputdir , copydir ))
302311 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' ],
304313 copydir ))
305314 return True
306315
@@ -370,7 +379,7 @@ def cucumber_put(args, tf_vars, ctl_creds, src, dest):
370379 """ Copy a file on the controller """
371380 ctl = get_controller_hostname (args , tf_vars )
372381 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" )
374383 else :
375384 ctl_creds ['hostname' ] = ctl
376385 ctl_creds ['timeout' ] = 300
@@ -400,9 +409,9 @@ def main():
400409 (args .outputdir , dir_existed ) = create_outputdir (
401410 args .outputdir , template_data ['timestamp' ])
402411 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 )
404413 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 )
406415 sys .exit (1 )
407416 config = read_config (args .tf , args .tf_variables_description_file , args .tf_variables_product_file )
408417 if not config :
@@ -420,21 +429,21 @@ def main():
420429 bastion_creds = {'hostname' : None , 'username' : args .bastion_user ,
421430 'port' : 22 , 'key_filename' : args .bastion_ssh_key }
422431 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 )
424433 try :
425434 terracumber .git .Git (args .gitrepo , args .gitref ,
426435 args .gitfolder , auth = git_creds , auto = True )
427436 results ['git' ] = True
428437 except Exception as e :
429- print ("ERROR: %s: %s" % (type (e ).__name__ , e ))
438+ logger . error ("ERROR: %s: %s" % (type (e ).__name__ , e ))
430439 results ['git' ] = False
431440
432441 if args .runall or args .runstep == 'provision' :
433- print ("Running terraform..." )
442+ logger . info ("Running terraform..." )
434443 results ['terraform' ] = run_terraform (args , tf_vars )
435444
436445 if args .runstep == 'saltshaker' :
437- print ("Running Salt Shaker tests..." )
446+ logger . info ("Running Salt Shaker tests..." )
438447 if args .saltshaker_cmd :
439448 cmd = args .saltshaker_cmd
440449 else :
@@ -454,28 +463,28 @@ def main():
454463
455464 # Only if terraform was successful or step cucumber is called
456465 if (args .runall and results ['terraform' ]) or args .runstep == 'cucumber' :
457- print ("Running command..." )
466+ logger . info ("Running command..." )
458467 if args .cucumber_cmd :
459468 cmd = args .cucumber_cmd
460469 else :
461470 cmd = config ['CUCUMBER_COMMAND' ]
462471 results ['output-tests' ] = cucumber_run (args , tf_vars , ctl_creds , cmd )
463472
464473 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 )
466475 results ['results' ] = get_results (args , tf_vars , config , ctl_creds )
467476
468477 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 )
470479 results ['results' ] = get_results_saltshaker (args , tf_vars , config , ctl_creds )
471480
472481 if args .runall or args .runstep == 'mail' :
473- print ("Preparing and sending email" )
482+ logger . info ("Preparing and sending email" )
474483 results ['mail' ] = send_mail (
475484 args , config , template_data , results ['output-tests' ])
476485
477486 if args .runstep == 'saltshaker_mail' :
478- print ("Preparing and sending email for Salt Shaker" )
487+ logger . info ("Preparing and sending email for Salt Shaker" )
479488 results ['mail' ] = send_mail (
480489 args , config , template_data , results ['output-tests' ], saltshaker = True )
481490
0 commit comments