forked from SAP/InfraBox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ib.py
executable file
·237 lines (195 loc) · 8.27 KB
/
ib.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#!/usr/bin/env python
import argparse
import os
import re
import subprocess
import logging
import sys
logging.basicConfig(
format='%(asctime)s,%(msecs)d %(levelname)-8s [%(filename)s:%(lineno)d] %(message)s',
datefmt='%d-%m-%Y:%H:%M:%S',
level=logging.WARNING
)
logger = logging.getLogger('ib')
IMAGES = [
{'name': 'gerrit-api', 'depends_on': ['images-base']},
{'name': 'gerrit-trigger', 'depends_on': ['images-base']},
{'name': 'gerrit-review', 'depends_on': ['images-base']},
{'name': 'github-trigger', 'depends_on': ['images-base']},
{'name': 'github-review', 'depends_on': ['images-base']},
{'name': 'collector-api', 'depends_on': ['images-base']},
{'name': 'job'},
{'name': 'opa'},
{'name': 'gc', 'depends_on': ['images-base']},
{'name': 'scheduler-kubernetes', 'depends_on': ['images-base']},
{'name': 'api', 'depends_on': ['images-base']},
{'name': 'build-dashboard-client'},
{'name': 'static', 'depends_on': ['build-dashboard-client']},
{'name': 'docker-registry-auth'},
{'name': 'docker-registry-nginx', 'depends_on': ['images-base']},
{'name': 'db', 'depends_on': ['images-base']},
{'name': 'postgres'},
{'name': 'service-aks'},
{'name': 'service-gardener'},
{'name': 'service-gcp'},
{'name': 'service-namespace'},
{'name': 'metrics'},
{'name': 'checker', 'depends_on': ['images-base']},
{'name': 'cluster-status', 'depends_on': ['images-base']},
{'name': 'status-cachet', 'depends_on': ['images-base']},
{'name': 'images-base'},
{'name': 'images-test'},
]
def execute(command, cwd=None, env=None, ignore_error=False, ignore_output=False):
if env is None:
env = os.environ
process = subprocess.Popen(command,
shell=False,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
cwd=cwd,
env=env,
universal_newlines=True)
# Poll process for new output until finished
while True:
line = process.stdout.readline()
if not line:
break
if ignore_output:
continue
print(line.rstrip())
process.wait()
if ignore_error:
return
exitCode = process.returncode
if exitCode != 0:
raise Exception(exitCode)
def _build_image(image, args):
if image.get('executed', False):
return
for d in image.get('depends_on', []):
for i in IMAGES:
if i['name'] == d:
_build_image(i, args)
break
image_name = '%s/infrabox/%s:%s' % (args.registry, image['name'], args.tag)
if image['name'] in ('images-base', 'images-test'):
job = 'ib/images/%s' % image['name']
else:
job = 'ib/deploy/%s' % image['name']
execute(['infrabox', 'run', job, '-t', image_name])
image['executed'] = True
def images_build(args):
for i in IMAGES:
if not re.match(args.filter, i['name']):
continue
_build_image(i, args)
if args.push:
images_push(args)
def images_push(args):
for image in IMAGES:
if not re.match(args.filter, image['name']):
continue
image_name = '%s/infrabox/%s:%s' % (args.registry, image['name'], args.tag)
if args.type == 'registry':
execute(['docker', 'push', image_name])
elif args.type == 'gcr':
execute(['gcloud', 'docker', '--', 'push', image_name])
else:
print('invalid type')
sys.exit(1)
def services_start(args):
if args.service_name == 'storage':
execute(['docker-compose', 'up'],
cwd=os.path.join(os.path.dirname(os.path.realpath(__file__)), 'infrabox', 'utils', 'storage'))
elif args.service_name == 'api':
p = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'src', 'api')
run = os.path.join(p, 'run_with_dummy.sh')
execute(['bash', run], cwd=p)
elif args.service_name == 'opa':
execute(['docker-compose', 'up'],
cwd=os.path.join(os.path.dirname(os.path.realpath(__file__)), 'infrabox', 'utils', 'opa'))
elif args.service_name == 'dashboard-client':
p = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'src', 'dashboard-client')
execute(['npm', 'run', 'dev'], cwd=p)
else:
print("Unknown service")
sys.exit(1)
def services_rm(args):
if args.service_name == 'storage':
execute(['docker-compose', 'rm', '-f'],
cwd=os.path.join(os.path.dirname(os.path.realpath(__file__)), 'infrabox', 'utils', 'storage'))
elif args.service_name == 'opa':
execute(['docker-compose', 'rm', '-f'],
cwd=os.path.join(os.path.dirname(os.path.realpath(__file__)), 'infrabox', 'utils', 'opa'))
else:
print("Unknown service")
sys.exit(1)
def services_kill(args):
if args.service_name == 'storage':
execute(['docker-compose', 'kill'],
cwd=os.path.join(os.path.dirname(os.path.realpath(__file__)), 'infrabox', 'utils', 'storage'))
elif args.service_name == 'opa':
execute(['docker-compose', 'kill'],
cwd=os.path.join(os.path.dirname(os.path.realpath(__file__)), 'infrabox', 'utils', 'opa'))
else:
print("Unknown service")
sys.exit(1)
def changelog_create(args):
# Build docker container
container_name = 'infrabox_changelog_container'
command = ['docker', 'build', '-t', container_name, 'src/utils/changelog_generator']
execute(command, cwd=os.getcwd())
command = ['docker', 'run', '-it', '-v', os.getcwd() + ':/infrabox_changelog', container_name]
changelog_token = os.environ.get('GITHUB_CHANGELOG_TOKEN')
if args.token is not None or changelog_token is not None:
# Token provided via arg has higher priority
if args.token is not None:
changelog_token = args.token
command.append('--token')
command.append(changelog_token)
command.append('--no-verbose')
repo_name = 'infrabox/infrabox'
command.append(repo_name)
execute(command, cwd=os.getcwd())
def main():
parser = argparse.ArgumentParser(prog="ib")
cmd_parser = parser.add_subparsers()
# Images
images = cmd_parser.add_parser('images')
sub_images = images.add_subparsers()
images_build_parser = sub_images.add_parser('build')
images_build_parser.set_defaults(func=images_build)
images_build_parser.add_argument("--registry", default='quay.io')
images_build_parser.add_argument("--tag", default='latest')
images_build_parser.add_argument("--filter", default='.*')
images_build_parser.add_argument("--push", action='store_true', default=False)
images_build_parser.add_argument("--type", default='registry')
images_push_parser = sub_images.add_parser('push')
images_push_parser.set_defaults(func=images_push)
images_push_parser.add_argument("--registry", default='quay.io')
images_push_parser.add_argument("--tag", default='latest')
images_push_parser.add_argument("--filter", default='.*')
images_push_parser.add_argument("--type", default='registry')
# Service
services = cmd_parser.add_parser('services')
sub_services = services.add_subparsers()
services_start_parser = sub_services.add_parser('start')
services_start_parser.set_defaults(func=services_start)
services_start_parser.add_argument("service_name", nargs="?", type=str, help="Service name")
services_rm_parser = sub_services.add_parser('rm')
services_rm_parser.set_defaults(func=services_rm)
services_rm_parser.add_argument("service_name", nargs="?", type=str, help="Service name")
services_kill_parser = sub_services.add_parser('kill')
services_kill_parser.set_defaults(func=services_kill)
services_kill_parser.add_argument("service_name", nargs="?", type=str, help="Service name")
# Github changelog
changelog = cmd_parser.add_parser('changelog')
sub_changelog = changelog.add_subparsers()
create_changelog_parser = sub_changelog.add_parser('create')
create_changelog_parser.set_defaults(func=changelog_create)
create_changelog_parser.add_argument("--token", required=False)
args = parser.parse_args()
args.func(args)
if __name__ == '__main__':
main()