-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompose-layout-migrate.sh
More file actions
executable file
·96 lines (76 loc) · 2.54 KB
/
compose-layout-migrate.sh
File metadata and controls
executable file
·96 lines (76 loc) · 2.54 KB
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
#!/usr/bin/env bash
#
# compose-layout-migrate.sh — deploy 폴더 구조 변경 후 compose 프로젝트 1회 마이그레이션
#
# 사용법:
# ./deploy/shared/scripts/compose-layout-migrate.sh <deploy_env> <service>
#
# 예시:
# ./deploy/shared/scripts/compose-layout-migrate.sh dev app
# ./deploy/shared/scripts/compose-layout-migrate.sh stage linkpie
set -o nounset -o errexit -o errtrace -o pipefail
DEPLOY_ENV="${1:-}"
SERVICE="${2:-}"
SCRIPT_DIR=$(dirname "$(realpath "${BASH_SOURCE[0]}")")
REPO_DIR="${DECK_REPO_DIR:-$(realpath "$SCRIPT_DIR/../../..")}"
function main::validate_deploy_env() {
case "$1" in
dev|stage) ;;
*) echo >&2 "ERROR: deploy_env must be dev or stage"; exit 1 ;;
esac
}
function main::validate_service() {
case "$1" in
app|linkpie|deskpie) ;;
*) echo >&2 "ERROR: service must be app, linkpie, or deskpie"; exit 1 ;;
esac
}
function main::rewrite_certbot_cron() {
local deploy_env="$1"
local crontab_file
crontab_file="$(mktemp)"
if crontab -l > "$crontab_file" 2>/dev/null; then
perl -0pi -e "s{(^[^\\n]*?)(?:DECK_DEPLOY_ENV=\\w+\\s+)?/home/ubuntu/deck/deploy/(?:shared/scripts|scripts)/certbot-renew\\.sh}{\$1DECK_DEPLOY_ENV=${deploy_env} /home/ubuntu/deck/deploy/shared/scripts/certbot-renew.sh}mg" "$crontab_file"
crontab "$crontab_file"
fi
rm -f "$crontab_file"
}
function main::migrate_env_file() {
local old_compose_dir="$1"
local new_compose_dir="$2"
if [[ -f "$new_compose_dir/.env" ]]; then
return 0
fi
if [[ ! -f "$old_compose_dir/.env" ]]; then
echo >&2 "ERROR: Missing $new_compose_dir/.env. Move the existing .env or create it from .env.example before deploy."
exit 1
fi
mv "$old_compose_dir/.env" "$new_compose_dir/.env"
}
function main::recreate_bind_mount_services() {
local new_compose_dir="$1"
local service="$2"
cd "$new_compose_dir"
docker compose up -d --force-recreate --no-deps \
"nginx-$service" \
loki \
promtail \
prometheus \
grafana
}
function main() {
[[ -n "$DEPLOY_ENV" && -n "$SERVICE" ]] || {
echo >&2 "Usage: $0 <deploy_env> <service>"
exit 1
}
main::validate_deploy_env "$DEPLOY_ENV"
main::validate_service "$SERVICE"
local old_compose_dir="$REPO_DIR/deploy/$DEPLOY_ENV/tencent/compose"
local new_compose_dir="$REPO_DIR/deploy/compose/tencent/$DEPLOY_ENV"
main::rewrite_certbot_cron "$DEPLOY_ENV"
main::migrate_env_file "$old_compose_dir" "$new_compose_dir"
main::recreate_bind_mount_services "$new_compose_dir" "$SERVICE"
}
if [[ "${BASH_SOURCE[0]}" == "$0" ]]; then
main
fi