88registered_patchers : typing .List [typing .Callable ] = []
99
1010
11- __version__ = '0.1.1 '
11+ __version__ = '0.1.2 '
1212
1313
14- def default_on_error (e : Exception ):
15- raise e
14+ def default_on_error (error : Exception ):
15+ raise error
16+
17+
18+ def _get (d : dict , key : str , default ):
19+ res = d .get (key , default )
20+ return default if res is None else res
1621
1722
1823def register_patcher (
@@ -39,44 +44,44 @@ def patch_image(
3944 on_patcher_error = default_on_error
4045
4146 orig_config = client .api .inspect_image (image .id )['Config' ]
42- orig_entrypoint = orig_config ['Entrypoint' ]
43- if not orig_entrypoint :
44- orig_entrypoint = ''
4547
4648 # create a container
47- logger .info (f 'Creating container for "{ image } "' )
48- c : Container = client .containers .run (
49+ logger .info ('Creating container for "%s"' , image )
50+ container : Container = client .containers .run (
4951 image , detach = True , entrypoint = '/bin/sh' ,
5052 remove = True , tty = True , network_mode = 'host'
5153 )
52- logger .info (f 'Container: "{ c . name } " (id: { c . id } )' )
54+ logger .info ('Container: "%s " (id: "%s")' , container . name , container . id )
5355
5456 try :
5557 for patch_func in registered_patchers :
5658 try :
57- logger .info (f 'calling patcher func "{ patch_func .__name__ } "' )
58- patch_func (c )
59- except Exception as e :
60- on_patcher_error (e )
59+ logger .info ('calling patcher func "%s"' , patch_func .__name__ )
60+ patch_func (container )
61+ except Exception as error :
62+ on_patcher_error (error )
6163
6264 # commit
6365 logger .info ('Commit container' )
64- result_image = c .commit (conf = {'Entrypoint' : orig_entrypoint })
66+ result_image = container .commit (conf = {
67+ 'Entrypoint' : _get (orig_config , 'Entrypoint' , []),
68+ 'Cmd' : _get (orig_config , 'Cmd' , [])
69+ })
6570
66- logger .info (f 'New image id: { result_image .id } ' )
71+ logger .info ('New image id: "%s"' , result_image .id )
6772
6873 # re-tag
69- logger .info (f 'tag new image with { len (image .tags )} tags' )
74+ logger .info ('tag new image with %d tags' , len (image .tags ))
7075 for tag in image .tags :
71- logger .info (f '-tag "{ tag } "' )
76+ logger .info ('-tag "%s"' , tag )
7277 result_image .tag (tag )
7378
7479 # fetch image again with tags
7580 return client .images .get (result_image .id )
7681 finally :
7782 # fetch the updated container object
78- c = client .containers .get (c .id )
79- if c .status == 'running' :
83+ container = client .containers .get (container .id )
84+ if container .status == 'running' :
8085 # stop container (should be removed)
81- logger .info (f 'Stopping container "{ c . name } "' )
82- c .stop ()
86+ logger .info ('Stopping container "%s"' , container . name )
87+ container .stop ()
0 commit comments