1616from recipe import Recipe , PipelineException
1717from stimela import docker
1818
19- from stimela .utils import stimela_logger
19+ from stimela .utils import logger
2020
2121LOG_HOME = os .path .expanduser ("~/.stimela" )
22- LOG_IMAGES = LOG_HOME + "/stimela_images.log"
23- LOG_CONTAINERS = LOG_HOME + "/stimela_containers.log"
24- LOG_PROCESS = LOG_HOME + "/stimela_process.log"
25- LOG_CABS = LOG_HOME + "/stimela_cab.log"
22+ LOG_FILE = LOG_HOME + "/stimela_logfile.json"
2623
2724
2825BASE = os .listdir (cargo .BASE_PATH )
@@ -83,13 +80,18 @@ def build(argv):
8380 help = "Do not use cache when building the image" )
8481
8582 args = parser .parse_args (argv )
83+ log = logger .StimelaLogger (LOG_FILE )
8684
8785 if args .base :
8886 for image in BASE :
8987 dockerfile = "{:s}/{:s}" .format (cargo .BASE_PATH , image )
9088 image = "stimela/{0}:{1}" .format (image , __version__ )
9189 docker .build (image ,
9290 dockerfile )
91+
92+ log .log_image (image , replace = True )
93+ log .write ()
94+
9395 return 0
9496
9597 workdir = "/home/{}/output/" .format (USER )
@@ -101,6 +103,7 @@ def build(argv):
101103
102104 no_cache = ["--no-cache" ] if args .no_cache else []
103105
106+
104107 if args .cab :
105108 cab_args = args .cab .split ("," )
106109
@@ -115,15 +118,10 @@ def build(argv):
115118 path ,
116119 build_args = build_args , args = no_cache )
117120
118- img = stimela_logger .Image (LOG_CABS )
119- img .add (dict (name = cab ))
120- img .write ()
121+ log .log_image (image , replace = True , cab = True )
122+ log .write ()
121123 return
122124
123- # clear old cabs
124- img = stimela_logger .Image (LOG_CABS )
125- img .clear ()
126-
127125 for image in CAB :
128126 IGNORE = args .ignore_cabs .split ("," )
129127 if image in NOT_PUBLIC + IGNORE :
@@ -135,9 +133,9 @@ def build(argv):
135133 dockerfile ,
136134 build_args = build_args , args = no_cache )
137135
138- img .add (dict (name = image ))
136+ log .log_image (image , replace = True , cab = True )
137+ log .write ()
139138
140- img .write ()
141139
142140
143141def info (cabname , header = False ):
@@ -206,7 +204,6 @@ def run(argv):
206204 args = parser .parse_args (argv )
207205 tag = None
208206
209-
210207 _globals = dict (STIMELA_INPUT = args .input , STIMELA_OUTPUT = args .output ,
211208 STIMELA_MSDIR = args .msdir ,
212209 CAB_TAG = tag )
@@ -248,14 +245,14 @@ def pull(argv):
248245 help = "Tag" )
249246
250247 args = parser .parse_args (argv )
248+ log = logger .StimelaLogger (LOG_FILE )
251249
252250 if args .image :
253251 for image in args .image :
254- img = stimela_logger .Image (LOG_IMAGES )
255252
256253 if not img .find (image ):
257254 docker .pull (image )
258- img . add ( dict ( name = image , tag = tagargs . tag ) )
255+ log . log_image ( image )
259256 else :
260257
261258 base = []
@@ -266,11 +263,11 @@ def pull(argv):
266263 base = set (base )
267264
268265 for image in base :
269- img = stimela_logger .Image (LOG_IMAGES )
270-
271- if not img .find (image ) and image not in ["stimela/ddfacet" , "radioastro/ddfacet" ]:
266+ if image not in ["stimela/ddfacet" , "radioastro/ddfacet" ]:
272267 docker .pull (image )
273- img .add (dict (name = image , tag = args .tag ))
268+ log .log_image (image )
269+
270+ log .write ()
274271
275272
276273def images (argv ):
@@ -282,15 +279,16 @@ def images(argv):
282279 add = parser .add_argument
283280
284281 add ("-c" , "--clear" , action = "store_true" ,
285- help = "Clear images log file " )
282+ help = "Clear the logfile that keeps track of stimela images. This does not do anythig to the images. " )
286283
287284 args = parser .parse_args (argv )
288285
289- img = stimela_logger . Image ( stimela . LOG_IMAGES )
290- img .display ()
286+ log = logger . StimelaLogger ( LOG_FILE )
287+ log .display ('images' )
291288
292289 if args .clear :
293- img .clear ()
290+ log .clear ('images' )
291+ log .write ()
294292
295293
296294def containers (argv ):
@@ -302,14 +300,15 @@ def containers(argv):
302300 add = parser .add_argument
303301
304302 add ("-c" , "--clear" , action = "store_true" ,
305- help = "Clear containers log file" )
303+ help = "Clear the log file that keeps track of stimela containers. This doesn't do anything to the containers. " )
306304
307305 args = parser .parse_args (argv )
308306
309- conts = stimela_logger . Container ( stimela . LOG_CONTAINERS )
310- conts .display ()
307+ log = logger . StimelaLogger ( LOG_FILE )
308+ log .display ('containers' )
311309 if args .clear :
312- conts .clear ()
310+ log .clear ('containers' )
311+ log .write ()
313312
314313
315314def ps (argv ):
@@ -321,14 +320,15 @@ def ps(argv):
321320 add = parser .add_argument
322321
323322 add ("-c" , "--clear" , action = "store_true" ,
324- help = "Clear Log file " )
323+ help = "Clear logfile that keeps track of stimela processes. This doesn't do anything ot the processes themselves. " )
325324
326325 args = parser .parse_args (argv )
327326
328- procs = stimela_logger . Process ( stimela . LOG_PROCESS )
329- procs .display ()
327+ log = logger . StimelaLogger ( LOG_FILE )
328+ log .display ('processes' )
330329 if args .clear :
331- procs .clear ()
330+ log .clear ('processes' )
331+ log .write ()
332332
333333
334334def kill (argv ):
@@ -344,42 +344,96 @@ def kill(argv):
344344
345345 args = parser .parse_args (argv )
346346
347- procs = stimela_logger . Process ( LOG_PROCESS )
347+ log = logger . StimelaLogger ( LOG_FILE )
348348
349- for pid in map ( int , args .pid ) :
349+ for pid in args .pid :
350350
351- found = procs . find ( pid )
351+ found = pid in log . info [ 'processes' ]. keys ( )
352352
353353 if not found :
354- print "Could not find process {0}" .format (pid )
354+ print ( "Could not find process {0}" .format (pid ) )
355355 continue
356356
357- conts = stimela_logger .Container (LOG_CONTAINERS )
358- lines = []
357+ try :
358+ os .kill (int (pid ), signal .SIGINT )
359+ except OSError :
360+ raise OSError ('Process with PID {} could not be killed' .format (pid ))
361+
362+ log .remove ('processes' , pid )
363+ log .write ()
359364
360- procs .rm (pid )
361- procs .write ()
362365
363- for cont in conts . lines :
364- if cont . find ( str ( pid )) > 0 :
365- lines . append ( cont )
366+ def clean ( argv ) :
367+ for i , arg in enumerate ( argv ) :
368+ if ( arg [ 0 ] == '-' ) and arg [ 1 ]. isdigit (): argv [ i ] = ' ' + arg
366369
367- for line in lines :
368- cont , _id , utime , _pid , status = line .split ()
369- if status .find ("removed" )< 0 :
370- cont_ = docker .Container (None , cont , None , None )
371- cont_ .started = True
372- cont_ .stop ()
373- cont_ .remove ()
370+ parser = ArgumentParser (description = 'Convience tools for cleaning up after stimela' )
371+ add = parser .add_argument
374372
375- conts .rm (line )
373+ add ("-ai" , "--all-images" , action = "store_true" ,
374+ help = "Remove all images pulled/built by stimela. This include CAB images" )
375+
376+ add ("-ab" , "--all-base" , action = "store_true" ,
377+ help = "Remove all base images" )
376378
377- conts .write ()
379+ add ("-ac" , "--all-cabs" , action = "store_true" ,
380+ help = "Remove all CAB images" )
381+
382+ add ("-aC" , "--all-containers" , action = "store_true" ,
383+ help = "Stop and/or Remove all stimela containers" )
384+
385+ args = parser .parse_args (argv )
386+
387+ log = logger .StimelaLogger (LOG_FILE )
388+
389+ if args .all_images :
390+ images = log .info ['images' ].keys ()
391+ for image in images :
392+ utils .xrun ('docker' , ['rmi' , image ])
393+ log .remove ('images' , image )
394+ log .write ()
395+
396+ if args .all_base :
397+ images = log .info ['images' ].keys ()
398+ for image in images :
399+ if log .info ['images' ][image ]['CAB' ] is False :
400+ utils .xrun ('docker' , ['rmi' , image ])
401+ log .remove ('images' , image )
402+ log .write ()
403+
404+ if args .all_cabs :
405+ images = log .info ['images' ].keys ()
406+ for image in images :
407+ if log .info ['images' ][image ]['CAB' ]:
408+ utils .xrun ('docker' , ['rmi' , image ])
409+ log .remove ('images' , image )
410+ log .write ()
411+
412+
413+ if args .all_containers :
414+ containers = log .info ['containers' ].keys ()
415+ for container in containers :
416+ cont = docker .Container (log .info ['containers' ][container ]['IMAGE' ], container )
417+ try :
418+ status = cont .info ()['State' ]['Status' ]
419+ except OSError :
420+ print ('Could not inspect container {}. It probably doesn\' t exist, will remove it from log' .format (container ))
421+ status = False
422+ log .remove ('containers' , container )
423+ log .write ()
424+ continue
425+
426+ if status == 'running' :
427+ # Kill the container instead of stopping it, so that effect can be felt py parent process
428+ utils .xrun ('docker' , ['kill' , container ])
429+ cont .remove ()
430+ log .remove ('containers' , container )
431+ log .write ()
432+ elif status == 'exited' :
433+ cont .remove ()
434+ log .remove ('containers' , container )
435+ log .write ()
378436
379- try :
380- os .kill (pid , signal .SIGKILL )
381- except OSError :
382- pass
383437
384438def main (argv ):
385439 for i , arg in enumerate (argv ):
@@ -404,7 +458,8 @@ def main(argv):
404458 options = []
405459 commands = dict (pull = pull , build = build , run = run ,
406460 images = images , cabs = cabs , ps = ps ,
407- containers = containers , kill = kill )
461+ containers = containers , kill = kill ,
462+ clean = clean )
408463
409464 command = "failure"
410465
@@ -438,6 +493,7 @@ def main(argv):
438493cabs : List active stimela containers
439494ps : List running stimela scripts
440495kill : Gracefully kill runing stimela process
496+ clean : Clean up tools for stimela
441497
442498""" )
443499
0 commit comments