-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenv_tools.py
100 lines (77 loc) · 2.28 KB
/
env_tools.py
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
# Implement
### docker
### 0. database setup
### 1. generate commit id d
### 2. generate tag name d
### 3. build docker image d
import subprocess
import re
from clize import run
from server.util.tools import EnvTool
def get_git_commit_id() -> str:
a = subprocess.check_output(['git', 'rev-parse', 'HEAD'])
git_commit_id = a.decode(encoding='utf-8').strip()
assert len(git_commit_id) == 40
assert re.match('^[a-fA-F0-9]+$', git_commit_id)
return git_commit_id
def get_image_latest() -> str:
base_image_name = 'server'
tag = 'musinsa'
return ':'.join([base_image_name, tag])
def get_base_image_name() -> str:
return 'mysql:8.0'
def run_commands(commands: str):
subprocess.run(
commands,
shell=True,
executable='/bin/bash',
check=True,
)
def build_image():
git_commit_id = get_git_commit_id()
image_latest = get_image_latest()
base_image = get_base_image_name()
build_env = EnvTool.get_env()
commands = f"""
set -x
set -e
VER={git_commit_id}
echo git_commit_id=$VER
docker build --label env={build_env} --label git-commit-id=$VER --build-arg BASE_IMAGE={base_image} \
-t {image_latest} .
echo done : git-commit-id is $VER
echo done : build image '{image_latest}'
"""
run_commands(commands)
def test():
git_commit_id = get_git_commit_id()
image_latest = get_image_latest()
base_image = get_base_image_name()
build_env = EnvTool.get_env()
commands = f"""
set -x
set -e
echo test : {image_latest} start!
docker run -v `pwd`/tests/:/app/tests -v `pwd`/src:/app/src -e ENV={build_env} \
{image_latest} test
"""
run_commands(commands)
def setup():
git_commit_id = get_git_commit_id()
image_latest = get_image_latest()
base_image = get_base_image_name()
build_env = EnvTool.get_env()
commands = f"""
set -x
set -e
echo setup : {image_latest} start!
docker run -v `pwd`/tests/:/app/tests -v `pwd`/src:/app/src -e ENV={build_env} -e PYTHONPATH=/app/src \
{image_latest} setup
"""
run_commands(commands)
if __name__ == '__main__':
run({
'build': build_image,
'test': test,
'setup': setup,
})