Statistics
| Branch: | Tag: | Revision:

root / doc / examples / basic-oob @ f7b769b1

History | View | Annotate | Download (2.2 kB)

1 d59fea3d René Nussbaumer
#!/bin/bash
2 d59fea3d René Nussbaumer
#
3 d59fea3d René Nussbaumer
4 d59fea3d René Nussbaumer
# Copyright (C) 2011 Google Inc.
5 d59fea3d René Nussbaumer
#
6 d59fea3d René Nussbaumer
# This program is free software; you can redistribute it and/or modify
7 d59fea3d René Nussbaumer
# it under the terms of the GNU General Public License as published by
8 d59fea3d René Nussbaumer
# the Free Software Foundation; either version 2 of the License, or
9 d59fea3d René Nussbaumer
# (at your option) any later version.
10 d59fea3d René Nussbaumer
#
11 d59fea3d René Nussbaumer
# This program is distributed in the hope that it will be useful, but
12 d59fea3d René Nussbaumer
# WITHOUT ANY WARRANTY; without even the implied warranty of
13 d59fea3d René Nussbaumer
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 d59fea3d René Nussbaumer
# General Public License for more details.
15 d59fea3d René Nussbaumer
#
16 d59fea3d René Nussbaumer
# You should have received a copy of the GNU General Public License
17 d59fea3d René Nussbaumer
# along with this program; if not, write to the Free Software
18 d59fea3d René Nussbaumer
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 d59fea3d René Nussbaumer
# 02110-1301, USA.
20 d59fea3d René Nussbaumer
21 d59fea3d René Nussbaumer
SSH_USER='root'
22 d59fea3d René Nussbaumer
SSH_FLAGS='-q -oStrictHostKeyChecking=no'
23 d59fea3d René Nussbaumer
24 d59fea3d René Nussbaumer
EXIT_SUCCESS=0
25 d59fea3d René Nussbaumer
EXIT_FAILURE=1
26 d59fea3d René Nussbaumer
EXIT_UNKNOWN=2
27 d59fea3d René Nussbaumer
28 d59fea3d René Nussbaumer
_run_ssh() {
29 d59fea3d René Nussbaumer
  local host="$1"
30 d59fea3d René Nussbaumer
  local command="$2"
31 d59fea3d René Nussbaumer
32 d59fea3d René Nussbaumer
  ssh $SSH_FLAGS "$SSH_USER@$host" "$command" 1>&2
33 d59fea3d René Nussbaumer
  return $?
34 d59fea3d René Nussbaumer
}
35 d59fea3d René Nussbaumer
36 d59fea3d René Nussbaumer
_power_on() {
37 d59fea3d René Nussbaumer
  echo 'power-on not supported in this script' >&2
38 d59fea3d René Nussbaumer
  exit $EXIT_FAILURE
39 d59fea3d René Nussbaumer
}
40 d59fea3d René Nussbaumer
41 d59fea3d René Nussbaumer
_power_off() {
42 d59fea3d René Nussbaumer
  local host="$1"
43 d59fea3d René Nussbaumer
44 d59fea3d René Nussbaumer
  if ! _run_ssh "$host" 'shutdown -h now'; then
45 d59fea3d René Nussbaumer
    echo "Failure during ssh to $host" >&2
46 d59fea3d René Nussbaumer
    exit $EXIT_FAILURE
47 d59fea3d René Nussbaumer
  fi
48 d59fea3d René Nussbaumer
}
49 d59fea3d René Nussbaumer
50 d59fea3d René Nussbaumer
_power_cycle() {
51 d59fea3d René Nussbaumer
  local host="$1"
52 d59fea3d René Nussbaumer
53 d59fea3d René Nussbaumer
  if ! _run_ssh "$host" 'shutdown -r now'; then
54 d59fea3d René Nussbaumer
    echo "Failure during ssh to $host" >&2
55 d59fea3d René Nussbaumer
    exit $EXIT_FAILURE
56 d59fea3d René Nussbaumer
  fi
57 d59fea3d René Nussbaumer
}
58 d59fea3d René Nussbaumer
59 d59fea3d René Nussbaumer
_power_status() {
60 d59fea3d René Nussbaumer
  local host="$1"
61 d59fea3d René Nussbaumer
62 d59fea3d René Nussbaumer
  if fping -q "$host" > /dev/null 2>&1; then
63 d59fea3d René Nussbaumer
    echo '{ "powered": true }'
64 d59fea3d René Nussbaumer
  else
65 d59fea3d René Nussbaumer
    echo '{ "powered": false }'
66 d59fea3d René Nussbaumer
  fi
67 d59fea3d René Nussbaumer
}
68 d59fea3d René Nussbaumer
69 d59fea3d René Nussbaumer
_health() {
70 d59fea3d René Nussbaumer
  echo 'health not supported in this script' >&2
71 d59fea3d René Nussbaumer
  exit $EXIT_FAILURE
72 d59fea3d René Nussbaumer
}
73 d59fea3d René Nussbaumer
74 d59fea3d René Nussbaumer
_action() {
75 d59fea3d René Nussbaumer
  local command="$1"
76 d59fea3d René Nussbaumer
  local host="$2"
77 d59fea3d René Nussbaumer
78 d59fea3d René Nussbaumer
  case "$command" in
79 d59fea3d René Nussbaumer
    power-on)
80 d59fea3d René Nussbaumer
      _power_on "$host"
81 d59fea3d René Nussbaumer
      ;;
82 d59fea3d René Nussbaumer
    power-off)
83 d59fea3d René Nussbaumer
      _power_off "$host"
84 d59fea3d René Nussbaumer
      ;;
85 d59fea3d René Nussbaumer
    power-cycle)
86 d59fea3d René Nussbaumer
      _power_cycle "$host"
87 d59fea3d René Nussbaumer
      ;;
88 d59fea3d René Nussbaumer
    power-status)
89 d59fea3d René Nussbaumer
      _power_status "$host"
90 d59fea3d René Nussbaumer
      ;;
91 d59fea3d René Nussbaumer
    health)
92 d59fea3d René Nussbaumer
      _health "$host"
93 d59fea3d René Nussbaumer
      ;;
94 d59fea3d René Nussbaumer
    *)
95 d59fea3d René Nussbaumer
      echo "Unsupported command '$command'" >&2
96 d59fea3d René Nussbaumer
      exit $EXIT_FAILURE
97 d59fea3d René Nussbaumer
      ;;
98 d59fea3d René Nussbaumer
  esac
99 d59fea3d René Nussbaumer
}
100 d59fea3d René Nussbaumer
101 d59fea3d René Nussbaumer
main() {
102 d59fea3d René Nussbaumer
  if [[ $# != 2 ]]; then
103 d59fea3d René Nussbaumer
    echo "Wrong argument count, got $#, expected 2" >&2
104 d59fea3d René Nussbaumer
    exit $EXIT_FAILURE
105 d59fea3d René Nussbaumer
  fi
106 d59fea3d René Nussbaumer
107 d59fea3d René Nussbaumer
  _action "$@"
108 d59fea3d René Nussbaumer
109 d59fea3d René Nussbaumer
  exit $EXIT_SUCCESS
110 d59fea3d René Nussbaumer
}
111 d59fea3d René Nussbaumer
112 d59fea3d René Nussbaumer
main "$@"