5555DOWNLOAD_CHUNK_SIZE = 1024 * 1024
5656
5757# set up logger
58- LOGGER = logging .getLogger (__name__ )
58+ LOG = logging .getLogger (__name__ )
5959
6060# flag to indicate whether we've received and processed the stop signal
6161INFRA_STOPPED = False
@@ -92,12 +92,12 @@ def run(self):
9292 self .func (self .params )
9393 except Exception :
9494 if not self .quiet :
95- LOGGER .warning ('Thread run method %s(%s) failed: %s' %
95+ LOG .warning ('Thread run method %s(%s) failed: %s' %
9696 (self .func , self .params , traceback .format_exc ()))
9797
9898 def stop (self , quiet = False ):
9999 if not quiet and not self .quiet :
100- LOGGER .warning ('Not implemented: FuncThread.stop(..)' )
100+ LOG .warning ('Not implemented: FuncThread.stop(..)' )
101101
102102
103103class ShellCommandThread (FuncThread ):
@@ -142,9 +142,9 @@ def convert_line(line):
142142 self .process .communicate ()
143143 except Exception as e :
144144 if self .process and not self .quiet :
145- LOGGER .warning ('Shell command error "%s": %s' % (e , self .cmd ))
145+ LOG .warning ('Shell command error "%s": %s' % (e , self .cmd ))
146146 if self .process and not self .quiet and self .process .returncode != 0 :
147- LOGGER .warning ('Shell command exit code "%s": %s' % (self .process .returncode , self .cmd ))
147+ LOG .warning ('Shell command exit code "%s": %s' % (self .process .returncode , self .cmd ))
148148
149149 def is_killed (self ):
150150 if not self .process :
@@ -164,7 +164,7 @@ def stop(self, quiet=False):
164164 import psutil
165165
166166 if not self .process :
167- LOGGER .warning ("No process found for command '%s'" % self .cmd )
167+ LOG .warning ("No process found for command '%s'" % self .cmd )
168168 return
169169
170170 parent_pid = self .process .pid
@@ -176,7 +176,7 @@ def stop(self, quiet=False):
176176 self .process = None
177177 except Exception :
178178 if not quiet :
179- LOGGER .warning ('Unable to kill process with pid %s' % parent_pid )
179+ LOG .warning ('Unable to kill process with pid %s' % parent_pid )
180180
181181
182182class JsonObject (object ):
@@ -332,6 +332,14 @@ def in_docker():
332332 return config .in_docker ()
333333
334334
335+ def has_docker ():
336+ try :
337+ run ('docker ps' )
338+ return True
339+ except Exception :
340+ return False
341+
342+
335343def is_port_open (port_or_url , http_path = None , expect_success = True ):
336344 port = port_or_url
337345 host = 'localhost'
@@ -413,7 +421,7 @@ def merge_recursive(source, destination):
413421 merge_recursive (value , node )
414422 else :
415423 if not isinstance (destination , dict ):
416- LOGGER .warning ('Destination for merging %s=%s is not dict: %s' %
424+ LOG .warning ('Destination for merging %s=%s is not dict: %s' %
417425 (key , value , destination ))
418426 destination [key ] = value
419427 return destination
@@ -475,7 +483,7 @@ def ensure_readable(file_path, default_perms=None):
475483 with open (file_path , 'rb' ):
476484 pass
477485 except Exception :
478- LOGGER .info ('Updating permissions as file is currently not readable: %s' % file_path )
486+ LOG .info ('Updating permissions as file is currently not readable: %s' % file_path )
479487 os .chmod (file_path , default_perms )
480488
481489
@@ -522,19 +530,19 @@ def download(url, path, verify_ssl=True):
522530 try :
523531 if not os .path .exists (os .path .dirname (path )):
524532 os .makedirs (os .path .dirname (path ))
525- LOGGER .debug ('Starting download from %s to %s (%s bytes)' % (url , path , r .headers .get ('content-length' )))
533+ LOG .debug ('Starting download from %s to %s (%s bytes)' % (url , path , r .headers .get ('content-length' )))
526534 with open (path , 'wb' ) as f :
527535 for chunk in r .iter_content (DOWNLOAD_CHUNK_SIZE ):
528536 total += len (chunk )
529537 if chunk : # filter out keep-alive new chunks
530538 f .write (chunk )
531- LOGGER .debug ('Writing %s bytes (total %s) to %s' % (len (chunk ), total , path ))
539+ LOG .debug ('Writing %s bytes (total %s) to %s' % (len (chunk ), total , path ))
532540 else :
533- LOGGER .debug ('Empty chunk %s (total %s) from %s' % (chunk , total , url ))
541+ LOG .debug ('Empty chunk %s (total %s) from %s' % (chunk , total , url ))
534542 f .flush ()
535543 os .fsync (f )
536544 finally :
537- LOGGER .debug ('Done downloading %s, response code %s' % (url , r .status_code ))
545+ LOG .debug ('Done downloading %s, response code %s' % (url , r .status_code ))
538546 r .close ()
539547 s .close ()
540548
@@ -684,7 +692,7 @@ def unzip(path, target_dir):
684692 try :
685693 zip_ref = zipfile .ZipFile (path , 'r' )
686694 except Exception as e :
687- LOGGER .warning ('Unable to open zip file: %s: %s' % (path , e ))
695+ LOG .warning ('Unable to open zip file: %s: %s' % (path , e ))
688696 raise e
689697 # Make sure to preserve file permissions in the zip file
690698 # https://www.burgundywall.com/post/preserving-file-perms-with-python-zipfile-module
@@ -792,12 +800,12 @@ def generate_ssl_cert(target_file=None, overwrite=False, random=False):
792800 return file_content
793801
794802
795- def run_safe (_python_lambda , print_error = True , ** kwargs ):
803+ def run_safe (_python_lambda , print_error = False , ** kwargs ):
796804 try :
797805 return _python_lambda (** kwargs )
798806 except Exception as e :
799807 if print_error :
800- print ('Unable to execute function: %s' % e )
808+ LOG . warning ('Unable to execute function: %s' % e )
801809
802810
803811def run_cmd_safe (** kwargs ):
0 commit comments