-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathvalidation.sh
More file actions
executable file
·493 lines (465 loc) · 16.4 KB
/
Copy pathvalidation.sh
File metadata and controls
executable file
·493 lines (465 loc) · 16.4 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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
#! /usr/bin/env bash
#
# Skip everything if this is set
#
if [ "$BASIC_SETUP_SKIP_ENVIRONMENT_VALIDATION" == "true" ]; then
exit 0
fi
#
# Error handling
#
SET_E_AFTER=true
if [[ $- =~ e ]]; then
SET_E_AFTER=false
else
set -e
fi
# set e to the right value after running the script
function update_e {
if [ "$SET_E_AFTER" == "true" ]; then
set +e
fi
}
#
# global defaults
#
BASIC_SETUP_DATA_DIRECTORY="$HOME/.basic-setup/"
CUSTOM_LABELS=false
ERROR_MESSAGES=0
FORCE=false
LABELS=("core")
PREVIOUSLY_VALIDATED_FILE_NAME=".environment_validated_by_environment-validation"
RUN_INSTALLS=false
SHOW_HELP=false
SUPPORTED_PACKAGE_MANAGERS=("apt-get" "brew" "curl" "pacman" "dnf" "winget")
TARGET_BRANCH="main"
VERBOSITY=${BASIC_SETUP_VERBOSITY:--1}
#
# load environment variables
#
. basic-setup-set-env
#
# Skip everything if this is set
#
if [ "$BASIC_SETUP_SKIP_ENVIRONMENT_VALIDATION" == "true" ]; then
exit 0
fi
#
# computed values (often can't be alphabetical)
#
if (( $VERBOSITY == -1 )); then
VERBOSITY=${BASIC_SETUP_VERBOSITY:-0}
fi
ALLOW_CURL_INSTALLS="${BASIC_SETUP_ENVIRONMENT_VALIDATION_ALLOW_CURL_INSTALLS:-false}"
DEFAULT_OVERRIDE_DIR="$(general-get-basic-setup-dir)/resources/install/index.d"
PACKAGES="$(cat "$(general-get-basic-setup-dir)/resources/install/index.json")"
PACKAGES_OVERRIDE_DIR="${BASIC_SETUP_ENVIRONMENT_VALIDATION_INDEX_OVERRIDE_DIRECTORY_PATH:-$DEFAULT_OVERRIDE_DIR}"
PACKAGES_OVERRIDE_DIR="$([ ! -z "$PACKAGES_OVERRIDE_DIR" ] && [ -d "$PACKAGES_OVERRIDE_DIR" ] && echo "$PACKAGES_OVERRIDE_DIR" || echo "")"
SKIP_LATEST_CHECK="${BASIC_SETUP_ENVIRONMENT_VALIDATION_SKIP_LATEST_CHECK:-false}"
SKIP_PORCELAIN="${BASIC_SETUP_ENVIRONMENT_VALIDATION_SKIP_PORCELAIN:-false}"
SKIP_EVERYTHING="${BASIC_SETUP_ENVIRONMENT_VALIDATION_SKIP_EVERYTHING:-false}"
TARGET_BRANCH="${BASIC_SETUP_ENVIRONMENT_VALIDATION_TARGET_BRANCH:-$TARGET_BRANCH}"
#
# helper functions
#
# script help message
function help {
command_for_help="$(basename "$0")"
cat <<- EOF
----------
usage: $command_for_help <arguments>
----------
description: This script will validate that everything that is needed is included in your environment.
----------
-c|--allow-curl - (flag, current: $ALLOW_CURL_INSTALLS) Allow curl installs and validations, this can also be set with 'export BASIC_SETUP_ENVIRONMENT_VALIDATION_ALLOW_CURL_INSTALLS=true'.
-f|--force - (flag, current: $FORCE) Force the validation (don't skip if previously passed).
-h|--help - (flag, current: $SHOW_HELP) Print this help message and exit.
-i|--install - (flag, current: $RUN_INSTALLS) Run installs and upgrade as needed instead of erroring.
-l|--label - (multi-optional, current: (${LABELS[@]}) The union of label(s) that should be used to filter the packages, any addition will replace the default.
-s|--skip-latest - (flag, current: $SKIP_LATEST_CHECK) Skip latest check, this can also be set with 'export BASIC_SETUP_ENVIRONMENT_VALIDATION_SKIP_LATEST_CHECK=true'.
-v|--verbose - (multi-flag, current: $VERBOSITY) Increase the verbosity by 1.
----------
note: This script will error out if the environment is misconfigured. It should also tell you what can be done to correct the issue.
note: This script will not install anything by default, you must pass the -i|--install flag to do so.
note: Set BASIC_SETUP_ENVIRONMENT_VALIDATION_SKIP_EVERYTHING to true to skip everything.
----------
examples:
validate environment - $command_for_help
install/update environment with curl enabled - $command_for_help -i -c
install/update environment with curl enabled with all packages - $command_for_help -i -c -l all
----------
EOF
}
# ensure a specific command is installed
function is_command_installed {
local command_name="$1"
local command_installed_command="$2"
if [ ! -z "$command_installed_command" ] && [ "$command_installed_command" != "null" ]; then
local output="$(echo $command_installed_command | bash)"
if $output; then
echo "true"
else
echo "false"
fi
else
general-command-installed -c "$1"
fi
}
# select the correct package manager and return it's content for a given package
function get_package_manager_content {
local package_manager_name=""
local package_content="$1"
for i in "${SUPPORTED_PACKAGE_MANAGERS[@]}"; do
# skip the package manager if it's not installed
if [ "$(is_command_installed "$i")" == false ]; then
continue
fi
# skip the package manager if it's not found or enabled
local package_manager_content="$(echo "$package_content" | jq '."package-managers"[] | select((."manager-name" == "'"$i"'") and .enabled == true)')"
if [ -z "$package_manager_content" ]; then
continue
fi
# skip the package manager if it's curl and curl isn't allowed
if [ "$ALLOW_CURL_INSTALLS" == false ] && [ "$i" == "curl" ]; then
continue
fi
# set the package manager if it's not been set, or it's been set to curl
if [ -z "$package_manager_name" ] || [ "$package_manager_name" == "curl" ]; then
package_manager_name="$i"
fi
done
if [ -z "$package_manager_name" ]; then
echo "no valid package manager found for $(echo "$package_content" | jq '.name')" 1>&2
# return empty if not found
echo ""
else
echo "$package_content" | jq '."package-managers"[] | select(."manager-name" == "'"$package_manager_name"'")'
fi
}
# ensure a modern jq version is being used
function check_for_jq {
local is_jq_installed=$(is_command_installed "jq")
if [ $is_jq_installed == false ]; then
echo "\`jq\` must be installed to get a list of to be installed packages. Please follow these instructions - https://stedolan.github.io/jq/download/"
help
update_e
exit 1
fi
}
# ensure a modern bash version is being used
function check_for_bash {
# First and foremost we must have modern bash and jq
if [[ "$BASH_VERSION" =~ ^3.*$ ]]; then
echo "Bash 3 installed... please install bash (brew/apt/etc install bash)" 1>&2
echo "if you have already done that ensure you aren't calling with an alias to MacOS bash (which defaults to 3, and is where this usually happens)" 1>&2
help
update_e
exit 1
fi
}
# exit early if this has been run recently
function check_for_skip {
# Include important flags in the file name
PREVIOUSLY_VALIDATED_FILE_NAME="${PREVIOUSLY_VALIDATED_FILE_NAME}_$(echo "${LABELS[@]}" | sed 's/ /_/g')_${ALLOW_CURL_INSTALLS}"
mkdir -p $BASIC_SETUP_DATA_DIRECTORY
if [ "$FORCE" != "true" ] && [ "$(find "$BASIC_SETUP_DATA_DIRECTORY" -maxdepth 1 -name $PREVIOUSLY_VALIDATED_FILE_NAME -mmin -1440)" ]; then
# echo "previously validated - skipping" 1>&2
update_e
exit 0
fi
}
# check for latest
function check_for_latest_basic_setup_git {
local old_dir="$(pwd)"
local exit_code=0
local error_message=""
local basic_setup_dir="$(general-get-basic-setup-dir)"
{
cd "$basic_setup_dir"
if [ ! -z "$(git status --porcelain)" ] && [ "$SKIP_PORCELAIN" != "true" ]; then
error_message="Error checking for latest, git not porcelain at ${basic_setup_dir}. Please commit/stash your changes. You can also skip this step with \`export BASIC_SETUP_ENVIRONMENT_VALIDATION_SKIP_PORCELAIN=\"true\"\`"
false
else
git fetch -p
local current_branch="$(git branch --show-current)"
local upstream="$(git rev-parse --abbrev-ref --symbolic-full-name @{u})"
local diff="$(git rev-list ${current_branch}...${upstream} --count)"
if [[ -z "$diff" ]]; then
error_message="Error checking for latest, git unable to get diff on branch $current_branch. Please ensure you have a remote set up. You can also skip this step with \`export BASIC_SETUP_ENVIRONMENT_VALIDATION_SKIP_PORCELAIN=\"true\"\`"
false
fi
if (( $diff > 0 )); then
error_message="Branch '${current_branch}' not at latest (or you haven't pushed your changes), please update ${basic_setup_dir} or run \`basic-setup-update\` for main."
false
else
(( $VERBOSITY > 0 )) && echo "Git is at latest" || true
if [ "$current_branch" != "$TARGET_BRANCH" ]; then
error_message="Git (at ${basic_setup_dir}) is not on the target branch (${TARGET_BRANCH}). It is on ${current_branch}. You can change the target with \`export BASIC_SETUP_ENVIRONMENT_VALIDATION_TARGET_BRANCH=\"$current_branch\"\`."
false
fi
fi
fi
} || {
local exit_code=$?
if (( $exit_code == 0 )); then
local exit_code=1
fi
if [ -z "$error_message" ]; then
local error_message="error during environment-validation when checking for latest..."
fi
}
cd "$old_dir"
if (( $exit_code > 0 )); then
echo "$error_message" 1>&2
exit $exit_code
fi
}
# Check for the tools described in the packages after filtering
function check_for_tools {
local labels="$(printf '%s\n' "${LABELS[@]}" | jq -R . | jq -sc .)"
(($VERBOSITY > 0)) && echo "checking for tools with labels: ${labels[@]}"
local packages_keys="$(echo $PACKAGES | jq -r '.packages[] | select(any(.labels; . | contains('$labels')) and .enabled == true) | .name')"
while read package_key; do
(($VERBOSITY > 1)) && echo "running for $package_key"
local package_content="$(echo "$PACKAGES" | jq '.packages[] | select(.name == "'"$package_key"'")')"
should_be_installed "$package_content"
done <<< $packages_keys # can't use echo pipe because that puts the loop in a subshell
}
# ensure the OS specific tooling is installed (e.g. GNU Mac tools)
function check_for_os_specific_tooling {
if [ "$(environment-os-type --mac)" == "true" ]; then
if [ "$(brew list --formula | grep coreutils)" != "coreutils" ]; then
echo "unable to find coreutils. Install with brew install coreutils" 1>&2
help
update_e
exit 1
fi
fi
}
# fail after running everything to generate a list
function handle_overall_errors {
if (( $ERROR_MESSAGES > 0 )); then
echo "Found Failures, check logs. Run with -h for help." 1>&2
echo "For install or upgrade errors you can run \`environment-validation\` with:" 1>&2
echo " -i to install" 1>&2
echo " -c to allow curl-commands" 1>&2
echo " -l for each of the current labels - ${LABELS[@]}" 1>&2
update_e
exit 1
else
(($VERBOSITY > 0)) && echo "No errors found. count - $ERROR_MESSAGES" || true
fi
}
# Get the install command for the package manager
function get_package_manager_install_command {
local package_manager="$1"
local package="$2"
local install_command="unknown install command"
(($VERBOSITY > 1)) && echo "finding install command for $package_manager and $package" 1>&2
[ "$package_manager" == "apt-get" ] && local install_command="sudo apt-get install $package -y"
[ "$package_manager" == "brew" ] && local install_command="brew install $package"
[ "$package_manager" == "curl" ] && local install_command="environment-curl-commands-${package} -f -i"
[ "$package_manager" == "pacman" ] && local install_command="sudo pacman -S --noconfirm $package"
[ "$package_manager" == "dnf" ] && local install_command="sudo dnf install $package -y"
[ "$package_manager" == "winget" ] && local install_command="winget install -e --id $package"
echo "$install_command"
}
# Check for the latest packages
function check_for_latest_package_from_package_manager {
if [ "$SKIP_LATEST_CHECK" == true ]; then
return 0
fi
local package_manager="$1"
if [ "$(is_command_installed "$package_manager")" != true ]; then
return 0
fi
local package="$2"
(($VERBOSITY > 1)) && echo "checking for latest for $package_manager and $package" 1>&2
if [ "$package_manager" == "apt-get" ]; then
sudo apt-get update -y > /dev/null
local apt_results="$(apt-get --just-print upgrade | grep '^[0-9]* upgraded, [0-9]* newly installed, [0-9]* to remove and [0-9]* not upgraded\.$')"
if [[ "$apt_results" =~ [1-9] ]]; then
if [ "$RUN_INSTALLS" == false ]; then
echo "ERROR: Please upgrade apt packages, 'sudo apt upgrade'." 1>&2
update_e
exit 1
else
(($VERBOSITY > 1)) && echo "found newer packages for apt, upgrading..." 1>&2
sudo apt-get upgrade -y
sudo apt-get autoremove -y
fi
fi
fi
if [ "$package_manager" == "brew" ]; then
if [ ! -z "$(brew outdated)" ]; then
if [ "$RUN_INSTALLS" == false ]; then
echo "ERROR: Please upgrade brew packages 'brew upgrade'." 1>&2
update_e
exit 1
else
(($VERBOSITY > 1)) && echo "found newer packages for brew, upgrading..." 1>&2
brew upgrade
fi
fi
fi
if [ "$package_manager" == "curl" ]; then
local curl_command="environment-curl-commands-${package}"
if (( $($curl_command -t >/dev/null 2>&1; echo $?) > 0 )); then
if [ "$RUN_INSTALLS" == false ]; then
echo "ERROR: ${package} is out of date. Please run '$curl_command -f -i' (or -h for help)." 1>&2
update_e
exit 1
else
(($VERBOSITY > 1)) && echo "found newer packages for curl, upgrading..." 1>&2
$curl_command -f -i
fi
fi
fi
if [ "$package_manager" == "pacman" ]; then
if [ ! -z "$(pacman -Qu)" ]; then
if [ "$RUN_INSTALLS" == false ]; then
echo "ERROR: Please upgrade pacman packages 'pacman -Syu'." 1>&2
update_e
exit 1
else
(($VERBOSITY > 1)) && echo "found newer packages for pacman, syncing..." 1>&2
sudo pacman -Syu
fi
fi
fi
if [ "$package_manager" == "dnf" ]; then
if [ ! -z "$(dnf check-update -q)" ]; then
if [ "$RUN_INSTALLS" == false ]; then
echo "ERROR: Please upgrade dnf packages 'dnf update'." 1>&2
update_e
exit 1
else
(($VERBOSITY > 1)) && echo "found newer packages for dnf, updating..." 1>&2
sudo dnf update -y
fi
fi
fi
if [ "$package_manager" == "winget" ]; then
(($VERBOSITY > 0)) && echo "not implemented...." 1>&2
fi
}
# run the logic for a package that should be installed
function should_be_installed {
local package_content=$1
local command_name=$(echo "$package_content" | jq -r '.command')
local human_name=$(echo "$package_content" | jq -r '.name')
local extra=$(echo "$package_content" | jq -r '."install-page"')
local package_manager_content="$(get_package_manager_content "$package_content")"
local package_name="$human_name"
local package_manager_name=""
local package_manager_install_command="Manually install $human_name."
if [ ! -z "$package_manager_content" ]; then
local package_name=$(echo "$package_manager_content" | jq -r '."package-name"')
local package_manager_name=$(echo "$package_manager_content" | jq -r '."manager-name"')
local package_manager_install_command=$(get_package_manager_install_command "$package_manager_name" "$package_name")
fi
local installed_command=$(echo "$package_manager_content" | jq -r '."installed-command"')
local is_command_installed=$(is_command_installed "$command_name" "$installed_command")
if [ "$is_command_installed" == "false" ]; then
if [ "$RUN_INSTALLS" == false ]; then
(($VERBOSITY > 1)) && echo "$command_name failed"
local message="unable to find $human_name ($command_name), '$package_manager_install_command' - $extra"
echo "$message" 1>&2
((ERROR_MESSAGES+=1))
else
$package_manager_install_command
fi
else
(($VERBOSITY > 0)) && echo "$command_name already installed with $package_manager_name." || true
if [ "$package_manager_name" == "curl" ]; then
check_for_latest_package_from_package_manager "$package_manager_name" "$package_name"
fi
fi
}
# check for updates
function update_tools {
for i in $SUPPORTED_PACKAGE_MANAGERS; do
check_for_latest_package_from_package_manager "$i" all
done
}
#
# CLI parsing
#
PARAMS=""
while (("$#")); do
case "$1" in
# Allow Curl flag
-c | --allow-curl)
ALLOW_CURL_INSTALLS=true
shift
;;
# Force flag
-f | --force)
FORCE=true
shift
;;
# help flag
-h | --help)
SHOW_HELP=true
shift
;;
# run installs flag
-i | --install)
RUN_INSTALLS=true
shift
;;
# label multi-optional argument
-l | --label)
if [ -n "$2" ] && [ "${2:0:1}" != "-" ]; then
if [ $CUSTOM_LABELS == false ]; then
LABELS=()
CUSTOM_LABELS=true
fi
LABELS+=("$2")
shift 2
else
echo "Error: Argument for $1 is missing" >&2
help
exit 1
fi
;;
# skip latest check flag
-s | --skip-latest)
SKIP_LATEST_CHECK=true
shift
;;
# verbosity multi-flag
-v | --verbose)
((VERBOSITY+=1))
shift
;;
# unsupported flags and arguments
-* | --*=)
echo "Error: Unsupported flag $1" >&2
help
update_e
exit 1
;;
# preserve positional arguments
*)
PARAMS="$PARAMS $1"
shift
;;
esac
done
#
# Do the work
#
[ $SHOW_HELP == true ] && help && update_e && exit 0
[ "$SKIP_EVERYTHING" == true ] && update_e && exit 0
check_for_skip
check_for_latest_basic_setup_git
check_for_jq
check_for_bash
check_for_os_specific_tooling
check_for_tools
update_tools
handle_overall_errors
# If everything worked, note it so that future checks can be skipped
touch "${BASIC_SETUP_DATA_DIRECTORY}${PREVIOUSLY_VALIDATED_FILE_NAME}"
update_e