1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
|
#cloud-config
# could/should set local mirror here or proxy here
# apt_proxy: http://{{server_host}}:8000/
misc_bucket:
- &maas_enlist |
#### IPMI setup ######
# If IPMI network settings have been configured statically, you can
# make them DHCP. If 'true', the IPMI network source will be changed
# to DHCP.
IPMI_CHANGE_STATIC_TO_DHCP="false"
# In certain hardware, the parameters for the ipmi_si kernel module
# might need to be specified. If you wish to send parameters, uncomment
# the following line.
#IPMI_SI_PARAMS="type=kcs ports=0xca2"
TEMP_D=$(mktemp -d "${TMPDIR:-/tmp}/${0##*/}.XXXXXX")
IPMI_CONFIG_D="${TEMP_D}/ipmi.d"
BIN_D="${TEMP_D}/bin"
OUT_D="${TEMP_D}/out"
PATH="$BIN_D:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
mkdir -p "$BIN_D" "$OUT_D" "$IPMI_CONFIG_D"
load_modules() {
modprobe ipmi_msghandler
modprobe ipmi_devintf
modprobe ipmi_si ${IPMI_SI_PARAMS}
udevadm settle
}
add_bin() {
cat > "${BIN_D}/$1"
chmod "${2:-755}" "${BIN_D}/$1"
}
add_ipmi_config() {
cat > "${IPMI_CONFIG_D}/$1"
chmod "${2:-644}" "${IPMI_CONFIG_D}/$1"
}
add_ipmi_config "01-user-privileges.ipmi" <<"END_IPMI_USER_PRIVILEGES"
Section User3
Enable_User Yes
Lan_Enable_IPMI_Msgs Yes
Lan_Privilege_Limit Administrator
EndSection
END_IPMI_USER_PRIVILEGES
add_ipmi_config "02-global-config.ipmi" <<"END_IPMI_CONFIG"
Section Lan_Channel
Volatile_Access_Mode Always_Available
Volatile_Enable_User_Level_Auth Yes
Volatile_Channel_Privilege_Limit Administrator
Non_Volatile_Access_Mode Always_Available
Non_Volatile_Enable_User_Level_Auth Yes
Non_Volatile_Channel_Privilege_Limit Administrator
EndSection
END_IPMI_CONFIG
add_bin "maas-ipmi-autodetect" <<"END_MAAS_IPMI_AUTODETECT"
#!/usr/bin/python
import os
import commands
import glob
import re
import string
import random
import time
import json
def detect_ipmi():
# TODO: Detection could be improved.
(status, output) = commands.getstatusoutput('ipmi-locate')
show_re = re.compile('(IPMI\ Version:) (\d\.\d)')
res = show_re.search(output)
if res == None:
found = glob.glob("/dev/ipmi[0-9]")
if len(found):
return (True, "UNKNOWN: %s" % " ".join(found))
return (False, "")
return (True, res.group(2))
def is_ipmi_dhcp():
(status, output) = commands.getstatusoutput('bmc-config --checkout --key-pair="Lan_Conf:IP_Address_Source"')
show_re = re.compile('IP_Address_Source\s+Use_DHCP')
res = show_re.search(output)
if res == None:
return False
return True
def set_ipmi_network_source(source):
(status, output) = commands.getstatusoutput('bmc-config --commit --key-pair="Lan_Conf:IP_Address_Source=%s"' % source)
def get_ipmi_ip_address():
(status, output) = commands.getstatusoutput('bmc-config --checkout --key-pair="Lan_Conf:IP_Address"')
show_re = re.compile('([0-9]{1,3}[.]?){4}')
res = show_re.search(output)
return res.group()
def commit_ipmi_user_settings(user, password):
(status, output) = commands.getstatusoutput('bmc-config --commit --key-pair="User3:Username=%s"' % user)
(status, output) = commands.getstatusoutput('bmc-config --commit --key-pair="User3:Password=%s"' % password)
def commit_ipmi_settings(config):
(status, output) = commands.getstatusoutput('bmc-config --commit --filename %s' % config)
def get_maas_power_settings(user, password, ipaddress):
return "%s,%s,%s" % (user, password, ipaddress)
def get_maas_power_settings_json(user, password, ipaddress):
power_params = {"power_address": ipaddress, "power_pass": password, "power_user": user}
return json.dumps(power_params)
def generate_random_password(min=8,max=15):
length=random.randint(min,max)
letters=string.ascii_letters+string.digits
return ''.join([random.choice(letters) for _ in range(length)])
def main():
import argparse
parser = argparse.ArgumentParser(
description='send config file to modify IPMI settings with')
parser.add_argument("--configdir", metavar="folder",
help="specify config file directory", default=None)
parser.add_argument("--dhcp-if-static", action="store_true",
dest="dhcp", help="set network source to DHCP if Static", default=False)
parser.add_argument("--commission-creds", action="store_true",
dest="commission_creds", help="Create IPMI temporary credentials", default=False)
args = parser.parse_args()
# Check whether IPMI exists or not.
(status, ipmi_version) = detect_ipmi()
if status != True:
# if False, then failed to detect ipmi
exit(1)
# Check whether IPMI is being set to DHCP. If it is not, and
# '--dhcp-if-static' has been passed, Set it to IPMI to DHCP.
if not is_ipmi_dhcp() and args.dhcp:
set_ipmi_network_source("Use_DHCP")
# allow IPMI 120 seconds to obtain an IP address
time.sleep(120)
# create user/pass
if args.commission_creds:
IPMI_MAAS_USER="maas-commission"
else:
IPMI_MAAS_USER="maas"
IPMI_MAAS_PASSWORD=generate_random_password()
# Configure IPMI user/password
commit_ipmi_user_settings(IPMI_MAAS_USER, IPMI_MAAS_PASSWORD)
# Commit other IPMI settings
if args.configdir:
for file in os.listdir(args.configdir):
commit_ipmi_settings(os.path.join(args.configdir, file))
# get the IP address
IPMI_IP_ADDRESS = get_ipmi_ip_address()
if IPMI_IP_ADDRESS == "0.0.0.0":
# if IPMI_IP_ADDRESS is 0.0.0.0, wait 60 seconds and retry.
set_ipmi_network_source("Static")
time.sleep(2)
set_ipmi_network_source("Use_DHCP")
time.sleep(60)
IPMI_IP_ADDRESS = get_ipmi_ip_address()
if IPMI_IP_ADDRESS is None or IPMI_IP_ADDRESS == "0.0.0.0":
# Exit (to not set power params in MAAS) if no IPMI_IP_ADDRESS
# has been detected
exit(1)
if args.commission_creds:
print get_maas_power_settings_json(IPMI_MAAS_USER, IPMI_MAAS_PASSWORD, IPMI_IP_ADDRESS)
else:
print get_maas_power_settings(IPMI_MAAS_USER, IPMI_MAAS_PASSWORD, IPMI_IP_ADDRESS)
if __name__ == '__main__':
main()
END_MAAS_IPMI_AUTODETECT
# we could obtain the interface that booted from the kernel cmdline
# thanks to 'IPAPPEND' (http://www.syslinux.org/wiki/index.php/SYSLINUX)
url="{{server_url}}"
host=""
ip=$(ifconfig eth0 | awk '$1 == "inet" { sub("addr:","",$2); print $2; }') &&
[ -n "${ip}" ] && host=$(dig +short -x $ip) && host=${host%.}
# load ipmi modules
load_modules
pargs=""
if $IPMI_CHANGE_STATIC_TO_DHCP; then
pargs="--dhcp-if-static"
fi
power_params=$(maas-ipmi-autodetect --configdir "$IPMI_CONFIG_D" ${pargs} --commission-creds) &&
[ -n "${power_params}" ] && power_params=${power_params%.} && power_type=ipmi
# Try maas-enlist without power parameters on failure for older versions of
# maas-enlist without power parameter support
maas-enlist --serverurl "$url" ${host:+--hostname "${host}"} ${power_params:+--power-params "${power_params}" --power-type "${power_type}"}>/tmp/enlist.out ||\
maas-enlist --serverurl "$url" ${host:+--hostname "${host}"} >/tmp/enlist.out
if [ $? -eq 0 ]; then
msg="successfully enlisted to '$url'"
[ -n "$host" ] && msg="$msg with hostname '$host'" ||
msg="$msg without hostname"
echo
echo "=== $(date -R): $msg"
cat /tmp/enlist.out
echo =============================================
sleep 10
else
user="ubuntu"
pass="ubuntu"
echo "$user:$pass" | chpasswd
bfile="/tmp/block-poweroff"
{ echo "#!/bin/sh"; echo "touch $bfile"; } > /etc/profile.d/A01-block.sh
chmod 755 /etc/profile.d/A01-block.sh
echo
echo =============================================
echo "failed to enlist system maas server '$host'"
echo "sleeping 60 seconds then poweroff"
echo
echo "login with '$user:$pass' to debug and disable poweroff"
echo
cat /tmp/enlist.out
echo =============================================
sleep 60
[ -e $bfile ] && exit 0
fi
- &write_poweroff_job |
cat >/etc/init/maas-poweroff.conf <<EOF
description "poweroff when maas task is done"
start on stopped cloud-final
console output
task
script
[ ! -e /tmp/block-poweroff ] || exit 0
poweroff
end script
EOF
# reload required due to lack of inotify in overlayfs (LP: #882147)
initctl reload-configuration
packages: [ maas-enlist, freeipmi-tools ]
output: {all: '| tee -a /var/log/cloud-init-output.log'}
runcmd:
- [ sh, -c, *maas_enlist ]
- [ sh, -c, *write_poweroff_job ]
|