~allenap/maas/neighbours-service-live

« back to all changes in this revision

Viewing changes to etc/maas/templates/power/ipmi.template

  • Committer: Gavin Panella
  • Date: 2015-10-14 19:48:39 UTC
  • mfrom: (3852.1.524 maas)
  • Revision ID: gavin.panella@canonical.com-20151014194839-xgjmom6qzxyj71pn
Merge trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# -*- mode: shell-script -*-
2
 
#
3
 
# Control a system via ipmipower
4
 
#
5
 
 
6
 
# Parameters.
7
 
# If power_change is 'query', echo the current state of the
8
 
# machine: 'on' or 'off'.
9
 
power_change={{power_change}}
10
 
power_address={{power_address}}
11
 
power_user={{power_user}}
12
 
power_pass={{power_pass}}
13
 
power_driver={{power_driver}}
14
 
power_off_mode={{power_off_mode}}
15
 
ipmipower={{ipmipower}}
16
 
ipmi_chassis_config={{ipmi_chassis_config}}
17
 
config={{config_dir}}/{{ipmi_config}}
18
 
 
19
 
# If ip_address was supplied and power_address is not explicitly set then
20
 
# use ip_address because it gets discovered on-the-fly based on mac_address.
21
 
# We don't want to use it unilaterally because mac_address may be the host's
22
 
# MAC, so only fall back if someone deliberately left power_address empty.
23
 
{{if ip_address and not power_address}}
24
 
power_address={{ip_address}}
25
 
{{endif}}
26
 
 
27
 
# This workaround is required on many BMCs, and should have no impact
28
 
# on BMCs that don't require it.
29
 
# See https://bugs.launchpad.net/maas/+bug/1287964
30
 
workarounds="-W opensesspriv"
31
 
 
32
 
# Determines the power command needed to execute the desired
33
 
# action. This function receives ${power_change} as argument.
34
 
formulate_power_command() {
35
 
    case $1 in
36
 
    'on') echo '--cycle --on-if-off' ;;
37
 
    'off')
38
 
        if [ "$power_off_mode" = "soft" ];
39
 
        then
40
 
            echo '--soft'
41
 
        else
42
 
            echo '--off'
43
 
        fi ;;
44
 
    'query') echo '--stat' ;;
45
 
    *)
46
 
        echo "Got unknown power state from ipmipower: '$1'" >&2
47
 
        exit 1
48
 
    esac
49
 
}
50
 
 
51
 
# Issue command to ipmipower, for the given system.
52
 
issue_ipmi_command() {
53
 
    # See https://launchpad.net/bugs/1053391 for details of this workaround
54
 
    local driver_option="" user_option=""
55
 
    if [ -n "$power_driver" ]
56
 
        then
57
 
          driver_option="--driver-type=${power_driver}"
58
 
    fi
59
 
    if [ -n "$power_user" ]
60
 
        then
61
 
          user_option="-u ${power_user}"
62
 
    fi
63
 
 
64
 
    if [ "$power_change" != "query" ]
65
 
    then
66
 
        # Use C locale to force English error messages.
67
 
        result=$(echo workaround |\
68
 
        LC_ALL=C ${ipmi_chassis_config} ${workarounds} ${driver_option} -h ${power_address} ${user_option} -p ${power_pass} --commit --filename ${config} 2>&1)
69
 
 
70
 
        if echo $result | grep -q "password invalid"
71
 
        then
72
 
            echo "Invalid password" >&2
73
 
            exit 2
74
 
        fi
75
 
    fi
76
 
 
77
 
    # Use C locale to force English error messages.
78
 
    result=$(echo workaround |\
79
 
    LC_ALL=C ${ipmipower} ${workarounds} ${driver_option} -h ${power_address} ${user_option} -p ${power_pass} "$@")
80
 
 
81
 
    if echo $result | grep -q "password invalid"
82
 
    then
83
 
        echo "Invalid password" >&2
84
 
        exit 2
85
 
    fi
86
 
 
87
 
    case "$result" in
88
 
        *:* )
89
 
                # Result looks like the usual IPMI output:
90
 
                # <ipmi-ip-address>: <on/off>, just return the <on|off>
91
 
                # part.
92
 
                echo ${result} | cut -d ':' -f 2
93
 
                ;;
94
 
        * )
95
 
                echo ${result};;
96
 
    esac
97
 
}
98
 
 
99
 
# This script deliberately does not check the current power state
100
 
# before issuing the requested power command. See bug 1171418 for an
101
 
# explanation.
102
 
power_command=$(formulate_power_command ${power_change})
103
 
issue_ipmi_command ${power_command}