#!/bin/bash -l
# Copyright (C) 2016 Ian Kelling
-# This program is under GPL v. 3 or later, see <http://www.gnu.org/licenses/>
-# distro setup full. (assuming we already synced data files to the host)
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+
+# http://www.apache.org/licenses/LICENSE-2.0
+
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
set -eE -o pipefail
-trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
+cleanup() { :; }
+trap 'cleanup; echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
reboot=true
if [[ $1 == -r ]]; then
shift
fi
+usage() {
+ cat <<EOF
+Usage: ${0##*/} [OPTIONS] HOST
+distro setup full using fai. (assuming we already synced data files to the host)
+
+--no-r Don't ssh to host and reboot.
+-k ssh to host and kexec, don't use pxe. implies --no-r
+-h|--help Print help and exit.
+
+Note: Uses GNU getopt options parsing style
+EOF
+ exit $1
+}
+
+##### begin command line parsing ########
+
+reboot=true
+kexec=false
+temp=$(getopt -l help,no-r hk "$@") || usage 1
+eval set -- "$temp"
+while true; do
+ case $1 in
+ --no-r) reboot=false; shift ;;
+ -k) kexec=true; reboot=false; shift ;;
+ -h|--help) usage ;;
+ --) shift; break ;;
+ *) echo "$0: Internal error!" ; exit 1 ;;
+ esac
+done
host=$1
-if [[ ! $host || $host == -h ]]; then
+##### end command line parsing ########
+
+
+if [[ ! $host ]]; then
echo "$0: error: expected 1 arg of hostname"
exit 1
fi
-set -x
-if $reboot; then
- ssh $host sudo bash <<'EOF' || [[ $? == 255 ]]
-touch /tmp/keyscript-off
-reboot ||:
-EOF
+e() { echo "$@"; "$@"; }
+if $kexec; then
+ e fai-redep
+ e myfai-chboot $host
+ e fai-kexec $host ||:
+else
+ cleanup() { pxe-server; }
+ e pxe-server $host fai
+
+ if $reboot; then
+ # untested, this caused hang using here doc.
+ ssh $host "touch /tmp/keyscript-off; sudo reboot" ||: &
+ fi
+
+ e pxe-server -a
+ cleanup() { :; }
fi
-pxe-server fai $host
-while ! ssh $host :; do
+error=true
+for ((i=0; i<240; i++)); do
+ if timeout -s 9 10 ssh $host :; then
+ error=false
+ break
+ fi
sleep 5
done
-dsremote $host
+if $error; then
+ echo "$0: error: timeout"
+ exit 1
+fi
+e dsremote $host