-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit_repo.py
352 lines (273 loc) · 8.42 KB
/
git_repo.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
import pathlib
import tempfile
import subprocess
import urllib.parse
import logging
from os import getenv
import click
logger = logging.getLogger(__name__)
@click.group()
@click.option(
"--log-level",
help="Log level",
type=click.Choice(["critical", "error", "warning", "info", "debug"]),
default="critical",
show_default=True,
)
def cli(log_level):
handler = ClickLoggingHandler()
logger.addHandler(handler)
logger.setLevel(log_level.upper())
if getenv("HELM_DEBUG") == "1":
logger.setLevel("DEBUG")
@cli.command(
"add"
)
@click.argument("NAME")
@click.argument("GIT_URL")
@click.option(
"--branch",
default="master",
help="Git branch to use",
show_default=True,
)
@click.option(
"--index-path",
help="Path to index file in Git repository",
default="index.yaml",
show_default=True,
)
def add(name, git_url, branch, index_path):
"""
Add a Git chart repository.
"""
url = "git-repo+index:{!s}?branch={!s}&index_path={!s}&name={!s}".format(
git_url,
branch,
index_path,
name
)
sh("helm repo add {!s} '{!s}'".format(name, url), show=True)
@cli.command(
"index"
)
@click.argument(
"CHART_DIRS",
nargs=-1,
)
@click.option(
"--out",
help="File to write the index to",
type=click.File("w", atomic=True),
default="-",
show_default=True,
)
@click.option(
"--merge",
help="Merge the generated index from the given index",
type=click.Path(dir_okay=False, file_okay=True, exists=True),
default=None
)
@click.option("--skip-prompt/--no-skip-prompt", default=False)
def index(chart_dirs, out, merge, skip_prompt):
"""
Generate an index file from chart directories.
"""
if not chart_dirs:
click_exit(0)
if not skip_prompt:
_, git_log, _ = sh("git log -1")
click.secho(git_log, fg="yellow", err=True)
click.confirm(
"Proceed with files from this commit?",
default=True,
abort=True,
err=True
)
_, commit_ref, _ = sh("git log -1 --format='%H'")
_, git_root, _ = sh("git rev-parse --show-toplevel")
sh("helm repo update", show=True)
with tempfile.TemporaryDirectory() as tmp_dir:
tmp_dir = pathlib.Path(tmp_dir)
if merge is None:
merge = tmp_dir / "merge.yaml"
for chart_dir in chart_dirs:
chart_dir = pathlib.Path(chart_dir).resolve().relative_to(git_root)
sh(
"git --work-tree='{!s}' checkout '{!s}' -- '{!s}'".format(
tmp_dir,
commit_ref,
chart_dir
)
)
tmp_chart_dir = tmp_dir / chart_dir
sh(
"helm dependency update --skip-refresh '{!s}'".format(
tmp_chart_dir
)
)
sh(
"helm package --destination '{!s}' '{!s}'".format(
tmp_chart_dir,
tmp_chart_dir,
)
)
url = "git-repo+chart://{!s}/{!s}".format(
commit_ref,
chart_dir
)
sh(
"helm repo index --url '{!s}' --merge '{!s}' '{!s}'".format(
url,
merge,
tmp_chart_dir,
)
)
merge = tmp_chart_dir / "index.yaml"
with open(merge, "r") as index_fobj, out as out_fobj:
out_fobj.writelines(index_fobj)
click_exit(0, "Successfully wrote index to '{!s}'".format(out.name))
@cli.command("fetch", hidden=True)
@click.argument("cert_file")
@click.argument("key_file")
@click.argument("ca_file")
@click.argument("url")
def fetch(cert_file, key_file, ca_file, url):
"""
Output a chart tarball or repo index to STDOUT.
"""
logger.debug("input url %s", url)
if url.startswith("git-repo+index"):
print_index(url)
elif url.startswith("git-repo+chart"):
print_chart_tarball(url)
else:
click_exit(1, "Unable to handle {!s}".format(url))
def print_index(url):
parsed_url = urllib.parse.urlparse(url)
logger.debug("parsed url %r", parsed_url)
git_url = parsed_url.path
query_params = urllib.parse.parse_qs(parsed_url.query)
logger.debug("parsed query params %r", query_params)
repo_name = query_params.get("name", [None])[0]
git_branch = query_params.get("branch", ["master"])[0]
git_index_path = query_params.get("index_path", ["index.yaml"])[0]
plugin_home = pathlib.Path(getenv("HELM_PLUGIN_DIR"))
if not repo_name:
click_exit(1, "invalid url; must set 'name=' query paramater")
git_dir = plugin_home / "git" / repo_name
if not git_dir.exists():
git(git_dir, "init --bare '{!s}'".format(git_dir))
git(git_dir, "remote add origin '{!s}'".format(git_url))
git(
git_dir,
"fetch origin '{!s}:{!s}'".format(git_branch, git_branch),
)
_, out, _ = git(
git_dir,
"show '{!s}':'{!s}'".format(
git_branch,
git_index_path
)
)
out = out.replace(
"git-repo+chart://",
"git-repo+chart://{!s}/".format(repo_name)
)
click.echo(out)
def print_chart_tarball(url):
parsed_url = urllib.parse.urlparse(url)
logger.debug("parsed url %r", parsed_url)
repo_name = parsed_url.netloc
path = pathlib.Path(parsed_url.path)
commit_ref = path.parts[1]
git_path = "/".join(path.parts[2:-1])
logger.debug("repo_name: %s", repo_name)
logger.debug("commit_ref: %s", commit_ref)
logger.debug("git_path: %s", git_path)
plugin_home = pathlib.Path(getenv("HELM_PLUGIN_DIR"))
git_dir = plugin_home / "git" / repo_name
git(
git_dir,
"fetch origin {!s}:{!s}".format(commit_ref, commit_ref),
)
with tempfile.TemporaryDirectory() as tmp_dir:
tmp_dir = pathlib.Path(tmp_dir)
git(
git_dir,
"--work-tree='{!s}' checkout 'refs/heads/{!s}' -- '{!s}'".format(
tmp_dir,
commit_ref,
git_path
)
)
tmp_chart_dir = tmp_dir / git_path
sh(
"helm dependency update --skip-refresh '{!s}'".format(
tmp_chart_dir
)
)
sh(
"helm package --destination '{!s}' '{!s}'".format(
tmp_chart_dir,
tmp_chart_dir,
)
)
chart_tgz_loc = tmp_chart_dir / path.name
chart_tgz_fobj = click.open_file(chart_tgz_loc, "rb")
stdout_fobj = click.open_file("-", "wb")
with chart_tgz_fobj as chart_tgz_fobj, stdout_fobj as stdout_fobj:
stdout_fobj.writelines(chart_tgz_fobj)
def git(git_dir, cmd, *args, **kwargs):
return sh(
"git --git-dir '{!s}' {!s}".format(git_dir, cmd),
*args,
**kwargs
)
def click_exit(ret, msg=None):
logger.debug("exiting with returncode %s; message %s", ret, msg)
if msg:
if ret != 0:
click.secho(msg, fg="red", err=True)
else:
click.secho(msg, fg="green", err=True)
raise SystemExit(ret)
def sh(*args, show=False, hide_cmd=False, hide_out=False, hide_err=False,
exit_on_error=True, **kwargs):
proc = subprocess.Popen(
*args,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
**kwargs
)
logger.debug("cmd: %s", proc.args)
if show and not hide_cmd:
click.secho(proc.args, bold=True, err=True)
out, err = proc.communicate()
ret = proc.returncode
if out:
out = out.decode("utf-8").strip()
logger.debug("stdout: %s", out)
if out and show and not hide_out:
click.secho(out, fg="green", err=True)
if err:
err = err.decode("utf-8").strip()
logger.debug("stderr: %s", err)
if err and show and not hide_err:
click.secho(err, fg="red", err=True)
if exit_on_error and ret != 0:
raise SystemExit(ret)
return ret, out, err
class ClickLoggingHandler(logging.Handler):
def emit(self, record):
try:
click.secho(
self.format(record),
err=True,
**getattr(record, "click", {})
)
except Exception:
self.handleError(record)
if __name__ == "__main__":
cli.main(prog_name="helm git-repo")