forked from getupcloud/helm-charts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helm-mirror
executable file
·63 lines (51 loc) · 1.95 KB
/
helm-mirror
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
#!/usr/bin/env python3
import os
import sys
import yaml
import requests
from urllib.parse import urlparse, urljoin
if len(sys.argv) != 4:
print("Usage: {sys.argv[0]} [root-dir] [repo-prefix] [repo-url]")
print("Examples:")
print(" helm-mirror ./repo prometheus-community https://prometheus-community.github.io/helm-charts")
print(" helm-mirror /var/www/charts internal/prometheus-community https://prometheus-community.github.io/helm-charts")
sys.exit(1)
repo_root = sys.argv[1]
repo_prefix = sys.argv[2]
root = os.path.join(repo_root, repo_prefix)
def save_url(root, url):
parsed_url = urlparse(url)
path = os.path.join(root, parsed_url.path.lstrip('/'))
filename = os.path.basename(path)
if os.path.exists(path):
#print(f'exists: {parsed_url.geturl()} -> {path}')
return parsed_url.path
print(f'get: {url} -> ', end='')
response = requests.get(url)
os.makedirs(os.path.dirname(path), exist_ok=True)
print(f'{path}')
with open(path, 'wb') as fd:
for chunk in response.iter_content(chunk_size=4096):
fd.write(chunk)
return parsed_url.path
for repo in sys.argv[3:]:
repo_index = '/'.join([repo.strip('/'), 'index.yaml'])
print(f'Index: {repo_index}')
r = requests.get(repo_index)
if r.encoding != r.apparent_encoding:
r.encoding = r.apparent_encoding
if not r.encoding:
r.encoding = 'utf-8'
index = yaml.load(r.text, Loader=yaml.FullLoader)
for name, charts in index['entries'].items():
for chart in charts:
url = chart['urls'][0]
chart_url = urljoin(repo_index, url)
path = save_url(root, chart_url)
chart['urls'] = [ os.path.join(os.path.join('/', repo_prefix), path.lstrip('/')) ]
index_url = urlparse(repo_index)
index_filename = os.path.join(root, index_url.path.lstrip('/'))
print(f'Saving index: {index_filename}')
os.makedirs(os.path.dirname(index_filename), exist_ok=True)
with open(index_filename, 'w') as fd:
fd.write(yaml.dump(index, default_flow_style=False))