-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathbuild.py
executable file
·131 lines (105 loc) · 3.69 KB
/
build.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/usr/bin/env python3
try:
import argparse
import docker
import logging
import yaml
except ImportError:
print(
'Import error.',
'You need to install requirements.',
'pip3 install -r requirements.txt',
sep='\n',
)
exit(1)
def build(level, registry_url, args):
try:
image_tag = '{registry_url}/suzenescape/{level_name}'.format(
registry_url = registry_url, level_name = level['name']
)
_, build_log = client.images.build(
path='chains/chain{level_chain}/level{level_vl}'.format(
level_chain=level['chain'].zfill(2), level_vl=level['level']
),
tag=image_tag,
buildargs={
'USERNAME': level['name'],
'CONFIG': level.get("config", "NONE"),
'USERHOME': "root" if level.get("rohome") else level["name"],
'FLAG': level.get("flag", "NONE"),
},
rm=True,
forcerm=True,
)
if args.verbose:
build_log_string = ''.join([item.get('stream', '') for item in build_log])[:-1]
logging.info(
'{image_tag} build log:\n{build_log}'.format(image_tag=image_tag, build_log=build_log_string)
)
if not args.build_only:
push(image_tag)
except (docker.errors.BuildError, docker.errors.APIError) as exc:
logging.error('build error:')
logging.error(exc)
exit(1)
return
def push(image):
result = client.images.push(image)
logging.info('push status:')
logging.info(result[:-1])
return
def argp():
parser = argparse.ArgumentParser()
parser.add_argument(
'-b', '--build_only', help='build only, not push images', action='store_true'
)
#parser.add_argument('-t', '--token', help='token, used as message to encrypt')
parser.add_argument('-v', '--verbose', help='log enable', action='count')
# parser.add_argument('-f', '--vars-yaml', help='path to yaml level vars file')
parser.add_argument('task', nargs='+', help='task to build list')
return parser.parse_args()
def query_add(qtask):
build_query[qtask] = levels_map[qtask]
build_query[qtask]['level'] = qtask
try:
servers = levels_map[qtask]['servers']
except KeyError:
servers = []
pass
for server in servers:
stask = server['name'][len('suzen') :]
build_query[stask] = server
build_query[stask]['level'] = stask
build_query[stask]['chain'] = levels_map[qtask]['chain']
if __name__ == '__main__':
logging.basicConfig(format='%(asctime)s %(message)s', level=logging.INFO)
client = docker.client.from_env()
try:
client.ping()
except docker.errors.APIError as exc:
logging.error('docker client not init')
logging.error(exc)
exit(1)
args = argp()
logging.info('Start')
try:
with open('ansible/vars.yaml', 'r') as stream:
yml = yaml.load(stream, Loader=yaml.BaseLoader)
except yaml.YAMLError as exc:
logging.error(exc)
exit(1)
registry_url = yml['registry_url']
levels_map = {level['name'][len('suzen') :]: level for level in yml['levels']}
build_query = {}
if args.task[0] == 'all':
for task in levels_map.keys():
query_add(task)
else:
for task in args.task:
if task in levels_map.keys():
query_add(task)
else:
logging.warning('{task} in not available: skipping'.format(task=task))
for level in build_query:
logging.info('build level {level}'.format(level=level))
build(build_query[level], registry_url, args)