-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
scheduler-deploy
executable file
·82 lines (65 loc) · 3.35 KB
/
scheduler-deploy
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
#!/usr/bin/env bash
set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x
source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions"
source "$PLUGIN_CORE_AVAILABLE_PATH/config/functions"
source "$PLUGIN_CORE_AVAILABLE_PATH/common/property-functions"
scheduler-nomad-scheduler-deploy() {
declare desc="deploys an image tag for a given application"
declare trigger="scheduler-nomad scheduler-deploy"
declare DOKKU_SCHEDULER="$1" APP="$2" IMAGE_TAG="$3"
if [[ "$DOKKU_SCHEDULER" != "nomad" ]]; then
return
fi
local line PROC_TYPE PROC_COUNT CONTAINER_INDEX
local DOKKU_SCALE_FILE="$DOKKU_ROOT/$APP/DOKKU_SCALE"
local DEPLOYMENT_ID="$(date +%s)"
local TMP_JOB_FILE
TMP_JOB_FILE=$(mktemp "/tmp/${FUNCNAME[0]}.XXXX")
trap 'rm -rf "$TMP_JOB_FILE" > /dev/null' RETURN INT TERM EXIT
dokku_log_info2 "Deploying via nomad"
IMAGE=$(get_deploying_app_image_name "$APP" "$IMAGE_TAG")
plugn trigger pre-deploy "$APP" "$IMAGE_TAG"
ENV_VAR_NAME="$(fn-plugin-property-get "git" "$APP" "rev-env-var")"
if [[ -z "$ENV_VAR_NAME" ]] && ! fn-plugin-property-exists "git" "$APP" "rev-env-var"; then
ENV_VAR_NAME="GIT_REV"
fi
GIT_REV=$(config_get "$APP" "$ENV_VAR_NAME")
_NOMAD_ADDR=$(fn-plugin-property-get "scheduler-nomad" "$APP" "nomad-addr" "")
[[ -n "$_NOMAD_ADDR" ]] && export NOMAD_ADDR="$_NOMAD_ADDR"
while read -r line || [[ -n "$line" ]]; do
[[ "$line" =~ ^#.* ]] && continue
line="$(strip_inline_comments "$line")"
PROC_TYPE=${line%%=*}
PROC_COUNT=${line#*=}
dokku_log_info1 "Deploying ${PROC_TYPE} to ${PROC_COUNT}"
# todo: pull out all of these configuration variables
DOKKU_CHECK_INTERVAL=10
DOKKU_CHECK_PATH=/
DOKKU_CHECKS_TIMEOUT=30
DOKKU_DOCKER_STOP_TIMEOUT=10
DOKKU_WAIT_TO_RETIRE=10
RESOURCE_CPU=$(plugn trigger resource-get-property "$APP" "$PROC_TYPE" "reserve" "cpu" || echo 100)
RESOURCE_MEMORY=$(plugn trigger resource-get-property "$APP" "$PROC_TYPE" "limit" "memory" || echo 256)
RESOURCE_NETWORK_MBITS=$(plugn trigger resource-get-property "$APP" "$PROC_TYPE" "limit" "network" || echo 10)
NOMAD_DATACENTER=$(fn-plugin-property-get "scheduler-nomad" "$APP" "nomad-datacenter" "dc1")
local template="$PLUGIN_AVAILABLE_PATH/scheduler-nomad/templates/web.hcl.sigil"
[[ "$PROC_TYPE" != web ]] && template="$PLUGIN_AVAILABLE_PATH/scheduler-nomad/templates/background.hcl.sigil"
# TODO: figure out a way to shim in environment variables
SIGIL_PARAMS=(-f "$template" APP="$APP"
CHECK_INTERVAL="$DOKKU_CHECK_INTERVAL" CHECK_TIMEOUT="$DOKKU_CHECKS_TIMEOUT" CHECK_PATH="$DOKKU_CHECK_PATH"
RESOURCE_CPU="$RESOURCE_CPU" RESOURCE_MEMORY="$RESOURCE_MEMORY"
RESOURCE_NETWORK_MBITS="$RESOURCE_NETWORK_MBITS"
DATACENTER="$NOMAD_DATACENTER" IMAGE="$IMAGE"
ENVIRONMENT_VARIABLES=""
GIT_REV="$GIT_REV" DEPLOYMENT_ID="$DEPLOYMENT_ID"
KILL_TIMEOUT="$DOKKU_DOCKER_STOP_TIMEOUT" SHUTDOWN_DELAY="$DOKKU_WAIT_TO_RETIRE"
PROCESS_COUNT="$PROC_COUNT" PROCESS_TYPE="$PROC_TYPE")
sigil "${SIGIL_PARAMS[@]}" | cat -s > $TMP_JOB_FILE
levant deploy -force-count $TMP_JOB_FILE | sed "s/^/ /"
done < "$DOKKU_SCALE_FILE"
dokku_log_info2 "Deploy complete"
dokku_log_info1 "Running post-deploy"
plugn trigger core-post-deploy "$APP" "" "" "$IMAGE_TAG"
plugn trigger post-deploy "$APP" "" "" "$IMAGE_TAG"
}
scheduler-nomad-scheduler-deploy "$@"