5306d9736707a8b683e9fdb4da8be9a57889659f
[automated-distro-installer] / fai-redep
1 #!/bin/bash
2 # Copyright (C) 2016 Ian Kelling
3
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
8
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 set -eE -o pipefail
18 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
19
20 x="$(readlink -f "$BASH_SOURCE")"; cd ${x%/*}
21
22 usage() {
23 cat <<EOF
24 usage: ${0##*/} [-h|--help] [HOST] [DISTRO]
25 Deploy fai config (the one in nfs) to HOST or default faiserver
26 Specify DISTRO for setting up DESKTOP packages.
27
28 Note: uses paths specific to authors machine.
29 EOF
30 exit $1
31 }
32 case $1 in
33 -h|--help) usage ;;
34 esac
35
36 host=${1:-faiserver}
37 distro=$2
38
39 # i use faiserver as a dns alias, but ssh key is associated with
40 # a canonical hostname and we will have ssh warning spam unless we
41 # use it, so look it up just to avoid the warning spam.
42 faiserver_host=$(chost $host) || faiserver_host=$host
43
44 rsync -rlp --delete --relative --exclude /fai/config/basefiles/ fai/config root@$faiserver_host:/srv
45
46
47 scp -q ~/.ssh/home.pub \
48 root@$faiserver_host:/srv/fai/config/files/root/.ssh/authorized_keys/GRUB_PC
49 # todo: automatically disable faiserver after a period so
50 # these files are not exposed.
51 sudo scp -qr /q/root/luks /q/root/shadow \
52 root@$faiserver_host:/srv/fai/config/distro-install-common
53
54 # should tar ssh all the files, but these ones really justified it
55 tar -cz /p/c/machine_specific/*/filesystem/etc/ssh | \
56 ssh root@$faiserver_host tar -xz -C /srv/fai/config/distro-install-common
57
58
59 . /a/bin/distro-setup/pkgs
60 pall+=($(/a/bin/buildscripts/emacs -p; /a/bin/distro-setup/distro-pkgs $distro))
61 { echo PACKAGES install; echo "${pall[*]}"|sed 's/ /\n/g'; } | \
62 ssh root@$faiserver_host dd of=/srv/fai/config/package_config/DESKTOP 2>/dev/null ||: # broken pipe
63
64
65 rsync -rplt --delete /a/bin/fai-basefiles/basefiles root@$faiserver_host:/srv/fai/config
66 ssh root@$faiserver_host bash <<'EOF'
67 set -eE -o pipefail
68 set -x
69 # make it the root because pxe-kexec only looks there.
70 # It wouldn't be too hard to change if we needed.
71 # We could also just dump things in /srv/tftp, but fai
72 # has some defaults, which I don't even use, which expect
73 # the other directory, so it's kind of a tossup, whatever.
74 sed -ri 's,^ *(TFTP_DIRECTORY=).*,\1"/srv/tftp/fai",' /etc/default/tftpd-hpa
75 systemctl restart tftpd-hpa
76 chmod 644 /srv/fai/config/files/root/.ssh/authorized_keys/GRUB_PC
77 chmod -R a+rX /srv/fai/config/distro-install-common
78
79 changed=false
80 f=/srv/fai/nfsroot/root/.ssh/known_hosts
81 install -d -m 700 /srv/fai/nfsroot/root/.ssh
82 # the known hosts entries that fai already sets up are like
83 # IP,HOSTNAME key_info...
84 # we are skipping the ip, because it doesn't block ssh
85 # with a prompt as long as you have the user supplied hostname,
86 # and i don't want to deal with getting it, it's not adding
87 # any important security in this case.
88 if ! grep -xFq "$line" $f &>/dev/null; then
89 changed=true
90 printf "%s\n" "$line" >>$f
91 fi
92
93 if ! modprobe nfsd &>/dev/null; then
94 # no apt-cache on maru debian, because we are low on space already
95 sed -i '/^ *APTPROXY=/d' /srv/fai/config/class/DEBIAN.var
96 # maru debian doesn't have loopback devs created
97 if ! losetup -f; then
98 shopt -s nullglob
99 x=(/dev/loop*)
100 minor=0
101 if (( ${#x[@]} )); then
102 minor=$(( ${x[-1]#/dev/loop} + 1 ))
103 fi
104 mknod -m660 /dev/loop$minor b 7 $minor
105 losetup -f
106 fi
107 # -B boo only iso, no nfsroot, no paritial miorr, no config space.
108 # -f = force, for overwriting
109 # -S = make squash image for http booting
110 # -d config space url, instead of putting it in the squash.img,
111 # this just makes it so that we don't have to regenerate the img
112 # when the config changes.
113 cd /srv/fai/config
114 tar czf /var/www/faiserver/html/config.tar.gz .
115 if $changed || [[ ! -e /var/www/faiserver/html/squash.img ]]; then
116 # note, on maru, selinux needs to be disabled in android before
117 # this will work.
118 mount
119 export debug=true
120 fai-cd -d http://faiserver:8080/config.tar.gz -f -M -S /var/www/faiserver/html/squash.img
121 mount
122 fi
123 fi
124 EOF