-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmigrate-split-project
executable file
·250 lines (215 loc) · 11.1 KB
/
migrate-split-project
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
#!/usr/bin/env python3
import argparse
import glob
from pathlib import Path
import shutil
import subprocess
import sys
from typing import Any
import yaml
def _convert_values_filename(values_filename: str, chart: str = '.') -> str:
if values_filename.startswith(".."):
return values_filename
if chart == '.':
if values_filename in ("./values.yaml", "values.yaml"):
return "./values-base.yaml"
else:
if values_filename in (f"./{chart}/values.yaml", f"{chart}/values.yaml"):
return "./values-base.yaml"
if values_filename.startswith(f"./{chart}/") or values_filename.startswith(f"{chart}/"):
return f"./{values_filename[len(f'./{chart}/'):]}"
raise ValueError(f"Invalid values filename {values_filename}, chart {chart}")
def _move_application(release: dict[str, Any], helmfile: str, helmfile_path: Path, to_remove: set[str], dry_run: bool, verbose: bool = False):
release_name = release["name"]
application_path = helmfile_path.parent
new_application_path = helmfile_path.parent.parent / release_name
new_helmfile_path = new_application_path / "helmfile-base.yaml"
if application_path != new_application_path:
print(f"cp -ar {application_path / release['chart']} {new_application_path}")
if not dry_run:
#new_application_path.mkdir(parents=True, exist_ok=True)
shutil.copytree(application_path / release['chart'], new_application_path)
subprocess.run(["git", "add", new_application_path])
to_remove.add(application_path / release['chart'])
if len(helmfile['releases']) != 1:
new_release = {**release, 'chart': '.'}
if 'values' in new_release:
new_release['values'] = [_convert_values_filename(v, release['chart']) for v in new_release['values']]
for values_filename in new_release['values']:
if values_filename == './values-base.yaml':
print(f"git mv {new_application_path / 'values.yaml'} {new_application_path / 'values-base.yaml'}")
if not dry_run:
subprocess.run(["git", "mv", new_application_path / 'values.yaml', new_application_path / 'values-base.yaml'])
continue
new_helmfile = {
'repositories': helmfile['repositories'],
'releases': [new_release],
}
print(f"Create helmfile {new_helmfile_path}")
if verbose:
print('With:')
print(yaml.dump(new_helmfile))
if not dry_run:
with open(new_helmfile_path, "w") as new_helmfile_file:
yaml.dump(new_helmfile, new_helmfile_file)
subprocess.run(["git", "add", new_helmfile_path])
new_helmfile_bis = {
'repositories': helmfile['repositories'],
'releases': [{**release, 'chart': '.', 'values': ['./values.yaml']}],
}
new_helmfile_bis_path = new_application_path / "helmfile.yaml"
if new_helmfile_bis_path in to_remove:
to_remove.remove(new_helmfile_bis_path)
print(f"Create helmfile {new_helmfile_bis_path}")
if verbose:
print('With:')
print(yaml.dump(new_helmfile_bis))
if not dry_run:
with open(new_helmfile_bis_path, "w") as new_helmfile_bis_file:
yaml.dump(new_helmfile_bis, new_helmfile_bis_file)
subprocess.run(["git", "add", new_helmfile_bis_path])
else:
if release['chart'] == '.':
print(f"git mv {helmfile_path} {new_helmfile_path}")
if not dry_run:
subprocess.run(["git", "mv", helmfile_path, new_helmfile_path])
with open(new_helmfile_path) as new_helmfile_file:
new_helmfile = yaml.load(new_helmfile_file, Loader=yaml.SafeLoader)
new_release = new_helmfile['releases'][0]
new_release['values'] = [_convert_values_filename(v) for v in new_release['values']]
values_path = new_helmfile_path.parent / "values.yaml"
if values_path.exists():
print(f"git mv {values_path} {new_application_path / 'values-base.yaml'}")
if not dry_run:
subprocess.run(["git", "mv", values_path, new_application_path / 'values-base.yaml'])
with open(new_helmfile_path, "w") as new_helmfile_file:
yaml.dump(new_helmfile, new_helmfile_file)
new_helmfile_bis = {
'repositories': helmfile['repositories'],
'releases': [{**release, 'chart': '.', 'values': ['./values.yaml']}],
}
new_helmfile_bis_path = new_application_path / "helmfile.yaml"
if new_helmfile_bis_path in to_remove:
to_remove.remove(new_helmfile_bis_path)
print(f"Create helmfile {new_helmfile_bis_path}")
if verbose:
print('With:')
print(yaml.dump(new_helmfile_bis))
if not dry_run:
with open(new_helmfile_bis_path, "w") as new_helmfile_bis_file:
yaml.dump(new_helmfile_bis, new_helmfile_bis_file)
subprocess.run(["git", "add", new_helmfile_bis_path])
else:
print(f"The chart path should be '.' instead of '{release['chart']}'")
sys.exit(1)
new_values_path = new_application_path / "values.yaml"
print(f"Create value file {new_values_path}")
if verbose:
print('With: {}')
if new_values_path.exists() and not dry_run:
print(f"Extra values file found {new_values_path}")
sys.exit(1)
if not dry_run:
with new_values_path.open("w") as new_values_file:
new_values_file.write("{}")
subprocess.run(["git", "add", new_values_path])
def _main():
parser = argparse.ArgumentParser(description='Migration script to split a helmfile project into multiple project')
parser.add_argument('--status', action='store_true', help='List projects to migrate')
parser.add_argument('--verify', action='store_true', help='Verify destinations conflict')
parser.add_argument('--dry-run', action='store_true', help='Do not write files')
parser.add_argument('--verbose', action='store_true', help='Verbose output')
parser.add_argument('helmfile', nargs="?", type=str, help='Helmfile to migrate')
args = parser.parse_args()
if args.status:
print('Projects to migrate:')
for helmfile_filename in [*glob.glob("apps/int/**/helmfile.yaml", recursive=True), *glob.glob("apps/prod/**/helmfile.yaml", recursive=True)]:
with open(helmfile_filename) as helmfile_file:
helmfile = yaml.load(helmfile_file, Loader=yaml.SafeLoader)
if len(helmfile["releases"]) > 1:
print(f'Project {helmfile_filename} is not splitted')
continue
for release in helmfile["releases"]:
if release["name"] != Path(helmfile_filename).parent.name:
print(f'Project {helmfile_filename} is in the wrong folder, should be in {release["name"]}')
continue
sys.exit(0)
if args.verify:
print('Verify destination conflict')
print('---------------------------')
destinations = set()
for projects_directory_name in [*glob.glob("apps/int/*/*/", recursive=True), *glob.glob("apps/prod/*/*/", recursive=True)]:
project_path = Path(projects_directory_name)
helmfile_path = project_path / "helmfile.yaml"
if helmfile_path.exists():
with open(helmfile_path) as helmfile_file:
helmfile = yaml.load(helmfile_file, Loader=yaml.SafeLoader)
for release in helmfile["releases"]:
destination = project_path.parent / release['name']
if destination in destinations:
print(f'Destination {destination} already used')
destinations.add(destination)
else:
destination = project_path.parent / project_path.name
if destination in destinations:
print(f'Destination {destination} already used')
destinations.add(destination)
print("Destinations")
print("------------")
for destination in destinations:
print(destination)
print()
print(f'{len(destinations)} projects found')
sys.exit(0)
if not args.helmfile:
print('Please provide a folder to migrate')
sys.exit(1)
helmfile_path = Path(args.helmfile)
if not helmfile_path.exists():
print(f'{helmfile_path} does not exist')
sys.exit(1)
with open(helmfile_path) as helmfile_file:
helmfile = yaml.load(helmfile_file, Loader=yaml.SafeLoader)
to_remove = {helmfile_path}
if len(helmfile.get('releases', [])) == 0:
application_path = helmfile_path.parent
to_remove.clear()
print(f"git mv {helmfile_path} {application_path / 'helmfile-base.yaml'}")
if not args.dry_run:
subprocess.run(["git", "mv", helmfile_path, application_path / 'helmfile-base.yaml'])
with open(helmfile_path, 'w') as helmfile_file:
yaml.dump({
'repositories': [],
'releases': [],
}, helmfile_file)
if (application_path / 'values.yaml').exists():
print(f"git mv {application_path / 'values.yaml'} {application_path / 'values-base.yaml'}")
if not args.dry_run:
subprocess.run(["git", "mv", application_path / 'values.yaml', application_path / 'values-base.yaml'])
return
for release in helmfile["releases"]:
if release['chart'] != '.':
_move_application(release, helmfile, helmfile_path, to_remove, args.dry_run, args.verbose)
for release in helmfile["releases"]:
if release['chart'] == '.':
_move_application(release, helmfile, helmfile_path, to_remove, args.dry_run, args.verbose)
for folder_to_remove in to_remove:
print(f"git rm -r {folder_to_remove}")
if not args.dry_run:
subprocess.run(["git", "rm", "-r", folder_to_remove])
if not args.dry_run:
files = set()
for line in subprocess.run(["git", "status", "--porcelain"], stdout=subprocess.PIPE, encoding='utf-8').stdout.splitlines():
status, file_name = line.strip().split(' ', 1)
if status in ('M', 'A', 'R', "AM"):
files.add(file_name)
subprocess.run(["pre-commit", "run", "--all-files", "merge-helm-values"])
subprocess.run(["pre-commit", "run", "--all-files", "end-of-file-fixer"])
subprocess.run(["pre-commit", "run", "--all-files", "prettier"])
for line in subprocess.run(["git", "status", "--porcelain"], stdout=subprocess.PIPE, encoding='utf-8').stdout.splitlines():
status, file_name = line.strip().split(' ', 1)
if status in ('M', 'A', 'R', "AM"):
files.add(file_name)
subprocess.run(["git", "add", *files])
if __name__ == '__main__':
_main()