-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathblackbox
More file actions
296 lines (245 loc) · 15.2 KB
/
blackbox
File metadata and controls
296 lines (245 loc) · 15.2 KB
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#!/usr/bin/env bash
# @file blackbox
# @brief https://github.com/ProblemSetters/devops-blackbox/blob/2404/blackbox
# @description Blackbox evaluation framework
# @description Main executable
#
# @arg $1 type=enum<ansible|aws|chef|docker|git|kubernetes|linux|puppet|shell|terraform> Module name
# @arg $2 type=enum<setup|check> Action name
# @arg $3 type=string Question ID
#
# @set BLACKBOX_FLAG__DEBUG_MODE type=enum<yes|no> Debug flag
# @set BLACKBOX_FLAG__STEP_PROVISION type=enum<yes|no> Provision step flag, indicates whether the "provision" step is included or excluded (for development process only)
# @set BLACKBOX_FLAG__STEP_BUILD type=enum<yes|no> Build step flag, indicates whether the "build" step is included or excluded (for development process only)
# @set BLACKBOX_FLAG__STEP_EVALUATE type=enum<yes|no> Evaluate step flag, indicates whether the "evaluate" step is included or excluded (for development process only)
# @set BLACKBOX_PROVISION_WITH_OPTS type=function,default=auto Provision hook definition, a function that implements an additional set of instructions that must be executed during the "provision" step
# @set BLACKBOX_BUILD_WITH_OPTS type=function,default=auto Build hook definition, a function that implements an additional set of instructions that must be executed during the "build" step
#
# @set BLACKBOX_DIR readonly,type=string,default=auto Path to the Blackbox installation directory (sets automatically, do not override)
# @set BLACKBOX_SPAWN readonly,type=string,default=auto Name of the docker container in which the question is being evaluated (sets automatically, do not override)
# @set BLACKBOX_USER readonly,type=uid,default=auto ID of the user in the docker container in which the question is being evaluated (sets automatically, do not override)
# @set BLACKBOX_USER_NAME readonly,type=string,default=auto Name of the user in the docker container in which the question is being evaluated (sets automatically, do not override)
# @set BLACKBOX_USER_HOME readonly,type=path,default=auto Path to the home directory of the user in the docker container in which the question is being evaluated (sets automatically, do not override)
# @set BLACKBOX_USER_QUESTION_DIR readonly,type=path,default=auto Path to the question directory in the docker container in which the question is being evaluated (sets automatically, do not override)
# @set BLACKBOX_VERSION readonly,type=string,default=auto [DEPRECATED] Version of the Blackbox to use (sets automatically, do not override)
# @set BLACKBOX_HEAP_DIR readonly,type=path,default=auto Path to the question "heap" directory, a place where question assets might be generated (sets automatically, do not override)
# @set BLACKBOX_STORAGE_DIR readonly,type=path,default=auto Path to the question "artifacts" directory, a place where question artifacts will be generated (sets automatically, do not override)
# @set BLACKBOX_BUILD_LOG readonly,type=path,default=auto Path to the build log (sets automatically, do not override)
# @set BLACKBOX_BUILD_STREAM_DISPLAY_LINES readonly,type=integer,default=auto Number of output lines before trim and ellipsis
#
# @example
# # Setup the "git" module question with the id "1234567-git-question-id"
# /path/to/blackbox git setup "1234567-git-question-id"
# # Evaluate the "git" module question with the id "1234567-git-question-id"
# /path/to/blackbox git check "1234567-git-question-id"
#
# @see blackbox.__init
blackbox() {
export BLACKBOX_DIR=/blackbox
# shellcheck disable=SC2155
# export BLACKBOX_SPAWN=$(awk -v master="$(awk -v master="$(</proc/sys/kernel/random/uuid)" -v user="blackbox-spawn" 'END { print ($0 == "blackbox") ? user : master }' <(findmnt -n --mountpoint="/blackbox" --output="SOURCE"))" -v user="" 'END { print ($0 == "systemd") ? master : user }' <(ps -p 1 -o comm=))
export BLACKBOX_SPAWN=$(awk -v master="$(</proc/sys/kernel/random/uuid)" -v user="blackbox-spawn" 'END { print ($0 == "blackbox") ? user : master }' <(findmnt -n --mountpoint="/blackbox" --output="SOURCE"))
# shellcheck disable=SC2155
export BLACKBOX_USER="1000:1000"
# shellcheck disable=SC2155
export BLACKBOX_USER_NAME="ubuntu"
# shellcheck disable=SC2155
export BLACKBOX_USER_HOME="/home/${BLACKBOX_USER_NAME}"
# shellcheck disable=SC2155
export BLACKBOX_USER_QUESTION_DIR=$(awk -v master="${BLACKBOX_USER_HOME}/${3}" -v user="" 'END { print ($0 == "systemd") ? master : user }' <(ps -p 1 -o comm=))
# shellcheck disable=SC2155
export BLACKBOX_VERSION="2404"
export BLACKBOX_FLAG__DEBUG_MODE=${BLACKBOX_FLAG__DEBUG_MODE:-no}
# shellcheck disable=SC2155
export BLACKBOX_FLAG__STEP_PROVISION=$(awk -v master="yes" -v user="${BLACKBOX_FLAG__STEP_PROVISION:-yes}" 'END { print ($0 == "blackbox") ? user : master }' <(findmnt -n --mountpoint="/blackbox" --output="SOURCE"))
# shellcheck disable=SC2155
export BLACKBOX_FLAG__STEP_BUILD=$(awk -v master="yes" -v user="${BLACKBOX_FLAG__STEP_BUILD:-yes}" 'END { print ($0 == "blackbox") ? user : master }' <(findmnt -n --mountpoint="/blackbox" --output="SOURCE"))
# shellcheck disable=SC2155
export BLACKBOX_FLAG__STEP_EVALUATE=$(awk -v master="yes" -v user="${BLACKBOX_FLAG__STEP_EVALUATE:-yes}" 'END { print ($0 == "blackbox") ? user : master }' <(findmnt -n --mountpoint="/blackbox" --output="SOURCE"))
# shellcheck disable=SC2155
export BLACKBOX_PROVISION_WITH_OPTS=$(sed "s/\({\);/\1/g; s/\(;\)\{2\}/\1/g" <(awk 'ORS="; "' <(sed '1,2d; $d; s/[[:space:]]$//g; s/^[[:space:]]\+//g' <(declare -f BLACKBOX_PROVISION_WITH_OPTS))))
# shellcheck disable=SC2155
export BLACKBOX_BUILD_WITH_OPTS=$(sed "s/\({\);/\1/g; s/\(;\)\{2\}/\1/g" <(awk 'ORS="; "' <(sed '1,2d; $d; s/[[:space:]]$//g; s/^[[:space:]]\+//g' <(declare -f BLACKBOX_BUILD_WITH_OPTS))))
export BLACKBOX_HEAP_DIR="/tmp/${3}"
export BLACKBOX_STORAGE_DIR="/var/save/${3}"
export BLACKBOX_BUILD_LOG="${BLACKBOX_STORAGE_DIR}/build.log"
export BLACKBOX_BUILD_STREAM_DISPLAY_LINES=255
# shellcheck disable=SC2034
typeset -gx BLACKBOX_SCENARIO_PATH=$(realpath "$0")
# shellcheck disable=SC2034
typeset -gx BLACKBOX_MODULE_NAME=$1
# shellcheck disable=SC2034
typeset -gx BLACKBOX_MODULE_ACTION_NAME=$2
# shellcheck disable=SC2034
typeset -gx BLACKBOX_QUESTION_ID=$3
# shellcheck disable=SC2034
typeset -gx SECONDS=0
# @section blackbox.*
blackbox.framework() {
# @section blackbox.framework.*
# Includes a path as namespace, handles if file has "OOP encapsulation" instruction
#
# @arg $1 type=string Namespace to load
# @arg $2 type=...mixed Namespace args
#
# @example
# # Include a path as namespace, handles if file has "OOP encapsulation" instruction
# blackbox.framework.import blackbox.framework.exception
# # Include a path as namespace, handles if file has "OOP encapsulation" instruction with args
# blackbox.framework.import "blackbox.module.git.setup.provision:step" "arg1" "arg2"
function blackbox.framework.import() {
typeset namespace=$1
typeset -a args=("${@:2}")
local destination_namespace_name=${namespace%:*}
# shellcheck disable=SC2155
local destination_namespace_extension=$(sed -n "s/.*://p" <<<"$namespace")
if [ -z "$destination_namespace_extension" ]; then
destination_namespace_extension=$(rev <(cut -d "." -f 2 <(rev <<<"$namespace")))
fi
# shellcheck disable=SC2155
local destination_definition=$(declare -f "$destination_namespace_name")
if [ -n "$destination_definition" ]; then
return
fi
# shellcheck disable=SC2155
local destination_definition_path=$(printf "%s/%s.%s" "$BLACKBOX_DIR" "$(sed "s/\./\//g" <(cut -d "." -f 2- <<<"$destination_namespace_name"))" "$destination_namespace_extension")
if [ ! -e "$destination_definition_path" ]; then
blackbox.framework.trace "${FUNCNAME[0]}" "$*" <<<""
if ( ! declare -f blackbox.framework.exception.raise &>/dev/null ); then
printf "error: *** definition '%s' does not exist\n" "$destination_definition_path"
kill $$
fi
blackbox.framework.exception.raise "definition '%s' does not exist" "$destination_definition_path"
fi
# shellcheck disable=SC1090
destination_definition=$(source "$destination_definition_path" && declare -f "$destination_namespace_name")
# shellcheck disable=SC2155
local destination_definition_source_placeholder=$(sed -n '/^[[:space:]]*:[[:space:]]<<<[[:space:]]blackbox\.[[:lower:][:digit:].:]\+;\{0,1\}$/p' <<<"$destination_definition")
if [ -z "$destination_definition_source_placeholder" ]; then
blackbox.framework.trace "${FUNCNAME[0]}" "$*" <<<""
# shellcheck disable=SC1090
source "$(printf "%s/%s.%s" "$BLACKBOX_DIR" "$(sed "s/\./\//g" <(cut -d "." -f 2- <<<"$destination_namespace_name"))" "$destination_namespace_extension")" && {
eval "$destination_namespace_name" "${args[@]}"
}
return
fi
# shellcheck disable=SC2155
local source_namespace=$(sed -n 's/^[[:space:]]*:[[:space:]]<<<[[:space:]]\(blackbox\.[[:lower:][:digit:].:]\+\);\{0,1\}$/\1/p' <<<"$destination_definition")
local source_namespace_name=${source_namespace%:*}
# shellcheck disable=SC2155
local source_namespace_extension=$(sed -n "s/.*://p" <<<"$source_namespace")
if [ -z "$source_namespace_extension" ]; then
source_namespace_extension=$(rev <(cut -d "." -f 2 <(rev <<<"$source_namespace")))
fi
# shellcheck disable=SC2155
local source_definition_path=$(printf "%s/%s.%s" "$BLACKBOX_DIR" "$(sed "s/\./\//g" <(cut -d "." -f 2- <<<"$source_namespace_name"))" "$source_namespace_extension")
if [ ! -e "$source_definition_path" ]; then
blackbox.framework.trace "${FUNCNAME[0]}" "$*" <<<""
if ( ! declare -f blackbox.framework.exception.raise &>/dev/null ); then
printf "error: *** definition '%s' does not exist\n" "$source_definition_path"
kill $$
fi
blackbox.framework.exception.raise "definition '%s' does not exist" "$source_definition_path"
fi
# shellcheck disable=SC1090
source_definition=$(source "$source_definition_path" && declare -f "$source_namespace_name")
# shellcheck disable=SC2155
local source_definition_destination_placeholder=$(sed -n "/[[:space:]]*:[[:space:]]<<<[[:space:]]blackbox\.[[:lower:][:digit:].*:]\+;\{0,1\}/p" <<<"$source_definition")
# shellcheck disable=SC2155
local source_definition_destination_namespace=$(sed -n "s/[[:space:]]*:[[:space:]]<<<[[:space:]]\(blackbox\.[[:lower:][:digit:].*:]\+\);\{0,1\}/\1/p" <<<"$source_definition_destination_placeholder")
if ( ! grep -q "$(printf '^%s$' "$source_definition_destination_namespace")" <<<"$destination_namespace_name" ); then
blackbox.framework.trace "${FUNCNAME[0]}" "$*" "$(printf '<%s> %s' "$source_definition_destination_namespace" "$destination_namespace_name")" <<<""
if ( ! declare -f blackbox.framework.exception.raise &>/dev/null ); then
printf "error: *** definition '%s' cannot be extended with '%s'\n" "$destination_namespace_name" "$source_definition_destination_namespace"
kill $$
fi
blackbox.framework.exception.raise "definition '%s' cannot be extended with '%s'" "$destination_namespace_name" "$source_definition_destination_namespace"
fi
source_definition=$(sed '1,2d; $d' <<<"$source_definition")
# shellcheck disable=SC2016
destination_definition=$(sed "$(printf '1,2d; $d; /%s/d; s/&/\\\&/g' "$destination_definition_source_placeholder")" <<<"$destination_definition")
blackbox.framework.trace "${FUNCNAME[0]}" "$*" "$(printf '&%s %s' "$source_namespace_name" "$destination_namespace_name")" <<<""
eval "$(printf '%s() { %s }; %s' "$destination_namespace_name" "${source_definition/${source_definition_destination_placeholder}/${destination_definition}}" "$destination_namespace_name")" "${args[@]}"
}
# Outputs the formatted trace
#
# @stdin Trace header
#
# @arg $1 type=string Entrypoint
# @arg $2 type=...mixed Trace args
# @arg $3 type=...mixed Trace opts
#
# @stdout Formatted trace with optional header
#
# @example
# # Output the formatted trace with header
# blackbox.framework.trace "${FUNCNAME[@]}" "$*" <<<"initialize"
# # Output the formatted trace without header
# blackbox.framework.trace "${FUNCNAME[@]}" "$*" <<<""
function blackbox.framework.trace() {
# shellcheck disable=SC2155
declare header=$(</dev/stdin)
typeset entrypoint=$1
typeset args=("$2")
typeset opts=("$3")
if [ -n "${args[*]}" ]; then
args=("$(sed ':a;N;$!ba;s/\n/\t/g; s/[[:space:]]\+/ /g' <<<"${args[*]/#/ < }")")
fi
if [ -n "${opts[*]}" ]; then
opts=("${opts[*]/#/ | }")
fi
(
if [ -n "$header" ]; then
printf "\n"
printf "\033[0;37m// %s@%s: %s%s%s\033[0m\n" "$(date -u --iso-8601=seconds)" "$(date --date="@${SECONDS}" "+%M:%S")" "${entrypoint[0]}" "${args[*]}" "${opts[*]}"
printf "\033[0;34m[%s]\033[0m\n" "${header^^}"
printf "\033[0;37m%s\033[0m\n" "$(printenv | sort | awk 'ORS=" "')"
printf "\033[0;37m--\033[0m\n"
else
printf "\033[0;37m// %s@%s: %s%s%s {...}\033[0m\n" "$(date -u --iso-8601=seconds)" "$(date --date="@${SECONDS}" "+%M:%S")" "${entrypoint[0]}" "${args[*]}" "${opts[*]}"
fi
) | if ( tput setaf &>/dev/null ); then cat; else sed "s/\x1b\[[0-9;]*m//g"; fi
}
}; blackbox.framework
# @section blackbox.*
# Constructor
#
# If the Blackbox installation does not exist, this function downloads the framework and installs it
#
# @arg $1 type=enum<ansible|aws|chef|docker|git|kubernetes|linux|puppet|shell|terraform> Module name
# @arg $2 type=...mixed Module args
#
# @see blackbox
function blackbox.__init() {
typeset module_name=$1
typeset -a module_args=("${@:2}")
{
shopt -os allexport
. /etc/environment
shopt -ou allexport
}
{
blackbox.framework.trace "${FUNCNAME[@]}" "$*" <<<"initialize"
}
if [ ! -e "$BLACKBOX_DIR" ]; then
wget -S --output-document="/tmp/blackbox.zip" "https://codeload.github.com/ProblemSetters/devops-blackbox/zip/refs/heads/${BLACKBOX_VERSION}" && {
unzip /tmp/blackbox.zip -x "devops-blackbox-2404/.github/*" "devops-blackbox-2404/docs/*" "devops-blackbox-2404/tests/*" "devops-blackbox-2404/.gitignore" "devops-blackbox-2404/Makefile.*" "devops-blackbox-2404/README.md" "devops-blackbox-2404/Vagrantfile" -d /tmp && {
mv "/tmp/devops-blackbox-${BLACKBOX_VERSION}" "$BLACKBOX_DIR"
rm -rf /tmp/*blackbox*
}
} || printf "error: *** blackbox '%s' is not available\n" "$BLACKBOX_VERSION"
fi
blackbox.framework.import blackbox.framework.exception
blackbox.framework.import blackbox.framework.flag
blackbox.framework.import blackbox.framework.inventory
blackbox.framework.import blackbox.framework.module
if [ -z "$module_name" ]; then
return;
fi
blackbox.framework.inventory.install.directory "$BLACKBOX_USER_QUESTION_DIR" && {
pushd "$BLACKBOX_USER_QUESTION_DIR" && {
blackbox.framework.module.load "$module_name" "${module_args[@]}"
}
}
} && blackbox.__init "$@"
}; blackbox "$@"