|
1 | 1 | #!/usr/bin/env bash
|
2 | 2 | set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x
|
3 | 3 | source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions"
|
4 |
| -source "$PLUGIN_AVAILABLE_PATH/checks/functions" |
5 |
| -source "$PLUGIN_AVAILABLE_PATH/config/functions" |
6 |
| -source "$PLUGIN_CORE_AVAILABLE_PATH/proxy/functions" |
7 |
| - |
8 |
| -dokku_deploy_cmd() { |
9 |
| - declare desc="deploy phase" |
10 |
| - local cmd="deploy" |
11 |
| - [[ -z $2 ]] && dokku_log_fail "Please specify an app to deploy" |
12 |
| - local APP="$2"; local IMAGE_TAG="$3"; local IMAGE=$(get_deploying_app_image_name "$APP" "$IMAGE_TAG") |
13 |
| - verify_app_name "$APP" |
14 |
| - plugn trigger pre-deploy "$APP" "$IMAGE_TAG" |
15 |
| - |
16 |
| - is_image_herokuish_based "$IMAGE" && local DOKKU_HEROKUISH=true |
17 |
| - local DOKKU_SCALE_FILE="$DOKKU_ROOT/$APP/DOKKU_SCALE" |
18 |
| - local oldids=$(get_app_container_ids "$APP") |
19 |
| - |
20 |
| - local DOKKU_DEFAULT_DOCKER_ARGS=$(: | plugn trigger docker-args-deploy "$APP" "$IMAGE_TAG") |
21 |
| - local DOKKU_IS_APP_PROXY_ENABLED="$(is_app_proxy_enabled "$APP")" |
22 |
| - local DOKKU_DOCKER_STOP_TIMEOUT="$(config_get "$APP" DOKKU_DOCKER_STOP_TIMEOUT || true)" |
23 |
| - [[ $DOKKU_DOCKER_STOP_TIMEOUT ]] && DOCKER_STOP_TIME_ARG="--time=${DOKKU_DOCKER_STOP_TIMEOUT}" |
24 |
| - |
25 |
| - local line; local PROC_TYPE; local PROC_COUNT; local CONTAINER_INDEX |
26 |
| - while read -r line || [[ -n "$line" ]]; do |
27 |
| - [[ "$line" =~ ^#.* ]] && continue |
28 |
| - line="$(strip_inline_comments "$line")" |
29 |
| - PROC_TYPE=${line%%=*} |
30 |
| - PROC_COUNT=${line#*=} |
31 |
| - CONTAINER_INDEX=1 |
32 |
| - |
33 |
| - if [[ "$(is_app_proctype_checks_disabled "$APP" "$PROC_TYPE")" == "true" ]]; then |
34 |
| - dokku_log_info1 "zero downtime is disabled for app ($APP.$PROC_TYPE). stopping currently running containers" |
35 |
| - local cid proctype_oldids="$(get_app_container_ids "$APP" "$PROC_TYPE")" |
36 |
| - for cid in $proctype_oldids; do |
37 |
| - dokku_log_info2 "stopping $APP.$PROC_TYPE ($cid)" |
38 |
| - # shellcheck disable=SC2086 |
39 |
| - docker stop $DOCKER_STOP_TIME_ARG "$cid" &> /dev/null |
40 |
| - done |
41 |
| - fi |
42 |
| - |
43 |
| - while [[ $CONTAINER_INDEX -le $PROC_COUNT ]]; do |
44 |
| - local id=""; local port=""; local ipaddr="" |
45 |
| - local DOKKU_CONTAINER_ID_FILE="$DOKKU_ROOT/$APP/CONTAINER.$PROC_TYPE.$CONTAINER_INDEX" |
46 |
| - local DOKKU_IP_FILE="$DOKKU_ROOT/$APP/IP.$PROC_TYPE.$CONTAINER_INDEX" |
47 |
| - local DOKKU_PORT_FILE="$DOKKU_ROOT/$APP/PORT.$PROC_TYPE.$CONTAINER_INDEX" |
48 |
| - |
49 |
| - # start the app |
50 |
| - local DOCKER_ARGS="$DOKKU_DEFAULT_DOCKER_ARGS" |
51 |
| - local DOCKER_ARGS+=" -e DYNO='$PROC_TYPE.$CONTAINER_INDEX' " |
52 |
| - [[ "$DOKKU_TRACE" ]] && local DOCKER_ARGS+=" -e TRACE=true " |
53 |
| - |
54 |
| - [[ -n "$DOKKU_HEROKUISH" ]] && local START_CMD="/start $PROC_TYPE" |
55 |
| - |
56 |
| - if [[ -z "$DOKKU_HEROKUISH" ]]; then |
57 |
| - local DOKKU_DOCKERFILE_PORTS=($(config_get "$APP" DOKKU_DOCKERFILE_PORTS || true)) |
58 |
| - local DOKKU_DOCKERFILE_START_CMD=$(config_get "$APP" DOKKU_DOCKERFILE_START_CMD || true) |
59 |
| - local DOKKU_PROCFILE_START_CMD=$(get_cmd_from_procfile "$APP" "$PROC_TYPE") |
60 |
| - local START_CMD=${DOKKU_DOCKERFILE_START_CMD:-$DOKKU_PROCFILE_START_CMD} |
61 |
| - fi |
62 |
| - |
63 |
| - if [[ "$PROC_TYPE" == "web" ]]; then |
64 |
| - if [[ -z "${DOKKU_DOCKERFILE_PORTS[*]}" ]]; then |
65 |
| - local port=5000 |
66 |
| - local DOKKU_DOCKER_PORT_ARGS+="-p $port" |
67 |
| - else |
68 |
| - local p |
69 |
| - for p in ${DOKKU_DOCKERFILE_PORTS[*]};do |
70 |
| - if [[ ! "$p" =~ .*udp.* ]]; then |
71 |
| - # set port to first non-udp port |
72 |
| - local p=${p//\/tcp} |
73 |
| - local port=${port:="$p"} |
74 |
| - fi |
75 |
| - local DOKKU_DOCKER_PORT_ARGS+=" -p $p " |
76 |
| - done |
77 |
| - fi |
78 |
| - if [[ "$DOKKU_IS_APP_PROXY_ENABLED" == "true" ]]; then |
79 |
| - # shellcheck disable=SC2086 |
80 |
| - local id=$(docker run $DOKKU_GLOBAL_RUN_ARGS -d -e PORT=$port $DOCKER_ARGS $IMAGE $START_CMD) |
81 |
| - local ipaddr=$(docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' "$id") |
82 |
| - # Docker < 1.9 compatibility |
83 |
| - if [[ -z $ipaddr ]]; then |
84 |
| - local ipaddr=$(docker inspect --format '{{ .NetworkSettings.IPAddress }}' "$id") |
85 |
| - fi |
86 |
| - else |
87 |
| - # shellcheck disable=SC2086 |
88 |
| - local id=$(docker run $DOKKU_GLOBAL_RUN_ARGS -d $DOKKU_DOCKER_PORT_ARGS -e PORT=$port $DOCKER_ARGS $IMAGE $START_CMD) |
89 |
| - local port=$(docker port "$id" "$port" | sed 's/[0-9.]*://') |
90 |
| - local ipaddr=127.0.0.1 |
91 |
| - fi |
92 |
| - else |
93 |
| - # shellcheck disable=SC2086 |
94 |
| - local id=$(docker run $DOKKU_GLOBAL_RUN_ARGS -d $DOCKER_ARGS $IMAGE $START_CMD) |
95 |
| - fi |
96 |
| - |
97 |
| - kill_new() { |
98 |
| - declare desc="wrapper function to kill newly started app container" |
99 |
| - local id="$1" |
100 |
| - docker inspect "$id" &> /dev/null && docker stop "$id" > /dev/null && docker kill "$id" &> /dev/null |
101 |
| - trap - INT TERM EXIT |
102 |
| - kill -9 $$ |
103 |
| - } |
104 |
| - |
105 |
| - # run checks first, then post-deploy hooks, which switches proxy traffic |
106 |
| - trap 'kill_new $id' INT TERM EXIT |
107 |
| - if [[ "$(is_app_proctype_checks_disabled "$APP" "$PROC_TYPE")" == "false" ]]; then |
108 |
| - dokku_log_info1 "Attempting pre-flight checks" |
109 |
| - plugn trigger check-deploy "$APP" "$id" "$PROC_TYPE" "$port" "$ipaddr" |
110 |
| - fi |
111 |
| - trap - INT TERM EXIT |
112 |
| - |
113 |
| - # now using the new container |
114 |
| - [[ -n "$id" ]] && echo "$id" > "$DOKKU_CONTAINER_ID_FILE" |
115 |
| - [[ -n "$ipaddr" ]] && echo "$ipaddr" > "$DOKKU_IP_FILE" |
116 |
| - [[ -n "$port" ]] && echo "$port" > "$DOKKU_PORT_FILE" |
117 |
| - |
118 |
| - # cleanup pre-migration files |
119 |
| - rm -f "$DOKKU_ROOT/$APP/CONTAINER" "$DOKKU_ROOT/$APP/IP" "$DOKKU_ROOT/$APP/PORT" |
120 |
| - |
121 |
| - local CONTAINER_INDEX=$(( CONTAINER_INDEX + 1 )) |
122 |
| - done |
123 |
| - # cleanup when we scale down |
124 |
| - if [[ "$PROC_COUNT" == 0 ]]; then |
125 |
| - local CONTAINER_IDX_OFFSET=0 |
126 |
| - else |
127 |
| - local CONTAINER_IDX_OFFSET=$((PROC_COUNT + 1)) |
128 |
| - fi |
129 |
| - local container_state_filetype |
130 |
| - for container_state_filetype in CONTAINER IP PORT; do |
131 |
| - cd "$DOKKU_ROOT/$APP" |
132 |
| - find . -maxdepth 1 -name "$container_state_filetype.$PROC_TYPE.*" -printf "%f\n" | sort -t . -k 3 -n | tail -n +$CONTAINER_IDX_OFFSET | xargs rm -f |
133 |
| - done |
134 |
| - done < "$DOKKU_SCALE_FILE" |
135 |
| - |
136 |
| - dokku_log_info1 "Running post-deploy" |
137 |
| - plugn trigger post-deploy "$APP" "$port" "$ipaddr" "$IMAGE_TAG" |
138 |
| - |
139 |
| - # kill the old container |
140 |
| - if [[ -n "$oldids" ]]; then |
141 |
| - |
142 |
| - if [[ -z "$DOKKU_WAIT_TO_RETIRE" ]]; then |
143 |
| - local DOKKU_APP_DOKKU_WAIT_TO_RETIRE=$(config_get "$APP" DOKKU_WAIT_TO_RETIRE || true) |
144 |
| - local DOKKU_GLOBAL_DOKKU_WAIT_TO_RETIRE=$(config_get --global DOKKU_WAIT_TO_RETIRE || true) |
145 |
| - local DOKKU_WAIT_TO_RETIRE=${DOKKU_APP_DOKKU_WAIT_TO_RETIRE:="$DOKKU_GLOBAL_DOKKU_WAIT_TO_RETIRE"} |
146 |
| - fi |
147 |
| - |
148 |
| - # Let the old container finish processing requests, before terminating it |
149 |
| - local WAIT="${DOKKU_WAIT_TO_RETIRE:-60}" |
150 |
| - dokku_log_info1 "Shutting down old containers in $WAIT seconds" |
151 |
| - local oldid |
152 |
| - for oldid in $oldids; do |
153 |
| - dokku_log_info2 "$oldid" |
154 |
| - done |
155 |
| - ( |
156 |
| - exec >/dev/null 2>/dev/null </dev/null |
157 |
| - trap '' INT HUP |
158 |
| - sleep "$WAIT" |
159 |
| - for oldid in $oldids; do |
160 |
| - # Attempt to stop, if that fails, then force a kill as docker seems |
161 |
| - # to not send SIGKILL as the docs would indicate. If that fails, move |
162 |
| - # on to the next. |
163 |
| - # shellcheck disable=SC2086 |
164 |
| - docker stop $DOCKER_STOP_TIME_ARG "$oldid" \ |
165 |
| - || docker kill "$oldid" \ |
166 |
| - || plugn trigger retire-container-failed "$APP" # plugin trigger for event logging |
167 |
| - done |
168 |
| - ) & disown -a |
169 |
| - # Use trap since disown/nohup don't seem to keep child alive |
170 |
| - # Give child process just enough time to set the traps |
171 |
| - sleep 0.1 |
172 |
| - fi |
173 |
| -} |
174 | 4 |
|
| 5 | +shift 1 |
175 | 6 | dokku_deploy_cmd "$@"
|
0 commit comments