-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstart.py
More file actions
146 lines (115 loc) · 5.07 KB
/
start.py
File metadata and controls
146 lines (115 loc) · 5.07 KB
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
import os
import sys
import subprocess
import click
VALID_APPS = ['app', 'web']
VALID_ENVIRONMENTS = ['testing']
GIT_EXECUTABLE = os.getenv('GIT_EXECUTABLE', 'git')
ENVIRONMENTS = {
'testing': {
'head_title': 'Tenna Graph',
'head_description': 'Tenna Graph',
},
}
@click.group(chain=True)
def cli():
"""Automcatically build & deploy TennaGraph"""
@click.command()
@click.option('--environment', default='testing',
type=click.Choice(VALID_ENVIRONMENTS),
help="Environment to deploy: %s" % ', '.join(VALID_ENVIRONMENTS),
prompt="Environment to deploy: %s" % ', '.join(VALID_ENVIRONMENTS))
@click.option('--apps', default='all',
help="Apps to build: %s" % ', '.join(VALID_APPS),
prompt="Apps to build: %s, use commas for multiple" % ', '.join(VALID_APPS))
@click.option('--details/--no-details', default=False, help='Show detailed output')
@click.option('--interactive/--noninteractive', default=False, help='Enabled/Disabled interactive prompts')
def build(environment, apps, details, interactive):
"""Build microservices into Docker images"""
apps_list = apps.split(',')
# click.clear()
if 'all' in apps_list:
warning = 'Building all apps make take a few minutes.'
click.echo(click.style(warning, fg='yellow'))
if not interactive:
build_app(environment, details=details)
build_web(environment, details=details)
else:
# Changing abort to True will abort whole program execution
if click.confirm('Do you want to continue?', abort=False):
build_app(environment, details=details)
build_web(environment, details=details)
else:
for app in apps_list:
if app not in VALID_APPS:
raise click.ClickException("Invalid app %s" % app)
if not interactive:
getattr(sys.modules[__name__], "build_%s" % app)(environment, details=details)
else:
if click.confirm("Do you want to continue building %s?" % app):
getattr(sys.modules[__name__], "build_%s" % app)(environment, details=details)
# click.pause()
cli.add_command(build)
def build_app(environment, *args, **kwargs):
details = __details_helper(kwargs)
click.echo(click.style("Building 'app'", blink=True, bold=True))
if 'app_path' in kwargs:
app_path = kwargs['app_path']
else:
app_path = os.path.abspath(os.path.join('.', os.pardir, 'TennaGraph', 'app'))
version_path = os.path.join(app_path, 'VERSION')
version_file = open(version_path, 'r')
version = version_file.readline()
cmd = "cd %s; /bin/sh bin/release.sh" % app_path
build_result = subprocess.check_output(cmd, shell=True)
if details:
click.echo(click.style(build_result.decode('utf-8'), fg='magenta'))
new_version_file = open(version_path, 'r')
new_version = new_version_file.readline()
if new_version == version:
raise click.ClickException("App build failed, staying at version %s" % version)
click.echo(click.style("Successfully built App version %s" % new_version, fg='green'))
def build_web(environment, *args, **kwargs):
details = __details_helper(kwargs)
click.echo(click.style("Building 'web'", blink=True, bold=True))
if 'app_path' in kwargs:
app_path = kwargs['app_path']
else:
app_path = os.path.abspath(os.path.join('.', os.pardir, 'TennaGraph', 'web'))
version_path = os.path.join(app_path, 'VERSION')
version_file = open(version_path, 'r')
version = version_file.readline()
api_base = ENVIRONMENTS[environment]['api_base']
head_title = ENVIRONMENTS[environment]['head_title']
head_description = ENVIRONMENTS[environment]['head_description']
cmd = 'cd %s; API_BASE_URL=%s HEAD_TITLE=%s HEAD_DESCRIPTION=%s /bin/sh bin/release.sh' % (app_path, head_title, head_description, api_base)
build_result = subprocess.check_output(cmd, shell=True)
if details:
click.echo(click.style(build_result.decode('utf-8'), fg='magenta'))
new_version_file = open(version_path, 'r')
new_version = new_version_file.readline()
if new_version == version:
raise click.ClickException("Web build failed, staying at version %s" % version)
click.echo(click.style("Successfully built Web version %s" % new_version, fg='green'))
def __details_helper(kwargs):
if 'details' in kwargs and kwargs['details']:
return True
return False
def __write_build_version(file_path, identifier, version):
"""Update Kubernetes config file with new image version"""
with open(file_path) as fp:
lines = fp.readlines()
new_lines = []
for line in lines:
if line.find(identifier) > -1:
parts = line.split(identifier)
parts[-1] = version + '\n'
new_line = identifier.join(parts)
new_lines.append(new_line)
else:
new_lines.append(line)
fp2 = open(file_path, 'w')
fp2.write(''.join(new_lines))
fp2.close()
if __name__ == '__main__':
cli()