-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdevcontainer
executable file
·168 lines (139 loc) · 3.96 KB
/
devcontainer
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
#!/usr/bin/env python3
#
# Docker automation shell script.
# Dependencies: docker
#
# Author: Michal Svorc <[email protected]>
# License: MIT license (https://opensource.org/licenses/MIT)
# Guidelines: https://google.github.io/styleguide/pyguide
import argparse
import logging
import os
import pdb
import subprocess
from enum import Enum
# ===============================================================================
# Variables
# ===============================================================================
VERSION = "2.0.0"
IMAGE_NAME = "devcontainer"
NETWORK = "bridge"
USER_NAME = "user"
environments = [
"base",
"nodejs",
"python",
"golang",
]
script_path: str = os.path.dirname(os.path.realpath(__file__))
class Command(Enum):
BUILD = "build"
RUN = "run"
def parse_arguments():
parser = argparse.ArgumentParser(
description="Docker automation shell script.",
)
parser.add_argument(
"-v",
"--version",
action="version",
version=f"%(prog)s {VERSION}",
help="show program version and exit",
)
parser.add_argument(
"-e",
"--env",
choices=environments,
default=environments[0],
help=f"specify Docker environment: {environments}",
)
parser.add_argument(
"command",
choices=[command.value for command in Command],
help="specify Docker command",
)
parser.add_argument(
"remaining_args", nargs=argparse.REMAINDER, help="All remaining arguments"
)
return parser.parse_args()
def main():
set_logging_level(logging.INFO)
enable_debug_mode()
args = parse_arguments()
image = f"{IMAGE_NAME}:{args.env}"
dockerfile = f"Dockerfile.{args.env}"
if args.command == Command.BUILD.value:
build(image, dockerfile, args.remaining_args)
if args.command == Command.RUN.value:
run(image, NETWORK, args.remaining_args)
def build(image, dockerfile, rest_of_args):
SCRIPTS_PATH = "/opt/scripts"
env = {"BUILDKIT_INLINE_CACHE": "1"}
dockerfile_path = f"{script_path}/dockerfiles/{dockerfile}"
command = [
"docker",
"build",
]
args = [
"--file",
dockerfile_path,
"--tag",
image,
]
build_args = {"USER_NAME": USER_NAME, "SCRIPTS_PATH": SCRIPTS_PATH}
command.extend(args)
for key, value in build_args.items():
command.extend(["--build-arg", f"{key}={value}"])
command.extend(rest_of_args)
command.append(script_path)
logging.info(f"Building image {image} based on {dockerfile}.")
subprocess.run(command, env={**os.environ, **env}, check=True)
def run(image, network, rest_of_args):
container = image.replace("/", "-").replace(":", "-")
command = [
"docker",
"run",
]
args = [
"-it",
"--network",
network,
"--name",
container,
"--init",
]
volume_targets = [
".aws",
".gnupg",
".ssh",
".secret",
"work",
]
volumes = generate_volume_mounts(USER_NAME, volume_targets)
command.extend(args)
command.extend(volumes)
command.extend(rest_of_args)
command.append(image)
logging.info(f"Running container {container}.")
subprocess.run(command, check=True)
def generate_volume_mounts(user_name, volume_targets):
volumes = []
for name in volume_targets:
volumes.extend(
[
"--mount",
f"type=volume,source=devcontainer_{name.strip('.')},target=/home/{user_name}/{name}",
]
)
return volumes
def set_logging_level(level):
logging.basicConfig(level=level)
def enable_debug_mode():
debug_env = os.environ.get("DEBUG", "").lower()
if debug_env in ["1", "true", "yes"]:
set_logging_level(logging.DEBUG)
logging.basicConfig(level=logging.DEBUG)
logging.debug("Debug mode enabled.")
pdb.set_trace()
if __name__ == "__main__":
main()