-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcontainer_detail.py
executable file
·80 lines (64 loc) · 2.04 KB
/
container_detail.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/env python3
# Author: SC - [email protected]
# v0.1 - aka - Tested to the point it looks like it works
import sys
import json
import tarfile
class dContainer(object):
def __init__(self, fname):
self.file_name = fname
self.manifest = None
self.config = None
self.layers = None
self.load()
self.parse_manifest()
def load(self):
try:
self.tfile = tarfile.open(sys.argv[1])
except Exception as e:
print(e)
raise e
def list_files(self):
for f in self.tfile.getnames():
yield f
def print_files(self):
for f in self.tfile.getnames():
print(f)
def check_file_exists(self, fname):
for f in self.list_files():
if fname == f:
return True
def get_file(self, fname):
try:
fdata = self.tfile.extractfile(fname)
return fdata
except Exception as e:
raise e
return None
def get_manifest(self):
if self.check_file_exists("manifest.json"):
fdata = self.get_file("manifest.json")
manifest = json.loads(fdata.read())
return manifest
return False
def parse_manifest(self):
m = self.get_manifest()
self.manifest = m
for l in m:
l_config = l.get('Config', False)
if l_config != False:
self.config = json.loads(self.get_file(l_config).read())
l_repo = l.get('RepoTags', False)
l_layers = l.get('Layers', False)
if l_layers != False:
self.layers = l_layers
def print_config(self):
print("Architecture: {:>20}".format(self.config.get('architecture', "?")))
print("Config:")
for l in self.config.get('history', {}):
print(" Timestamp: {:>30}: | {}".format(l.get('created', ''), l.get('created_by', '')))
def main():
c = dContainer(sys.argv[1])
c.print_config()
if __name__ == "__main__":
main()