-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathrun-container-if-not-exists.xsh
77 lines (63 loc) · 2.28 KB
/
run-container-if-not-exists.xsh
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
#!/usr/bin/env xonsh
# Copyright (c) 2025 Toradex
# SPDX-License-Identifier: MIT
##
# This script run a container if a container with the same name is not
# already running.
##
# use the xonsh environment to update the OS environment
$UPDATE_OS_ENVIRON = True
# Get the full log of error
$XONSH_SHOW_TRACEBACK = True
# this script should handle the subprocess errors
$RAISE_SUBPROC_ERROR = False
# clean the workspace set device default to use the local docker engine
$DOCKER_HOST = ""
import os
import argparse
from xonsh.procs.pipelines import CommandPipeline
from torizon_templates_utils.errors import Error,Error_Out,last_return_code
from torizon_templates_utils.colors import Color,BgColor,print
arg_parser = argparse.ArgumentParser()
arg_parser.add_argument(
"--container-runtime",
type=str,
required=True,
help="The container runtime to use"
)
arg_parser.add_argument(
"--run-arguments",
type=str,
required=True,
help="The arguments to run the container"
)
arg_parser.add_argument(
"--container-name",
type=str,
required=True,
help="The name of the container"
)
args = arg_parser.parse_args()
container_runtime = args.container_runtime.replace("\"", "")
run_arguments = args.run_arguments.replace("\"", "")
run_arguments = run_arguments.replace("'", "")
container_name = args.container_name.replace("\"", "")
if "GITLAB_CI" in os.environ:
print("ℹ️ :: GITLAB_CI :: ℹ️")
$DOCKER_HOST = "tcp://docker:2375"
# debug
print(f"Container Runtime: {container_runtime}")
print(f"Run Arguments: {run_arguments}")
print(f"Container Name: {container_name}")
# this is the way to attribute a type to a variable using xonsh
# only receiving the object from !() is not enough for pylsp
_exec_container_info: CommandPipeline = {}
_exec_container_info = !(@(container_runtime) container inspect @(container_name))
if _exec_container_info.returncode == 0:
print(f"Container {container_name} already created")
print(f"Checking if container {container_name} is running...")
else:
if "No such container" in _exec_container_info.err:
print("Container does not exists. Starting ...")
print(f"Cmd: {container_runtime} run --name {container_name} {run_arguments}")
evalx(f"{container_runtime} run --name {container_name} {run_arguments}")