-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen.py
68 lines (58 loc) · 1.82 KB
/
gen.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
import os
import yaml
BOXES = 'boxes.yaml'
CONFIG = 'config.yaml'
BASE = 'Dockerfile'
SCRIPT = 'setup.sh'
PREINSTALL = 'preinstall.sh'
INSTALL = 'install.sh'
BUILD = 'build.sh'
PUSH = 'push.sh'
PATH = './boxes'
with open(CONFIG, 'r') as f:
config = yaml.load(f, Loader=yaml.FullLoader)
with open(INSTALL, 'r') as f:
install_sh = f.read()
ID = config['docker']['id']
REPO = config['docker']['repo']
AUTOGEN_SCRIPT =\
"""#!/bin/sh\n
# This file is generated automatically.
"""
def main():
with open(BASE, 'r') as f:
f.readline()
base = f.read()
with open(BOXES, 'r') as f:
yml = yaml.load(f, Loader=yaml.FullLoader)
for box_name, box in yml['boxes'].items():
dir_p = os.path.join(PATH, box_name)
setup = os.path.join(dir_p, SCRIPT)
preinstall = os.path.join(dir_p, PREINSTALL)
install = os.path.join(dir_p, INSTALL)
build = os.path.join(dir_p, BUILD)
push = os.path.join(dir_p, PUSH)
dockerfile = os.path.join(dir_p, 'Dockerfile')
if not os.path.exists(dir_p):
os.mkdir(dir_p)
if not os.path.exists(setup):
with open(setup, 'w') as f:
f.write(AUTOGEN_SCRIPT)
if not os.path.exists(preinstall):
with open(preinstall, 'w') as f:
f.write(AUTOGEN_SCRIPT)
if not os.path.exists(install):
with open(install, 'w') as f:
f.write(install_sh)
with open(dockerfile, 'w') as f:
f.write(f"FROM {box['image']}\n")
f.write(base)
with open(build, 'w') as f:
f.write(f"""#!/bin/sh
docker build . -t {ID}/{REPO}:{box['tag']}
""")
with open(push, 'w') as f:
f.write(f"""#!/bin/sh
docker push {ID}/{REPO}:{box['tag']}
""")
main()