-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathfunctions
executable file
·131 lines (112 loc) · 4.45 KB
/
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
#!/usr/bin/env bash
set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x
DOKKU_REQUIRE_PREFIX="${DOKKU_REQUIRE_PREFIX:-dokku}"
source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions"
require_get_python_executable() {
declare -a executables=("python3" "python2" "python")
local executable
for e in "${executables[@]}"
do
if ( command -v "$e" >/dev/null 2>&1 ); then
echo "$e"
break
fi
done
}
require_get_python_version() {
declare desc="determine the install Python version"
eval "$(require_get_python_executable) -c 'from __future__ import print_function; import sys; print(sys.version_info.major)'"
}
require_get_app_plugins(){
local JSON_NODE=$1
local python_version=$(require_get_python_version)
if [ "$python_version" -ge 3 ]; then
cat | python3 -c 'import json,sys;obj=json.load(sys.stdin);print(json.dumps(obj'"${JSON_NODE}"').strip("\""))';
else
cat | python -c 'import json,sys;obj=json.load(sys.stdin);print json.dumps(obj'"${JSON_NODE}"').strip("\"")';
fi
}
require_get_json_len(){
local JSON_NODE=$1
local python=$(require_get_python_executable)
local python_version=$(require_get_python_version)
if [ "$python_version" -ge 3 ]; then
cat | python3 -c 'import json,sys;obj=json.load(sys.stdin);print(len(obj'"${JSON_NODE}"'))';
else
cat | python -c 'import json,sys;obj=json.load(sys.stdin);print len(obj'"${JSON_NODE}"')';
fi
}
require_install_link() {
declare desc="installs and links plugin passed in"
declare APP="$1" PLUGIN="$2"
if [[ -d "$PLUGIN_AVAILABLE_PATH/$PLUGIN/" ]]; then
if ! dokku "$PLUGIN:exists" "$APP" > /dev/null 2>&1; then
dokku_log_info1 "Setting up $PLUGIN"
dokku "$PLUGIN:create" "$APP"
fi
if ! dokku "$PLUGIN:linked" "$APP" "$APP" > /dev/null 2>&1; then
DOKKU_CONFIG_RESTART=false dokku "$PLUGIN:link" "$APP" "$APP"
fi
else
dokku_log_info1 "$PLUGIN plugin is not available on your system - attempting to contine without $PLUGIN"
fi
}
require_install_commands() {
declare desc="runs commands passed in from plugin list"
declare APP="$1" INDEX="$2"
local COMMAND COUNT PLUGIN TOTAL
COUNT=0
PLUGIN=$(require_get_app_plugins "[\"$DOKKU_REQUIRE_PREFIX\"][\"plugins\"][${INDEX}][\"name\"]" < "$APP_JSON_FILE" 2>&1)
if [[ -d "$PLUGIN_AVAILABLE_PATH/$PLUGIN/" ]]; then
TOTAL=$(require_get_json_len "[\"$DOKKU_REQUIRE_PREFIX\"][\"plugins\"][${INDEX}][\"commands\"]" < "$APP_JSON_FILE" 2>&1)
while [[ "$COUNT" -lt "$TOTAL" ]]; do
COMMAND=$(require_get_app_plugins "[\"$DOKKU_REQUIRE_PREFIX\"][\"plugins\"][${INDEX}][\"commands\"][${COUNT}]" < "$APP_JSON_FILE" 2>&1)
if [[ $? -ne 0 ]]; then
break
fi
COMMAND="${COMMAND//\$APP/$APP}"
# shellcheck disable=SC2086
DOKKU_CONFIG_RESTART=false dokku $COMMAND || break
let COUNT=COUNT+1
done
else
dokku_log_info1 "$PLUGIN plugin is not available on your system - attempting to contine without $PLUGIN"
fi
}
require_add_volumes() {
declare desc="adds volumes from app.json file"
declare APP="$1"
local APPPATH COUNT HOSTPATH TOTAL
COUNT=0
if TOTAL=$(require_get_json_len "[\"$DOKKU_REQUIRE_PREFIX\"][\"volumes\"]" < "$APP_JSON_FILE" 2>&1); then
dokku_log_info1 "Setting up $TOTAL volumes from app.json"
while [[ "$COUNT" -lt "$TOTAL" ]]; do
HOSTPATH=$(require_get_app_plugins "[\"$DOKKU_REQUIRE_PREFIX\"][\"volumes\"][${COUNT}][\"host\"]" < "$APP_JSON_FILE" 2>&1)
APPPATH=$(require_get_app_plugins "[\"$DOKKU_REQUIRE_PREFIX\"][\"volumes\"][${COUNT}][\"app\"]" < "$APP_JSON_FILE" 2>&1)
# "phases" key is optional
set +eo pipefail
PHASES=$(require_get_app_plugins "[\"$DOKKU_REQUIRE_PREFIX\"][\"volumes\"][${COUNT}][\"phases\"]" < "$APP_JSON_FILE" 2>&1)
set -eo pipefail
if [[ -z "$PHASES" ]]; then
PHASES="build,deploy,run"
fi
if [[ $? -ne 0 ]] || [[ -z "$HOSTPATH" ]] || [[ -z "$APPPATH" ]]; then
break
fi
# swap $APP for app name in commands
HOSTPATH="${HOSTPATH//\$APP/$APP}"
APPPATH="${APPPATH//\$APP/$APP}"
mkdir -p "$HOSTPATH"
dokku docker-options:add "$APP" "$PHASES" -v "$HOSTPATH:$APPPATH"
let COUNT=COUNT+1
done
fi
}
require_mode_cmd() {
declare desc="sets DOKKU_REQUIRE_PREFIX"
declare MODE="$2"
local cmd="mode"
[[ -d $DOKKU_ROOT/.dokkurc ]] || mkdir -p "$DOKKU_ROOT/.dokkurc"
echo "Setting DOKKU_REQUIRE_PREFIX to $MODE"
echo "export DOKKU_REQUIRE_PREFIX=$MODE" > "$DOKKU_ROOT/.dokkurc/DOKKU_REQUIRE_PREFIX"
}