-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
command-functions
executable file
·164 lines (140 loc) · 5.14 KB
/
command-functions
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
#!/usr/bin/env bash
source "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/config"
source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions"
source "$PLUGIN_CORE_AVAILABLE_PATH/common/property-functions"
source "$PLUGIN_AVAILABLE_PATH/maintenance/internal-functions"
source "$PLUGIN_AVAILABLE_PATH/nginx-vhosts/functions"
set -eo pipefail
[[ $DOKKU_TRACE ]] && set -x
cmd-maintenance-report() {
declare desc="displays a maintenance report for one or more apps"
declare cmd="maintenance:report"
[[ "$1" == "$cmd" ]] && shift 1
declare APP="$1" INFO_FLAG="$2"
local INSTALLED_APPS
INSTALLED_APPS=$(dokku_apps)
if [[ -n "$APP" ]] && [[ "$APP" == --* ]]; then
INFO_FLAG="$APP"
APP=""
fi
if [[ -z "$APP" ]] && [[ -z "$INFO_FLAG" ]]; then
INFO_FLAG="true"
fi
if [[ -z "$APP" ]]; then
for app in $INSTALLED_APPS; do
cmd-maintenance-report-single "$app" "$INFO_FLAG" | tee || true
done
else
cmd-maintenance-report-single "$APP" "$INFO_FLAG"
fi
}
cmd-maintenance-report-single() {
declare APP="$1" INFO_FLAG="$2"
if [[ "$INFO_FLAG" == "true" ]]; then
INFO_FLAG=""
fi
verify_app_name "$APP"
local flag_map=(
"--maintenance-enabled: $(fn-maintenance-enabled "$APP")"
)
if [[ -z "$INFO_FLAG" ]]; then
dokku_log_info2_quiet "${APP} maintenance information"
for flag in "${flag_map[@]}"; do
key="$(echo "${flag#--}" | cut -f1 -d' ' | tr - ' ')"
dokku_log_verbose "$(printf "%-30s %-25s" "${key^}" "${flag#*: }")"
done
else
local match=false
local value_exists=false
for flag in "${flag_map[@]}"; do
valid_flags="${valid_flags} $(echo "$flag" | cut -d':' -f1)"
if [[ "$flag" == "${INFO_FLAG}:"* ]]; then
value=${flag#*: }
size="${#value}"
if [[ "$size" -ne 0 ]]; then
echo "$value" && match=true && value_exists=true
else
match=true
fi
fi
done
[[ "$match" == "true" ]] || dokku_log_fail "Invalid flag passed, valid flags:${valid_flags}"
[[ "$value_exists" == "true" ]] || dokku_log_fail "not deployed"
fi
}
cmd-maintenance-custom-page() {
declare desc="imports a tarball from stdin; should contain at least maintenance.html"
declare APP="$2"
local TEMP_DIR
[[ -z "$APP" ]] && dokku_log_fail "Please specify an app to run the command on"
verify_app_name "$APP"
[[ -t 0 ]] && dokku_log_fail "Tar archive containing at least maintenance.html expected on stdin"
dokku_log_info1 "Importing custom maintenance page..."
TEMP_DIR=$(mktemp -d)
trap 'rm -rf "$TEMP_DIR" > /dev/null' RETURN INT TERM EXIT
pushd "$TEMP_DIR" >/dev/null
tar xvf - <&0
[[ ! -f "$TEMP_DIR/maintenance.html" ]] && dokku_log_fail "Tar archive missing maintenance.html"
mkdir -p "$DOKKU_ROOT/$APP/maintenance"
# shellcheck disable=SC2086
mv $TEMP_DIR/* "$DOKKU_ROOT/$APP/maintenance"
popd &>/dev/null || pushd "/tmp" >/dev/null
dokku_log_verbose "Done"
}
cmd-maintenance-disable() {
declare desc="take the app out of maintenance mode"
declare cmd="maintenance:disable"
[[ "$1" == "$cmd" ]] && shift 1
if [[ "$1" == "maintenance:off" ]]; then
dokku_log_warn "Deprecated: Please use maintenance:disable"
shift 1
fi
# Support --app/$DOKKU_APP_NAME flag by reordering args into "$cmd $DOKKU_APP_NAME $@"
local argv=("$@")
[[ -n "$DOKKU_APP_NAME" ]] && set -- "$DOKKU_APP_NAME" $@
##
declare APP="$1"
local NGINX_CONF_D="$DOKKU_ROOT/$APP/nginx.conf.d"
[[ -z "$APP" ]] && dokku_log_fail "Please specify an app to run the command on"
verify_app_name "$APP"
if [[ ! -f "$NGINX_CONF_D/maintenance.conf" ]]; then
dokku_log_info1 "Maintenance mode already disabled"
return
fi
dokku_log_info1 "Disabling maintenance mode for $APP..."
[[ -f "$NGINX_CONF_D/maintenance.conf" ]] && rm "$NGINX_CONF_D/maintenance.conf"
validate_nginx "$APP" && restart_nginx "$APP" >/dev/null
dokku_log_verbose "Done"
}
cmd-maintenance-enable() {
declare desc="put the app into maintenance mode"
declare cmd="maintenance:enable"
[[ "$1" == "$cmd" ]] && shift 1
if [[ "$1" == "maintenance:on" ]]; then
dokku_log_warn "Deprecated: Please use maintenance:enable"
shift 1
fi
# Support --app/$DOKKU_APP_NAME flag by reordering args into "$cmd $DOKKU_APP_NAME $@"
local argv=("$@")
[[ -n "$DOKKU_APP_NAME" ]] && set -- "$DOKKU_APP_NAME" $@
##
declare APP="$1"
local NGINX_CONF_D="$DOKKU_ROOT/$APP/nginx.conf.d"
local MAINTENANCE_APP_D="$DOKKU_ROOT/$APP/maintenance"
[[ -z "$APP" ]] && dokku_log_fail "Please specify an app to run the command on"
verify_app_name "$APP"
if [[ -f "$NGINX_CONF_D/maintenance.conf" ]]; then
dokku_log_info1 "Maintenance mode already enabled"
return
fi
dokku_log_info1 "Enabling maintenance mode for $APP..."
[[ -d "$NGINX_CONF_D" ]] || mkdir "$NGINX_CONF_D"
if [[ ! -d "$MAINTENANCE_APP_D" ]]; then
mkdir "$MAINTENANCE_APP_D"
cp "$MAINTENANCE_PLUGIN_ROOT/templates/maintenance.html" "$MAINTENANCE_APP_D"
fi
cp "$MAINTENANCE_PLUGIN_ROOT/templates/maintenance.conf" "$NGINX_CONF_D"
sed -i "s,{APP_ROOT},$DOKKU_ROOT/$APP," "$NGINX_CONF_D/maintenance.conf"
validate_nginx "$APP" && restart_nginx "$APP" >/dev/null
dokku_log_verbose "Done"
}