-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtilt.py
More file actions
214 lines (171 loc) · 8.09 KB
/
Copy pathtilt.py
File metadata and controls
214 lines (171 loc) · 8.09 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
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
import os
from os.path import join, relpath, basename
from jinja2 import Environment, PackageLoader, select_autoescape
from cloudharness_model import HarnessMainConfig, GitDependencyConfig
from cloudharness_utils.constants import APPS_PATH, BASE_IMAGES_PATH, \
STATIC_IMAGES_PATH
from .helm import KEY_TASK_IMAGES
from .utils import find_dockerfiles_paths, app_name_from_path, get_image_name, \
guess_build_dependencies_from_dockerfile, get_image_source
from . import HERE, CH_ROOT
env = Environment(
loader=PackageLoader(package_name="ch_cli_tools", package_path="templates/tilt"),
autoescape=select_autoescape()
)
def relpath_if(p1, p2):
if os.path.isabs(p1):
return p1
return relpath(p1, p2)
def get_all_images(helm_values: HarnessMainConfig) -> dict[str, str]:
all_images = {**helm_values[KEY_TASK_IMAGES]}
for app_name, app in helm_values.apps.items():
if app.harness.deployment and app.harness.deployment.image:
all_images[app_name] = app.harness.deployment.image
return all_images
def create_tilt_configuration(root_paths, helm_values: HarnessMainConfig, manage_task_images=True, output_path='.', name='', namespace='', domain=''):
template_name = 'tilt-template.tpl'
tilt_template = env.get_template(template_name)
apps = helm_values.apps
artifacts = {}
overrides = {}
all_images = get_all_images(helm_values)
def remove_tag(image_name):
return image_name.split(":")[0]
def get_image_tag(name):
return remove_tag(all_images[name])
builds = {}
def build_artifact(
app_name: str,
context_path: str,
requirements: list[str] = None,
dockerfile_path: str = '',
additional_build_args: dict[str, str] = None,
is_app: bool = False,
is_task: bool = False,
parent_app_name: str = None,
app_key: str = None,
) -> dict:
app_key = app_key or app_name
build_args = {
'DEBUG': 'true' if helm_values.local or helm_values.debug else ''
}
if additional_build_args:
build_args.update(additional_build_args)
if requirements:
for req in requirements:
build_args.update({req.replace('-', '_').upper(): get_image_tag(req)})
build_args.update(get_image_source(helm_values))
image_name = get_image_tag(app_key)
artifact_spec = {
'name': app_name,
'app_key': app_key,
'image': image_name,
'context': context_path,
'is_app': is_app,
'is_task': is_task,
'parent_app_name': parent_app_name,
'docker': {
'dockerfile': join(dockerfile_path, 'Dockerfile'),
'buildArgs': build_args
}
}
return artifact_spec
base_images = set()
def process_build_dockerfile(
dockerfile_path: str,
root_path: str,
global_context: bool = False,
requirements: list[str] = None,
app_name: str = None,
app_key: str = None,
is_app: bool = True
) -> None:
if app_name is None:
app_name = app_name_from_path(basename(dockerfile_path))
is_app = False
app_key = app_key or app_name
if app_key in helm_values.apps and not helm_values.apps[app_key]['build']:
return
if app_name in helm_values[KEY_TASK_IMAGES] or app_key in helm_values.apps:
context_path = relpath_if(root_path, output_path) if global_context else relpath_if(dockerfile_path, output_path)
builds[app_name] = context_path
base_images.add(get_image_name(app_key))
artifacts[app_name] = build_artifact(
app_name,
context_path,
dockerfile_path=relpath(dockerfile_path, output_path),
requirements=requirements or guess_build_dependencies_from_dockerfile(dockerfile_path),
additional_build_args=get_additional_build_args(helm_values, app_key),
is_app=is_app,
app_key=app_key
)
if app_key in helm_values.apps and helm_values.apps[app_key].harness.dependencies and helm_values.apps[app_key].harness.dependencies.git:
artifacts[app_name]['hooks'] = {
'before': [git_clone_hook(conf, context_path) for conf in helm_values.apps[app_key].harness.dependencies.git]
}
images = set()
for root_path in root_paths:
base_dockerfiles = find_dockerfiles_paths(
join(root_path, BASE_IMAGES_PATH))
for dockerfile_path in base_dockerfiles:
process_build_dockerfile(dockerfile_path, root_path, global_context=True)
for root_path in root_paths:
static_dockerfiles = find_dockerfiles_paths(
join(root_path, STATIC_IMAGES_PATH))
for dockerfile_path in static_dockerfiles:
process_build_dockerfile(dockerfile_path, root_path)
for root_path in root_paths:
apps_path = join(root_path, APPS_PATH)
# Get all dockerfiles in the applications directory, including those in subdirectories
app_dockerfiles = find_dockerfiles_paths(apps_path)
for dockerfile_path in app_dockerfiles:
app_relative_to_skaffold = os.path.relpath(
dockerfile_path, output_path)
app_relative_to_base = os.path.relpath(dockerfile_path, apps_path)
app_name = app_name_from_path(app_relative_to_base)
app_key = app_name
if app_key not in apps:
if 'tasks' in app_relative_to_base and manage_task_images:
parent_app_name = app_name_from_path(
app_relative_to_base.split('/tasks')[0])
parent_app_key = parent_app_name
if parent_app_key in apps:
artifacts[app_key] = build_artifact(
app_name,
app_relative_to_skaffold,
guess_build_dependencies_from_dockerfile(dockerfile_path),
dockerfile_path=dockerfile_path,
is_task=True,
parent_app_name=parent_app_name)
elif app_name in helm_values[KEY_TASK_IMAGES]:
process_build_dockerfile(dockerfile_path, root_path,
requirements=guess_build_dependencies_from_dockerfile(dockerfile_path), dockerfile_path=dockerfile_path, app_name=app_name)
continue
if app_name in helm_values["apps"]:
is_app = helm_values["apps"][app_name]["harness"]["service"]["auto"]
app_name = helm_values["apps"][app_name]["harness"]["subdomain"]
process_build_dockerfile(dockerfile_path, root_path, requirements=guess_build_dependencies_from_dockerfile(dockerfile_path), app_name=app_name, app_key=app_key, is_app=is_app)
app = apps[app_key]
if not app['build']:
continue
images = [artifact for artifact in artifacts.values() if artifact['image']]
apps = [artifact for artifact in artifacts.values() if artifact['is_app']]
with open(os.path.join(output_path, 'Tiltfile'), "w") as f:
f.write(tilt_template.render(ch_root=CH_ROOT, name=name, namespace=namespace, images=images, apps=apps, domain=domain))
return None
def git_clone_hook(conf: GitDependencyConfig, context_path: str):
return {
'command': [
'sh',
join(os.path.dirname(os.path.dirname(HERE)), 'clone.sh'),
conf.branch_tag,
conf.url,
join(context_path, "dependencies", conf.path or os.path.basename(conf.url).split('.')[0])
]
}
def get_additional_build_args(helm_values: HarnessMainConfig, app_key: str) -> dict[str, str]:
if app_key not in helm_values.apps:
return None
if not (helm_values.apps[app_key].harness.dockerfile and helm_values.apps[app_key].harness.dockerfile.build_args):
return None
return helm_values.apps[app_key].harness.dockerfile.build_args