-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtool.py
More file actions
executable file
·201 lines (161 loc) · 5.2 KB
/
tool.py
File metadata and controls
executable file
·201 lines (161 loc) · 5.2 KB
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
#!/usr/bin/env python3
# MIT License
#
# Copyright (c) 2022 Kenta Kabashima
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
"""Tool to create and test Docker image."""
import datetime
import os
import subprocess
import click
THIS_DIR = os.path.dirname(os.path.realpath(__file__))
GITLAB_IMAGE_URL = "registry.gitlab.com/musicscience37projects/docker/pipenv-docker"
DOCKER_HUB_IMAGE_URL = "musicscience37/pipenv"
IMAGE_TAGS = [
"jammy", # 22.04 LTS
"noble", # 24.04
"questing", # 25.10
"resolute", # 26.04 LTS
"alpine", # Alpine Linux
]
IMAGE_DIR_DICT = {
"jammy": "since_jammy",
"noble": "since_jammy",
"questing": "since_jammy",
"resolute": "since_jammy",
"alpine": "alpine",
}
LATEST_IMAGE_TAG = "noble"
@click.group()
def cli():
pass
def _run_command(command: list[str]):
"""Run a command.
Args:
command (list[str]): Command.
"""
click.secho(f"> {command}", bold=True)
subprocess.run(command, check=True, cwd=THIS_DIR)
def _create_time_stamp() -> str:
"""Create a time stamp.
Returns:
str: Time stamp.
"""
return datetime.datetime.now().strftime("%Y%m%d")
def _build(image_tag: str, image_full_name: str):
"""Build Docker image.
Args:
image_tag (str): Tag of the image.
image_full_name (str): Full name of the image.
"""
_run_command(
[
"docker",
"build",
"-t",
image_full_name,
"--build-arg",
f"IMAGE_TAG={image_tag}",
IMAGE_DIR_DICT[image_tag],
]
)
def _test(image_full_name: str):
"""Test Docker image.
Args:
image_full_name (str): Full name of the image.
"""
_run_command(
[
"docker",
"run",
"--rm",
"-v",
f"{THIS_DIR}/test:/home/test",
image_full_name,
"/home/test/run_test.sh",
]
)
def _tag_and_upload(image_full_name: str, another_image_full_name: str):
_run_command(["docker", "tag", image_full_name, another_image_full_name])
_run_command(["docker", "push", another_image_full_name])
def _upload(image_tag: str, image_full_name: str):
"""Upload Docker image.
Args:
image_tag (str): Tag of the image.
image_full_name (str): Full name of the image.
"""
_run_command(
[
"docker",
"login",
"-u",
os.environ["CI_REGISTRY_USER"],
"-p",
os.environ["CI_REGISTRY_PASSWORD"],
os.environ["CI_REGISTRY"],
]
)
_run_command(["docker", "push", image_full_name])
_tag_and_upload(
image_full_name=image_full_name,
another_image_full_name=f"{GITLAB_IMAGE_URL}:{image_tag}-{_create_time_stamp()}",
)
if image_tag == LATEST_IMAGE_TAG:
_tag_and_upload(
image_full_name=image_full_name,
another_image_full_name=f"{GITLAB_IMAGE_URL}:latest",
)
_run_command(
[
"docker",
"login",
"-u",
os.environ["HUB_USER_NAME"],
"-p",
os.environ["HUB_ACCESS_TOKEN"],
]
)
_tag_and_upload(
image_full_name=image_full_name,
another_image_full_name=f"{DOCKER_HUB_IMAGE_URL}:{image_tag}",
)
if image_tag == LATEST_IMAGE_TAG:
_tag_and_upload(
image_full_name=image_full_name,
another_image_full_name=f"{DOCKER_HUB_IMAGE_URL}:latest",
)
@cli.command()
@click.argument("image_tag", type=click.Choice(IMAGE_TAGS))
def test(image_tag: str):
"""Build and test Docker image."""
image_full_name = f"{GITLAB_IMAGE_URL}:{image_tag}-test"
_build(image_tag=image_tag, image_full_name=image_full_name)
_test(image_full_name=image_full_name)
@cli.command()
@click.argument("image_tag", type=click.Choice(IMAGE_TAGS))
def update(image_tag: str):
"""Build, test, and update Docker image."""
image_full_name = f"{GITLAB_IMAGE_URL}:{image_tag}"
_build(image_tag=image_tag, image_full_name=image_full_name)
_test(image_full_name=image_full_name)
_upload(image_tag=image_tag, image_full_name=image_full_name)
if __name__ == "__main__":
cli()