-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanage.sh
executable file
·227 lines (201 loc) · 6.25 KB
/
manage.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
#! /bin/bash
CurDir="$(cd "$(dirname "${BASH_SOURCE[0]}" )" && pwd)"
#####################################################################
# mysql environments, you can configure it according you own needs. #
#####################################################################
# set data dir
MYSQL_DATA=/data/mysql/data
MYSQL_LOG=/data/mysql/logs
if [ -z ${MYSQL_ROOT_PASSWORD} ]; then
MYSQL_ROOT_PASSWORD=root123
fi
if [ -z ${MYSQL_DATABASE} ]; then
MYSQL_DATABASE=ppic_test
fi
MYSQL_IMAGE=mysql:8.0
MYSQL_CONTAINER_NAME=mysql
if [ -z ${MYSQL_CONNECTION_URL} ]; then
MYSQL_CONNECTION_URL=root:${MYSQL_ROOT_PASSWORD}@${MYSQL_CONTAINER_NAME}:33060
fi
if [ -z ${PLATFORM} ]; then
PLATFORM=local
fi
########################################################################
# ppic app environments, you can configure it according you own needs. #
########################################################################
PPIC_CI_IMAGE=registry.cn-hangzhou.aliyuncs.com/leosocy/ppic:ci
PPIC_CONTAINER_NAME=ppic
BUILD_DIR=build
APP_EXE=./ppic_app
APP_TEST_NAME=ppic_test
PPIC_DOCKER_NETWORK=ppic-net
###########################
# Google cpplint settings #
###########################
CPP_LINT_IMAGE=registry.cn-hangzhou.aliyuncs.com/leosocy/cpplint
CPP_LINT_CONTAINER_NAME=cpplint
error_return() {
echo "[ERROR] $1"
exit 1
}
check_exec_success() {
if [ "$1" != "0" ]; then
echo "[ERROR] $2 failed!"
exit 1
else
echo "[INFO] $2 success!"
fi
}
updateimages() {
docker pull ${MYSQL_IMAGE}
docekr pull ${PPIC_CI_IMAGE}
}
is_mysql_running() {
docker inspect ${MYSQL_CONTAINER_NAME} 1>/dev/null 2>&1
return $?
}
is_docker_network_exist() {
nn=$(docker network ls -f name=$1 | tail -n 1 | awk '{print $2}')
if [ "${nn}" = "$1" ]; then
return 1
fi
return 0
}
startmysql() {
if [ "${PLATFORM}" != "local" ]; then
error_return "You can only start mysql container in **local**!"
fi
docker pull ${MYSQL_IMAGE}
check_exec_success "$?" "pulling mysql image"
docker kill ${MYSQL_CONTAINER_NAME} 1>/dev/null 2>&1
docker rm -v ${MYSQL_CONTAINER_NAME} 1>/dev/null 2>&1
docker network create ${PPIC_DOCKER_NETWORK}
docker run -d --rm --name ${MYSQL_CONTAINER_NAME} \
--network ${PPIC_DOCKER_NETWORK} \
-v ${CurDir}/configs/mysql/conf:/etc/mysql/conf.d \
-v ${MYSQL_DATA}:/var/lib/mysql \
-v ${MYSQL_LOG}:/var/log/mysql \
-e MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD} \
-e MYSQL_DATABASE=${MYSQL_DATABASE} \
--log-opt max-size=10m \
--log-opt max-file=9 \
${MYSQL_IMAGE}
check_exec_success "$?" "start mysql container"
}
reloadmysql() {
destroymysql
startmysql
}
restartmysql() {
stopmysql
startmysql
}
stopmysql() {
docker stop ${MYSQL_CONTAINER_NAME} 1>/dev/null 2>&1
docker rm -v ${MYSQL_CONTAINER_NAME} 1>/dev/null 2>&1
docker network rm ${PPIC_DOCKER_NETWORK}
}
destroymysql() {
stopmysql
rm -rf ${MYSQL_DATA}
rm -rf ${MYSQL_LOG}
check_exec_success "$?" "destroy mysql container"
}
createdb() {
docker exec ${MYSQL_CONTAINER_NAME} mysql -uroot -p${MYSQL_ROOT_PASSWORD} \
-e "CREATE DATABASE IF NOT EXISTS $1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci;" 1>/dev/null 2>&1
check_exec_success "$?" "create database $1"
}
dropdb() {
docker exec ${MYSQL_CONTAINER_NAME} mysql -uroot -p${MYSQL_ROOT_PASSWORD} \
-e "DROP DATABASE IF EXISTS $1" 1>/dev/null 2>&1
check_exec_success "$?" "drop database $1"
}
need_start_services() {
if [ "${PLATFORM}" = "local" ]; then
is_mysql_running
if [ "$?" != "0" ]; then
return 1
fi
fi
return 0
}
start_services() {
need_start_services
if [ $? = 1 ]; then
startmysql
fi
}
startapp() {
start_services
is_docker_network_exist ${PPIC_DOCKER_NETWORK}
if [ $? = 0 ]; then
restartmysql
fi
# start the rpc server
docker stop ${PPIC_CONTAINER_NAME} 2>/dev/null
docker rm -v ${PPIC_CONTAINER_NAME} 2>/dev/null
docker run -it --rm --name ${PPIC_CONTAINER_NAME} --network ${PPIC_DOCKER_NETWORK} \
-v ${CurDir}:/home/ppic -w /home/ppic \
-e MYSQL_CONNECTION_URL=${MYSQL_CONNECTION_URL} \
-e MYSQL_DATABASE=${MYSQL_DATABASE} \
${PPIC_CI_IMAGE} sh -c " \
rm -rf ${BUILD_DIR} && mkdir -p ${BUILD_DIR} && cd ${BUILD_DIR} \
&& cmake ../app && make -j && ${APP_EXE} \
"
}
runtest() {
start_services
is_docker_network_exist ${PPIC_DOCKER_NETWORK}
if [ $? = 0 ]; then
restartmysql
fi
docker stop ${PPIC_CONTAINER_NAME} 2>/dev/null
docker rm -v ${PPIC_CONTAINER_NAME} 2>/dev/null
docker run -it --rm --name ${PPIC_CONTAINER_NAME} --network ${PPIC_DOCKER_NETWORK} \
-v ${CurDir}:/home/ppic -w /home/ppic \
-e MYSQL_CONNECTION_URL=${MYSQL_CONNECTION_URL} \
-e MYSQL_DATABASE=${MYSQL_DATABASE} \
${PPIC_CI_IMAGE} sh -c " \
mkdir -p ${BUILD_DIR} && cd ${BUILD_DIR} \
&& cmake ../tests && make -j build_and_test
"
}
cpplint() {
docker stop ${CPP_LINT_CONTAINER_NAME} 2>/dev/null
docker rm -v ${CPP_LINT_CONTAINER_NAME} 2>/dev/null
docker run -it --rm --name ${CPP_LINT_CONTAINER_NAME} \
-v ${CurDir}:/home/ppic -w /home \
${CPP_LINT_IMAGE} sh -c " \
cpplint --linelength=120 --extensions=h,cpp,cc,c \
\$(find ppic/app -name *.h -or -name *.c -or -name *.cc -or -name *.cpp)
"
}
################
# script start #
################
case "$1" in
startapp) startapp ;;
runtest) runtest ;;
cpplint) cpplint ;;
createdb) createdb $2 ;;
dropdb) dropdb $2 ;;
startmysql) startmysql ;;
stopmysql) stopmysql ;;
reloadmysql) reloadmysql ;;
restartmysql) restartmysql ;;
destroymysql) destroymysql ;;
updateimages) updateimages ;;
*)
echo "Usage:"
echo "./manage.sh startapp"
echo "./manage.sh runtest"
echo "./manage.sh cpplint"
echo "./manage.sh createdb|dropdb"
echo "./manage.sh startmysql|stopmysql|destroymysql"
echo "./manage.sh restartmysql(stop+start)|reloadmysql(destroy+start)"
echo "./manage.sh updateimages"
exit 1
;;
esac
exit 0