-
Notifications
You must be signed in to change notification settings - Fork 239
/
prepare.py
474 lines (423 loc) · 15 KB
/
prepare.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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
import json
import pathlib
import subprocess
import sys
import time
import click
import paramiko
import requests
import yaml
import mlrun.utils
logger = mlrun.utils.create_logger(level="debug", name="automation")
class SystemTestPreparer:
class Constants:
ssh_username = "iguazio"
ci_dir_name = "mlrun-automation"
homedir = pathlib.Path("/home/iguazio/")
workdir = homedir / ci_dir_name
mlrun_code_path = workdir / "mlrun"
system_tests_env_yaml = pathlib.Path("tests") / "system" / "env.yml"
git_url = "https://github.com/mlrun/mlrun.git"
provctl_releases = "https://api.github.com/repos/iguazio/provazio/releases"
provctl_release_search_amount = 3
provctl_binary_format = "provctl-{release_name}-linux-amd64"
def __init__(
self,
mlrun_version: str,
mlrun_commit: str,
override_image_registry: str,
override_image_repo: str,
override_mlrun_images: str,
data_cluster_ip: str,
data_cluster_ssh_password: str,
app_cluster_ssh_password: str,
github_access_token: str,
mlrun_dbpath: str,
webapi_direct_http: str,
framesd_url: str,
username: str,
access_key: str,
iguazio_version: str,
password: str = None,
debug: bool = False,
):
self._logger = logger
self._debug = debug
self._mlrun_version = mlrun_version
self._mlrun_commit = mlrun_commit
self._override_image_registry = (
override_image_registry.strip().strip("/") + "/"
if override_image_registry is not None
else override_image_registry
)
self._override_image_repo = override_image_repo
self._override_mlrun_images = override_mlrun_images
self._data_cluster_ip = data_cluster_ip
self._data_cluster_ssh_password = data_cluster_ssh_password
self._app_cluster_ssh_password = app_cluster_ssh_password
self._github_access_token = github_access_token
self._iguazio_version = iguazio_version
self._env_config = {
"MLRUN_DBPATH": mlrun_dbpath,
"V3IO_API": webapi_direct_http,
"V3IO_FRAMESD": framesd_url,
"V3IO_USERNAME": username,
"V3IO_ACCESS_KEY": access_key,
}
if password:
self._env_config["V3IO_PASSWORD"] = password
self._logger.info("Connecting to data-cluster", data_cluster_ip=data_cluster_ip)
if not self._debug:
self._ssh_client = paramiko.SSHClient()
self._ssh_client.set_missing_host_key_policy(paramiko.WarningPolicy)
self._ssh_client.connect(
data_cluster_ip,
username=self.Constants.ssh_username,
password=data_cluster_ssh_password,
)
def run(self):
# for sanity clean up before starting the run
self.clean_up_remote_workdir(close_ssh_client=False)
self._prepare_test_env()
self._override_mlrun_api_env()
provctl_path = self._download_provctl()
self._patch_mlrun(provctl_path)
def clean_up_remote_workdir(self, close_ssh_client: bool = True):
self._logger.info(
"Cleaning up remote workdir", workdir=str(self.Constants.homedir)
)
self._run_command(
f"rm -rf {self.Constants.workdir}", workdir=str(self.Constants.homedir)
)
if close_ssh_client and not self._debug:
self._ssh_client.close()
def _run_command(
self,
command: str,
args: list = None,
workdir: str = None,
stdin: str = None,
live: bool = True,
suppress_errors: bool = False,
local: bool = False,
) -> str:
workdir = workdir or str(self.Constants.workdir)
stdout, stderr, exit_status = "", "", 0
log_command_location = "locally" if local else "on data cluster"
self._logger.debug(
f"Running command {log_command_location}",
command=command,
args=args,
stdin=stdin,
workdir=workdir,
)
if self._debug:
return ""
try:
if local:
stdout, stderr, exit_status = self._run_command_locally(
command, args, workdir, stdin, live, suppress_errors
)
else:
stdout, stderr, exit_status = self._run_command_remotely(
command, args, workdir, stdin, live, suppress_errors
)
except (paramiko.SSHException, RuntimeError) as exc:
self._logger.error(
f"Failed running command {log_command_location}",
command=command,
error=exc,
stdout=stdout,
stderr=stderr,
exit_status=exit_status,
)
raise
else:
self._logger.debug(
f"Successfully ran command {log_command_location}",
command=command,
stdout=stdout,
stderr=stderr,
exit_status=exit_status,
)
return stdout
def _run_command_remotely(
self,
command: str,
args: list = None,
workdir: str = None,
stdin: str = None,
live: bool = True,
suppress_errors: bool = False,
) -> (str, str, int):
workdir = workdir or self.Constants.workdir
stdout, stderr, exit_status = "", "", 0
command = f"cd {workdir}; " + command
if args:
command += " " + " ".join(args)
stdin_stream, stdout_stream, stderr_stream = self._ssh_client.exec_command(
command
)
if stdin:
stdin_stream.write(stdin)
stdin_stream.close()
if live:
while True:
line = stdout_stream.readline()
stdout += line
if not line:
break
print(line, end="")
else:
stdout = stdout_stream.read()
stderr = stderr_stream.read()
exit_status = stdout_stream.channel.recv_exit_status()
if exit_status != 0 and not suppress_errors:
raise RuntimeError(f"Command failed with exit status: {exit_status}")
return stdout, stderr, exit_status
@staticmethod
def _run_command_locally(
command: str,
args: list = None,
workdir: str = None,
stdin: str = None,
live: bool = True,
suppress_errors: bool = False,
) -> (str, str, int):
stdout, stderr, exit_status = "", "", 0
if workdir:
command = f"cd {workdir}; " + command
if args:
command += " " + " ".join(args)
process = subprocess.Popen(
command,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
stdin=subprocess.PIPE,
shell=True,
)
if stdin:
process.stdin.write(bytes(stdin, "ascii"))
process.stdin.close()
if live:
for line in iter(process.stdout.readline, b""):
stdout += str(line)
sys.stdout.write(line.decode(sys.stdout.encoding))
else:
stdout = process.stdout.read()
stderr = process.stderr.read()
exit_status = process.wait()
if exit_status != 0 and not suppress_errors:
raise RuntimeError(f"Command failed with exit status: {exit_status}")
return stdout, stderr, exit_status
def _get_provctl_version_and_url(self):
response = requests.get(
self.Constants.provctl_releases,
headers={"Authorization": f"token {self._github_access_token}"},
)
response.raise_for_status()
provazio_releases = json.loads(response.content)
stable_provazio_releases = list(
filter(lambda release: release["tag_name"] != "unstable", provazio_releases)
)
latest_provazio_releases = stable_provazio_releases[
: self.Constants.provctl_release_search_amount
]
for provazio_release in latest_provazio_releases:
for asset in provazio_release["assets"]:
if asset["name"] == self.Constants.provctl_binary_format.format(
release_name=provazio_release["name"]
):
self._logger.debug(
"Got provctl release url",
release=provazio_release["name"],
name=asset["name"],
url=asset["url"],
)
return asset["name"], asset["url"]
raise RuntimeError(
f"provctl binary not found in {self.Constants.provctl_release_search_amount} latest releases"
)
def _prepare_test_env(self):
self._run_command(
"mkdir", args=["-p", str(self.Constants.workdir)],
)
contents = yaml.safe_dump(self._env_config)
filepath = str(self.Constants.system_tests_env_yaml)
self._logger.debug("Populating system tests env.yml", filepath=filepath)
self._run_command(
"cat > ", args=[filepath], stdin=contents, local=True,
)
def _override_mlrun_api_env(self):
version_specifier = (
f"mlrun[complete] @ git+https://github.com/mlrun/mlrun@{self._mlrun_commit}"
if self._mlrun_commit
else "mlrun[complete]"
)
data = {
"MLRUN_HTTPDB__BUILDER__MLRUN_VERSION_SPECIFIER": version_specifier,
}
if self._override_image_registry:
data["MLRUN_IMAGES_REGISTRY"] = f"{self._override_image_registry}"
override_mlrun_registry_manifest = {
"apiVersion": "v1",
"data": data,
"kind": "ConfigMap",
"metadata": {"name": "mlrun-override-env", "namespace": "default-tenant"},
}
manifest_file_name = "override_mlrun_registry.yml"
self._run_command(
"cat > ",
args=[manifest_file_name],
stdin=yaml.safe_dump(override_mlrun_registry_manifest),
)
self._run_command(
"kubectl", args=["apply", "-f", manifest_file_name],
)
def _download_provctl(self):
provctl, provctl_url = self._get_provctl_version_and_url()
self._logger.debug("Downloading provctl to data node", provctl_url=provctl_url)
self._run_command(
"curl",
args=[
"--verbose",
"--location",
"--remote-header-name",
"--remote-name",
"--header",
'"Accept: application/octet-stream"',
"--header",
f'"Authorization: token {self._github_access_token}"',
provctl_url,
],
)
self._run_command("chmod", args=["+x", provctl])
return provctl
def _patch_mlrun(self, provctl_path):
time_string = time.strftime("%Y%m%d-%H%M%S")
self._logger.debug(
"Creating mlrun patch archive", mlrun_version=self._mlrun_version
)
mlrun_archive = f"./mlrun-{self._mlrun_version}.tar"
override_image_arg = ""
if self._override_mlrun_images:
override_image_arg = f"--override-images {self._override_mlrun_images}"
self._run_command(
f"./{provctl_path}",
args=[
f"--logger-file-path={str(self.Constants.workdir)}/provctl-create-patch-{time_string}.log",
"create-patch",
"appservice",
override_image_arg,
f"--target-iguazio-version={str(self._iguazio_version)}",
"mlrun",
self._mlrun_version,
mlrun_archive,
],
)
self._logger.info("Patching MLRun version", mlrun_version=self._mlrun_version)
self._run_command(
f"./{provctl_path}",
args=[
f"--logger-file-path={str(self.Constants.workdir)}/provctl-patch-mlrun-{time_string}.log",
"--app-cluster-password",
self._app_cluster_ssh_password,
"--data-cluster-password",
self._data_cluster_ssh_password,
"patch",
"appservice",
"mlrun",
mlrun_archive,
],
)
@click.group()
def main():
pass
@main.command(context_settings=dict(ignore_unknown_options=True))
@click.argument("mlrun-version", type=str, required=True)
@click.option(
"--override-image-registry",
"-oireg",
default=None,
help="Override default mlrun docker image registry.",
)
@click.option(
"--override-image-repo",
"-oirep",
default=None,
help="Override default mlrun docker image repository name.",
)
@click.option(
"--override-mlrun-images",
"-omi",
default=None,
help="Override default images (comma delimited list).",
)
@click.option(
"--mlrun-commit",
"-mc",
default=None,
help="The commit (in mlrun/mlrun) of the tested mlrun version.",
)
@click.argument("data-cluster-ip", type=str, required=True)
@click.argument("data-cluster-ssh-password", type=str, required=True)
@click.argument("app-cluster-ssh-password", type=str, required=True)
@click.argument("github-access-token", type=str, required=True)
@click.argument("mlrun-dbpath", type=str, required=True)
@click.argument("webapi-direct-url", type=str, required=True)
@click.argument("framesd-url", type=str, required=True)
@click.argument("username", type=str, required=True)
@click.argument("access-key", type=str, required=True)
@click.argument("iguazio-version", type=str, default=None, required=True)
@click.argument("password", type=str, default=None, required=False)
@click.option(
"--debug",
"-d",
is_flag=True,
help="Don't run the ci only show the commands that will be run",
)
def run(
mlrun_version: str,
mlrun_commit: str,
override_image_registry: str,
override_image_repo: str,
override_mlrun_images: str,
data_cluster_ip: str,
data_cluster_ssh_password: str,
app_cluster_ssh_password: str,
github_access_token: str,
mlrun_dbpath: str,
webapi_direct_url: str,
framesd_url: str,
username: str,
access_key: str,
iguazio_version: str,
password: str,
debug: bool,
):
system_test_preparer = SystemTestPreparer(
mlrun_version,
mlrun_commit,
override_image_registry,
override_image_repo,
override_mlrun_images,
data_cluster_ip,
data_cluster_ssh_password,
app_cluster_ssh_password,
github_access_token,
mlrun_dbpath,
webapi_direct_url,
framesd_url,
username,
access_key,
iguazio_version,
password,
debug,
)
try:
system_test_preparer.run()
except Exception as exc:
logger.error("Failed running system test automation", exc=exc)
raise
if __name__ == "__main__":
main()