-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.py
executable file
·183 lines (159 loc) · 6.84 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#! /usr/bin/env python3
import argparse
import os
import subprocess
import sys
import yaml
from pathlib import Path
from jinja2 import Template
from pprint import pprint
MACROS = '''
{% macro all_volumes() %}
{%- for v in volumes -%}
- {{ v }}:/service-config/{{ v }}
{% endfor -%}
{% endmacro -%}
{% macro all_servers() %}
{%- for v in http_ports -%}
- {{ v }}
{% endfor -%}
{% endmacro -%}
'''
class Builder:
def __init__(self, conf):
self._conf = conf
with open(os.path.join('apps', f'{conf.app}.yml')) as file:
self._app = yaml.safe_load(file)
def run(self):
self._errors = 0
self._build()
self._deploy()
def _error(self, condition, msg):
if condition:
self._errors += 1
print(f"***** {msg}")
return condition
def _check_errors(self):
if self._errors > 0:
print("***** ERRORS in spec - aborting.")
sys.exit(1)
def _read_specs(self, services, **args):
specs = dict()
vols = list()
ports = dict()
for service in services:
try:
file_name = os.path.join('services', service, 'compose.yml')
with open(file_name) as file:
template = Template(MACROS + '\n' + file.read())
# print('>'*50, template.render(services=services, **args))
spec = yaml.safe_load(template.render(services=services, **args))
vols.extend([ v.split(':')[0] for v in spec.get('volumes') or [] ])
self._error(not isinstance(spec, dict), f"Malformed compose specifiation for {service}")
specs[service] = spec
except FileNotFoundError:
self._error(True, f"No 'compose.yml' file for service {service} ({file_name})")
else:
self._check_errors()
if 'build' in spec and 'image' in spec:
print(f"service {service}: found 'build' and 'image' directives - building & ignoring image")
spec.pop('image', None)
if spec.get('build'):
spec['build'] = os.path.join('services', service)
if spec.get('http_port'):
ports[service] = (spec['http_port'], spec.get('network_mode') == 'host')
spec.pop('http_port', None)
self._check_errors()
args['services'] = services
args['volumes'] = list(set(vols))
args['http_ports'] = ports
return (specs, args)
def _build(self):
errors = 0
# secrets
for name in [ '/service-config/config/.secrets.yml',
'~/Documents/service-config/config/.secrets.yml' ]:
name = os.path.expanduser(name)
if os.path.exists(name):
with open(name) as file:
secrets = yaml.safe_load(file)
print(f"read secrets from {name}")
break
else:
print("***** WARNING: .secrets.yaml not found")
secrets = {
'TZ': 'America/Los_Angeles'
}
# sys.exit(1)
# services used by app
services = self._app.get('services')
# pass 1: extract volumes from compose.yml files
specs, args = self._read_specs(services, secrets=secrets, volumes=[], http_ports={})
# pass 2: process compose.yml files with correct volumes data
specs, args = self._read_specs(**args)
# assemble docker-compose.yml
dc = dict()
dc['version'] = '2'
dc['services'] = specs
# balena needs listing of all volumes
volumes = args['volumes']
if len(volumes) > 0: dc['volumes'] = dict.fromkeys(volumes)
# add path to volumes (when running with docker rather than balena)
volumes_dir = self._app.get('volumes_dir')
if volumes_dir:
for spec in specs.values():
if spec.get('volumes'):
spec['volumes'] = list(set([ os.path.join(volumes_dir, v) for v in spec.get('volumes') ]))
for v in spec.get('volumes'):
p, _ = v.split(':')
Path(p).mkdir(parents=True, exist_ok=True)
# write docker-compose.yml
def represent_none(self, _):
return self.represent_scalar('tag:yaml.org,2002:null', '')
yaml.add_representer(type(None), represent_none)
with open(f'docker-compose.yml', 'w') as file:
file.write(f"# MACHINE GENERATED from {conf.app} - DO NOT EDIT\n\n")
yaml.dump(dc, file, default_flow_style=False, sort_keys=False)
def _deploy(self):
if self._conf.action == 'none':
return
if not self._app.get('fleets'):
return
for fleet in self._app.get('fleets'):
try:
print(f"{'-'*30} {self._conf.action} to fleet {fleet}")
cmd = [ 'balena', self._conf.action, fleet ]
if conf.nocache: cmd.append('--nocache')
if conf.build: cmd.append('--build')
if conf.debug: cmd.append('--debug')
print(f"{' '.join(cmd)}")
subprocess.run(cmd, check=True)
except subprocess.CalledProcessError:
print(f"***** {self._conf.action} to {fleet} failed")
sys.exit(1)
if not conf.tag: continue
ps = subprocess.Popen(('balena', 'releases', fleet), stdout=subprocess.PIPE)
release = subprocess.check_output(('awk', 'NR==2 {print $1}'), stdin=ps.stdout)
ps.wait()
release = release.decode().strip()
os.system(f"balena tag set {conf.tag} --release {release}")
def args(argv):
parser = argparse.ArgumentParser(description='Assemble docker app from spec file and compose-template and push to balena fleet')
parser.add_argument('app',
help='app specification in app/ folder')
parser.add_argument('action', default='deploy', nargs='?',
help='deploy (default, build locally), push (build on Balena server), build (build locally but do not deploy) or none (only create docker-compose.yml)')
parser.add_argument('--tag', default=None,
help='optional balena release tag')
parser.add_argument('--nocache', action='store_true',
help="don't use previously built images when building the app")
parser.add_argument('--build', action='store_true',
help="force build (deploy)")
parser.add_argument('--debug', '-d', action='store_true',
help="print debugging output")
return parser.parse_args()
if __name__ == '__main__':
conf = args(sys.argv)
print(conf)
builder = Builder(args(sys.argv))
builder.run()