-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup
executable file
·572 lines (481 loc) · 12.8 KB
/
setup
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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
#!/bin/bash
#=============================================================================
#
# FILE: setup
#
# USAGE: setup [-h] [-d] [-i] [-s steps]
#
# DESCRIPTION: Set up a Debian-based Linux machine
#
# OPTIONS: see function ’usage’ below
# REQUIREMENTS: jq
# BUGS: ---
# NOTES: ---
# AUTHOR: Brian Clark, bclarkx2
# COMPANY: ---
#=============================================================================
## Settings
set -e # Fail on invalid command
set -u # Fail on unset variable
set -o pipefail # Pipe status is rightmost non-zero exit code
## Constants
declare -r BOLD="$(tput bold)"
declare -r NORMAL="$(tput sgr0)"
declare -xr UBUNTU_SETUP_TODO="$(pwd)/TODO"
## Display functions
#######################################
# Display the script usage message
# Arguments
# None
# Outputs
# Writes usage message to STDOUT
#######################################
usage() {
printf '%s\n' "Usage: $(basename "$0") [-h] [-d] [-i] [-s steps]
Set up a debian-based Linux machine.
where:
-h, --help - show this help text
-d, --dry-run - run through script without executing anything
-i, --interactive - pause before executing each install script
-s, --steps - select which install steps to run. format is a
comma separated list of one or more of the
following values:
PACKAGES
SNAPS
REPOS
SCRIPTS"
}
#######################################
# Show a formatted error message
# Arguments
# A series of error arguments to be printed
# Outputs
# Writes all input to STDERR with a timestamp
#######################################
err() {
echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: $*" >&2
}
#######################################
# Print an indicator marking the start of a new script step
# Arguments
# Title of the new step
# Outputs
# Prints the indicator to STDOUT
#######################################
step() {
local -r title="$1"
printf '\n%s%s%s\n' '===> ' "${title}" '...'
}
## Common
#######################################
# Handle executing installation actions
# based on whether we are in a dry run
# Globals
# DRY_RUN
# Arguments
# Statement to be executed
#######################################
run() {
local -r _exe="$1"
if "${DRY_RUN}"; then
echo "Dry run: ${_exe}"
else
bash -c "${_exe}"
fi
}
#######################################
# Ask the user to select an action to take
# Arguments
# Prompt to show the user, and a regex
# to be used to validate the first letter
# of the user's response (ex: '[yn]')
# Outputs:
# If the user entered a valid action,
# the first letter is printed to STDOUT
# in lower case. Otherwise, nothing is
# printed.
#######################################
ask_action() {
local -r prompt="$1"
local -r allowed="$2"
local response
read -p "${prompt}"$'\n' response
local -ru code="${response:0:1}"
if [[ "${code}" =~ ${allowed} ]]; then
echo "${code}"
fi
}
## Prereqs
#######################################
# Ensure that all system components needed
# to execute the rest of the script are installed.
# Arguments
# None
#######################################
verify_prereqs() {
step 'Installing pre-reqs'
# Need git over SSH
run 'sudo apt install git jq'
echo
run 'prereqs/generate-ssh-key'
}
## Packages
#######################################
# Print the items contained within an
# item set.
# Globals
# BOLD
# NORMAL
# Arguments
# - The directory the item set is in
# - The path of the item file
# Outputs
# The contents of the set in columns
#######################################
print_set() {
local -r dir="$1"
local -r item_file="$2"
local -r item_set="$(basename "${item_file}")"
# Print header for the item set
echo
echo "--- Set: ${dir}/${BOLD}${item_set}${NORMAL}"
# Print contents of set, with two space indent
cat "${item_file}" | column -x -c 40 | sed 's/^/ /'
echo
}
#######################################
# Allow user to select some items from a set.
# Arguments
# - Path to the item set file
# - Name of an array of items in the set
# Outputs
# Asks user whether to include each
# item in set on STDOUT
#######################################
edit_set() {
local -r item_file="$1"
local -n item_pkgs="$2"
readarray -t lines < "${item_file}"
for line in "${lines[@]}"; do
printf '\n%s\n' "${line}"
local action="$(ask_action 'y/n?' '[YN]')"
if [[ "${action}" == "Y" ]]; then
item_pkgs+=("${line}")
printf 'Adding %s to list\n' "${line}"
fi
done
}
#######################################
# Build array of items to install
# Arguments
# - Name of array to contain all items
# - Directory containing item set files
# Outputs
# Asks user what to do with each item
# set in the directory on STDOUT
#######################################
build_list() {
local -n all_items="$1"
local -r dir="$2"
step "Collecting $dir"
for item_file in ${dir}/*; do
print_set "${dir}" "${item_file}"
local action="$(ask_action '(I)nstall, (S)kip, or (E)dit?' '[IES]')"
case "${action}" in
I)
mapfile -t -O "${#all_items[@]}" all_items < "${item_file}"
;;
E)
edit_set "${item_file}" all_items
;;
*)
continue
;;
esac
done
}
#######################################
# Install selected packages
# Arguments
# Name of array containing packages to install
# Outputs
# Prints progress to STDOUT
#######################################
install_packages() {
local -n install_pkgs="$1"
step 'Installing packages'
run "sudo apt install -y ${install_pkgs[*]}"
}
## Snaps
#######################################
# Install selected snaps
# Arguments
# Name of array containing snaps to install
# Outputs
# Prints progress to STDOUT
#######################################
install_snaps() {
local -n install_snaps="$1"
step 'Installing snaps'
for snap in "${install_snaps[@]}"; do
run "sudo snap install $snap"
done
}
## Repos
#######################################
# Extract one field from a repo string
# Arguments
# - Repo JSON string
# - Name of field to extract
# Outputs
# Prints field value to STDOUT
#######################################
field() {
local -r _repo="$1"
local -r _field="$2"
echo "${_repo}" | jq ".${_field}"
}
#######################################
# Construct map of repos to install
# Arguments
# - Name of map to construct (repo name -> repo string)
# - Directory containing repo manifest
# Outputs
# Asks user what to do with each repo on STDOUT
#######################################
build_repos() {
local -n repo_map="$1"
local -r dir="$2"
local repo_file rep
step 'Installing repos'
for repo_file in ${dir}/*; do
# Parse each repo in the file into an array
local file_repos=()
while read -r repo; do
file_repos+=("${repo}")
done < <(jq --compact-output '.[]' "${repo_file}")
# Cycle through and let user select which to install
for repo in "${file_repos[@]}"; do
echo "${repo}" | jq --raw-output '.'
local action="$(ask_action '(I)nstall or (S)kip?' '[IS]')"
if [[ "${action}" == "I" ]]; then
local name="$(field "${repo}" 'name')"
repo_map[${name}]="${repo}"
printf "Adding %s to list\n" "${name}"
fi
echo
done
done
}
#######################################
# Install selected repos and execute
# post-install scripts.
# NOTE: requires git, checks out submodules
# to master.
# Globals
# HOME
# Arguments
# Map of repos to install (repo name -> repo string)
# Outputs
# Prints progress to STDOUT
#######################################
install_repos() {
local -n repo_map="$1"
local repo_str
step 'Installing repos'
for repo_str in "${repo_map[@]}"; do
# Parse each repo into an assoc array using jq
local -A repo="($(
echo "${repo_str}" | \
jq -r '. | to_entries | .[] | "[" + .key + "]=\"" + .value + "\""'
))"
# Use git to clone repos
local target="${repo[location]/#\~/$HOME}"
run "git clone ${repo[repo]} ${target} || true"
# If installation failed, move on to the next
if (( $? != 0 )); then
err "Error cloning repo ${repo[name]}, aborting"
continue
fi
## Check out desired branch
if [[ -v repo[branch ]]; then
run "cd ${target}; git checkout ${repo[branch]}"
fi
# Handle submodules
run "cd ${target}; git submodule update --init --recursive" ;
run "cd ${target}; git submodule foreach --recursive git checkout master"
# Execute any post-install script
if [[ -v repo[script] ]]; then
run "( cd ${target} ; ${repo[script]} )"
fi
done
}
## Scripts
#######################################
# Display a description of a script
# Globals
# BOLD
# NORMAL
# Arguments
# Path to script
# Outputs
# Script header on STDOUT
#######################################
print_script() {
local -r script="$1"
local -r script_name="$(basename "${script}")"
local -r script_dir="$(dirname "${script}")"
printf '\n%s\n' "---Script: ${script_dir}/${BOLD}${script_name}${NORMAL}"
}
#######################################
# Build array of scripts to install
# Arguments
# - Name of array to contain all scripts
# - Directory containing scripts
# Outputs
# Asks user what to do about each
# script on STDOUT
#######################################
build_scripts() {
local -n all_scripts="$1"
local -r dir="$2"
step 'Collecting scripts'
for script in ${dir}/*; do
local script_name="$(basename "${script}")"
print_script "${script}"
printf '%s\n' "$("${script}" --help)"
local action="$(ask_action 'Y/n?' '[YN]')"
if [[ "${action}" == "Y" ]]; then
all_scripts+=("${script}")
fi
done
}
#######################################
# Install selected scripts. Will pause
# before each script if INTERACTIVE
# is set
# Globals
# UBUNTU_SETUP_TODO
# INTERACTIVE
# Arguments
# Name of array containing scripts to install
# Outputs
# Prints progress to STDOUT
#######################################
install_scripts() {
local -n run_scripts="$1"
step 'Running scripts'
# Execute all selected scripts
for script in "${scripts[@]}"; do
print_script "${script}"
"${INTERACTIVE}" && read -s -r -p "<Press enter to continue>"$'\n'
run "${script}"
done
}
## Main
#######################################
# Perform main script logic
# Globals
# UBUNTU_SETUP_TODO
# DO
# DRY-RUN
# INTERACTIVE
# Arguments
# None
#######################################
main() {
verify_prereqs
# Empty TODO file at start
> "${UBUNTU_SETUP_TODO}"
if "${DO[PACKAGES]}"; then
local -a pkgs=()
build_list pkgs 'packages'
install_packages pkgs
fi
if "${DO[SNAPS]}"; then
local -a snaps=()
build_list snaps 'snaps'
install_snaps snaps
fi
if "${DO[REPOS]}"; then
local -A repos
build_repos repos 'repos'
install_repos repos
fi
if "${DO[SCRIPTS]}"; then
local -a scripts=()
build_scripts scripts 'scripts'
install_scripts scripts
fi
printf '\n%s\n' 'Setup complete!'
# Print todo reminder, if TODO file has anything in it
local todo_size="$(stat --format=%s "${UBUNTU_SETUP_TODO}")"
if (( todo_size > 0 )); then
printf '%s\n' '-> Check TODO file for followups!'
fi
}
## Option parsing
declare PARAMS=""
declare DRY_RUN='false'
declare INTERACTIVE='false'
declare -A DO=( # By default, do all steps
[PACKAGES]='true'
[SNAPS]='true'
[REPOS]='true'
[SCRIPTS]='true'
)
while (( "$#" )); do
case "$1" in
-h| --help)
usage
exit 0
;;
-d|--dry-run)
DRY_RUN='true'
shift 1
;;
-i|--interactive)
INTERACTIVE='true'
shift 1
;;
-s|--steps)
declare -u steps="$2"
# Steps are required
if [[ -z "${steps}" ]]; then
err 'Error: missing arg to --steps.'
echo 'Enter one or more of the following, separated by commas:'
printf ' %s\n' "${!DO[@]}"
exit 1
fi
# If we are setting steps, default to doing none
for step in "${!DO[@]}"; do
DO[$step]='false'
done
# For each valid step, mark the DO array val as true
for step in "${steps//,/ }"; do
if [[ "${DO[$step]+asdf}" ]]; then
DO[$step]='true'
echo "Adding to pipeline: ${step}"
fi
done
shift 2
;;
--) # end argument parsing
shift
break
;;
-*|--*=) # unsupported flags
echo "Error: Unsupported flag $1" >&2
exit 1
;;
*) # preserve positional arguments
PARAMS="${PARAMS} $1"
shift
;;
esac
done
# set positional arguments in their proper place
eval set -- "${PARAMS}"
# Freeze configuration variables
readonly PARAMS DRY_RUN INTERACTIVE DO
main "$@"