-
Notifications
You must be signed in to change notification settings - Fork 0
/
crustil
executable file
·385 lines (359 loc) · 15.2 KB
/
crustil
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
#!/usr/bin/env bash
set -e
typeset -A config
typeset -A param
CRUSTIL="$(npm root -g)/crustil/"
function setParam() {
value=$2
if [[ "$value" == *"#lib"* ]]; then
value="${value//\#lib/$(npm root -g)\/crustil}"
fi
if [[ "$3" == "" ]]; then
echo "= set ${1} to ${value}"
fi
[[ ${1} = [\#!]* ]] || [[ ${1} = "" ]] || param[$1]=${value}
}
function setConfig() {
value=$2
if [[ "$value" == *"\$(pwd)"* ]]; then
value="${value//\$(pwd)/$(pwd)\/${param['config.file.path']}}"
fi
if [[ "$value" == *"\$(lib)"* ]]; then
value="${value//\$(lib)/$(npm root -g)\/crustil}"
fi
value="${value//\\/}"
value=$(echo $value | tr -s /)
if [[ "$3" == "" ]]; then
echo "= set ${1} to ${value}"
fi
[[ ${1} = [\#!]* ]] || [[ ${1} = "" ]] || config[$1]=${value}
VAR=${1^^}
export "CONFIG_${VAR//./_}=${value}"
}
# todo write usage
function usage() {
echo "$0 start|stop|restart|scale|sh|dev|logs|status|purge|swarm[registry|pull|push|deploy|status|rm|leave]"
# ./run.sh start law-service --project-path=../../micro-service/project --config-path=../config
echo "todo"
}
# Default config
setParam "config.file.path" "${CRUSTIL}" 0
setParam "config.file.name" "config.properties" 0
setParam "project.path" "${CRUSTIL}" 0
setParam "compose.path" "${CRUSTIL}" 0
setParam "working.directory" "${CRUSTIL}" 0
if [[ " $@ " =~ --working-directory=([^' ']+) ]]; then
setParam "working.directory" ${BASH_REMATCH[1]}
fi
if [[ " $@ " =~ --config-path=([^' ']+) ]]; then
setParam "config.file.path" ${BASH_REMATCH[1]}
fi
if [[ " $@ " =~ --config-name=([^' ']+) ]]; then
setParam "config.file.name" ${BASH_REMATCH[1]}
fi
if [[ " $@ " =~ --project-path=([^' ']+) ]]; then
setParam "project.path" ${BASH_REMATCH[1]}
SERVICE_LIST_WITH_PATH=$(find ${param['project.path']} -type f | grep '.*-service\/pom.xml\|.*-gateway\/pom.xml' | egrep -o '.*\/([a-z-]+)-([a-z]+)')
fi
# --compose-path=[../,.,/home]
if [[ " $@ " =~ --compose-path=\[([^' ']+)\] ]]; then
setParam "compose.path" ${BASH_REMATCH[1]}
fi
function generate_default_properties_file() {
cat <<EOF > "${param['config.file.path']}/${param['config.file.name']}"
project.name=framework
project.version=0.0.1
project.domain=<domain.fr>
project.api=<api.domain.fr>
registry=registry.domain.fr/<project_name>
addon.pos=41
nginx.config.path=./nginx/
db.connector=db
db.root.host=%
db.database=fc
db.root.password=password
db.user=test
db.password=password
EOF
}
if [[ " $@ " == *" --generate-config-file "* ]]; then
generate_default_properties_file
fi
if [[ ! -f ${param['config.file.path']}/${param['config.file.name']} ]]; then
echo "error: ${param['config.file.path']}/${param['config.file.name']} not found!"
exit -1
fi
while IFS=$':= \t' read key value; do
if [[ "$key" != "" ]]; then
setConfig ${key} ${value}
fi
done < "${param['config.file.path']}/${param['config.file.name']}"
for service_key in "${!config[@]}"; do
if [[ "$service_key" == *"project.services."* ]]; then
if [[ "$INTERNAL_SERVICE_LIST" == "" ]]; then
INTERNAL_SERVICE_LIST="${config[$service_key]}"
else
INTERNAL_SERVICE_LIST="$INTERNAL_SERVICE_LIST,${config[$service_key]}"
fi
fi
done
function prod_init() {
for project_key in "${!config[@]}"; do
if [[ "$project_key" =~ project.(.*).name ]]; then
VAR_NAME=${project_key^^}
VAR_VERSION=${project_key//NAME/VERSION}
export "CONFIG_${VAR_NAME//./_}="$1"${config[$project_key]}"
export "CONFIG_${VAR_VERSION//./_}=${config[${project_key//name/version}]}"
echo "todo => check & fix"
echo "+ production mode: ${BASH_REMATCH[1]} ($1${config[$project_key]}, version: ${config[${project_key//name/version}]})"
#echo "+ production mode: ${BASH_REMATCH[1]} (${config['registry']}/${config[$project_key]}, version: ${config[${project_key//name/version}]})"
fi
done
}
if [[ " $@ " == *" --prod "* ]]; then
prod_init "${config['registry']}/"
else
prod_init
fi
for current_compose_file in ${param['compose.path']//,/ }; do
echo -e "\n# find compose files in: $current_compose_file"
for current_compose_file in $(find $current_compose_file -type f -exec readlink -f {} \; | grep '.*-compose.yml'); do
echo "-> found: $current_compose_file"
if [[ "$COMPOSE_FILE" == "" ]]; then
export COMPOSE_FILE="$current_compose_file"
else
export COMPOSE_FILE="$COMPOSE_FILE:$current_compose_file"
fi
done
done
echo ""
function service_list() {
echo ==============================
echo ========== Services ==========
echo ==============================
echo ${INTERNAL_SERVICE_LIST//,/$'\n'}
echo ==============================
}
function cmd() {
if [[ $2 == "all" ]]; then
docker-compose $1 ${INTERNAL_SERVICE_LIST//,/ }
elif [[ $2 == *"-service"* || $2 == *"-gateway"* ]]; then
docker-compose $1 $2
else
project_service=${config['project.services.'$2]}
if [[ "$project_service" != "" ]]; then
docker-compose $1 ${project_service//,/ }
else
usage
fi
fi
}
function sql() {
# $1 cmd sql dump & $2 file destination (sql)
echo -e "## dump '${config['db.database']}' database => $1"
docker exec -i ${config['project.name']}_${config['db.connector']}_1 sh -c "exec mysqldump $1 -uroot -p${config['db.root.password']}" > ./sql/$2.sql
}
if [[ $1 == "generate" ]]; then
mkdir -p output
docker-compose config > output/project.yml
elif [[ $1 == "save" ]]; then
mkdir -p output/images
for img in $(docker-compose config | awk '{if ($1 == "image:") print $2;}'); do
echo "extract ${img//\//-} image... "
docker save -o output/images/${img//\//-}.tar $img
done
echo ""
du -hs output/images/*
du -hs output/images
elif [[ $1 == "stop" ]]; then
if [[ $2 == "all" ]]; then
docker-compose down
else
cmd "rm -svf" $2
fi
elif [[ $1 == "purge" ]]; then
if [[ $(docker-compose ps -q) != "" ]]; then
docker rm -f $(docker-compose ps -q)
fi
if [[ $(docker images --filter=reference="*/${config['project.name']}/*" -q) != "" ]]; then
docker rmi -f $(docker images --filter=reference="*/${config['project.name']}/*" -q)
fi
docker volume prune
elif [[ $1 == "start" ]]; then
cmd "up -d --remove-orphans" $2
#if [[ $2 == "scale" ]]; then
# docker-compose up -d $(echo $(cat ./services-list-scale) | sed 's/ / --scale /g' | sed 's/^/--scale /') $(cat ./services-list-prod)
#fi
elif [[ $1 == "build" ]]; then
# todo move to external build script
if [[ $2 == "java-service" ]]; then
#docker build -t "custom-java-vertx:${config['project.'$3'.version']}" -f ${param['working.directory']}/java/Dockerfile ${param['working.directory']}/java
docker build -t "custom-java-vertx:${config['project.'$3'.version.base']}" -f ${param['working.directory']}/java/Dockerfile ${param['working.directory']}/java
for item in $SERVICE_LIST_WITH_PATH; do
if [[ "$item" =~ .*\/([a-z-]+-[a-z]+) ]]; then
current_item=${BASH_REMATCH[1]}
echo "build : $current_item"
mkdir -vp "${param['project.path']}/${current_item}/target/config"
echo "generate docker.json config"
( echo "cat <<EOF > ${param['project.path']}/${current_item}/target/config/docker.json" ; cat ${param['project.path']}/${current_item}/src/config/docker.json ) | sh
docker build --build-arg SERVICE=${current_item} -t "${config['project.'$3'.name']}/${current_item}" -f ${param['working.directory']}/java/Dockerfile.java ${item}/
fi
done
elif [[ $2 == "other" ]]; then
echo "other build"
docker-compose build $3
fi
elif [[ $1 == "tool" ]]; then
docker-compose up $2
elif [[ $1 == "restart" ]]; then
cmd "down" $2
cmd "up" $2
elif [[ $1 == "rm" ]]; then
cmd "rm -svf" $2
elif [[ $1 == "rebuild" ]]; then
cmd "down" all
cmd "rm -svf" all
java/build.sh
start all
elif [[ $1 == "update" ]]; then
if [[ $2 == "health-service" ]]; then
CONTAINER_NAME="health-service"
SERVICES=$(docker ps | awk '{print $NF}' | grep "${config['project.name']}_${CONTAINER_NAME}")
FIRST_NUM=$(docker ps | awk '{print $NF}' | grep "${config['project.name']}_${CONTAINER_NAME}" | grep -c ${config['project.name']})
echo ${CONTAINER_NAME}: ${SERVICES}
echo ${CONTAINER_NAME}: ${FIRST_NUM}
docker-compose up -d --no-recreate --scale "${CONTAINER_NAME}=$[${FIRST_NUM}*2]" "${CONTAINER_NAME}"
# todo rewrite
while read -r line; do
echo ${line}
docker stop ${line}
docker rm ${line}
done <<< ${SERVICES}
docker-compose up -d --no-recreate --scale "${CONTAINER_NAME}=${FIRST_NUM}" "${CONTAINER_NAME}"
elif [[ $2 == "list" ]]; then
# todo rewrite
while read -r CONTAINER_NAME; do
SERVICES=$(docker ps | awk '{print $NF}' | grep "${config['project.name']}_${CONTAINER_NAME}")
FIRST_NUM=$(docker ps | awk '{print $NF}' | grep "${config['project.name']}_${CONTAINER_NAME}" | grep -c ${config['project.name']})
echo ${CONTAINER_NAME}: ${SERVICES}
echo ${CONTAINER_NAME}: ${FIRST_NUM}
docker-compose up -d --no-recreate --scale "${CONTAINER_NAME}=$[${FIRST_NUM}*2]" "${CONTAINER_NAME}"
echo "=====> wait new container has alive"
sleep 90
while read -r line; do
echo ${line}
if [[ ${line} != *"api-gateway"* && ${line} != *"health-service"* ]]; then
IP=$(docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' ${line})
echo ${IP}
curl --header "Content-Type: application/json" \
--request POST \
--data '{"ip":"'${IP}'"}' \
http://health-service/api/trash
sleep 60
fi
docker stop ${line}
docker rm ${line}
done <<< ${SERVICES}
docker-compose up -d --no-recreate --scale "${CONTAINER_NAME}=${FIRST_NUM}" "${CONTAINER_NAME}"
done <<< $(cat ./services-list-update)
fi
elif [[ $1 == "logs" ]]; then
for i in ${*:2}; do
if [[ $i != *"--"* ]]; then
project_service+=${config['project.services.'$i]},
fi
done
echo "# add folowing services to logger"
echo "-> ${project_service//,/ }"
docker-compose logs -ft --tail=100 ${project_service//,/ }
elif [[ $1 == "sh" ]]; then
docker-compose exec $2 bash
elif [[ $1 == "scale" ]]; then
service_list services-list-scale
docker-compose up -d $(echo $(cat ./services-list-scale) | sed 's/ / --scale /g' | sed 's/^/--scale /') $(cat ./services-list-prod)
elif [[ $1 == "status" ]]; then
service_list
docker-compose ps --services
docker-compose ps
elif [[ $1 == "swarm" && $# > 1 ]]; then
if [[ $2 == "registry" ]]; then
docker service create --network mynet --replicas 2 --name registry -p 5000:5000 registry:2
docker service ls
elif [[ $2 == "pull" ]]; then
export CONFIG_PROJECT_NAME=${config['registry']}/${config['project.name']}
if [[ $3 != "" ]]; then
docker-compose pull
else
docker-compose pull
fi
elif [[ $2 == "push" && $3 != "all" ]]; then
docker tag ${config['project.name']}/$3 ${config['registry']}/${config['project.name']}/$3:${config['project.todo.version']}
docker push ${config['registry']}/${config['project.name']}/$3:${config['project.todo.version']}
docker tag ${config['registry']}/${config['project.name']}/$3:${config['project.todo.version']} ${config['registry']}/${config['project.name']}/$3:latest
docker push ${config['registry']}/${config['project.name']}/$3:latest
elif [[ $2 == "push" && $3 == "all" ]]; then
for item in $SERVICE_LIST_WITH_PATH; do
if [[ "$item" =~ .*\/([a-z-]+-[a-z]+) ]]; then
line=${BASH_REMATCH[1]}
echo $line
docker tag ${config['project.'$4'.name']}/${line} ${config['registry']}/${config['project.'$4'.name']}/${line}:${config['project.'$4'.version']}
docker push ${config['registry']}/${config['project.'$4'.name']}/${line}:${config['project.'$4'.version']}
docker tag ${config['registry']}/${config['project.'$4'.name']}/${line}:${config['project.'$4'.version']} ${config['registry']}/${config['project.'$4'.name']}/${line}:latest
docker push ${config['registry']}/${config['project.'$4'.name']}/${line}:latest
fi
done
elif [[ $2 == "bundle" ]]; then
docker-compose bundle -o ${config['project.name']}-bundle.dab
elif [[ $2 == "deploy" ]]; then
docker-compose bundle -o deploy-swarm.dab
docker stack deploy --bundle-file deploy-swarm.dab ${config['project.name']}
elif [[ $2 == "status" ]]; then
docker stack services ${config['project.name']}
elif [[ $2 == "rm" ]]; then
docker stack rm ${config['project.name']}
elif [[ $2 == "leave" ]]; then
docker swarm leave --force
fi
elif [[ $1 == "database" ]]; then
if [[ $2 == "apply" ]]; then
if [[ "${config['project.'$3'.sql.'$4]}" == "" ]]; then
echo "project $3 not found (or $4 option not set)"
exit -1;
fi
for sql_script in $(find ${config['project.'$3'.sql.'$4]} -type f -print | sort -n); do
echo -e "[$4-$3] apply SQL script <$sql_script>"
cat $sql_script | docker exec -i services_${config['db.connector']}_1 sh -c "exec mysql -uroot -p${config['db.root.password']}"
done
elif [[ $2 == "dump" ]]; then
echo -e "## dump '${config['db.database']}' database"
sql "--databases ${config['db.database']}" save/${config['db.database']}-dump
sql "--no-create-info --skip-triggers --no-create-db --databases ${config['db.database']}" save/${config['db.database']}-data-only-dump
elif [[ $2 == "restore" ]]; then
echo -e "Restore backup database"
cat sql/save/${config['db.database']}-dump.sql | docker exec -i ${config['project.name']}_${config['db.connector']}_1 sh -c "exec mysql ${config['db.database']} -uroot -p${config['db.root.password']}"
elif [[ $2 == "init-script" ]]; then
echo -e "## make init script for '${config['db.database']}'"
echo -e "## make database base structure script"
sql "--no-data --databases ${config['db.database']} --tables account token law group path" init/11-${config['db.database']}-init
echo -e "## make law data script"
sql "--extended-insert=FALSE --no-create-info --skip-triggers --no-create-db --databases ${config['db.database']} --tables law group path" init/12-${config['db.database']}-law-init
echo -e "## make account data script"
sql "--extended-insert=FALSE --no-create-info --skip-triggers --no-create-db --databases ${config['db.database']} --tables account" init/13-${config['db.database']}-account-init
elif [[ $2 == "addons-script" ]]; then
addon_nb=${config['addon.pos']}
# todo rewrite
while IFS= read -r addon; do
IFS=';' read -r -a data <<< ${addon}
name=${data[0]}
tables=${data[@]:1}
nb=$((addon_nb++))
echo -e "## make addon ${data[0]} script"
echo -e "${tables}"
#--no-data
sql "--no-create-db --databases ${config['db.database']} --tables ${tables}" init/${nb}-${config['db.database']}-addon-${name}-init
echo "done"
done <<< $(cat ./sql/addons)
fi
else
usage
fi