-
Notifications
You must be signed in to change notification settings - Fork 6
/
deploy
executable file
·2172 lines (1977 loc) · 76 KB
/
deploy
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
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
SCRIPT_VERSION=1.8.0
INSTALL_BUILD_LOCAL=${INSTALL_BUILD_LOCAL:-"TRUE"}
INSTALL_DEBUG=${INSTALL_DEBUG:-"FALSE"}
INSTALL_REBOOT=${INSTALL_REBOOT:-"FALSE"}
LOG_LEVEL=NOTICE
REMOTE_USER=${REMOTE_USER:-"$(whoami)"}
SECRET_USER="$(whoami)"
SECRET_HOST="$(hostname)"
SSH_PORT=${SSH_PORT:-"22"}
if [ -f ".deploy.env" ] ; then source .deploy.env ; fi
case "${1}" in
"--debug" )
export DEBUG_MODE=TRUE
;;
"--version" | "-v" )
echo "${SCRIPT_VERSION}"
exit 1
;;
"--help" | "-h" | "-?" )
printf "\033c"
cat << EOF
******************************************************************************************************************************
** NixOS Deployment - Revision ${SCRIPT_VERSION}
**
** - Based on a series of menus this tool will assist in deploying a new or existing NixOS Installation
** - This will install from either a NixOS ISO or an alternative operating system such as the OVH Rescue Image
** - Secrets Management using using SOPS
** - Basic Flake Management
**
** - Using Nix Flakes, this harnesses the power of 'disko' and 'nixos-anywhere' to perform remote installations or deployments
**
** - Prerequisites:
** - A working nix installation, preferably a NixOS installation
** - 'git', 'age', 'ssh-to-age', 'sops'
**
**
** - Usage:
** '$(basename "$0")' - Interative Mode (Default)
** '$(basename "$0")' --help - Yer lookin at it
** '$(basename "$0")' --debug - Shows Output of commands behind the scenes
** '$(basename "$0")' --changelog - Shows this tools Changelog
** '$(basename "$0")' --version - Shows this tools version
** '$(basename "$0")' --(arguments) - Pass any arguments to alter default behaviour
**
**
****************************************************************************************************************************
EOF
exit 99
;;
"--changelog" | "-c" )
if [ -f "$0"/CHANGELOG.md ] ; then
cat "$0"/CHANGELOG.md
exit 98
else
echo "Please see CHANGELOG.md in the source code repository"
exit 98
fi
;;
*)
:
;;
esac
########################################################################################
### System Functions ###
########################################################################################
### Colours
# Foreground (Text) Colors
cdgy="\e[90m" # Color Dark Gray
clg="\e[92m" # Color Light Green
clm="\e[95m" # Color Light Magenta
cwh="\e[97m" # Color White
# Turns off all formatting
coff="\e[0m" # Color Off
# Background Colors
bdr="\e[41m" # Background Color Dark Red
bdg="\e[42m" # Background Color Dark Green
bdb="\e[44m" # Background Color Dark Blue
bdm="\e[45m" # Background Color Dark Magenta
bdgy="\e[100m" # Background Color Dark Gray
blr="\e[101m" # Background Color Light Red
boff="\e[49m" # Background Color Off
## An attempt to shut down so much noise specifically for echo statements
output_off() {
if [ "${DEBUG_MODE,,}" = "true" ] ; then
set +x
fi
}
output_on() {
if [ "${DEBUG_MODE,,}" = "true" ] ; then
set -x
fi
}
### Text Coloration
print_debug() {
output_off
case "$LOG_LEVEL" in
"DEBUG" )
if [ "${DEBUG_MODE,,}" = "true" ] ; then
if [ "${COLORIZE_OUTPUT,,}" = "false" ] ; then
echo -e "[DEBUG] $SCRIPTPATH/$(basename "$0") ** $1"
else
echo -e "${bdm}[DEBUG]${boff} $SCRIPTPATH/$(basename "$0") ** $1"
fi
else
if [ "${COLORIZE_OUTPUT,,}" = "false" ] ; then
echo -e "[DEBUG] ** $1"
else
echo -e "${bdm}[DEBUG]${boff} ** $1"
fi
fi
;;
esac
output_on
}
print_error() {
output_off
case "$LOG_LEVEL" in
"DEBUG" | "NOTICE" | "WARN" | "ERROR")
if [ "${DEBUG_MODE,,}" = "true" ] ; then
if [ "${COLORIZE_OUTPUT,,}" = "false" ] ; then
echo -e "[ERROR] $SCRIPTPATH/$(basename "$0") ** $1"
else
echo -e "${blr}[ERROR]${boff} $SCRIPTPATH/$(basename "$0") ** $1"
fi
else
if [ "${COLORIZE_OUTPUT,,}" = "false" ] ; then
echo -e "[ERROR] ** $1"
else
echo -e "${blr}[ERROR]${boff} ** $1"
fi
fi
;;
esac
output_on
}
print_info() {
output_off
if [ "${DEBUG_MODE,,}" = "true" ] ; then
if [ "${COLORIZE_OUTPUT,,}" = "false" ] ; then
echo -e "[INFO] $SCRIPTPATH/$(basename "$0") ** $1"
else
echo -e "${bdg}[INFO]${boff} $SCRIPTPATH/$(basename "$0") ** $1"
fi
else
if [ "${COLORIZE_OUTPUT,,}" = "false" ] ; then
echo -e "[INFO] ** $1"
else
echo -e "${bdg}[INFO]${boff} ** $1"
fi
fi
output_on
}
print_notice() {
output_off
case "$LOG_LEVEL" in
"DEBUG" | "NOTICE" )
if [ "${DEBUG_MODE,,}" = "true" ] ; then
if [ "${COLORIZE_OUTPUT,,}" = "false" ] ; then
echo -e "[NOTICE] $SCRIPTPATH/$(basename "$0") ** $1"
else
echo -e "${bdgy}[NOTICE]${boff} $SCRIPTPATH/$(basename "$0") ** $1"
fi
else
if [ "${COLORIZE_OUTPUT,,}" = "false" ] ; then
echo -e "[NOTICE] ** $1"
else
echo -e "${bdgy}[NOTICE]${boff} ** $1"
fi
fi
;;
esac
output_on
}
print_warn() {
output_off
case "$LOG_LEVEL" in
"DEBUG" | "NOTICE" | "WARN" )
if [ "${DEBUG_MODE,,}" = "true" ] ; then
if [ "${COLORIZE_OUTPUT,,}" = "false" ] ; then
echo -e "[WARN] ** $1"
else
echo -e "${bdb}[WARN]${boff} $SCRIPTPATH/$(basename "$0") ** $1"
fi
else
if [ "${COLORIZE_OUTPUT,,}" = "false" ] ; then
echo -e "[WARN] ** $1"
else
echo -e "${bdb}[WARN]${boff} ** $1"
fi
fi
esac
output_on
}
## Quiet down output
silent() {
if [ "${SHOW_OUTPUT,,}" = "true" ] ; then
"$@"
else
"$@" > /dev/null 2>&1
fi
}
valid_ip() {
ip=$(getent ahosts "${1}" | grep STREAM | sed "/:/d" | awk '{print $1}')
stat=1
if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
OIFS=$IFS
IFS='.'
ip=($ip)
IFS=$OIFS
[[ ${ip[0]} -le 255 && ${ip[1]} -le 255 \
&& ${ip[2]} -le 255 && ${ip[3]} -le 255 ]]
stat=$?
fi
return $stat
}
var_false() {
[ "${1,,}" = "false" ] || [ "${1,,}" = "no" ]
}
var_notfalse() {
[ "${1,,}" != "false" ]
}
var_nottrue() {
[ "${1,,}" != "true" ]
}
var_true() {
[ "${1,,}" = "true" ] || [ "${1,,}" = "yes" ]
}
wait_for_keypress() {
read -n 1 -s -r -p "** Press any key to continue **"
}
#########################################
check_dependencies() {
os=$(cat /etc/os-release |grep ^ID= | cut -d = -f 2)
#set +e
case "${1}" in
git )
if ! type "git" > /dev/null 2>&1 ; then
print_warn "[check_dependencies] git not found, installing.."
case $os in
"debian" )
debian_version=$(cat /etc/os-release |grep ^VERSION_ID= | cut -d = -f 2 | cut -d '"' -f 2)
case $debian_version in
*)
print_debug "[check_dependencies] Debian ${debian_version} detected"
silent sudo apt-get update
silent sudo apt-get install -y git
;;
esac
;;
"arch" )
sed -i "s|SigLevel .* = Required DatabaseOptional|SigLevel = Never|g" /etc/pacman.conf
silent sudo pacman -Syy git --noconfirm
;;
esac
fi
;;
nix )
if ! type "nix" > /dev/null 2>&1 ; then
print_warn "[check_dependencies] nix not found, installing.."
case $os in
"debian" )
debian_version=$(cat /etc/os-release |grep ^VERSION_ID= | cut -d = -f 2 | cut -d '"' -f 2)
case $debian_version in
*)
print_debug "[check_dependencies] Debian ${debian_version} detected"
silent sudo sudo install -d -m755 -o "$(id -u)" -g "$(id -g)" /nix
silent curl -L https://nixos.org/nix/install | sh
source $HOME/.nix-profile/etc/profile.d/nix.sh
## TODO - Rootless Install https://nixos.wiki/wiki/Nix_Installation_Guide
;;
esac
;;
"arch" )
sed -i "s|SigLevel .* = Required DatabaseOptional|SigLevel = Never|g" /etc/pacman.conf
silent sudo pacman -Syy nix --noconfirm
;;
esac
fi
;;
ssh-to-age )
if ! type "ssh-to-age" > /dev/null 2>&1 ; then
print_error "[check_dependencies] ssh-to-age not found"
case $os in
"debian" )
debian_version=$(cat /etc/os-release |grep ^VERSION_ID= | cut -d = -f 2 | cut -d '"' -f 2)
case $debian_version in
*)
print_debug "[check_dependencies] Debian ${debian_version} detected"
exit 99
;;
esac
;;
"arch" )
sed -i "s|SigLevel .* = Required DatabaseOptional|SigLevel = Never|g" /etc/pacman.conf
#silent sudo pacman -Syy nix --noconfirm
exit 99
;;
esac
fi
;;
yq )
if ! type "yq" > /dev/null 2>&1 ; then
print_error "[check_dependencies] yq 4.xx not found"
case $os in
"debian" )
debian_version=$(cat /etc/os-release |grep ^VERSION_ID= | cut -d = -f 2 | cut -d '"' -f 2)
case $debian_version in
*)
print_debug "[check_dependencies] Debian ${debian_version} detected"
exit 99
;;
esac
;;
"arch" )
sed -i "s|SigLevel .* = Required DatabaseOptional|SigLevel = Never|g" /etc/pacman.conf
#silent sudo pacman -Syy nix --noconfirm
exit 99
;;
esac
fi
;;
esac
#set -e
}
check_for_repository() {
_dir_original=$(pwd)
if [ -f "flake.nix" ]; then
_dir_flake=$(pwd)
else
while [ ! -f "${_dir_flake}flake.nix" ] ; do
print_info "flake.nix not found! I need to work with in a repository with Nix Flakes"
COLUMNS=12
prompt="Where is your NixOS repository?"
options=( "Local Filesystem" "Git Repository" )
PS3="$prompt "
select opt in "${options[@]}" "Back" ; do
if (( REPLY == 1 + ${#options[@]} )) ; then
break
elif (( REPLY > 0 && REPLY <= ${#options[@]} )) ; then
break
else
echo "Invalid option. Try another one."
fi
done
r_repository_location=${opt}
case "${r_repository_location}" in
"Git Repository" )
check_dependencies git
counter=1
q_git_repository=" "
while [[ $q_git_repository = *" "* ]]; do
if [ $counter -gt 1 ] ; then print_error "Git Repositories cannot have spaces in them" ; fi ;
read -e -p "$(echo -e ${clg}** ${cdgy}Enter the location of your Git Repository:\ ${coff})" q_git_repository
(( counter+=1 ))
done
_dir_flake=$(mktemp -d)
print_info "Cloning Git repository to '${_dir_flake}'"
git clone ${q_git_repository} "${_dir_flake}"
git_clone_exit_code=$?
;;
"Local Filesystem" )
counter=1
q_git_repository=" "
while [[ $q_git_repository = *" "* ]]; do
if [ $counter -gt 1 ] ; then print_error "Git Repositories path cannot have spaces in them" ; fi ;
read -e -p "$(echo -e ${clg}** ${cdgy}Enter the location of your on your filesystem:\ ${coff})" q_git_repository
(( counter+=1 ))
done
_dir_flake=${q_git_repository}/
;;
esac
done
fi
}
check_host_availability() {
getent ahosts ${1} | grep STREAM | sed "/:/d" | awk '{print $1}' > /dev/null
host_available_exit=$?
remote_ip_tmp=$(getent ahosts ${1} | grep STREAM | sed "/:/d" | awk '{print $1}')
if [ "${host_available_exit}" = 0 ]; then
REMOTE_IP=${remote_ip_tmp}
else
print_warn "Couldn't resolve hostname, please enter in a new hostname or ip address"
task_set_ip_address
fi
}
cleanup() {
if [ -d "${_dir_remote_rootfs}" ]; then rm -rf "${_dir_remote_rootfs}"; fi
if [ -f "${_template_chooser}" ]; then rm -rf "${_template_chooser}"; fi
if [ -f "${luks_key}" ]; then rm -rf "${luks_key}"; fi
}
ctrl_c() {
cleanup
exit
}
flake_tools() {
check_dependencies nix
case "${1}" in
"edit" )
$EDITOR "${_dir_flake}"/flake.nix
;;
"update" )
print_info "Updating Nix Flake"
#sudo nix flake update "${_dir_flake}"/ --extra-experimental-features "nix-command flakes"
sudo nix flake update "${_dir_flake}"/
;;
"upgrade" )
print_info "Upgrading System"
sudo nixos-rebuild switch --flake "${_dir_flake}"/#"$(hostname)"
;;
esac
}
menu_diskconfig() {
if var_true "${disk_encryption}"; then
m_deploy_password="${cdgy}(${cwh}P${cdgy}) Update Encryption Password\\n"
fi
printf "\033c"
echo -e "${clm}"
cat << EOF
---------------------------
| Disk Configuration Menu |
---------------------------
You can change settings related to your disk configuration for new installs here.
You also have the capabilities of editing the hosts disk configuration
EOF
echo -e "${coff}"
read -p "$(echo -e ${cwh}CHANGE:${cdgy}\\n\\n\(${cwh}D${cdgy}\) Disk Template: ${deploy_disk_template}\\n${m_deploy_password}\(${cwh}S${cdgy}\) Swap Size \\n\\n${cwh}EDIT:${cdgy}\\n\\n\(${cwh}E${cdgy}\) Disk Template \\n${cdgy}\(${cwh}B${cdgy}\) Back to deploy menu\\n\\n${clg}** ${cdgy}What do you want to do\? : \ )" q_menu_host
case "${q_menu_host,,}" in
"d" | "disk" )
task_q_select_disktemplate
if [ -z "${PASSWORD_ENCRYPTION}" ] ; then task_generate_encryption_password ; fi
menu_diskconfig
;;
"e" | "edit" )
if [ -f "${_dir_flake}"/hosts/"${deploy_host}"/disks.nix ] ; then
$EDITOR "${_dir_flake}"/hosts/"${deploy_host}"/disks.nix
else
print_error "Can't edit Disk Template as it hasn't been selected yet"
sleep 5
fi
menu_diskconfig
;;
"p" | "password" )
task_generate_encryption_password
menu_diskconfig
;;
"s" | "swap" )
task_update_swap_size
menu_diskconfig
;;
"b" | "back" )
menu_deploy
;;
"q" | "exit" )
print_info "Quitting Script"
cleanup
;;
"?" | "help" )
echo -e "${clm}"
echo -e ""
;;
* )
menu_host
;;
esac
}
menu_execute_options() {
printf "\033c"
echo -e "${clm}"
cat << EOF
---------------------------
| Hosts Execution Options |
---------------------------
Build configuration locally and send remotely, or build on remote host
Reboot remote host after install
Set Allow more debug verbosity
EOF
echo -e "${coff}"
read -p "$(echo -e ${cdgy}\(${cwh}L${cdgy}\) Local Build: ${INSTALL_BUILD_LOCAL^}\\n\(${cwh}R${cdgy}\) Reboot after installation: ${INSTALL_REBOOT^}\\n\(${cwh}D${cdgy}\) Debug Installation: ${INSTALL_DEBUG^}\\n\\n\(${cwh}B${cdgy}\) Back to main menu\\n\\n${clg}** ${cdgy}What do you want to do\? : \ )" q_menu_hostexecution
case "${q_menu_hostexecution,,}" in
"l" | "build" )
if var_true "${INSTALL_BUILD_LOCAL}" ; then
INSTALL_BUILD_LOCAL=FALSE
else
INSTALL_BUILD_LOCAL=TRUE
fi
menu_execute_options
;;
"r" | "reboot" )
if var_true "${INSTALL_REBOOT}" ; then
INSTALL_REBOOT=FALSE
else
INSTALL_REBOOT=TRUE
fi
menu_execute_options
;;
"d" | "debug" )
if var_true "${INSTALL_DEBUG}" ; then
INSTALL_DEBUG=FALSE
else
INSTALL_DEBUG=TRUE
fi
menu_execute_options
;;
"b" | "back" )
menu_host
;;
"q" | "exit" )
print_info "Quitting Script"
cleanup
;;
"?" | "help" )
echo -e "${clm}"
echo -e ""
;;
* )
menu_host
;;
esac
}
menu_flaketools() {
if [ "${os}" = "nixos" ] ; then
option_upgrade="(${cwh}S${cdgy}) Upgrade running NixOS system\\n"
intro_upgrade="As this is a NixOS system, you can also use upgrade the running system from this menu."
fi
printf "\033c"
echo -e "${clm}"
cat << EOF
---------------
| Flake Tools |
---------------
Use this section to adjust configuration in your flake.nix file. You can then update its flake.lock file.
${intro_upgrade}
EOF
echo -e "${coff}"
read -p "$(echo -e ${cdgy}\\n\(${cwh}E${cdgy}\) Edit flake.nix\\n\(${cwh}U${cdgy}\) Update flake.lock\\n${option_upgrade}\\n${cwh}${coff}\\n${cdgy}\(${cwh}B${cdgy}\) Back to main menu\\n\\n${clg}** ${cdgy}What do you want to do\? : \ )" q_menu_flaketools
case "${q_menu_flaketools,,}" in
"e" | "edit" )
flake_tools edit
menu_flaketools
;;
"u" | "update" )
flake_tools update
menu_flaketools
;;
"s" | "system" )
if [ "${os}" = "nixos" ] ; then
flake_tools upgrade
menu_flaketools
else
print_error "Can't upgrade a non NixOS system"
menu_flaketools
fi
;;
"b" | "back" )
menu_startup
;;
"q" | "exit" )
print_info "Quitting Script"
cleanup
;;
"?" | "help" )
echo -e "${clm}"
echo -e ""
menu_flaketools
;;
* )
menu_flaketools
;;
esac
}
menu_host() {
if [ -f "${_dir_flake}"/hosts/"${deploy_host}"/secrets/secrets.yaml ] ; then
option_secrets="(${cwh}S${cdgy}) Host secrets.yaml \n"
fi
if [ "${REMOTE_IP}" != "" ]; then
menu_host_option_deploy="\\n${cwh}DEPLOY:${cdgy}\\n\\n${cdgy}(${cwh}N${cdgy}) New Installation\\n(${cwh}U${cdgy}) Update Existing Installation\\n"
fi
printf "\033c"
echo -e "${clm}"
cat << EOF
-------------
| Host Menu |
-------------
Fill in details of the IP Addresss - It may be auto populated if found in DNS, otherwise enter Hostname or IP
Change any settings for SSH, specifically the username, and make sure that you can support passwordless logins to the host in question.
Once ready, deploy the configuration.
You can also update the hosts configuration and you'll need to ensure that secrets have been generated.
EOF
echo -e "${coff}"
read -p "$(echo -e ${cdgy}Host: ${cwh}${deploy_host}${cdgy}\\n\\n${cwh}CHANGE:${cdgy}\\n\\n\(${cwh}I${cdgy}\) IP Address: ${cwh}${REMOTE_IP}${cdgy}\\n${cdgy}\(${cwh}R${cdgy}\) SSH Options\\n${menu_host_option_deploy}\\n${cwh}EDIT:${cdgy}\\n\\n\(${cwh}E${cdgy}\) Host Configuration \\n\(${cwh}F${cdgy}\) Flake \\n${option_secrets}${cwh}${coff}\\n${cdgy}\(${cwh}S${cdgy}\) Host Secrets\\n${cdgy}\(${cwh}X${cdgy}\) Remote Options\\n${cdgy}\(${cwh}B${cdgy}\) Back to host management menu\\n\\n${clg}** ${cdgy}What do you want to do\? : \ )" q_menu_host
case "${q_menu_host,,}" in
"i" | "ip" )
unset REMOTE_IP
task_set_ip_address
check_host_availability "${REMOTE_IP}"
menu_host
;;
"n" | "new" )
printf "\033c"
echo -e "${clm}"
cat << EOF
--------------------
| New Installation |
--------------------
Before the installation the following needs to occur:
- Setup Disk Drives
- Set Encryption passwords if necessary
- Ask for Network Card MAC Address if necessary
- Generate SSH Key
- Generate SOPS Secrets
- Generate sample host secret
EOF
task_parse_disk_config
if [ -z "${DISK_SWAP_SIZE_GB}" ] ; then task_update_swap_size; fi
if [ -z "${PASSWORD_ENCRYPTION}" ] ; then task_generate_encryption_password ; fi
if var_nottrue "${task_generate_ssh_key_new}" ; then task_generate_ssh_key new ;fi
if var_nottrue "${task_generate_age_secrets_new}" ; then task_generate_age_secrets new ; fi
if var_nottrue "${task_generate_sops_configuration_new}" ; then task_generate_sops_configuration new ; fi
if var_nottrue "${task_generate_host_secrets_new}" ; then task_generate_host_secrets new; fi
secret_rekey_silent=true
secret_tools rekey all
secret_rekey_silent=false
task_install_host
menu_host
;;
"u" | "update" )
task_update_host
menu_host
;;
"d" | "disk" )
menu_diskconfig
;;
"e" | "edit" )
$EDITOR "${_dir_flake}"/hosts/"${deploy_host}"/default.nix
menu_host
;;
"f" | "flake" )
$EDITOR "${_dir_flake}"/flake.nix
menu_host
;;
"s" | "secrets" )
menu_host_secrets
;;
"r" | "ssh" )
menu_ssh_options
;;
"x" | "xecute options" )
menu_execute_options
;;
"b" | "back" )
menu_host_management
;;
"q" | "exit" )
print_info "Quitting Script"
cleanup
;;
"?" | "help" )
echo -e "${clm}"
echo -e "${cwh}Host${cdgy} - Change host"
echo -e ""
echo -e "${cwh}IP Address${cdgy} - Change IP address of remote host "
echo -e ""
;;
* )
menu_host
;;
esac
}
menu_host_management() {
printf "\033c"
echo -e "${clm}"
cat << EOF
-------------------------
| Hosts Management Menu |
-------------------------
Choose a host to perform work on
Create a new template for a new never before installed host
Delete a host from filesystem
EOF
echo -e "${coff}"
read -p "$(echo -e ${cdgy}\(${cwh}H${cdgy}\) Choose Host to configure\\n\\n\(${cwh}C${cdgy}\) Create new host configuration\\n\(${cwh}D${cdgy}\) Delete host configuration\\n\\n\(${cwh}F${cdgy}\) Edit Flake \\n\(${cwh}B${cdgy}\) Back to main menu\\n\\n${clg}** ${cdgy}What do you want to do\? : \ )" q_menu_hostmanagement
case "${q_menu_hostmanagement,,}" in
"c" | "create" )
task_hostmanagement_create
menu_host
;;
"d" | "delete" )
task_hostmanagement_delete
menu_host_management
;;
"h" | "host" )
menu_host_management_select_host
menu_host
;;
"f" | "flake" )
$EDITOR "${_dir_flake}"/flake.nix
menu_host_management
;;
"b" | "back" )
menu_startup
;;
"q" | "exit" )
print_info "Quitting Script"
cleanup
;;
"?" | "help" )
echo -e "${clm}"
echo -e ""
;;
* )
menu_host
;;
esac
}
menu_host_management_select_host() {
COLUMNS=12
echo -e "${cwh}"
prompt="Which host do you want to target?"
options=( $(find ${_dir_flake}/hosts/* -maxdepth 0 -type d | rev | cut -d / -f 1 | rev | sed "/common/d" | xargs -0) )
PS3="$prompt "
select opt in "${options[@]}" "Back" ; do
if (( REPLY == 1 + ${#options[@]} )) ; then
menu_host
elif (( REPLY > 0 && REPLY <= ${#options[@]} )) ; then
break
else
echo "Invalid option. Try another one."
fi
done
echo -e "${cdgy}"
COLUMNS=$oldcolumns
export deploy_host=${opt}
if grep -q "hostname = \".*\"" "${_dir_flake}"/hosts/"${deploy_host}"/default.nix ; then
hname=$(grep "hostname = \".*\"" "${_dir_flake}"/hosts/"${deploy_host}"/default.nix | cut -d '"' -f 2)
if grep -q "domainname = \".*\"" "${_dir_flake}"/hosts/"${deploy_host}"/default.nix ; then
dname=".$(grep "domainname = \".*\"" "${_dir_flake}"/hosts/"${deploy_host}"/default.nix | cut -d '"' -f 2)"
elif grep -q "domainname = .*\".*\"" "${_dir_flake}"/hosts/common/default.nix ; then
dname=".$(grep "domainname = .*\".*\"" "${_dir_flake}"/hosts/common/default.nix | cut -d '"' -f 2)"
fi
if [ -n "${hname}" ]; then check_host_availability ${hname}${dname}; fi
fi
}
menu_host_secrets() {
if [ -f "${_dir_flake}"/hosts/"${deploy_host}"/secrets/secrets.yaml ]; then
menu_host_secrets_option_host_secrets="${cdgy}(${cwh}H${cdgy}) Host secrets management\\n"
fi
printf "\033c"
echo -e "${clm}"
cat << EOF
----------------------
| Secrets Management |
----------------------
In this first release, we are doing manual edits to files.
- Edit Global SOPS secrets configuration here.
- Edit Host Secrets
- Rekey existing secrets after adding any new keys or configurations.
EOF
echo -e "${coff}"
read -p "$(echo -e ${menu_host_secrets_option_host_secrets}${cdgy}\(${cwh}G${cdgy}\) Global secrets management\\n\(${cwh}R${cdgy}\) Rekey all secrets\\n${cwh}${coff}\\n${cdgy}\(${cwh}B${cdgy}\) Back to host menu\\n\\n${clg}** ${cdgy}What do you want to do\? : \ )" q_menu_host_secrets
case "${q_menu_host_secrets,,}" in
"g" | "global" )
menu_host_secrets_global
menu_host_secrets
;;
"h" | "host" )
if [ -f "${_dir_flake}"/hosts/"${deploy_host}"/secrets/secrets.yaml ]; then
menu_host_secrets_host
fi
menu_host_secrets
;;
"r" | "rekey" )
secret_tools rekey all
menu_host_secrets
;;
"b" | "back" )
menu_host
;;
"q" | "exit" )
print_info "Quitting Script"
cleanup
;;
"?" | "help" )
echo -e "${clm}"
echo -e "${cwh}Global${cdgy} - Opens the menu detailing changes to global secrets"
echo -e ""
echo -e "${cwh}Hosts${cdgy} - Opens the menu detailing changes to host secrets"
echo -e ""
echo -e "${cwh}Rekey${cdgy} - Rekeys all secrets from hosts, common, users"
echo -e ""
;;
* )
menu_host_secrets
;;
esac
}
menu_host_secrets_global() {
printf "\033c"
echo -e "${clm}"
cat << EOF
---------------------
| Secrets Additions |
---------------------
Global configuration is required to use secrets.
When you choose a new installation moficiations are performed to the .sops.yaml file.
If you do not see entries related to the host, choose to apply the configuration - Only do this once!
These entries must be in the .sops.yaml file be done otherwise you won't be able to login!
Type 'help' to manually add the entries.
EOF
echo -e "${coff}"
read -p "$(echo -e ${cdgy}\(${cwh}A${cdgy}\) Apply auto modifications to .sops.yaml\\n\(${cwh}E${cdgy}\) Edit .sops.yaml\\n${cwh}${coff}\\n${cdgy}\(${cwh}B${cdgy}\) Back to host secrets menu\\n\\n${clg}** ${cdgy}What do you want to do\? : \ )" q_menu_host_secrets_global
case "${q_menu_host_secrets_global,,}" in
"a" | "apply" )
task_generate_sops_configuration
;;
"e" | "edit" )
$EDITOR "${_dir_flake}"/.sops.yaml
menu_host_secrets_global
;;
"b" | "back" )
menu_host_secrets
;;
"q" | "exit" )
print_info "Quitting Script"
cleanup
;;
"?" | "help" )
echo -e "${clm}"
echo -e "${cwh}Edit${cdgy} - Edit the secrets file"
echo -e ""
cat <<EOF
In this first release, we are doing manual edits to files.
GLOBAL SECRETS
You must add the following to the global file:
- Copy this line and place it underneath the 'keys:' section
###
- &host_${deploy_host} ${_age_key_pub}
###
- Make sure that under the creation rules that it says something like this:
###
- path_regex: hosts/common/secrets/.*
key_groups:
- age:
- *host_${deploy_host}
- *host_host1
- *host_host2
- *host_host3
- *user_$(whoami)
- path_regex: users/secrets.yaml
key_groups:
- age:
- *host_${deploy_host}
- *host_host1
- *host_host2
- *host_host3
- *user_$(whoami)
Finally - Make sure there is a section for the host defined that looks similar to this:
- path_regex: hosts/soy/secrets/.*
key_groups:
- age:
- *host_${deploy_host}
- *user_$(whoami)
###
EOF
;;
* )
menu_host_secrets_global
;;
esac
}
menu_host_secrets_host() {
printf "\033c"
echo -e "${clm}"
cat << EOF
--------------------------
| Host Secrets Additions |
--------------------------
HOST SECRETS
Before you can do anything you'll need to crete an example secret.
Create an example secret. Delete everything in the file and replace it with the following line:
${deploy_host}: Example secret for ${deploy_host}
You can also add other secrets but the secret with starting with ${deploy_host} must exist.
EOF
echo -e "${coff}"
read -p "$(echo -e \\n${cdgy}\(${cwh}E${cdgy}\) Edit ${deploy_host}\/secrets\/secrets.yaml\\n${cwh}${coff}\\n${cdgy}\(${cwh}B${cdgy}\) Back to host secrets menu\\n\\n${clg}** ${cdgy}What do you want to do\? : \ )" q_menu_secrets_host
case "${q_menu_secrets_host,,}" in
"e" | "edit" )
task_generate_host_secrets
menu_host_secrets_host
;;
"b" | "back" )
menu_host_secrets
;;
"q" | "exit" )
print_info "Quitting Script"
cleanup
;;
"?" | "help" )
echo -e "${clm}"
echo -e "${cwh}Edit${cdgy} - Edit the secrets file"
echo -e ""
;;
* )
menu_host_secrets_host
;;
esac
}
menu_install_host() {
printf "\033c"
echo -e "${clm}"
cat << EOF
----------------
| Install Host |