forked from lukapeschke/dockerfile-from-image
-
Notifications
You must be signed in to change notification settings - Fork 0
/
entrypoint.py
executable file
·54 lines (45 loc) · 1.46 KB
/
entrypoint.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/python3
from sys import argv
from docker import Client
class ImageNotFound(Exception):
pass
class MainObj(object):
def __init__(self):
super(MainObj, self).__init__()
self.cmds = []
self.cli = Client(base_url='unix://var/run/docker.sock')
self._get_image(argv[-1])
self.hist = self.cli.history(self.img['RepoTags'][0])
self._parse_history()
self.cmds.reverse()
self._print_cmds()
def _print_cmds(self):
for i in self.cmds:
print(i)
def _get_image(self, img_hash):
imgs = self.cli.images()
for i in imgs:
if img_hash in i['Id']:
self.img = i
return
raise ImageNotFound("Image {} not found\n".format(img_hash))
def _insert_step(self, step):
if "#(nop)" in step:
to_add = step.split("#(nop) ")[1]
else:
to_add = ("RUN {}".format(step))
to_add = to_add.replace("&&", "\\\n &&")
self.cmds.append(to_add.strip(' '))
def _parse_history(self, rec=False):
first_tag = False
actual_tag = False
for i in self.hist:
if i['Tags']:
actual_tag = i['Tags'][0]
if first_tag and not rec:
break
first_tag = True
self._insert_step(i['CreatedBy'])
if not rec:
self.cmds.append("FROM {}".format(actual_tag))
my_obj = MainObj()