gitlab-runner-tart-driver 0.3.3__py3-none-any.whl → 0.3.5__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- __version__ = "0.3.3"
1
+ __version__ = "0.3.5"
@@ -9,8 +9,7 @@ from gitlab_runner_tart_driver.commands.run import run
9
9
 
10
10
  @click.group()
11
11
  @click.version_option(__version__, "--version", "-v", message="%(version)s")
12
- def cli():
13
- ...
12
+ def cli(): ...
14
13
 
15
14
 
16
15
  cli.add_command(config)
@@ -1,5 +1,6 @@
1
1
  import os
2
2
  import sys
3
+ import time
3
4
 
4
5
  import click
5
6
 
@@ -115,6 +116,12 @@ def prepare(
115
116
  if not p.tart_executor_timeout:
116
117
  p.tart_executor_timeout = timeout
117
118
 
119
+ # retrieve the SYSTEM_FAILURE_EXIT_CODE from the environment
120
+ system_failure_exit_code = os.getenv("SYSTEM_FAILURE_EXIT_CODE", None)
121
+ if not system_failure_exit_code:
122
+ click.secho("[WARNING] SYSTEM_FAILURE_EXIT_CODE not set, defaulting to '1'", fg="red")
123
+ system_failure_exit_code = 1
124
+
118
125
  tart = Tart(exec_path=tart_executable)
119
126
  tart_images = tart.list()
120
127
  tart_vm_map = {}
@@ -157,7 +164,7 @@ def prepare(
157
164
  tart.pull(p.ci_job_image)
158
165
  except:
159
166
  click.secho(f"[ERROR] Failed to pull image '{p.ci_job_image}'", fg="red")
160
- sys.exit(1)
167
+ sys.exit(system_failure_exit_code)
161
168
  else:
162
169
  click.echo(f"[INFO] Skipping '{p.ci_job_image}' [pull_policy={p.pull_policy}]")
163
170
 
@@ -182,14 +189,14 @@ def prepare(
182
189
  click.secho(
183
190
  f"[ERROR] The limit of running VMs [{p.tart_max_vm_count}] is exceeded [{len(list_running_vms)}].", fg="red"
184
191
  )
185
- sys.exit(1)
192
+ sys.exit(system_failure_exit_code)
186
193
 
187
194
  click.echo(f"[INFO] Cloning VM instance '{tart_vm_name}' from '{p.ci_job_image}'")
188
195
  try:
189
196
  tart.clone(p.ci_job_image, tart_vm_name)
190
197
  except:
191
198
  click.secho(f"[ERROR] failed to clone image f'{p.ci_job_image}'", fg="red")
192
- sys.exit(1)
199
+ sys.exit(system_failure_exit_code)
193
200
 
194
201
  if cpu or memory or p.display:
195
202
  click.echo(f"[INFO] Configuring instance '{tart_vm_name}' from '{p.ci_job_image}'")
@@ -228,24 +235,19 @@ def prepare(
228
235
  for v in volumes:
229
236
  volume_mounts.append(TartVolume.from_string(v))
230
237
 
231
- try:
232
- tart.run(tart_vm_name, volume_mounts, no_graphics=p.headless, softnet=p.softnet_enabled)
233
- except:
234
- click.secho(f"[ERROR] Failed to start VM '{tart_vm_name}'", fg="red")
235
- sys.exit(1)
236
-
237
- try:
238
- ip = tart.ip(tart_vm_name, timeout=p.timeout)
239
- except:
240
- click.secho(f"[ERROR] Failed to get IP of VM '{tart_vm_name}'", fg="red")
241
- sys.exit(1)
242
-
243
- if not ip:
244
- click.echo(f"[ERROR] Error, VM was not reacheable after '{p.timeout}' seconds")
245
- sys.exit(1)
238
+ _create_vm(
239
+ tart_client=tart,
240
+ vm_name=tart_vm_name,
241
+ volume_mounts=volume_mounts,
242
+ params=p,
243
+ system_failure_exit_code=system_failure_exit_code,
244
+ )
245
+ _get_vm_ip(tart_client=tart, vm_name=tart_vm_name, params=p, system_failure_exit_code=system_failure_exit_code)
246
+ ssh_session = _create_ssh_session(
247
+ tart_client=tart, vm_name=tart_vm_name, params=p, system_failure_exit_code=system_failure_exit_code
248
+ )
246
249
 
247
250
  try:
248
- ssh_session = tart.ssh_session(name=p.vm_name(), username=p.ssh_username, password=p.ssh_password)
249
251
  ssh_session.exec_ssh_command(
250
252
  f"sudo mkdir -p {remote_script_dir} && sudo chown {p.ssh_username}:{p.ssh_username} {remote_script_dir}",
251
253
  )
@@ -268,7 +270,7 @@ def prepare(
268
270
  )
269
271
  except:
270
272
  click.secho(f"[ERROR] Failed so prepare VM '{tart_vm_name}'", fg="red")
271
- sys.exit(1)
273
+ sys.exit(system_failure_exit_code)
272
274
 
273
275
  if p.install_gitlab_runner:
274
276
  click.echo(
@@ -278,8 +280,90 @@ def prepare(
278
280
  tart.install_gitlab_runner(name=tart_vm_name, username=p.ssh_username, password=p.ssh_password)
279
281
  except:
280
282
  click.secho(f"[ERROR] Failed to install GitLab Runner '{gitlab_runner_version}'", fg="red")
281
- sys.exit(1)
283
+ sys.exit(system_failure_exit_code)
282
284
 
283
285
  tart.print_spec(tart_vm_name)
284
286
 
285
287
  sys.exit(0)
288
+
289
+
290
+ def _create_vm(
291
+ tart_client, vm_name, volume_mounts, params, system_failure_exit_code, max_retries=3, retry_timeout_in_sec=5
292
+ ):
293
+ try:
294
+ tart_client.run(vm_name, volume_mounts, no_graphics=params.headless, softnet=params.softnet_enabled)
295
+ # check if vm is listed
296
+ retry_count = 0
297
+ while True:
298
+ try:
299
+ vm = tart_client.get(vm_name)
300
+ if vm:
301
+ click.echo(f"[INFO] VM '{vm_name}' is running")
302
+ break
303
+ else:
304
+ raise Exception("VM not found")
305
+ except Exception:
306
+ if retry_count < max_retries:
307
+ retry_count += 1
308
+ click.secho(
309
+ f"[WARNING] Failed to get IP of VM '{vm_name}'. [{retry_count+1}/{max_retries}] Retrying in '{retry_timeout_in_sec}' seconds...",
310
+ fg="yellow",
311
+ )
312
+ time.sleep(retry_timeout_in_sec)
313
+ else:
314
+ click.secho(f"[ERROR] VM '{vm_name}' could not be started.")
315
+ sys.exit(system_failure_exit_code)
316
+ except Exception:
317
+ click.secho(f"[ERROR] Failed to start VM '{vm_name}'", fg="red")
318
+ sys.exit(system_failure_exit_code)
319
+
320
+
321
+ def _get_vm_ip(tart_client, vm_name, params, system_failure_exit_code, max_retries=3, retry_timeout_in_sec=5):
322
+ retry_count = 0
323
+ while True:
324
+ try:
325
+ vm_ip_address = tart_client.ip(vm_name, timeout=params.timeout)
326
+ if vm_ip_address:
327
+ break
328
+ else:
329
+ raise Exception("VM IP not found")
330
+ except Exception:
331
+ if retry_count < max_retries:
332
+ retry_count += 1
333
+ click.secho(
334
+ f"[WARNING] VM with name '{vm_name}' was not found. Retrying in '{retry_timeout_in_sec}' seconds...",
335
+ fg="yellow",
336
+ )
337
+ time.sleep(retry_timeout_in_sec)
338
+ else:
339
+ click.secho(f"[ERROR] Failed to get IP of VM '{vm_name}'.")
340
+ sys.exit(system_failure_exit_code)
341
+
342
+ return vm_ip_address
343
+
344
+
345
+ def _create_ssh_session(tart_client, vm_name, params, system_failure_exit_code, max_retries=3, retry_timeout_in_sec=5):
346
+ ssh_session = None
347
+ retry_count = 0
348
+ while True:
349
+ try:
350
+ ssh_session = tart_client.ssh_session(
351
+ name=vm_name, username=params.ssh_username, password=params.ssh_password
352
+ )
353
+ if ssh_session:
354
+ break
355
+ else:
356
+ raise Exception("SSH Session could not be established.")
357
+ except Exception:
358
+ if retry_count < max_retries:
359
+ retry_count += 1
360
+ click.secho(
361
+ f"[WARNING] Failed to setup ssh connection with '{vm_name}'. Retrying in '{retry_timeout_in_sec}' seconds...",
362
+ fg="yellow",
363
+ )
364
+ time.sleep(retry_timeout_in_sec)
365
+ else:
366
+ click.secho(f"[ERROR] Failed to setup ssh connection with '{vm_name}'.")
367
+ sys.exit(system_failure_exit_code)
368
+
369
+ return ssh_session
@@ -32,6 +32,18 @@ def run(ssh_timeout, tart_executable, shell, script, stage):
32
32
  """Run commands."""
33
33
  p = GitLabCustomCommandConfig()
34
34
 
35
+ # retrieve the BUILD_FAILURE_EXIT_CODE from the environment
36
+ build_failure_exit_code = os.getenv("BUILD_FAILURE_EXIT_CODE", None)
37
+ if not build_failure_exit_code:
38
+ click.secho("[WARNING] BUILD_FAILURE_EXIT_CODE not set, defaulting to '1'", fg="red")
39
+ build_failure_exit_code = 1
40
+
41
+ # retrieve the SYSTEM_FAILURE_EXIT_CODE from the environment
42
+ system_failure_exit_code = os.getenv("SYSTEM_FAILURE_EXIT_CODE", None)
43
+ if not system_failure_exit_code:
44
+ click.secho("[WARNING] SYSTEM_FAILURE_EXIT_CODE not set, defaulting to '1'", fg="red")
45
+ system_failure_exit_code = 1
46
+
35
47
  if not p.tart_executor_shell:
36
48
  p.tart_executor_shell = shell
37
49
  ######################################################################
@@ -48,13 +60,13 @@ def run(ssh_timeout, tart_executable, shell, script, stage):
48
60
  f"[{stage}][ERROR] Could not establish SSH conntection to '{tart_vm_name}' after '{ssh_timeout}' seconds.",
49
61
  fg="red",
50
62
  )
51
- sys.exit(1)
63
+ sys.exit(system_failure_exit_code)
52
64
 
53
65
  try:
54
66
  ssh_session = tart.ssh_session(name=p.vm_name(), username=p.ssh_username, password=p.ssh_password)
55
67
  except:
56
68
  click.secho(f"[{stage}][ERROR] Could not establish SSH session with '{p.ssh_username}@{tart_ip}'", fg="red")
57
- sys.exit(1)
69
+ sys.exit(system_failure_exit_code)
58
70
 
59
71
  remote_temp_dir = "/opt/temp"
60
72
  script_name = os.path.basename(script)
@@ -67,4 +79,8 @@ def run(ssh_timeout, tart_executable, shell, script, stage):
67
79
  # ssh_session.exec_ssh_command(f"cd {remote_build_dir}")
68
80
  script_exit_code = ssh_session.exec_ssh_command(f"{p.shell} -l {remote_script_path}", get_pty=True)
69
81
 
70
- sys.exit(script_exit_code)
82
+ if script_exit_code != 0:
83
+ click.secho(f"[{stage}][ERROR] Script '{script}' failed with exit code '{script_exit_code}'", fg="red")
84
+ sys.exit(build_failure_exit_code)
85
+
86
+ sys.exit(0)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: gitlab-runner-tart-driver
3
- Version: 0.3.3
3
+ Version: 0.3.5
4
4
  Home-page: https://gitlab.com/schmieder.matthias/gitlab-runner-tart-driver
5
5
  Author: Matthias Schmieder
6
6
  Author-email: schmieder.matthias@gmail.com
@@ -13,7 +13,7 @@ Requires-Python: >=3.9
13
13
  Description-Content-Type: text/markdown
14
14
  License-File: LICENSE.txt
15
15
  Requires-Dist: click
16
- Requires-Dist: pydantic >=2.0.0
16
+ Requires-Dist: pydantic>=2.0.0
17
17
  Requires-Dist: pydantic-settings
18
18
  Requires-Dist: paramiko
19
19
  Requires-Dist: psutil
@@ -1,20 +1,20 @@
1
- gitlab_runner_tart_driver/__init__.py,sha256=8KcCYTXH99C2-gCLuPILJvtT9YftRWJsartIx6TQ2ZY,22
1
+ gitlab_runner_tart_driver/__init__.py,sha256=ThnCuF3X7rsQSd5PAea_jfYA70ZmhLvkFcLBxBPwZnY,22
2
2
  gitlab_runner_tart_driver/__main__.py,sha256=FiyMv64vDC-R8i3CGEP9QP48djZFHm7utyChwZJWCeY,107
3
- gitlab_runner_tart_driver/cli.py,sha256=1eFi9m8L4zcBO5eE4g4EP8phtE1znF54qt-xAigPsOE,596
3
+ gitlab_runner_tart_driver/cli.py,sha256=rCtFzi7i4JUX7VXteBHyII4sEbMzpn8SIpyjyKrQCBI,592
4
4
  gitlab_runner_tart_driver/commands/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
5
5
  gitlab_runner_tart_driver/commands/cleanup.py,sha256=nFe7TGdxsVSXNR9onm-3iCjmUo6SzujV5LrA_y439fs,1422
6
6
  gitlab_runner_tart_driver/commands/config.py,sha256=cacJ9ms5r3nZGZ_sS2d21Uoz7S-bMjB__lORUmeXZ-4,760
7
- gitlab_runner_tart_driver/commands/prepare.py,sha256=FCINFtBakV8hiKbHGba3ZWwyNwICp6UV5mKZVCyP8h4,10487
8
- gitlab_runner_tart_driver/commands/run.py,sha256=2grP79TARRL0xzmppRITbSpIH0WL4YPxlxq1fYQZxlM,2235
7
+ gitlab_runner_tart_driver/commands/prepare.py,sha256=JTxTlPnPELC9C2TMOLqvcu23jOL0sofMxdp9OwpfddU,13964
8
+ gitlab_runner_tart_driver/commands/run.py,sha256=kXQxi8nxv6QQRFzGrlqifLtFtbjXAYgjCiMVqXkb2_A,3065
9
9
  gitlab_runner_tart_driver/modules/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
10
10
  gitlab_runner_tart_driver/modules/gitlab_custom_command_config.py,sha256=XvE83QmQUcqtVMYZY3okGOgO86DBX6Y7xhXNenmKxFY,3086
11
11
  gitlab_runner_tart_driver/modules/gitlab_custom_driver_config.py,sha256=ujxlzP1ZbfsZyLIsghID5c_jLPSt-1pDazE2B5CPORY,447
12
12
  gitlab_runner_tart_driver/modules/tart.py,sha256=rD6IPtpz57dwsPGVBWaeKeVoqI1MoBvdtkqozXgmAxA,9914
13
13
  gitlab_runner_tart_driver/modules/utils.py,sha256=7ipKjy_5tC5iW67Na_A9XhF4o2lKcAqO8LRTTSJKNd0,923
14
14
  gitlab_runner_tart_driver/scripts/install-gitlab-runner.sh.j2,sha256=-rBzxZ92w7lMrTCVcjMHhlZgqPIK1RJKVoOc2wjZvck,2533
15
- gitlab_runner_tart_driver-0.3.3.dist-info/LICENSE.txt,sha256=TiYXQpEfbzcPBNGb7k1NslUngq_un-5cxGyT4W1S_f4,3303
16
- gitlab_runner_tart_driver-0.3.3.dist-info/METADATA,sha256=0rUA-uUqMDTOx657qWQbVNiMhQzuroumn8aIENgVZzs,20706
17
- gitlab_runner_tart_driver-0.3.3.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
18
- gitlab_runner_tart_driver-0.3.3.dist-info/entry_points.txt,sha256=xmvpGQf1wvFPy5wqDWXu8k5FD_k9KkCNCkpuworTsr0,82
19
- gitlab_runner_tart_driver-0.3.3.dist-info/top_level.txt,sha256=JjRzCs2sr24xG4SV_5tIBPpNC1Tec7I4AbK4IKMDqOc,26
20
- gitlab_runner_tart_driver-0.3.3.dist-info/RECORD,,
15
+ gitlab_runner_tart_driver-0.3.5.dist-info/LICENSE.txt,sha256=TiYXQpEfbzcPBNGb7k1NslUngq_un-5cxGyT4W1S_f4,3303
16
+ gitlab_runner_tart_driver-0.3.5.dist-info/METADATA,sha256=dI44fmUwC6k_iCygHAOKtp31CoAKM7uBme8-OPdYVo0,20705
17
+ gitlab_runner_tart_driver-0.3.5.dist-info/WHEEL,sha256=tZoeGjtWxWRfdplE7E3d45VPlLNQnvbKiYnx7gwAy8A,92
18
+ gitlab_runner_tart_driver-0.3.5.dist-info/entry_points.txt,sha256=xmvpGQf1wvFPy5wqDWXu8k5FD_k9KkCNCkpuworTsr0,82
19
+ gitlab_runner_tart_driver-0.3.5.dist-info/top_level.txt,sha256=JjRzCs2sr24xG4SV_5tIBPpNC1Tec7I4AbK4IKMDqOc,26
20
+ gitlab_runner_tart_driver-0.3.5.dist-info/RECORD,,
@@ -1,5 +1,5 @@
1
1
  Wheel-Version: 1.0
2
- Generator: bdist_wheel (0.43.0)
2
+ Generator: bdist_wheel (0.45.1)
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
5
5