Adding a basic oob helper as an example
authorRené Nussbaumer <rn@google.com>
Tue, 18 Jan 2011 15:22:06 +0000 (16:22 +0100)
committerRené Nussbaumer <rn@google.com>
Thu, 20 Jan 2011 09:58:16 +0000 (10:58 +0100)
This is just a plain stupid and simple out-of-band helper without
anything fancy. It uses plain ssh to power off / power cycle the
machine, does not support power on. It support power status using fping
to check if the host replies.

Signed-off-by: René Nussbaumer <rn@google.com>
Reviewed-by: Michael Hanselmann <hansmi@google.com>

doc/examples/basic-oob [new file with mode: 0755]

diff --git a/doc/examples/basic-oob b/doc/examples/basic-oob
new file mode 100755 (executable)
index 0000000..8e456fd
--- /dev/null
@@ -0,0 +1,112 @@
+#!/bin/bash
+#
+
+# Copyright (C) 2011 Google Inc.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+# 02110-1301, USA.
+
+SSH_USER='root'
+SSH_FLAGS='-q -oStrictHostKeyChecking=no'
+
+EXIT_SUCCESS=0
+EXIT_FAILURE=1
+EXIT_UNKNOWN=2
+
+_run_ssh() {
+  local host="$1"
+  local command="$2"
+
+  ssh $SSH_FLAGS "$SSH_USER@$host" "$command" 1>&2
+  return $?
+}
+
+_power_on() {
+  echo 'power-on not supported in this script' >&2
+  exit $EXIT_FAILURE
+}
+
+_power_off() {
+  local host="$1"
+
+  if ! _run_ssh "$host" 'shutdown -h now'; then
+    echo "Failure during ssh to $host" >&2
+    exit $EXIT_FAILURE
+  fi
+}
+
+_power_cycle() {
+  local host="$1"
+
+  if ! _run_ssh "$host" 'shutdown -r now'; then
+    echo "Failure during ssh to $host" >&2
+    exit $EXIT_FAILURE
+  fi
+}
+
+_power_status() {
+  local host="$1"
+
+  if fping -q "$host" > /dev/null 2>&1; then
+    echo '{ "powered": true }'
+  else
+    echo '{ "powered": false }'
+  fi
+}
+
+_health() {
+  echo 'health not supported in this script' >&2
+  exit $EXIT_FAILURE
+}
+
+_action() {
+  local command="$1"
+  local host="$2"
+
+  case "$command" in
+    power-on)
+      _power_on "$host"
+      ;;
+    power-off)
+      _power_off "$host"
+      ;;
+    power-cycle)
+      _power_cycle "$host"
+      ;;
+    power-status)
+      _power_status "$host"
+      ;;
+    health)
+      _health "$host"
+      ;;
+    *)
+      echo "Unsupported command '$command'" >&2
+      exit $EXIT_FAILURE
+      ;;
+  esac
+}
+
+main() {
+  if [[ $# != 2 ]]; then
+    echo "Wrong argument count, got $#, expected 2" >&2
+    exit $EXIT_FAILURE
+  fi
+
+  _action "$@"
+
+  exit $EXIT_SUCCESS
+}
+
+main "$@"