forked from dokku-alt/dokku-alt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdokku_common
More file actions
229 lines (188 loc) · 4.77 KB
/
Copy pathdokku_common
File metadata and controls
229 lines (188 loc) · 4.77 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
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
#!/bin/bash
BUILDSTEP_IMAGE="${BUILDSTEP_IMAGE:=ayufan/dokku-alt-buildstep:cedar-14}"
DOKKU_EXPOSED_PORTS="${DOKKU_EXPOSED_PORTS:=5000 8080 80}"
set -eo pipefail
[[ $DOKKU_TRACE ]] && set -x
function info() {
echo "-----> $@"
}
function info2() {
echo "=====> $@"
}
function verbose() {
echo " $@"
}
function fail() {
echo "$@" 1>&2
exit 1
}
function generate_random_password() {
< /dev/urandom tr -dc A-Za-z0-9 | head -c${1:-16} || true
}
function stop_container() {
docker stop "$@" 1>/dev/null 2>/dev/null || true
}
function remove_container() {
docker rm "$@" 1>/dev/null 2>/dev/null || true
}
function stop_and_remove_container() {
stop_container "$@"
remove_container "$@"
}
function remove_image() {
docker rmi "$@" 1>/dev/null 2>/dev/null || true
}
function verify_max_args() {
local MAX_ARGS="$1"
[[ $# -le $((MAX_ARGS+1)) ]] || fail "Invalid number of arguments ($(($#-1)), expected $MAX_ARGS)."
}
function sanitize_name() {
local NAME="$1"
[[ "$NAME" == "" ]] && fail "You must specify an $2"
local NAME2="${NAME//[^a-zA-Z0-9_-]//}"
[[ "$NAME" != "$NAME2" ]] && fail "Invalid $2"
return 0
}
function check_app_name() {
sanitize_name "$1" "app name"
APP="$1"
IMAGE_GENERIC="dokku/$APP"
if [[ "$IMAGE_TAG" != "" ]]; then
IMAGE="$IMAGE_GENERIC:$IMAGE_TAG"
else
IMAGE="$IMAGE_GENERIC:latest"
fi
APP_DIR="$DOKKU_ROOT/$APP"
APP_NAME_FILE="$APP_DIR/CONTAINER"
APP_NAME=""
[[ -f "$APP_NAME_FILE" ]] && APP_NAME="$(< "$APP_NAME_FILE")"
APP_CID_DIR="$APP_DIR/CONTAINERS"
APP_CACHE_NAME="cache_data_$APP"
APP_PERSISTENT_NAMES="$APP_CACHE_NAME"
APP_BUILD_LOCK="$APP_DIR/build.lock"
return 0
}
function get_app_container_name() {
echo "${1}_${APP}_$(date +%s)_$RANDOM"
}
function get_cid_file_name() {
[[ "$APP_CID_DIR" == "" ]] && fail "$APP: app not specified"
mkdir -p "$APP_CID_DIR"
echo "$APP_CID_DIR/${1}_$(date +%s)_$RANDOM.cid"
}
function config_get() {
[[ "$APP" == "" ]] && fail "$APP: app not specified"
[[ "$1" == "" ]] && fail "$APP: config key not specified"
: | pluginhook env-vars "$APP" | grep "^$1=" | cut -d"=" -f2-
}
function stop_and_remove_app_containers() {
[[ "$APP_CID_DIR" == "" ]] && fail "$APP: app not specified"
if [[ "$1" != "" ]]; then
info "Shutting down old containers ($1)"
stop_and_remove_container "$1_$APP" &
FILTER="$1_*.cid"
else
info "Shutting down old containers"
stop_and_remove_container "dokku_$APP" &
stop_and_remove_container "run_$APP" &
FILTER="*.cid"
fi
if [[ -d "$APP_CID_DIR" ]]; then
while read CID_FILE; do
(
CID="$(< "$CID_FILE")"
if [[ "$CID" != "$2" ]]; then
stop_and_remove_container "$(< "$CID_FILE")";
rm -f "$CID_FILE"
fi
) &
done < <(find "$APP_CID_DIR" -name "$FILTER")
fi
wait
}
function verify_app_name() {
check_app_name "$@"
[[ ! -d "$APP_DIR" ]] && fail "$APP: app doesn't exist"
return 0
}
function require_image() {
IMAGES=$(docker images "$1" | wc -l)
[[ $IMAGES -ne 0 ]] && return
fail "$1 image not found... Did you run 'dokku plugins-install' ?"
}
function release_app() {
dokku release "$@"
}
function deploy_app() {
dokku deploy "$@"
}
function redeploy_app() {
(
# run everything in subshell
verify_app_name "$1"
if ! docker_image_exists "$IMAGE_GENERIC:build"; then
verbose "Application ($IMAGE_GENERIC:build) is not yet deployed!"
# we don't fail here, because we assume that this is intended
exit 0
fi
release_app "$@"
deploy_app "$@"
)
}
function tag_image() {
docker tag -f "$1" "$2"
}
function wait_for_container() {
for i in 1 2 3 4 5 6 7 8 9 0; do
sleep 0.3s
if docker inspect "$1" > /dev/null; then
return 0
fi
done
return 0
}
function is_image_buildstep_based() {
docker run --entrypoint="/bin/bash" --rm "$@" -c "[[ -f /exec ]]"
}
function wait_for_id() {
while read ID; do
[[ "$ID" =~ ^[0-9a-f]+$ ]] && return
echo "$ID"
done
return 1
}
function commit_image() {
wait_for_id
wait_for_container "$ID"
test $(docker wait "$ID") -eq 0
docker commit "$ID" "$@" 1>/dev/null 2>/dev/null
docker rm -f "$ID" 1>/dev/null 2>/dev/null || true
return 0
}
function attach_and_commit_image() {
wait_for_id
wait_for_container "$ID"
docker logs -f "$ID"
test $(docker wait "$ID") -eq 0
docker commit "$ID" "$@" 1>/dev/null 2>/dev/null
docker rm -f "$ID" 1>/dev/null 2>/dev/null || true
return 0
}
function reset_timestamps() {
find "$@" -exec touch -t 201401010000 "{}" \;
}
function docker_image_exists() {
docker inspect "$@" >/dev/null 2>/dev/null
}
function print_acl_access() {
[[ -z "$1" ]] && fail "Missing ACL access mode"
[[ -z "$2" ]] && fail "Missing ACL parameter"
echo "$1"
echo "$2"
}
function print_app_acl_access() {
[[ -z "$APP" ]] && fail "No APP configured"
print_acl_access "$1" "$APP"
}
[[ -z "$DOKKU_ROOT" ]] && fail "DOKKU_ROOT not set"
CWD="$(cd "$(dirname "$0")" && pwd)"