1
# -*- mode: shell-script -*-
3
# Control a system via ipmipower
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}}
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}}
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"
32
# Determines the power command needed to execute the desired
33
# action. This function receives ${power_change} as argument.
34
formulate_power_command() {
36
'on') echo '--cycle --on-if-off' ;;
38
if [ "$power_off_mode" = "soft" ];
44
'query') echo '--stat' ;;
46
echo "Got unknown power state from ipmipower: '$1'" >&2
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" ]
57
driver_option="--driver-type=${power_driver}"
59
if [ -n "$power_user" ]
61
user_option="-u ${power_user}"
64
if [ "$power_change" != "query" ]
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)
70
if echo $result | grep -q "password invalid"
72
echo "Invalid password" >&2
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} "$@")
81
if echo $result | grep -q "password invalid"
83
echo "Invalid password" >&2
89
# Result looks like the usual IPMI output:
90
# <ipmi-ip-address>: <on/off>, just return the <on|off>
92
echo ${result} | cut -d ':' -f 2
99
# This script deliberately does not check the current power state
100
# before issuing the requested power command. See bug 1171418 for an
102
power_command=$(formulate_power_command ${power_change})
103
issue_ipmi_command ${power_command}