This repository has been archived by the owner on Feb 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
compose.sh
executable file
·237 lines (217 loc) · 8.1 KB
/
compose.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#!/bin/bash
# shell script to control docker-based development environment.
# (Docker and Docker-compose)
#
# Author : NoUnique (kofmap@gmail.com)
# Copyright 2020 NoUnique. All Rights Reserved
COMPOSE_PROJECT_NAME=""
DEFAULT_SERVICE="dev"
COMPOSE_FNAME="docker-compose.yml"
COMPOSE_VERSION="1.25.4"
SCRIPT_DIR="$(cd -P -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd -P)"
PROJECT_DIR="$(dirname -- "${SCRIPT_DIR}")"
DIRNAME="${PROJECT_DIR##*/}"
# by docker image & container naming rules
COMPOSE_PROJECT_NAME=${COMPOSE_PROJECT_NAME:="$(echo "${DIRNAME}" | sed 's/[^0-9a-zA-Z]*//g' | tr '[A-Z]' '[a-z]')"}
export COMPOSE_IMAGE_NAME=${COMPOSE_PROJECT_NAME}
COMPOSE_PROJECT_NAME="${USER}_${COMPOSE_PROJECT_NAME}"
function fn_configure() {
NO_CACHE=${NO_CACHE:=""}
IS_EXIST=${IS_EXIST:="FALSE"}
IS_RUNNING=${IS_RUNNING:="FALSE"}
RUN_TENSORBOARD=${RUN_TENSORBOARD:="FALSE"}
RUN_JUPYTER=${RUN_JUPYTER:="FALSE"}
DO_BUILD=${DO_BUILD:="FALSE"}
DO_RUN=${DO_RUN:="FALSE"}
DO_BASH=${DO_BASH:="FALSE"}
DO_KILL=${DO_KILL:="FALSE"}
DO_DOWN=${DO_DOWN:="FALSE"}
}
function fn_is_exist() {
IS_EXIST=`docker-compose -f ${SCRIPT_DIR}/${COMPOSE_FNAME} -p ${COMPOSE_PROJECT_NAME} ps -q ${DEFAULT_SERVICE}`
if [[ "${IS_EXIST}" != "FALSE" ]] && [[ -n "${IS_EXIST}" ]]; then
IS_EXIST="TRUE"
fi
}
function fn_is_running() {
IS_RUNNING=`docker ps -q --no-trunc | grep $(docker-compose -f ${SCRIPT_DIR}/${COMPOSE_FNAME} -p ${COMPOSE_PROJECT_NAME} ps -q ${DEFAULT_SERVICE})`
if [[ "${IS_RUNNING}" != "FALSE" ]] && [[ -n "${IS_RUNNING}" ]]; then
IS_RUNNING="TRUE"
fi
}
function fn_build() {
echo "Build '${COMPOSE_PROJECT_NAME}' docker image"
docker-compose -f ${SCRIPT_DIR}/${COMPOSE_FNAME} -p ${COMPOSE_PROJECT_NAME} build ${NO_CACHE} ${DEFAULT_SERVICE}
}
function fn_run() {
echo "Run '${COMPOSE_PROJECT_NAME}_${DEFAULT_SERVICE}' docker container"
fn_kill $1
docker-compose -f ${SCRIPT_DIR}/${COMPOSE_FNAME} -p ${COMPOSE_PROJECT_NAME} up -d ${DEFAULT_SERVICE}
}
function fn_run_tensorboard() {
TEMP=${DEFAULT_SERVICE}
DEFAULT_SERVICE="tensorboard"
fn_run
DEFAULT_SERVICE=${TEMP}
}
function fn_run_jupyter() {
TEMP=${DEFAULT_SERVICE}
DEFAULT_SERVICE="jupyter"
fn_run "rm"
echo "Set password for public access"
docker-compose -f ${SCRIPT_DIR}/${COMPOSE_FNAME} -p ${COMPOSE_PROJECT_NAME} exec ${DEFAULT_SERVICE} jupyter notebook password
echo "Restart jupyter server container(apply password)"
docker-compose -f ${SCRIPT_DIR}/${COMPOSE_FNAME} -p ${COMPOSE_PROJECT_NAME} restart ${DEFAULT_SERVICE}
echo "Enable jupyter-vim-binding nbextension"
docker-compose -f ${SCRIPT_DIR}/${COMPOSE_FNAME} -p ${COMPOSE_PROJECT_NAME} exec ${DEFAULT_SERVICE} jupyter nbextension enable vim_binding/vim_binding
docker-compose -f ${SCRIPT_DIR}/${COMPOSE_FNAME} -p ${COMPOSE_PROJECT_NAME} logs -t ${DEFAULT_SERVICE}
DEFAULT_SERVICE=${TEMP}
}
function fn_bash() {
fn_is_running
if [[ "${IS_RUNNING}" != "TRUE" ]]; then
fn_run
fi
echo "Connect to shell of '${COMPOSE_PROJECT_NAME}_${DEFAULT_SERVICE}' docker container"
fn_upgrade_compose
docker-compose -f ${SCRIPT_DIR}/${COMPOSE_FNAME} -p ${COMPOSE_PROJECT_NAME} exec ${DEFAULT_SERVICE} /bin/bash
}
function fn_kill() {
fn_is_running
if [[ "${IS_RUNNING}" == "TRUE" ]]; then
echo "Kill '${COMPOSE_PROJECT_NAME}_${DEFAULT_SERVICE}' docker container"
docker-compose -f ${SCRIPT_DIR}/${COMPOSE_FNAME} -p ${COMPOSE_PROJECT_NAME} kill ${DEFAULT_SERVICE}
if [[ "$1" == "rm" ]]; then
echo "Remove state of ${DEFAULT_SERVICE} service container"
docker-compose -f ${SCRIPT_DIR}/${COMPOSE_FNAME} -p ${COMPOSE_PROJECT_NAME} rm ${DEFAULT_SERVICE}
fi
else
echo "There is no running '${COMPOSE_PROJECT_NAME}_${DEFAULT_SERVICE}' docker container"
fi
}
function fn_down() {
fn_is_exist
if [[ "${IS_EXIST}" == "TRUE" ]]; then
echo "Down '${COMPOSE_PROJECT_NAME}' docker container"
docker-compose -f ${SCRIPT_DIR}/${COMPOSE_FNAME} -p ${COMPOSE_PROJECT_NAME} down -v
fi
}
function fn_main() {
fn_configure
if [[ "${DO_DOWN}" == "TRUE" ]]; then
fn_down
elif [[ "${DO_KILL}" == "TRUE" ]]; then
fn_kill
elif [[ "${DO_BASH}" == "TRUE" ]]; then
fn_bash
elif [[ "${DO_RUN}" == "TRUE" ]]; then
fn_run
elif [[ "${DO_BUILD}" == "TRUE" ]]; then
fn_build
fi
if [[ "${RUN_TENSORBOARD}" == "TRUE" ]]; then
fn_run_tensorboard
fi
if [[ "${RUN_JUPYTER}" == "TRUE" ]]; then
fn_run_jupyter
fi
}
function fn_upgrade_compose() {
verlt() {
[ "$1" = "$2" ] && return 1 || [ "$1" = "$(echo -e "$1\n$2" | sort -V | head -n1)" ]
}
CURRENT_COMPOSE_VERSION=$(docker-compose --version | sed 's/.*version\ //g' | sed 's/,.*//g')
if verlt ${CURRENT_COMPOSE_VERSION} ${COMPOSE_VERSION}; then
# compare current version to target version
echo "Upgrade docker-compose version from '${CURRENT_COMPOSE_VERSION}' to '${COMPOSE_VERSION}'"
echo "Installation needs 'sudo' permissions. Please enter your password."
sudo -k # make sure to ask for password on next sudo
if sudo true; then
sudo curl -L "https://github.com/docker/compose/releases/download/${COMPOSE_VERSION}/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
sudo ln -sf /usr/local/bin/docker-compose /usr/bin/docker-compose
else
echo "docker-compose is not upgraded"
exit 1
fi
fi
}
optspec=":bdrkstj-:"
while getopts "${optspec}" optchar; do
case ${optchar} in
-)
case "${OPTARG}" in
no-cache)
echo "Parsing option: '--${OPTARG}', build with no-cache";
NO_CACHE="--no-cache"
;;
project)
val="${!OPTIND}"; OPTIND=$(( $OPTIND + 1 ))
echo "Parsing option: '--${OPTARG}', value: '${val}'" >&2;
COMPOSE_PROJECT_NAME=${val}
;;
project=*)
val=${OPTARG#*=}
opt=${OPTARG%=$val}
echo "Parsing option: '--${opt}', value: '${val}'" >&2;
COMPOSE_PROJECT_NAME=${val}
;;
service)
val="${!OPTIND}"; OPTIND=$(( $OPTIND + 1 ))
echo "Parsing option: '--${OPTARG}', value: '${val}'" >&2;
DEFAULT_SERVICE=${val}
;;
service=*)
val=${OPTARG#*=}
opt=${OPTARG%=$val}
echo "Parsing option: '--${opt}', value: '${val}'" >&2;
DEFAULT_SERVICE=${val}
;;
release)
echo "Parsing option: '--${OPTARG}', release mode";
DEFAULT_SERVICE="release"
;;
tensorboard)
echo "Parsing option: '--${OPTARG}', run tensorboard";
RUN_TENSORBOARD="tensorboard"
;;
jupyter)
echo "Parsing option: '--${OPTARG}', run jupyter";
RUN_JUPYTER="TRUE"
;;
*)
if [ "${OPTERR}" == 1 ] || [ "${optspec:0:1}" != ":" ]; then
echo "Unknown option --${OPTARG}"
fi
;;
esac
;;
b)
DO_BUILD="TRUE"
;;
d)
DO_DOWN="TRUE"
;;
r)
DO_RUN="TRUE"
;;
s)
DO_BASH="TRUE"
;;
k)
DO_KILL="TRUE"
;;
t)
RUN_TENSORBOARD="TRUE"
;;
j)
RUN_JUPYTER="TRUE"
;;
*)
if [ "${OPTERR}" != 1 ] || [ "${optspec:0:1}" = ":" ]; then
echo "Non-option argument: '-${OPTARG}'"
fi
;;
esac
done
fn_main