forked from dokku/dokku-scheduler-kubernetes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinternal-functions
executable file
·140 lines (123 loc) · 4.75 KB
/
internal-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
#!/usr/bin/env bash
set -eo pipefail
[[ $DOKKU_TRACE ]] && set -x
source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions"
source "$PLUGIN_CORE_AVAILABLE_PATH/common/property-functions"
cmd-scheduler-kubernetes-rolling-update() {
declare desc="force a rolling update"
local cmd="scheduler-kubernetes:rolling-update" argv=("$@")
[[ ${argv[0]} == "$cmd" ]] && shift 1
declare APP="$1"
local DOKKU_SCHEDULER=$(get_app_scheduler "$APP")
if [[ "$DOKKU_SCHEDULER" != "kubernetes" ]]; then
dokku_log_fail "Scheduler for $APP is set to $DOKKU_SCHEDULER"
return 1
fi
local DOKKU_SCALE_FILE="$DOKKU_ROOT/$APP/DOKKU_SCALE"
if [[ ! -f "$DOKKU_SCALE_FILE" ]]; then
dokku_log_fail "No processes found for $APP"
return 1
fi
export KUBECONFIG="${DOKKU_ROOT}/.kube/config"
export KUBEDOG_KUBE_CONFIG="${DOKKU_ROOT}/.kube/config"
KUBE_ARGS=()
NAMESPACE="$(fn-plugin-property-get "scheduler-kubernetes" "$APP" "namespace" "")"
if [[ -n "$NAMESPACE" ]]; then
KUBE_ARGS+=("--namespace=$NAMESPACE")
fn-scheduler-kubernetes-ensure-namespace "$NAMESPACE" >/dev/null
fi
dokku_log_info1 "Triggering rolling-update for $APP"
while read -r line || [[ -n "$line" ]]; do
[[ "$line" =~ ^#.* ]] && continue
line="$(strip_inline_comments "$line")"
PROC_TYPE=${line%%=*}
"${DOKKU_LIB_ROOT}/data/scheduler-kubernetes/kubectl" "${KUBE_ARGS[@]}" patch deployment "${APP}-${PROC_TYPE}" --patch "{\"spec\":{\"template\":{\"metadata\":{\"labels\":{\"dokku.com/rolling-update-time\":\"$(date -u "+%Y-%m-%d-%H-%M-%S")\"}}}}}" | sed "s/^/ /"
done <"$DOKKU_SCALE_FILE"
}
cmd-scheduler-kubernetes-report() {
declare desc="displays a scheduler-kubernetes report for one or more apps"
local cmd="scheduler-kubernetes:report"
local INSTALLED_APPS=$(dokku_apps)
local APP="$2" INFO_FLAG="$3"
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-scheduler-kubernetes-report-single "$app" "$INFO_FLAG" | tee || true
done
else
cmd-scheduler-kubernetes-report-single "$APP" "$INFO_FLAG"
fi
}
cmd-scheduler-kubernetes-report-single() {
declare APP="$1" INFO_FLAG="$2"
if [[ "$INFO_FLAG" == "true" ]]; then
INFO_FLAG=""
fi
verify_app_name "$APP"
local flag_map=(
"--scheduler-kubernetes-namespace: $(fn-plugin-property-get "scheduler-kubernetes" "$APP" "namespace" "")"
)
if [[ -z "$INFO_FLAG" ]]; then
dokku_log_info2_quiet "${APP} scheduler-kubernetes 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
}
scheduler_docker_local_help_content_func() {
declare desc="return scheduler-kubernetes plugin help content"
cat <<help_content
scheduler-kubernetes:report [<app>] [<flag>], Displays a scheduler-kubernetes report for one or more apps
scheduler-kubernetes:set <app> <property> (<value>), Set or clear a scheduler-kubernetes property for an app
help_content
}
cmd-scheduler-kubernetes-help() {
if [[ $1 == "scheduler-kubernetes:help" ]]; then
echo -e 'Usage: dokku scheduler-kubernetes[:COMMAND]'
echo ''
echo 'Manages the scheduler-kubernetes integration for an app.'
echo ''
echo 'Additional commands:'
scheduler_docker_local_help_content_func | sort | column -c2 -t -s,
echo ''
elif [[ $(ps -o command= $PPID) == *"--all"* ]]; then
scheduler_docker_local_help_content_func
else
cat <<help_desc
scheduler-kubernetes, Manages the scheduler-kubernetes integration for an app
help_desc
fi
}
fn-scheduler-kubernetes-ensure-namespace() {
declare NAMESPACE="$1"
local NAMESPACE_TEMPLATE="$PLUGIN_AVAILABLE_PATH/scheduler-kubernetes/templates/namespace.json.sigil"
local TMP_FILE=$(mktemp "/tmp/${FUNCNAME[0]}.XXXX")
trap 'rm -rf "$TMP_FILE" > /dev/null' RETURN INT TERM EXIT
SIGIL_PARAMS=(NAME="$NAMESPACE")
sigil -f "$NAMESPACE_TEMPLATE" "${SIGIL_PARAMS[@]}" | cat -s >$TMP_FILE
"${DOKKU_LIB_ROOT}/data/scheduler-kubernetes/kubectl" apply -f "$TMP_FILE" | sed "s/^/ /"
}