gitlab-runner-tart-driver 0.1.5__tar.gz → 0.1.7__tar.gz

Sign up to get free protection for your applications and to get access to all the features.
Files changed (30) hide show
  1. {gitlab-runner-tart-driver-0.1.5/gitlab_runner_tart_driver.egg-info → gitlab-runner-tart-driver-0.1.7}/PKG-INFO +2 -1
  2. {gitlab-runner-tart-driver-0.1.5 → gitlab-runner-tart-driver-0.1.7}/README.md +1 -0
  3. gitlab-runner-tart-driver-0.1.7/gitlab_runner_tart_driver/__init__.py +1 -0
  4. gitlab-runner-tart-driver-0.1.7/gitlab_runner_tart_driver/commands/cleanup.py +43 -0
  5. {gitlab-runner-tart-driver-0.1.5 → gitlab-runner-tart-driver-0.1.7}/gitlab_runner_tart_driver/commands/prepare.py +53 -21
  6. {gitlab-runner-tart-driver-0.1.5 → gitlab-runner-tart-driver-0.1.7}/gitlab_runner_tart_driver/commands/run.py +18 -4
  7. {gitlab-runner-tart-driver-0.1.5 → gitlab-runner-tart-driver-0.1.7}/gitlab_runner_tart_driver/modules/gitlab_custom_command_config.py +11 -4
  8. {gitlab-runner-tart-driver-0.1.5 → gitlab-runner-tart-driver-0.1.7/gitlab_runner_tart_driver.egg-info}/PKG-INFO +2 -1
  9. {gitlab-runner-tart-driver-0.1.5 → gitlab-runner-tart-driver-0.1.7}/setup.py +1 -1
  10. gitlab-runner-tart-driver-0.1.5/gitlab_runner_tart_driver/__init__.py +0 -1
  11. gitlab-runner-tart-driver-0.1.5/gitlab_runner_tart_driver/commands/cleanup.py +0 -28
  12. {gitlab-runner-tart-driver-0.1.5 → gitlab-runner-tart-driver-0.1.7}/LICENSE.txt +0 -0
  13. {gitlab-runner-tart-driver-0.1.5 → gitlab-runner-tart-driver-0.1.7}/MANIFEST.in +0 -0
  14. {gitlab-runner-tart-driver-0.1.5 → gitlab-runner-tart-driver-0.1.7}/gitlab_runner_tart_driver/__main__.py +0 -0
  15. {gitlab-runner-tart-driver-0.1.5 → gitlab-runner-tart-driver-0.1.7}/gitlab_runner_tart_driver/cli.py +0 -0
  16. {gitlab-runner-tart-driver-0.1.5 → gitlab-runner-tart-driver-0.1.7}/gitlab_runner_tart_driver/commands/__init__.py +0 -0
  17. {gitlab-runner-tart-driver-0.1.5 → gitlab-runner-tart-driver-0.1.7}/gitlab_runner_tart_driver/commands/config.py +0 -0
  18. {gitlab-runner-tart-driver-0.1.5 → gitlab-runner-tart-driver-0.1.7}/gitlab_runner_tart_driver/modules/__init__.py +0 -0
  19. {gitlab-runner-tart-driver-0.1.5 → gitlab-runner-tart-driver-0.1.7}/gitlab_runner_tart_driver/modules/gitlab_custom_driver_config.py +0 -0
  20. {gitlab-runner-tart-driver-0.1.5 → gitlab-runner-tart-driver-0.1.7}/gitlab_runner_tart_driver/modules/tart.py +0 -0
  21. {gitlab-runner-tart-driver-0.1.5 → gitlab-runner-tart-driver-0.1.7}/gitlab_runner_tart_driver/modules/utils.py +0 -0
  22. {gitlab-runner-tart-driver-0.1.5 → gitlab-runner-tart-driver-0.1.7}/gitlab_runner_tart_driver/scripts/install-gitlab-runner.sh.j2 +0 -0
  23. {gitlab-runner-tart-driver-0.1.5 → gitlab-runner-tart-driver-0.1.7}/gitlab_runner_tart_driver.egg-info/SOURCES.txt +0 -0
  24. {gitlab-runner-tart-driver-0.1.5 → gitlab-runner-tart-driver-0.1.7}/gitlab_runner_tart_driver.egg-info/dependency_links.txt +0 -0
  25. {gitlab-runner-tart-driver-0.1.5 → gitlab-runner-tart-driver-0.1.7}/gitlab_runner_tart_driver.egg-info/entry_points.txt +0 -0
  26. {gitlab-runner-tart-driver-0.1.5 → gitlab-runner-tart-driver-0.1.7}/gitlab_runner_tart_driver.egg-info/not-zip-safe +0 -0
  27. {gitlab-runner-tart-driver-0.1.5 → gitlab-runner-tart-driver-0.1.7}/gitlab_runner_tart_driver.egg-info/requires.txt +0 -0
  28. {gitlab-runner-tart-driver-0.1.5 → gitlab-runner-tart-driver-0.1.7}/gitlab_runner_tart_driver.egg-info/top_level.txt +0 -0
  29. {gitlab-runner-tart-driver-0.1.5 → gitlab-runner-tart-driver-0.1.7}/pyproject.toml +0 -0
  30. {gitlab-runner-tart-driver-0.1.5 → gitlab-runner-tart-driver-0.1.7}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: gitlab-runner-tart-driver
3
- Version: 0.1.5
3
+ Version: 0.1.7
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
@@ -436,6 +436,7 @@ Usage: gitlab-runner-tart-driver run [OPTIONS] SCRIPT STAGE
436
436
  Options:
437
437
  --default-ssh-username TEXT username to login to a tart vm
438
438
  --default-ssh-password TEXT password to login to a tart vm
439
+ --timeout INTEGER SSH connection timeout in seconds
439
440
  -x, --tart-executable TEXT Path to the tart executable.
440
441
  --shell TEXT Path to the shell to be used for commands over
441
442
  ssh.
@@ -421,6 +421,7 @@ Usage: gitlab-runner-tart-driver run [OPTIONS] SCRIPT STAGE
421
421
  Options:
422
422
  --default-ssh-username TEXT username to login to a tart vm
423
423
  --default-ssh-password TEXT password to login to a tart vm
424
+ --timeout INTEGER SSH connection timeout in seconds
424
425
  -x, --tart-executable TEXT Path to the tart executable.
425
426
  --shell TEXT Path to the shell to be used for commands over
426
427
  ssh.
@@ -0,0 +1 @@
1
+ __version__ = "0.1.7"
@@ -0,0 +1,43 @@
1
+ import re
2
+
3
+ import click
4
+
5
+ from gitlab_runner_tart_driver.modules.gitlab_custom_command_config import GitLabCustomCommandConfig
6
+ from gitlab_runner_tart_driver.modules.tart import Tart
7
+
8
+
9
+ @click.command()
10
+ @click.option("-x", "--tart-executable", required=False, default="tart", type=str, help="Path to the tart executable.")
11
+ def cleanup(tart_executable):
12
+ """Command to greet a user."""
13
+ p = GitLabCustomCommandConfig()
14
+
15
+ tart = Tart(exec_path=tart_executable)
16
+ tart_vm_name = p.vm_name()
17
+
18
+ # remove specific VM that we started
19
+ try:
20
+ click.echo(f"[INFO] stopping '{tart_vm_name}'")
21
+ tart.stop(tart_vm_name)
22
+ except:
23
+ click.secho(f"[ERROR] failed to stop '{tart_vm_name}'", fg="red")
24
+
25
+ # cleanup leftovers from previous runs
26
+ _remove_stopped_vms(tart, f"{p.vm_name_prefix}-.*")
27
+
28
+
29
+ def _remove_stopped_vms(tart, pattern):
30
+ tart_images = tart.list()
31
+ tart_vm_map = dict()
32
+ # create map from images
33
+ for i in tart_images:
34
+ tart_vm_map[i.name] = i
35
+
36
+ for vm_name, vm in tart_vm_map.items():
37
+ if vm.running or not re.match(pattern, vm_name):
38
+ continue
39
+ try:
40
+ click.echo(f"[INFO] deleting '{vm_name}'")
41
+ tart.delete(vm.name)
42
+ except:
43
+ click.secho(f"[ERROR] failed to delete '{vm_name}'", fg="red")
@@ -194,8 +194,22 @@ def prepare(
194
194
  click.echo(f"[INFO] Found VM '{tart_vm_name}'. Going to delete it...")
195
195
  tart.delete(tart_vm_name)
196
196
 
197
+ # verfiy that the limit of running VMs is not exceeded
198
+ list_running_vms = []
199
+ for vm in tart.list():
200
+ if vm.running:
201
+ list_running_vms.append(vm)
202
+
203
+ if len(list_running_vms) >= p.tart_max_vm_count:
204
+ click.secho("[ERROR] The limit of running VMs is exceeded.", fg="red")
205
+ sys.exit(1)
206
+
197
207
  click.echo(f"[INFO] Cloning VM instance '{tart_vm_name}' from '{p.ci_job_image}'")
198
- tart.clone(p.ci_job_image, tart_vm_name)
208
+ try:
209
+ tart.clone(p.ci_job_image, tart_vm_name)
210
+ except:
211
+ click.secho(f"[ERROR] failed to clone image f'{p.ci_job_image}'", fg="red")
212
+ sys.exit(1)
199
213
 
200
214
  if cpu or memory or display:
201
215
  click.echo(f"[INFO] Configuring instance '{tart_vm_name}' from '{p.ci_job_image}'")
@@ -234,39 +248,57 @@ def prepare(
234
248
  for v in volumes:
235
249
  volume_mounts.append(TartVolume.from_string(v))
236
250
 
237
- tart.run(tart_vm_name, volume_mounts)
238
- ip = tart.ip(tart_vm_name, timeout=timeout)
251
+ try:
252
+ tart.run(tart_vm_name, volume_mounts)
253
+ except:
254
+ click.secho(f"[ERROR] Failed to start VM '{tart_vm_name}'", fg="red")
255
+ sys.exit(1)
256
+
257
+ try:
258
+ ip = tart.ip(tart_vm_name, timeout=timeout)
259
+ except:
260
+ click.secho(f"[ERROR] Failed to get IP of VM '{tart_vm_name}'", fg="red")
261
+ sys.exit(1)
262
+
239
263
  if not ip:
240
264
  click.echo(f"[ERROR] Error, VM was not reacheable after '{timeout}' seconds")
241
265
  sys.exit(1)
242
266
 
243
- ssh_session = tart.ssh_session(name=p.vm_name(), username=p.tart_ssh_username, password=p.tart_ssh_password)
244
- ssh_session.exec_ssh_command(
245
- f"sudo mkdir -p {remote_script_dir} && sudo chown {p.tart_ssh_username}:{p.tart_ssh_username} {remote_script_dir}",
246
- )
247
-
248
- for volume in volume_mounts:
249
- click.echo(f"[INFO] Setting up volume mount '{volume.name}'")
267
+ try:
268
+ ssh_session = tart.ssh_session(name=p.vm_name(), username=p.tart_ssh_username, password=p.tart_ssh_password)
250
269
  ssh_session.exec_ssh_command(
251
- f"sudo mkdir -p $(dirname {volume.dest}); sudo ln -sf '/Volumes/My Shared Files/{volume.name}' {volume.dest}",
270
+ f"sudo mkdir -p {remote_script_dir} && sudo chown {p.tart_ssh_username}:{p.tart_ssh_username} {remote_script_dir}",
252
271
  )
253
272
 
254
- # if cache and builds volumes are not mounted, make sure to create them locally inside the VM
255
- if not cache_dir:
256
- ssh_session.exec_ssh_command(
257
- f"sudo mkdir -p {remote_cache_dir} && sudo chown {p.tart_ssh_username}:{p.tart_ssh_username} {remote_cache_dir}",
258
- )
273
+ for volume in volume_mounts:
274
+ click.echo(f"[INFO] Setting up volume mount '{volume.name}'")
275
+ ssh_session.exec_ssh_command(
276
+ f"sudo mkdir -p $(dirname {volume.dest}); sudo ln -sf '/Volumes/My Shared Files/{volume.name}' {volume.dest}",
277
+ )
259
278
 
260
- if not builds_dir:
261
- ssh_session.exec_ssh_command(
262
- f"sudo mkdir -p {remote_build_dir} && sudo chown {p.tart_ssh_username}:{p.tart_ssh_username} {remote_build_dir}",
263
- )
279
+ # if cache and builds volumes are not mounted, make sure to create them locally inside the VM
280
+ if not cache_dir:
281
+ ssh_session.exec_ssh_command(
282
+ f"sudo mkdir -p {remote_cache_dir} && sudo chown {p.tart_ssh_username}:{p.tart_ssh_username} {remote_cache_dir}",
283
+ )
284
+
285
+ if not builds_dir:
286
+ ssh_session.exec_ssh_command(
287
+ f"sudo mkdir -p {remote_build_dir} && sudo chown {p.tart_ssh_username}:{p.tart_ssh_username} {remote_build_dir}",
288
+ )
289
+ except:
290
+ click.secho(f"[ERROR] Failed so prepare VM '{tart_vm_name}'", fg="red")
291
+ sys.exit(1)
264
292
 
265
293
  if install_gitlab_runner:
266
294
  click.echo(
267
295
  f"[INFO] Installing GitLab Runner '{gitlab_runner_version}' [force: '{force_install_gitlab_runner}']"
268
296
  )
269
- tart.install_gitlab_runner(name=tart_vm_name, username=p.tart_ssh_username, password=p.tart_ssh_password)
297
+ try:
298
+ tart.install_gitlab_runner(name=tart_vm_name, username=p.tart_ssh_username, password=p.tart_ssh_password)
299
+ except:
300
+ click.secho(f"[ERROR] Failed to install GitLab Runner '{gitlab_runner_version}'", fg="red")
301
+ sys.exit(1)
270
302
 
271
303
  tart.print_spec(tart_vm_name)
272
304
 
@@ -14,6 +14,9 @@ from gitlab_runner_tart_driver.modules.tart import Tart
14
14
  @click.option(
15
15
  "--default-ssh-password", default="admin", required=False, type=str, help="password to login to a tart vm"
16
16
  )
17
+ @click.option(
18
+ "--timeout", "ssh_timeout", default=60, required=False, type=int, help="SSH connection timeout in seconds"
19
+ )
17
20
  @click.option(
18
21
  "-x",
19
22
  "--tart-executable",
@@ -31,7 +34,7 @@ from gitlab_runner_tart_driver.modules.tart import Tart
31
34
  )
32
35
  @click.argument("script")
33
36
  @click.argument("stage")
34
- def run(default_ssh_username, default_ssh_password, tart_executable, shell, script, stage):
37
+ def run(default_ssh_username, default_ssh_password, ssh_timeout, tart_executable, shell, script, stage):
35
38
  """Run commands."""
36
39
  p = GitLabCustomCommandConfig()
37
40
 
@@ -45,10 +48,21 @@ def run(default_ssh_username, default_ssh_password, tart_executable, shell, scri
45
48
  ######################################################################
46
49
  tart = Tart(exec_path=tart_executable)
47
50
  tart_vm_name = p.vm_name()
48
- tart_ip = tart.ip(tart_vm_name, timeout=30)
49
- click.echo(f"[INFO] Establishing SSH conntection to '{p.tart_ssh_username}@{tart_ip}'")
50
51
 
51
- ssh_session = tart.ssh_session(name=p.vm_name(), username=p.tart_ssh_username, password=p.tart_ssh_password)
52
+ try:
53
+ tart_ip = tart.ip(tart_vm_name, timeout=ssh_timeout)
54
+ click.echo(f"[INFO] Establishing SSH conntection to '{p.tart_ssh_username}@{tart_ip}'")
55
+ except:
56
+ click.secho(
57
+ f"[ERROR] Could not establish SSH conntection to '{tart_vm_name}' after '{ssh_timeout}' seconds.", fg="red"
58
+ )
59
+ sys.exit(1)
60
+
61
+ try:
62
+ ssh_session = tart.ssh_session(name=p.vm_name(), username=p.tart_ssh_username, password=p.tart_ssh_password)
63
+ except:
64
+ click.secho(f"[ERROR] Could not establish SSH session with '{p.tart_ssh_username}@{tart_ip}'", fg="red")
65
+ sys.exit(1)
52
66
 
53
67
  click.echo("[INFO] Preparing workspace")
54
68
  remote_temp_dir = "/opt/temp"
@@ -1,6 +1,7 @@
1
1
  from typing import Optional
2
2
 
3
3
  from pydantic import BaseSettings
4
+ from pydantic import Field
4
5
 
5
6
 
6
7
  class GitLabCustomCommandConfig(BaseSettings):
@@ -13,9 +14,9 @@ class GitLabCustomCommandConfig(BaseSettings):
13
14
  ci_concurrent_project_id: str
14
15
  ci_runner_short_token: str
15
16
  ci_project_name: str
16
- ci_registry: str
17
- ci_registry_user: str
18
- ci_registry_password: str
17
+ ci_registry: Optional[str]
18
+ ci_registry_user: Optional[str]
19
+ ci_registry_password: Optional[str]
19
20
 
20
21
  tart_registry_username: Optional[str]
21
22
  tart_registry_password: Optional[str]
@@ -24,6 +25,8 @@ class GitLabCustomCommandConfig(BaseSettings):
24
25
  tart_ssh_username: Optional[str]
25
26
  tart_ssh_password: Optional[str]
26
27
 
28
+ tart_max_vm_count: Optional[int] = Field(default=2)
29
+
27
30
  class Config:
28
31
  """Define the prefix used by GitLab for all environment variables passed to a custom driver.
29
32
  see https://docs.gitlab.com/runner/executors/custom.html#stages
@@ -33,4 +36,8 @@ class GitLabCustomCommandConfig(BaseSettings):
33
36
 
34
37
  def vm_name(self):
35
38
  """Creates a unique name for a VM"""
36
- return f"{self.ci_project_name}-{self.ci_pipeline_id}-{self.ci_job_id}-{self.ci_concurrent_id}"
39
+ return f"{self.vm_name_prefix}-{self.ci_project_name}-{self.ci_pipeline_id}-{self.ci_job_id}-{self.ci_concurrent_id}"
40
+
41
+ @property
42
+ def vm_name_prefix(self):
43
+ return "grtd"
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: gitlab-runner-tart-driver
3
- Version: 0.1.5
3
+ Version: 0.1.7
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
@@ -436,6 +436,7 @@ Usage: gitlab-runner-tart-driver run [OPTIONS] SCRIPT STAGE
436
436
  Options:
437
437
  --default-ssh-username TEXT username to login to a tart vm
438
438
  --default-ssh-password TEXT password to login to a tart vm
439
+ --timeout INTEGER SSH connection timeout in seconds
439
440
  -x, --tart-executable TEXT Path to the tart executable.
440
441
  --shell TEXT Path to the shell to be used for commands over
441
442
  ssh.
@@ -10,7 +10,7 @@ this_directory = os.path.abspath(os.path.dirname(__file__))
10
10
  with open(os.path.join(this_directory, "README.md"), encoding="utf-8") as f:
11
11
  long_description = f.read()
12
12
 
13
- version = "0.1.5"
13
+ version = "0.1.7"
14
14
 
15
15
  version = f"{version}{os.environ.get('PIP_VERSION_POSTFIX','')}"
16
16
 
@@ -1 +0,0 @@
1
- __version__ = "0.1.5"
@@ -1,28 +0,0 @@
1
- import click
2
-
3
- from gitlab_runner_tart_driver.modules.gitlab_custom_command_config import GitLabCustomCommandConfig
4
- from gitlab_runner_tart_driver.modules.tart import Tart
5
-
6
-
7
- @click.command()
8
- @click.option("-x", "--tart-executable", required=False, default="tart", type=str, help="Path to the tart executable.")
9
- def cleanup(tart_executable):
10
- """Command to greet a user."""
11
- p = GitLabCustomCommandConfig()
12
-
13
- tart = Tart(exec_path=tart_executable)
14
- tart_vm_name = p.vm_name()
15
-
16
- tart_images = tart.list()
17
- tart_vm_map = dict()
18
- # create map from images
19
- for i in tart_images:
20
- tart_vm_map[i.name] = i
21
-
22
- if tart_vm_name in tart_vm_map:
23
- click.echo(f"[INFO] '{tart_vm_name}' found.")
24
- if tart_vm_map[tart_vm_name].running:
25
- click.echo(f"[INFO] stopping '{tart_vm_name}'")
26
- tart.stop(tart_vm_name)
27
- click.echo(f"[INFO] deleting '{tart_vm_name}'")
28
- tart.delete(tart_vm_name)