update webserver setup dependency
[automated-distro-installer] / myfai-chboot-local
1 #!/bin/bash
2
3 set -eE -o pipefail
4 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
5
6 case $1 in
7 -h|--help)
8 echo "see help from myfai-chboot"
9 exit 0
10 ;;
11 esac
12
13 [[ $EUID == 0 ]] || exec sudo "${BASH_SOURCE}" "$@"
14
15 e() { echo "$@"; "$@"; }
16
17 host=$1
18
19 type -t host &>/dev/null || apt-get -y install dnsutils
20 ip=$(host $host | sed -rn 's/^\S+ has address //p;T;q')
21 gateway_ip=$(route -n | sed -rn 's/^0\.0\.0\.0\s+(\S+).*/\1/p')
22 my_ip=$(host faiserver $gateway_ip | sed -rn 's/^\S+ has address //p;T;q')
23
24 if modprobe nfsd &>/dev/null; then
25 std_arg="-u nfs://faiserver/srv/fai/config"
26 root_arg="$my_ip:/srv/fai/nfsroot"
27 # fai-setup without -e sets the ip to the local_ip/local_network, eg 192.168.1.3/24
28 # I restrict it to one ip as simple but imperfect access control.
29 sed -ri --follow-symlinks '\%^/srv/fai/%d' /etc/exports
30 cat >>/etc/exports <<EOF
31 /srv/fai/config $ip/32(async,ro,no_subtree_check)
32 /srv/fai/nfsroot $ip/32(async,ro,no_subtree_check,no_root_squash)
33 EOF
34 exportfs -ra
35 else
36 std_arg="-u http://faiserver:8080/config.tar.gz"
37 root_arg="live:http://faiserver:8080/squash.img"
38 /a/exe/web-conf -i -p 8080 - apache2 faiserver <<EOF
39 <Location />
40 Deny from all
41 Allow from $ip
42 </Location>
43 EOF
44 fi
45
46 rm -f /srv/tftp/fai/pxelinux.cfg/*
47 if [[ ! $1 ]]; then
48 exit 0
49 fi
50
51
52 # man page doesn't explain this, but this deletes & thus disables
53 # all chboot systems.
54 e fai-chboot -Iv $std_arg default # set it to default to get a val out of it next
55 kernel=$(fai-chboot -L '^default$' | awk '{print $3}')
56 default_k_args=$(fai-chboot -L '^default$' | \
57 sed -r "s/^(\S+\s+){3}(.*)/\2/")
58 # example of default_k_args
59 # initrd=initrd.img-3.16.0-4-amd64 ip=dhcp root=192.168.1.3:/srv/fai/nfsroot aufs FAI_CONFIG_SRC=nfs://faiserver/srv/fai/config FAI_ACTION=install
60
61 k_args=()
62 for arg in $default_k_args; do
63 case $arg in
64 # default root arg is /srv/fai/nfsroot
65 root=*) k_args+=(root=$root_arg) ;;
66 *) k_args+=($arg) ;;
67 esac
68 done
69 rm -f /srv/tftp/fai/pxelinux.cfg/*
70 e fai-chboot -k "${k_args[*]}" -v -f verbose,sshd,createvt,reboot $std_arg $kernel "$host"