more aramo/jammy updates
[automated-distro-installer] / fai-redep
1 #!/bin/bash
2 # Copyright (C) 2019 Ian Kelling
3 # SPDX-License-Identifier: AGPL-3.0-or-later
4 set -eE -o pipefail
5 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
6
7 [[ $EUID == 0 ]] || exec sudo -E "${BASH_SOURCE[0]}" "$@"
8
9 readonly this_file="$(readlink -f -- "${BASH_SOURCE[0]}")"; cd "${this_file%/*}"
10
11 usage() {
12 cat <<EOF
13 usage: ${0##*/} [OPTIONS] [HOST]
14 Deploy fai config (the one in nfs) to HOST or default faiserver
15
16 Note, for booting from fai-cd, this needs to be called from myfai-chboot or that via pxe-server,
17 due to setting
18 echo FAI_ACTION=$fai_action >> /srv/fai/config/class/LAST.var
19 note FAI_ACTION might be able to be set elsewhere, like in grub for this case
20
21 -d DISTRO DISTRO for setting up fai class DESKTOP packages, for preinstalling stuff.
22 -t TARGET_HOST Copy only secrets for TARGET_HOST into the config space. Useful for virtual server
23 on hardware we don't control.
24 -h|--help Print help and exit
25
26 Note: uses paths specific to authors machine.
27 EOF
28 exit $1
29 }
30
31 ##### begin command line parsing ########
32
33 # ensure we can handle args with spaces or empty.
34 ret=0; getopt -T || ret=$?
35 [[ $ret == 4 ]] || { echo "Install util-linux for enhanced getopt" >&2; exit 1; }
36
37 temp=$(getopt -l help hd:t: "$@") || usage 1
38 eval set -- "$temp"
39 while true; do
40 case $1 in
41 -d) distro=$2; shift ;;
42 -t) target=$2; shift ;;
43 -h|--help) usage ;;
44 --) shift; break ;;
45 *) echo "$0: unexpected args: $*" >&2 ; usage 1 ;;
46 esac
47 shift
48 done
49 host=${1:-faiserver}
50
51 readonly host distro target
52
53 ##### end command line parsing ########
54
55 # i use faiserver as a dns alias, but ssh key is associated with
56 # a canonical hostname and we will have ssh warning spam unless we
57 # use it, so look it up just to avoid the warning spam.
58 faiserver_host=$(/a/exe/chost $host) || faiserver_host=$host
59
60 faiserver_addr=$(host $host | sed -rn 's/^\S+ has address //p;T;q' ||:)
61 if ! ip a | grep "^ *inet.\? $faiserver_addr" &>/dev/null; then
62 rpre="-e 'ssh -F $HOME/.ssh/confighome' root@$faiserver_host:"
63 faiserver_shell="ssh -F $HOME/.ssh/confighome root@$faiserver_host"
64 fi
65
66 # these are gitignored.
67 rsync -atL /home/iank/.ssh/authorized_keys fai/config/files/root/.ssh/authorized_keys/STANDARD
68 # we hssh and ssh_filter_btrbk for the initial btrbk (alternatively, I could open up the
69 # permissions in authorized_keys, but that just seems lazy)
70 install --owner=iank --group=iank -d fai/config/files/usr/local/bin/hssh
71 rsync -atL /a/bin/ds/hssh fai/config/files/usr/local/bin/hssh/STANDARD
72 install --owner=iank --group=iank -d fai/config/files/usr/local/bin/ssh_filter_btrbk.sh
73 rsync -atL /a/opt/btrbk/ssh_filter_btrbk.sh fai/config/files/usr/local/bin/ssh_filter_btrbk.sh/STANDARD
74
75 rsync -rlpt --delete --relative --exclude /fai/config/basefiles/ fai/config $rpre/srv
76
77 # todo: automatically disable faiserver after a period so
78 # these files are not available.
79
80 if [[ $target ]]; then
81 if test -e /q/root/shadow/$target; then
82 shadowfile=shadow/$target # empty otherwise
83 fi
84 rsync -lpt --files-from=- /q/root $rpre/srv/fai/config/distro-install-common <<EOF
85 luks/$target
86 luks/host-$target
87 $shadowfile
88 EOF
89 else
90 rsync -rlpt /q/root/shadow /q/root/luks $rpre/srv/fai/config/distro-install-common
91 fi
92
93 dirs=(/p/c/machine_specific/${target:-*}/filesystem/etc/ssh)
94 if [[ -e ${dirs[0]} ]]; then
95 rsync -rlpt --delete --relative ${dirs[@]} $rpre/srv/fai/config/distro-install-common
96 fi
97
98 . /a/bin/distro-setup/pkgs
99 pall+=($(/a/bin/buildscripts/emacs -p; /a/bin/distro-setup/distro-pkgs $distro))
100
101 printf "%s\n%s\n" "PACKAGES install" ${pall[*]} | \
102 $faiserver_shell dd of=/srv/fai/config/package_config/DESKTOP 2>/dev/null ||: # broken pipe
103
104
105 rsync -rplt --include '/*.gz' --exclude '/**' --delete-excluded $BASEFILE_DIR/ $rpre/srv/fai/config/basefiles/