~hp-moonshot/ubuntu/trusty/hp-moonshot/hp-proliant

« back to all changes in this revision

Viewing changes to usr/lib/hp-proliant/inc/common

  • Committer: Narinder Gupta
  • Date: 2014-09-04 20:37:03 UTC
  • Revision ID: narinder.gupta@canonical.com-20140904203703-2sk67uwbzk9fc4tk
branch a new version for HP proliant systems where there is no power management required.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/bin/bash
 
2
#
 
3
#    inc/common - helper bash functions, because Python is too good
 
4
#    Copyright (C) 2014 Canonical Ltd.
 
5
#
 
6
#    Authors: Marco Ceppi <marco.ceppi@canonical.com>
 
7
#
 
8
#    This program is free software: you can redistribute it and/or modify
 
9
#    it under the terms of the GNU General Public License as published by
 
10
#    the Free Software Foundation, version 3 of the License.
 
11
#
 
12
#    This program is distributed in the hope that it will be useful,
 
13
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
#    GNU General Public License for more details.
 
16
#
 
17
#    You should have received a copy of the GNU General Public License
 
18
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
19
#
 
20
 
 
21
_SETTINGS_FILE="${MICRO_CLUSTER_CFG:-~/.config/micro-cluster/settings.cfg}"
 
22
 
 
23
error() {
 
24
        echo "ERROR: $@" 1>&2
 
25
        exit 1
 
26
}
 
27
 
 
28
info() {
 
29
        echo "INFO: $@" 1>&2
 
30
}
 
31
 
 
32
detect_inet() {
 
33
  # $1 inet preface (eg: eth)
 
34
  # $2 max range (eg: 5)
 
35
 
 
36
  local tpl="${1:-eth}"
 
37
  local max=${2:-5}
 
38
 
 
39
  local iface=""
 
40
 
 
41
  for i in `seq 0 $max`; do
 
42
    if ifconfig ${tpl}${i} > /dev/null 2>&1; then
 
43
      echo "${tpl}${i}";
 
44
      return 0
 
45
    fi
 
46
  done
 
47
 
 
48
  return 1
 
49
}
 
50
 
 
51
config_get() {
 
52
  # $1 configuration key
 
53
  if [ ! -f "$_SETTINGS_FILE" ]; then
 
54
    return 72
 
55
  fi
 
56
 
 
57
  if ! config_key_exists "$1"; then
 
58
    return 1
 
59
  fi
 
60
 
 
61
  local kv=$(egrep "^$1: .*" $_SETTINGS_FILE)
 
62
  local v=$(echo "$kv" | sed -e 's/^.*: //')
 
63
  echo $v
 
64
}
 
65
 
 
66
config_key_exists() {
 
67
  # $1 configuration key
 
68
  if [ ! -f "$_SETTINGS_FILE" ]; then
 
69
    return 72
 
70
  fi
 
71
 
 
72
  if ! egrep "^$1: .*$" $_SETTINGS_FILE > /dev/null; then
 
73
    return 1
 
74
  fi
 
75
 
 
76
  return 0
 
77
}
 
78
 
 
79
config_set() {
 
80
  # $1 configuration key
 
81
  # $2 valuep
 
82
  if [ ! -f "$_SETTINGS_FILE" ]; then
 
83
    mkdir -p $(dirname $_SETTINGS_FILE)
 
84
  fi
 
85
 
 
86
  if config_key_exists "$1"; then
 
87
    sed -i "s/^$1: .*$/$1: $2/" $_SETTINGS_FILE
 
88
  else
 
89
    echo "$1: $2" >> $_SETTINGS_FILE
 
90
  fi
 
91
}
 
92
 
 
93
maas_logged_in() {
 
94
  if [ ! -f ~/.maascli.db ]; then
 
95
    return 72
 
96
  fi
 
97
 
 
98
  if [ "$(maas list)" == "" ]; then
 
99
    return 1
 
100
  fi
 
101
}
 
102
 
 
103
oauth_key() {
 
104
  echo "$(maas apikey --username=admin)"
 
105
}
 
106
 
 
107
oauth_login() {
 
108
  if [ -f /etc/hp-proliant.conf ]; then
 
109
           PREFIX=$(awk -F '[:]' NR==1'{print $3"."$4}' /etc/hp-proliant.conf)
 
110
  else
 
111
           PREFIX="10.14"
 
112
  fi
 
113
 
 
114
  if maas_logged_in; then
 
115
    return 0
 
116
  fi
 
117
 
 
118
  local user="${1:-admin}"
 
119
  local auth=`oauth_key "$user"`
 
120
  if [ "$auth" == "" ]; then
 
121
    echo "Unable to find user creds, does user exist?"
 
122
    return 1
 
123
  fi
 
124
 
 
125
  maas $user login http://${PREFIX}.4.1/MAAS "$auth"
 
126
  chown ubuntu: ~/.maascli.db
 
127
}
 
128
 
 
129
wake_me_up_before_you_go_go() {
 
130
        for i in $(seq 1 30); do
 
131
                if amttool $1 info 2>/dev/null | grep -qs "Powerstate:" 2>/dev/null; then
 
132
                        break
 
133
                fi
 
134
        done
 
135
}