-
Notifications
You must be signed in to change notification settings - Fork 0
/
go.zsh
executable file
·491 lines (430 loc) · 19.8 KB
/
go.zsh
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
#!/bin/zsh
#
# Configurations go into config.txt
#
# internal environment variables
set -a
kernel="$(uname -s)"
callee=$0
source $(dirname "$0")/lib/core.zsh
basedir=$(realpath $(dirname "$0"))
source $basedir/config.txt || exit 1
lib=$basedir/lib
playbooks=$basedir/playbooks
ssh_d=$basedir/.ssh
configcache=$basedir/.config
configtemplate=$lib/config.template
bootstrapper_cfg=$lib/bootstrapper_cfg.template
refresh=$basedir/.refresh
downloads=$basedir/downloads
os_img=$downloads/$OS_IMG
os_img_checksum=$os_img.sha256sums
os_mnt=$basedir/.mnt
source $lib/checks.zsh || exit 1
source $lib/image.zsh || exit 1
source $lib/firmware.zsh || exit 1
source $lib/create_ap.zsh || exit 1
# internal runtime variables
banner=false
## os variables
case $kernel in
Darwin)
alias PKG_UPDATE="brew update"
alias PKG_INSTALL="brew install"
alias PKG_LIST="brew list"
;;
Linux)
os_flavour=$(cat /etc/os-release | grep ID_LIKE | cut -d= -f2)
case $os_flavour in
arch)
alias PKG_UPDATE="yay -Sy"
alias PKG_INSTALL="yay -S"
alias PKG_LIST="yay -Q"
;;
debian)
alias PKG_UPDATE="sudo apt-get update"
alias PKG_INSTALL="sudo apt-get install -y"
alias PKG_LIST="sudo dpkg -l"
;;
*) logp usage "You have choosen an operating system that is not on good terms with the Federation." ;;
esac
;;
*) logp usage "You have choosen an operating system that is not on good terms with the Federation." ;;
esac
# end of environment variables
function clean_up()
{
[ ! -f $basedir/.tmp ] || rm -f $basedir/.tmp
[ ! -z "$(mount | grep $os_mnt)" ] && logp info "Unmounting $os_mnt" && sudo umount $os_mnt && rmdir $os_mnt
case $1 in
INT) logp fatal "The button of death has entered the room." ;;
EXIT) [ $banner = true ] && logp endsection ;;
TERM) logp fatal "The program seizes to live." ;;
esac
}
trap "clean_up INT" INT
trap "clean_up TERM" TERM
trap "clean_up EXIT" EXIT
function banner()
{
banner=true
clear
case $kernel in
Darwin) PRETTY_NAME="Mac OSx" ;;
Linux) source /etc/os-release ;;
esac
cols=$(tput cols)
date=$(date)
whoami=$(whoami)
ip="$(ifconfig | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*' | grep -v '127.0.0.1' | tail -n1)"
beacon="$(whoami)@$HOST ($ip)"
zsh -c "echo -e \"\e[1m\e[33m+$(termFill '-' $((cols - 2)))+\""
printf "|\e[0m`tput bold` %-$((cols - $((15 + ${#PRETTY_NAME} + ${#beacon})) ))s%-5s\e[1m\e[33m |\n" "$callee -- ROS:$ROS_RELEASE" "running $PRETTY_NAME @ $beacon"
printf "\e[1m\e[33m| %-$((cols - 4))s\e[1m\e[33m |\n" ""
if [ $# -gt 0 ]; then; printf "\e[1m\e[33m| %-$((cols - $((4 + ${#date})) ))s%-5s\e[1m\e[33m |\n" "$1" "`date`"; fi
logp beginsection
}
function killSshAgent()
{
eval "$(ssh-agent -k)" &>/dev/null
return 0
}
function makeManageAble()
{
echo $DEFAULT_PASS | sshpass ssh-copy-id -i $ssh/admin $DEFAULT_USER@$RHOST -o PreferredAuthentications=password -o PubkeyAuthentication=no
}
function prepareAdminEnvironment()
{
[ ! -d $ssh_d ] && mkdir -p $ssh_d
ADMIN_KEY=$(realpath $ssh_d)/$ADMIN_USER
if [ ! -f $ADMIN_KEY ] && [ "$ACTION" = "bootstrap" ]; then
logp info "Generating sshkey $ADMIN_KEY"
prepareDependency ssh-keygen
ssh-keygen -f $ADMIN_KEY -q -N "" || logp fatal "Couldn't generate ansible's bloody key!"
fi
}
function prepareAnsibleEnvironment()
{
prepareDependency ansible
[ ! -d $ssh_d ] && mkdir -p $ssh_d
ANSIBLE_USER=ansible
ANSIBLE_KEY=$(realpath $ssh_d)/ansible
if [ ! -f $ANSIBLE_KEY ] && [ "$ACTION" = "bootstrap" ]; then
logp info "Generating sshkey $ANSIBLE_KEY"
prepareDependency ssh-keygen
ssh-keygen -f $ANSIBLE_KEY -q -N "" || logp fatal "Couldn't generate ansible's bloody key!"
fi
}
function ansibleRunPlaybook
{
logp info "user:$DEFAULT_USER pass:$DEFAULT_PASS"
target=$1
if [ $# -eq 2 ] && [ "$2" = "firstrun" ]; then
ansibleoptions="ansible_port=$RPORT ansible_ssh_user=$DEFAULT_USER ansible_ssh_pass=$DEFAULT_PASS ansible_python_interpreter=/usr/bin/python3"
else
[ ! -f $ANSIBLE_KEY ] && logp fatal "No ansible ssh key found."
ansibleoptions="ansible_port=$RPORT ansible_ssh_user=$ANSIBLE_USER ansible_ssh_private_key_file=$ANSIBLE_KEY ansible_python_interpreter=/usr/bin/python3 "
fi
ansible-playbook -i $RHOST,\
-e $ansibleoptions \
$basedir/playbooks/$target.yml
}
function readConfigcache() { export $(grep -f $configtemplate $configcache) }
function writeConfigcache() { env | grep -f $configtemplate > $configcache }
function getUserInfo()
{
checkConfigcacheExists $configcache && { readConfigcache || logp fatal "configfile' $configfile' has corrupted." }
case $ACTION in
bootstrap)
case $2 in
raspberry)
{ [ ! -f $configcache ] || [[ $(wc -l $configcache | sed -e 's/^[[:space:]]*//' | cut -f1 -d\ ) -lt 3 ]] } && logp info "Your attention is required. The experiment requires you to answer truely and wholeheartedly."
[ -z "${RHOST+x}" ] && logp question "remote host's network address" && read -r RHOST
[ -z "${RPORT+x}" ] && logp question "remote host's port" && read -r RPORT
[ -z "${RHOSTNAME+x}" ] && logp question "remote host's hostname" && read -r RHOSTNAME
[ -z "${DEFAULT_USER+x}" ] && logp question "remote host's first login user" && read -r DEFAULT_USER
[ -z "${DEFAULT_PASS+x}" ] && logp question "remote host's first login pass" && read -r DEFAULT_PASS
;;
raspberry-microsd)
logp info "Your attention is required. The experiment requires you to answer truely and wholeheartedly."
if which lsblk; then
logp info "Block devices : "
lsblk -f
fi
logp question "Destination microsd card (or other blockdevice)" && read -r blk_dev
[ -z "${WIFI_SSID+x}" ] && logp question "Wifi address" && read -r WIFI_SSID
[ -z "${WIFI_PASS+x}" ] && logp question "Wifi password" && read -r WIFI_PASS
;;
arduino)
[ -z "${RHOST+x}" ] && logp question "remote host's network address" && read -r RHOST
[ -z "${RPORT+x}" ] && logp question "remote host's port" && read -r RPORT
[ -z "${RHOSTNAME+x}" ] && logp question "remote host's hostname" && read -r RHOSTNAME
;;
*) logp fatal "CRASH @ getUserInfo" ;;
esac
;;
run)
case $2 in
playbook)
[ -z "${RHOST+x}" ] && logp question "remote host's network address" && read -r RHOST
;;
*) logp fatal "CRASH @ getUserInfo" ;;
esac
;;
create)
case $2 in
accesspoint)
[ -z "${WIFI_SSID+x}" ] && logp question "Wifi address" && read -r WIFI_SSID
[ -z "${WIFI_PASS+x}" ] && logp question "Wifi password" && read -r WIFI_PASS
[ -z "${WIFI_DEV+x}" ] && { which ifconfig && ifconfig || true } && logp question "Wifi device" && read -r WIFI_DEV
[ -z "${UPLINK_DEV+x}" ] && { which ifconfig && ifconfig || true } && logp question "Uplink device" && read -r UPLINK_DEV
;;
*) logp fatal "CRASH @ getUserInfo" ;;
esac
;;
shell)
{ [ ! -f $configcache ] || [[ $(wc -l $configcache | sed -e 's/^[[:space:]]*//' | cut -f1 -d\ ) -lt 3 ]] } && logp info "Your attention is required. The experiment requires you to answer truely and wholeheartedly."
[ -z "${RHOST+x}" ] && logp question "remote host's network address" && read -r RHOST
[ -z "${RPORT+x}" ] && logp question "remote host's port" && read -r RPORT
;;
*) logp fatal "CRASH @ getUserInfo" ;;
esac
writeConfigcache
}
function prepareDependency()
{
dep=$1
if ! command -v $dep &>/dev/null && ! PKG_LIST $dep &>/dev/null; then
logp info "Dependency '$dep' is missing. Attempting to install:"
PKG_UPDATE || logp fatal "Couldn't update from repositories"
PKG_INSTALL $dep || logp fatal "Couldn't install dependency '$dep'!"
fi
}
function prepareAllDependencies()
{
U=0
DEPENDENCIES_A=($(echo $DEPENDENCIES | tr '\t' ' '))
for dep in "${DEPENDENCIES_A[@]}"
do
if ! command -v $dep &> /dev/null; then
if [ $U -eq 0 ]; then
PKG_UPDATE || logp fatal "Couldn't update packages"
U=1
fi
PKG_INSTALL $dep || logp fatal "Couldn't install dependency '$dep'!"
fi
done
logp info "All dependencies were met."
}
#https://stackoverflow.com/questions/3258243/check-if-pull-needed-in-git
function pullMaster()
{
logp info "Checking for update.."
prepareDependency git
if git remote update 1>/dev/null ; then
upstream=${1:-'@{u}'}
local=$(git rev-parse @ $basedir)
remote=$(git rev-parse "$upstream" $basedir)
if [ $local != $remote ]; then
p=$(pwd)
cd $basedir
git pull 1>/dev/null
git submodule update --init --remote 1>/dev/null
logp info "Bootstrapper has evolved!"
cd $p
fi
else; return 1; fi
}
function prepareEnvironment()
{
killSshAgent
[ "$(echo $SHELL | rev | cut -d\/ -f1 | rev)" = "zsh" ] || logp fatal "$callee requires to be run with zsh."
checkConnectivity && checkMasterUpdate && { pullMaster || logp warning "An update has failed you. Your computer will explode now." } || true
}
function handleFlags()
{
[ $# -eq 0 ] && usage
# read action options
for ARG in $@; do
[ "${ARG}" = "bootstrap" ] && ACTION="${ARG}" && break
[ "${ARG}" = "run" ] && ACTION="${ARG}" && break
[ "${ARG}" = "create" ] && ACTION="${ARG}" && break
[ "${ARG}" = "dependencies" ] && ACTION="${ARG}" && break
[ "${ARG}" = "shell" ] && ACTION="${ARG}" && break
[ "${ARG}" = "clean" ] && ACTION="${ARG}" && break
[ "${ARG}" = "reset" ] && ACTION="${ARG}" && break
[ "${ARG}" = "help" ] && ACTION="${ARG}" && break
done
[ "$ACTION" = "" ] && usage
}
function performActions()
{
case $ACTION in
bootstrap) #############################################################
[ $# -lt 2 ] && logp usage "Bootstrap what?"
case $2 in
raspberry) #####################################################
banner "God looked down and agreed with this strategy. Bootstrapping raspberry.."
getUserInfo $@ || logp fatal "Failed to get your info"
checkConnectivity || logp fatal "The network doesn't believe you have connected to it."
checkIsReachable $RHOST || logp fatal "Host '$RHOST' is not reachable at this time (ping test)."
prepareAnsibleEnvironment || logp fatal "The Ansible Environment has denied your request."
prepareAdminEnvironment || logp fatal "The Admin Environment has denied your request."
checkIsManageable $RHOST $RPORT "$DEFAULT_USER" "$DEFAULT_PASS" || makeManageAble || logp fatal "Host $RHOST is not talkative at the moment."
if ! checkIsManageable $RHOST $RPORT $ANSIBLE_USER "NULL" $ANSIBLE_KEY; then
target="ansible_user"; logp info "Started running playbook $target...";
ansibleRunPlaybook $target firstrun || logp fatal "The machine is still resisting. $target rules have failed to comply!"
if checkIsManageable $RHOST $RPORT $DEFAULT_USER $DEFAULT_PASS; then
target="lock"; logp info "Started running playbook $target...";
ansibleRunPlaybook $target || logp fatal "The machine is still resisting. $target rules have failed to comply!"
else
logp fatal "Your $RHOST gave us trouble. Please give us the right credentials."
fi
sleep 5 # lock fucks with ssh-server
fi
target="disable-unattended-upgrades"; logp info "Started running playbook $target...";
ansibleRunPlaybook $target || logp fatal "The machine is still resisting. $target rules have failed to comply!"
target="system"; logp info "Started running playbook $target...";
ansibleRunPlaybook $target || logp fatal "The machine is still resisting. $target rules have failed to comply!"
target="firewall"; logp info "Started running playbook $target...";
ansibleRunPlaybook $target || logp fatal "The machine is still resisting. $target rules have failed to comply!"
export NUSER="$ADMIN_USER" NKEY="$ADMIN_KEY" NGROUPS="$ADMIN_GROUPS"
target="user"; logp info "Started running playbook $target for user '$NUSER'...";
ansibleRunPlaybook $target || logp fatal "The machine is still resisting. $target rules have failed to comply!"
target="firmware"; logp info "Started running playbook $target...";
ansibleRunPlaybook $target || logp fatal "The machine is still resisting. $target rules have failed to comply!"
target="ros"; logp info "Started running playbook $target...";
ansibleRunPlaybook $target || logp fatal "The machine is still resisting. $target rules have failed to comply!"
target="arduino"; logp info "Started running playbook $target...";
ansibleRunPlaybook $target || logp fatal "The machine is still resisting. $target rules have failed to comply!"
target="bootstrapper_cfg"; logp info "Started running playbook $target...";
ansibleRunPlaybook $target || logp fatal "The machine is still resisting. $target rules have failed to comply!"
logp info "The machine has spoken. Bootstrap complete."
;;
raspberry-microsd) #############################################
banner "Electrons! We summon you to carry out this microsd bootstrapping thing!"
getUserInfo $@ || logp fatal "Failed to get your info"
image_prepare || logp fatal "Image couldn't be prepared at this moment."
image_write || logp fatal "Failed to write image."
logp info "The image was written succesfully."
firmware_prepare_network || logp fatal "Failed to apply network information: enter network info manualy!"
firmware_prepare_bootloader || logp fatal "Failed preparing bootloader."
logp info "The network configuration has decided in favour of the Federation. It was a wise decision."
[ ! -z "$(mount | grep $os_mnt)" ] && logp info "Unmounting $os_mnt" && { sudo umount $os_mnt || logp warning "Failed unmounting $os_mnt" }
logp info "Syncing last blocks to disk.." && sudo sync
logp info "By the analog Gods and the digital! The image was build. Yalla let us bootstrap a raspberry."
;;
arduino) #######################################################
banner "Arduino here to bootstrap your spine."
[ $# -gt 2 ] && [ ! -f $3 ] && logp fatal "Optional last argument should point to firmware file!"
[ ! -f $ARDUINO_HEX ] && logp fatal "Store the compiled arduino firmware file @ $ARDUINO_HEX"
getUserInfo $@ || logp fatal "Failed to get your info"
prepareAdminEnvironment || logp fatal "The Admin Environment has denied your request."
[ -f $ADMIN_KEY ] || logp fatal "No admin key -> Bootstrap raspberry first."
checkIsReachable $RHOST || logp fatal "Host '$RHOST' is not reachable at this time (ping test)."
checkIsManageable $RHOST $RPORT $ADMIN_USER "NULL" $ADMIN_KEY || logp fatal "Host $RHOST is not talkative at the moment."
prepareDependency scp
logp info "Copying over '$ARDUINO_HEX'"
scp -i $ADMIN_KEY -P $RPORT $ARDUINO_HEX $ADMIN_USER@$RHOST:~/upload/ || logp fatal "Couldn't upload '$ARDUINO_HEX'"
logp info "Attempting to flash remotely.."
ssh -i $ADMIN_KEY -p $RPORT $ADMIN_USER@$RHOST 'sudo zsh -s' < $lib/arduino_flash.zsh || logp fatal "More luck next time."
;;
arduino-env) ###################################################
[ $# -eq 3 ] || logp usage "$callee bootstrap arduino-env [DIRECTORY]]"
mkdir -p $3 && logp info "Created '$3'" || logp fatal "Failed to create directory '$3'!"
for package in "${ARDUINO_PACKAGES[@]}"; do prepareDependency $package; done
git clone git@github.com:$GIT_ORG/$GIT_LLC.git $3 || logp fatal "Couldn't clone Arduino-env!"
;;
*) logp usage "You have choosen an option that is not on good terms with the Federation." ;;
esac
;;
run) ###################################################################
[ $# -lt 2 ] && logp usage "Run where?"
case $2 in
playbook)
[ $# -lt 3 ] && logp usage "Run what playbook? \n\n$(cd $playbooks && print -rl -- *.yml(:r)) "
[ -f $playbooks/$3.yml ] || logp usage "Playbook $3 not found in $playbooks"
ANSIBLE_KEY=$(realpath $ssh_d)/ansible
ANSIBLE_USER=ansible
[ -f $ANSIBLE_KEY ] || logp fatal "No ansible key -> Bootstrap raspberry first."
banner "Playbook '$3' is running.."
getUserInfo $@ || logp fatal "Failed to get your info"
prepareAdminEnvironment || logp fatal "The Admin Environment has denied your request."
checkConnectivity || logp fatal "The network doesn't believe you have connected to it."
checkIsManageable $RHOST $RPORT $ANSIBLE_USER "NULL" $ANSIBLE_KEY || logp fatal "Host $RHOST is not talkative at the moment."
target="$3"; logp info "Started running playbook $target...";
ansibleRunPlaybook $target || logp fatal "The machine is still resisting. $target rules have failed to comply!"
logp info "The playbook has come to a conclusion."
;;
*) logp usage "You have choosen an option that is not on good terms with the Federation." ;;
esac
;;
create) ################################################################
case $2 in
accesspoint)
# TODO: remove the following when a proper fix for os dependencies is found
#[ "$kernel" = "Linux" ] && [ "$os_flavour" = "arch" ] || logp fatal "This function depends on AUR package create_ap. Since you are not running a flavour of Arch Linux I canno't help you. Setup a wifi accespoint manually."
#which create_ap || which yay || logp fatal "I don't know how to get create_ap from the AUR. Do you have 'yay' installed? If not install it and run again, or get a hold of 'create_ap' yourself."
banner "The Federation has sent you an accesspoint."
getUserInfo $@ || logp fatal "Failed to get your info"
create_accesspoint || logp fatal "Killed or failed."
;;
*) logp usage "You have choosen an option that is not on good terms with the Federation." ;;
esac
;;
shell) #################################################################
getUserInfo $@ || logp fatal "Failed to get your info"
[ -f $ADMIN_KEY ] || logp fatal "You have to bootstrap the raspberry first, to set the key."
prepareAdminEnvironment || logp fatal "The Admin Environment has denied your request."
{ [ -z "${RPORT+x}" ] || [ -z "${RHOST+x}" ] || [ -z "${ADMIN_USER+x}" ] || [ -z "${ADMIN_KEY+x}" ] } && logp fatal "Crash! Variables not set."
ssh -p $RPORT -o PreferredAuthentications=publickey -i $ADMIN_KEY $ADMIN_USER@$RHOST
[ $? -eq 130 ] || logp fatal "Shell couldn't be attained."
;;
dependencies) #######################$##################################
prepareAllDependencies || logp fatal "Missing dependencies."
;;
clean) #################################################################
[ -f $ARDUINO_HEX ] && rm -f $ARDUINO_HEX && logp info "Cleaned configcache '$ARDUINO_HEX'"
[ -d $downloads ] && rm -rf $downloads && logp info "Cleaned out downloads folder : $downloads."
;;
reset) #################################################################
logp warning_nnl "Are you sure? This will also delete ansible/user keys! -> Legal demands that you type IMNOTANIDIOT to continue : "; read -r response
if [ "$response" = "IMNOTANIDIOT" ]; then
[ -d "$ssh" ] && rm -rf $ssh && logp info "Cleaned ssh folder with keys '$ssh'"
[ -f $refresh ] && rm -f $refresh && logp info "Cleaned git update refresh file '$refresh'"
[ -f $configcache ] && rm -f $configcache && logp info "Cleaned configcache '$configcache'"
[ -f $ARDUINO_HEX ] && rm -f $ARDUINO_HEX && logp info "Cleaned configcache '$ARDUINO_HEX'"
[ -d $downloads ] && rm -rf $downloads && logp info "Cleaned out downloads folder : $downloads."
else logp fatal "Probably a wise decision."; fi
;;
help|*) ################################################################
usage
;;
esac
}
function usage()
{
(logp usage "")
cat<<-EOF
$callee bootstrap raspberry
$callee bootstrap raspberry-microsd
$callee bootstrap arduino
$callee bootstrap arduino [.hex file]
$callee bootstrap arduino-env
$callee run playbook [playbook]
$callee create accesspoint
$callee shell
$callee dependencies # pull in dependencies
$callee clean # deletes replaceable data
$callee reset # this clears out more than you might want
$callee help
EOF
exit 1
}
function main()
{
prepareEnvironment || logp fatal "The Environment has denied your request."
handleFlags $@
performActions $@
}
main $@