forked from dokku/dokku
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dokku
executable file
·188 lines (156 loc) · 5.27 KB
/
dokku
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
#!/usr/bin/env bash
set -eo pipefail
shopt -s nullglob
export DOKKU_DISTRO=${DOKKU_DISTRO:="ubuntu"}
export DOKKU_IMAGE=${DOKKU_IMAGE:="progrium/buildstep"}
export DOKKU_ROOT=${DOKKU_ROOT:=~dokku}
export PLUGIN_PATH=${PLUGIN_PATH:="/var/lib/dokku/plugins"}
export DOKKU_NOT_IMPLEMENTED_EXIT=10
export DOKKU_VALID_EXIT=0
source "$PLUGIN_PATH/common/functions"
[[ -f $DOKKU_ROOT/dokkurc ]] && source $DOKKU_ROOT/dokkurc
[[ -d $DOKKU_ROOT/.dokkurc ]] && for f in $DOKKU_ROOT/.dokkurc/*; do source $f; done
[[ $DOKKU_TRACE ]] && set -x
parse_args "$@"
for arg in "$@"; do
[[ "$arg" =~ ^--.* ]] && shift 1
done
! has_tty && DOKKU_QUIET_OUTPUT=1
if [[ $(id -un) != "dokku" && $1 != plugins-install* && $1 != "plugins-update" ]]; then
sudo -u dokku -E -H $0 "$@"
exit
fi
if [[ -n "$SSH_ORIGINAL_COMMAND" ]]; then
export -n SSH_ORIGINAL_COMMAND
if [[ $1 =~ config-* ]];then
xargs $0 <<<$SSH_ORIGINAL_COMMAND
exit $?
else
$0 $SSH_ORIGINAL_COMMAND
exit $?
fi
fi
case "$1" in
receive)
APP="$2"; IMAGE="dokku/$APP"; IMAGE_SOURCE_TYPE="$3"; TMP_WORK_DIR="$4"
dokku_log_info1 "Cleaning up..."
dokku cleanup
dokku_log_info1 "Building $APP from $IMAGE_SOURCE_TYPE..."
dokku build "$APP" "$IMAGE_SOURCE_TYPE" "$TMP_WORK_DIR"
dokku_log_info1 "Releasing $APP..."
dokku release "$APP" "$IMAGE_SOURCE_TYPE"
dokku_log_info1 "Deploying $APP..."
dokku deploy "$APP"
dokku_log_info2 "Application deployed:"
dokku urls "$APP" | sed "s/^/ /"
echo
;;
deploy)
APP="$2"; IMAGE="dokku/$APP"
pluginhook pre-deploy $APP
if [[ -f "$DOKKU_ROOT/$APP/CONTAINER" ]]; then
oldid=$(< "$DOKKU_ROOT/$APP/CONTAINER")
fi
# start the app
DOCKER_ARGS=$(: | pluginhook docker-args $APP deploy)
DOCKER_ARGS+=$(: | pluginhook docker-args-deploy $APP)
BIND_EXTERNAL=$(pluginhook bind-external-ip $APP)
is_image_buildstep_based "$IMAGE" && START_CMD="/start web"
if [[ "$BIND_EXTERNAL" = "false" ]];then
port=5000
id=$(docker run -d -e PORT=$port $DOCKER_ARGS $IMAGE $START_CMD)
ipaddr=$(docker inspect --format '{{ .NetworkSettings.IPAddress }}' $id)
else
id=$(docker run -d -p 5000 -e PORT=5000 $DOCKER_ARGS $IMAGE $START_CMD)
port=$(docker port $id 5000 | sed 's/[0-9.]*://')
ipaddr=127.0.0.1
fi
echo $ipaddr > "$DOKKU_ROOT/$APP/IP"
# if we can't post-deploy successfully, kill new container
kill_new() {
docker inspect $id &> /dev/null && docker kill $id > /dev/null
trap - INT TERM EXIT
kill -9 $$
}
# run checks first, then post-deploy hooks, which switches Nginx traffic
trap kill_new INT TERM EXIT
dokku_log_info1 "Running pre-flight checks"
pluginhook check-deploy $id $APP $port ${ipaddr:-localhost}
# now using the new container
echo $id > "$DOKKU_ROOT/$APP/CONTAINER"
echo $port > "$DOKKU_ROOT/$APP/PORT"
echo "http://$(< "$DOKKU_ROOT/HOSTNAME"):$port" > "$DOKKU_ROOT/$APP/URL"
dokku_log_info1 "Running post-deploy"
pluginhook post-deploy $APP $port $ipaddr
trap - INT TERM EXIT
# kill the old container
if [[ -n "$oldid" ]]; then
# Let the old container finish processing requests, before terminating it
WAIT="${DOKKU_WAIT_TO_RETIRE:-60}"
dokku_log_info1 "Shutting down old container in $WAIT seconds"
(
exec >/dev/null 2>/dev/null </dev/null
trap '' INT HUP
sleep $WAIT
docker kill $oldid
) & disown -a
# Use trap since disown/nohup don't seem to keep child alive
# Give child process just enough time to set the traps
sleep 0.1
fi
;;
cleanup)
# delete all non-running container
# shellcheck disable=SC2046
docker rm $(docker ps -a -f 'status=exited' -q) &> /dev/null &
# delete unused images
# shellcheck disable=SC2046
docker rmi $(docker images -f 'dangling=true' -q) &> /dev/null &
;;
plugins)
ls -1 -d $PLUGIN_PATH/*/
;;
plugins-install)
pluginhook install
;;
plugins-install-dependencies)
pluginhook dependencies
;;
plugins-update)
pluginhook update
;;
# DEPRECATED as of v0.3.14
deploy:all)
echo "*DEPRECATED* in v0.3.14: deploy:all will be removed in future versions"
dokku ps:restartall
;;
help|'')
echo "Usage: dokku [--quiet|--trace|--rm-container|--rm|--force] COMMAND <app> [command-specific-options]"
echo ""
echo "Options:"
cat<<EOF | pluginhook commands help | sort
help Print the list of commands
plugins Print active plugins
plugins-install Install active plugins
plugins-update Update active plugins
EOF
;;
*)
implemented=0
for script in $PLUGIN_PATH/*/commands; do
set +e; $script "$@" ; exit_code=$? ; set -e
if [ "$exit_code" -eq "$DOKKU_NOT_IMPLEMENTED_EXIT" ]; then
continue
fi
implemented=1
if [ "$exit_code" -ne "$DOKKU_VALID_EXIT" ]; then
exit $exit_code
fi
done
if [ "$implemented" -eq 0 ]; then
dokku_log_warn "\`$*\` is not a dokku command."
dokku_log_warn "See \`dokku help\` for a list of available commands."
exit 1
fi
;;
esac