forked from IndustryEssentials/ymir
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ymir.sh
executable file
·188 lines (158 loc) · 4.91 KB
/
ymir.sh
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
#!/bin/bash
set -e
DOCKER_BACKEND='industryessentials/ymir-backend'
DOCKER_WEB='industryessentials/ymir-web'
DEV_SOURCE_BACKEND_PIP='https://pypi.mirrors.ustc.edu.cn/simple'
DEV_SOURCE_WEB_NPM='https://registry.npmmirror.com'
FIELD_LABEL_TOOL='LABEL_TOOL'
FIELD_LABEL_TOOL_LF='label_free'
ENV_FILE='.env'
FIELD_SERVER_RUNTIME='SERVER_RUNTIME'
FIELD_DEPLOY_MODULE_HOST_PORT='DEPLOY_MODULE_HOST_PORT'
check_docker_compose_version() {
MIN_DC_VER="1.29.2"
if ! command -v docker-compose &> /dev/null; then
echo "please install docker-compose ${MIN_DC_VER} or newer version."
exit
fi
if ! echo "$(docker-compose version --short) ${MIN_DC_VER}" | tr " " "\n" | sort -V | head -n 1 | grep -Eq "^${MIN_DC_VER}$"; then
echo "please upgrade docker-compose to ${MIN_DC_VER} or newer version."
exit
fi
}
check_docker_version() {
MIN_DOCKER_VER="20.10"
if ! command -v docker &> /dev/null; then
echo "please install docker ${MIN_DOCKER_VER} or newer version."
echo "see https://www.howtogeek.com/devops/how-to-install-docker-and-docker-compose-on-linux/ for details"
exit
fi
if ! echo "$(docker version --format '{{.Client.Version}}') ${MIN_DOCKER_VER}" | tr " " "\n" | sort -V | head -n 1 | grep -Eq "^${MIN_DOCKER_VER}$"; then
echo "please upgrade docker to ${MIN_DOCKER_VER} or newer version."
echo "see https://www.howtogeek.com/devops/how-to-install-docker-and-docker-compose-on-linux/ for details"
exit
fi
}
has_nvidia_driver(){
if ! command -v nvidia-smi &> /dev/null; then
return 1
fi
if [ $(nvidia-smi -L | grep UUID:| wc -l) -eq 0 ]; then
return 1
fi
return 0
}
check_server_runtime(){
if cat ${ENV_FILE} | grep -oE "^${FIELD_SERVER_RUNTIME}=nvidia$"; then
if ! has_nvidia_driver; then
echo "please make sure Nvidia driver is installed when server runtime is set to nvidia."
echo "find your Nvidia drivers here: https://www.nvidia.com/Download/Find.aspx"
exit
fi
fi
}
stop() {
docker-compose -f docker-compose.yml -f docker-compose.dev.yml -f docker-compose.labelfree.yml \
-f docker-compose.modeldeploy.yml down
}
pre_start() {
stop
}
set_label_tool() {
if ! cat ${ENV_FILE} | grep "${FIELD_LABEL_TOOL}=$"; then
echo "LabelFree already set"
return
fi
cat <<- EOF
Would you like to start the LabelFree tool? (Y/N)
EOF
while true; do
read -p "You choose ([Y]/N)?" yn
case $yn in
[Yy]*|"" ) sed -i.bk "s/^${FIELD_LABEL_TOOL}=.*/${FIELD_LABEL_TOOL}=${FIELD_LABEL_TOOL_LF}/" ${ENV_FILE} && rm -f ${ENV_FILE}.bk; break;;
[Nn]* ) break;;
* ) echo "Please answer Y/N.";;
esac
done
}
start_label_tool() {
if cat ${ENV_FILE} | grep "${FIELD_LABEL_TOOL}=$"; then
echo "no LabelFree set, skip."
return
fi
if cat ${ENV_FILE} | grep "${FIELD_LABEL_TOOL}=${FIELD_LABEL_TOOL_LF}"; then
echo "LabelFree set, starting..."
docker-compose -f docker-compose.labelfree.yml up -d
return
else
echo "unsupported label tool"
exit
fi
}
start_deploy_module() {
if cat ${ENV_FILE} | grep -oE "^${FIELD_DEPLOY_MODULE_HOST_PORT}=$"; then
echo "DEPLOY_MODULE_HOST_PORT not set, skip deploy module startup"
return
fi
if ! cat ${ENV_FILE} | grep -oE "^${FIELD_DEPLOY_MODULE_HOST_PORT}=[0-9]{1,5}$"; then
echo "DEPLOY_MODULE_HOST_PORT is invalid"
exit
fi
echo "deploy module, starting..."
docker-compose -f docker-compose.modeldeploy.yml up -d
}
start() {
check_docker_version
check_docker_compose_version
check_server_runtime
pre_start
start_deploy_module
if [[ $1 == 'dev' ]]; then
printf '\nin dev mode, building images.\n'
docker build \
-t ${DOCKER_BACKEND} \
--build-arg PIP_SOURCE=${DEV_SOURCE_BACKEND_PIP} \
[email protected]:IndustryEssentials/ymir.git#dev:/ymir -f Dockerfile.backend
docker build \
-t ${DOCKER_WEB} \
--build-arg NPM_REGISTRY=${DEV_SOURCE_WEB_NPM} \
[email protected]:IndustryEssentials/ymir.git#dev:/ymir/web
sed -i.bk "s/^${FIELD_UUID}=.*$/${FIELD_UUID}=testdev/" ${ENV_FILE} && rm -f *.bk
else
printf '\nin prod mode, starting service.\n'
fi
set_label_tool
docker-compose up -d
start_label_tool
}
update() {
stop
cat <<- EOF
Before proceed, make sure to BACKUP your YMIR-workplace folder.
Only supports to upgrade from 1.1.0 (22-May) to 2.0.0 (22-Oct), otherwise may cause data damage.
EOF
while true; do
read -p "Continue (y/n)?" yn
case $yn in
[Yy]* ) docker-compose -f docker-compose.updater.yml up; break;;
* ) break;;
esac
done
}
print_help() {
printf '\nUsage: \n bash ymir.sh start/stop/update.\n'
}
# main
main() {
if [ "$EUID" -eq 0 ]
then echo "Error: using sudo, this will cause permission issue."
exit
fi
if [[ $# -eq 0 ]]; then
print_help
else
"$@"
fi
}
printf 'Welcome to Ymir world.\n'
main "$@"