various fixes
[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 DISTRO is default this distro, or DISTRO for setting up DESKTOP packages.
27 EOF
28 exit $1
29 }
30 case $1 in
31 -h|--help) usage ;;
32 esac
33
34 host=${1:-faiserver}
35
36
37 # i use faiserver as a dns alias, but ssh key is associated with
38 # a canonical hostname and we will have ssh warning spam unless we
39 # use it, so look it up just to avoid the warning spam.
40 faiserver_host=$(chost $host) || faiserver_host=$host
41
42 rsync -rlp --delete --relative --exclude /fai/config/basefiles/ fai/config root@$faiserver_host:/srv
43
44
45 scp -q ~/.ssh/home.pub \
46 root@$faiserver_host:/srv/fai/config/files/root/.ssh/authorized_keys/GRUB_PC
47 # todo: automatically disable faiserver after a period so
48 # these files are not exposed.
49 sudo scp -qr /q/root/luks /q/root/shadow \
50 root@$faiserver_host:/srv/fai/config/distro-install-common
51
52 # should tar ssh all the files, but these ones really justified it
53 tar -cz /p/c/machine_specific/*/filesystem/etc/ssh | \
54 ssh root@$faiserver_host tar -xz -C /srv/fai/config/distro-install-common
55
56
57 . /a/bin/distro-setup/pkgs
58 pall+=($(/a/bin/buildscripts/emacs -p; /a/bin/distro-setup/distro-pkgs $2))
59 { echo PACKAGES install; echo "${pall[*]}"|sed 's/ /\n/g'; } | \
60 ssh root@$faiserver_host dd of=/srv/fai/config/package_config/DESKTOP 2>/dev/null ||: # broken pipe
61
62
63 # built BELENOS basefile with mk-basefile -J BELENOS64. it's stored in
64 # it's own repo which is published alongside this one called
65 # fai-basefiles due to being a large binary file.
66
67 declare -A sums
68 while read -r sum file; do
69 sums[$file]=$sum
70 done < <(cat /a/bin/fai-basefiles/md5sums.txt)
71
72 { timeout 2 curl -s http://fai-project.org/download/basefiles/md5sums.txt ||:; } |
73 while read -r sum file; do
74 if [[ ${sums[$file]} && ${sums[$file]} != $sum ]]; then
75 echo "${0##*/}: WARNING!!!!!!!!! NEW UPSTREAM BASEFILE: $file"
76 fi
77 done
78 rsync -r --delete /a/bin/fai-basefiles/basefiles root@$faiserver_host:/srv/fai/config
79 ssh root@$faiserver_host bash <<'EOF'
80 set -eE -o pipefail
81 set -x
82 # make it the root because pxe-kexec only looks there.
83 # It wouldn't be too hard to change if we needed.
84 # We could also just dump things in /srv/tftp, but fai
85 # has some defaults, which I don't even use, which expect
86 # the other directory, so it's kind of a tossup, whatever.
87 sed -ri 's,^ *(TFTP_DIRECTORY=).*,\1"/srv/tftp/fai",' /etc/default/tftpd-hpa
88 systemctl restart tftpd-hpa
89 chmod 644 /srv/fai/config/files/root/.ssh/authorized_keys/GRUB_PC
90 chmod -R a+rX /srv/fai/config/distro-install-common
91 # this basefile has tar acls bug, so I'm using my own
92 # local one for now.
93 #cd /srv/fai/config/basefiles
94 #u=http://fai-project.org/download/basefiles/XENIAL64.tar.xz
95 #wget -nv -N $u
96
97 changed=false
98 f=/srv/fai/nfsroot/root/.ssh/known_hosts
99 # the known hosts entries that fai already sets up are like
100 # IP,HOSTNAME key_info...
101 # we are skipping the ip, because it doesn't block ssh
102 # with a prompt as long as you have the user supplied hostname,
103 # and i don't want to deal with getting it, it's not adding
104 # any important security in this case.
105 if ! grep -xFq "$line" $f; then
106 changed=true
107 printf "%s\n" "$line" >>$f
108 fi
109
110 if ! modprobe nfsd &>/dev/null; then
111 # no apt-cache on maru debian, because we are low on space already
112 sed -i '/^ *APTPROXY=/d' /srv/fai/config/class/DEBIAN.var
113 # maru debian doesn't have loopback devs created
114 if ! losetup -f; then
115 shopt -s nullglob
116 x=(/dev/loop*)
117 minor=0
118 if (( ${#x[@]} )); then
119 minor=$(( ${x[-1]#/dev/loop} + 1 ))
120 fi
121 mknod -m660 /dev/loop$minor b 7 $minor
122 losetup -f
123 fi
124 # -B boo only iso, no nfsroot, no paritial miorr, no config space.
125 # -f = force, for overwriting
126 # -S = make squash image for http booting
127 # -d config space url, instead of putting it in the squash.img,
128 # this just makes it so that we don't have to regenerate the img
129 # when the config changes.
130 cd /srv/fai/config
131 tar czf /var/www/faiserver/html/config.tar.gz .
132 if $changed || [[ ! -e /var/www/faiserver/html/squash.img ]]; then
133 # note, on maru, selinux needs to be disabled in android before
134 # this will work.
135 mount
136 export debug=true
137 fai-cd -d http://faiserver:8080/config.tar.gz -f -M -S /var/www/faiserver/html/squash.img
138 mount
139 fi
140 fi
141 EOF