various improvements
[automated-distro-installer] / pxe-server
index 4d18ccb742dcad33e9d9691372a0e711e4ac4ef8..4fcf40c134cd472d3ba50aaa26687298846c5da1 100755 (executable)
@@ -3,37 +3,81 @@
 # Setup dhcp server to point to tftp server,
 # and depending on the type, setup the tftp server.
 
-# usage: $0 [TYPE]
-# default distro is the base debian/fedora type. others are fai &  arch
+# usage: $0 TYPE
+# default distro is the base debian/fedora type. others are fai & arch.
+# for no pxe server, use a no-op like : or true.
 
 set -eE -o pipefail
-trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?"' ERR
+trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
 
-action=${1:-default}
+
+usage() {
+    cat <<EOF
+Usage: ${0##*/} [OPTIONS] TYPE [HOST]
+One line description
+
+TYPE is one of arch, plain, fai, or : for no pxe server.
+HOST makes the pxe server only for that specific host
+
+-h|--help  Print help and exit
+--         Subsequent arguments are never treated as options
+EOF
+    exit $1
+}
+
+##### begin command line parsing ########
+
+
+args=()
+while [[ $1 ]]; do
+    case $1 in
+        --) shift; break ;;
+        -h|--help) usage ;;
+        *) args+=("$1"); shift ;;
+    esac
+done
+args+=("$@")
+
+
+read type host <<<"${args[@]}"
+
+if [[ ! $type ]]; then
+    echo "$0: error: exptected 1 argument of type"
+    usage 1
+fi
+
+if [[ $host ]]; then
+    host_tag="tag:$host,"
+fi
+
+##### end command line parsing ########
 
 arch() {
-    default
-    cat <<'EOF'
+    plain
+    cat <<EOF
 dhcp-option-force=209,boot/syslinux/archiso.cfg
 dhcp-option-force=210,/arch/
-dhcp-boot=/arch/boot/syslinux/lpxelinux.0
+dhcp-boot=${host_tag}/arch/boot/syslinux/lpxelinux.0
 EOF
 }
 
-default() {
-    cat <<'EOF'
+plain() {
+    # if arch was used before, this additionally needs
+    # the tftp link in /mnt/usb to be changed.
+    cat <<EOF
 enable-tftp
 tftp-root=/mnt/usb/tftpboot
-dhcp-boot=pxelinux.0
+dhcp-boot=${host_tag}pxelinux.0
 EOF
 }
 
 fai() {
-    cat <<'EOF'
-dhcp-boot=fai/pxelinux.0,faiserver.lan,faiserver.lan
+    cat <<EOF
+$set_host_tag
+dhcp-boot=${host_tag}fai/pxelinux.0,faiserver.lan,faiserver.lan
 EOF
 }
 
 
-$action | ssh wrt "cedit pxe-server /etc/dnsmasq.conf || /etc/init.d/dnsmasq restart
-if [[ $action == arch ]]; then arch-pxe-mount; fi"
+$type | ssh wrt "cedit pxe-server /etc/dnsmasq.conf || /etc/init.d/dnsmasq restart
+if [[ $type == arch ]]; then arch-pxe-mount; fi"