cleanup docs, getopt arg parsing
[automated-distro-installer] / pxe-server
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
18 # Setup dhcp server to point to tftp server,
19 # and depending on the type, setup the tftp server.
20
21 # usage: $0 TYPE
22 # default distro is the base debian/fedora type. others are fai & arch.
23 # for no pxe server, use a no-op like : or true.
24
25 set -eE -o pipefail
26 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
27
28
29 usage() {
30 cat <<EOF
31 Usage: ${0##*/} [OPTIONS] TYPE [HOST]
32 One line description
33
34 TYPE is one of arch, plain, fai, or : for no pxe server.
35 HOST makes the pxe server only for that specific host
36
37 -r Don't redeploy fai config.
38 -a Wait for 2 dhcp acks, then disable the pxe server after a delay.
39 First ack is for pxe boot, 2nd ack is for os boot. Sometimes
40 on debian, there is a 3rd one shortly after the 2nd. I can't remember
41 exactly why this caused a problem, but I'm hoping the sleep
42 will take care of it.
43 -h|--help Print help and exit
44
45
46 Note: Uses GNU getopt options parsing style
47 EOF
48 exit $1
49 }
50
51 ##### begin command line parsing ########
52
53 args=()
54 redep=true
55 acks=2
56 wait=false
57 temp=$(getopt -l help hra "$@") || usage 1
58 eval set -- "$temp"
59 while true; do
60 case $1 in
61 -h|--help) usage ;;
62 -r) redep=false; shift ;;
63 -a) wait=true; shift ;;
64 --) shift; break ;;
65 *) echo "$0: Internal error!" ; exit 1 ;;
66 esac
67 done
68
69 read type host <<<"$@"
70
71 if [[ ! $type ]]; then
72 echo "$0: error: exptected 1 argument of type"
73 usage 1
74 fi
75
76 if [[ $host ]]; then
77 host_tag="tag:$host,"
78 fi
79
80 ##### end command line parsing ########
81
82 e() {
83 echo "$@"
84 "$@"
85 }
86
87 arch() {
88 cat <<EOF
89 dhcp-option-force=209,boot/syslinux/archiso.cfg
90 dhcp-option-force=210,/arch/
91 dhcp-boot=${host_tag}/arch/boot/syslinux/lpxelinux.0
92 EOF
93 }
94
95 plain() {
96 # if arch was used before, this additionally needs
97 # the tftp link in /mnt/usb to be changed.
98 cat <<EOF
99 dhcp-boot=${host_tag}pxelinux.0
100 EOF
101 }
102
103 fai() {
104 cat <<EOF
105 $set_host_tag
106 dhcp-boot=${host_tag}fai/pxelinux.0,faiserver.lan,faiserver.lan
107 EOF
108 }
109
110 ack-wait() {
111 wait_count=$1
112 if [[ $host ]]; then
113 host_regex=" $host"
114 fi
115 regex=".*DHCPACK.*$host_regex$"
116 i=0
117 tmp=$(mktemp)
118 while (( i != wait_count )) && read line; do
119 if [[ $line =~ $regex ]]; then
120 i=$((i+1))
121 echo $line
122 fi
123 done < <(ssh wrt logread -f)
124 e sleep 20
125 }
126
127 set-pxe() {
128 ${1:-$type} | ssh wrt "cedit pxe-server /etc/dnsmasq.conf || /etc/init.d/dnsmasq restart
129 if [[ $type == arch ]]; then arch-pxe-mount; fi"
130 }
131
132 if ! $wait && [[ $type != : ]]; then
133 set-pxe
134
135 if [[ $type == fai ]]; then
136 if $redep; then
137 fai-redep
138 fi
139 faiserver-enable
140 fi
141 fi
142
143 if $wait; then
144 # fai's debian jessie 8.5ish does 2 dhcp requests when booting,
145 # roughly 4 seconds apart. Earlier
146 # versions did just 1. Now testing on a vm, it does 1.
147 # bleh.
148 echo "waiting for $acks dhcp acks then disabling pxe"
149 ack-wait $acks
150 set-pxe :
151
152 # previously tried waiting for one more ack then disabling faiserver,
153 # since it can contain sensitive info, so turn it off when not in use,
154 # but disabling that for now as it's inconvenient to clean this
155 # up and run it in the background etc.
156
157 # if [[ $type == fai ]]; then
158 # echo "waiting for 1 dhcp ack then disabling fai server"
159 # ack-wait 1
160 # faiserver-disable
161 # fi
162 fi