57
57
add_bin "maas-ipmi-autodetect" <<"END_MAAS_IPMI_AUTODETECT"
69
# XXX: andreserl 2013-04-09 bug=1064527: Try to detect if node
70
# is a Virtual Machine. If it is, do not try to detect IPMI.
71
with open('/proc/cpuinfo', 'r') as cpuinfo:
73
if line.startswith('model name') and 'QEMU' in line:
76
(status, output) = commands.getstatusoutput('ipmi-locate')
77
show_re = re.compile('(IPMI\ Version:) (\d\.\d)')
78
res = show_re.search(output)
80
found = glob.glob("/dev/ipmi[0-9]")
82
return (True, "UNKNOWN: %s" % " ".join(found))
84
return (True, res.group(2))
87
(status, output) = commands.getstatusoutput('bmc-config --checkout --key-pair="Lan_Conf:IP_Address_Source"')
88
show_re = re.compile('IP_Address_Source\s+Use_DHCP')
89
res = show_re.search(output)
94
def set_ipmi_network_source(source):
95
(status, output) = commands.getstatusoutput('bmc-config --commit --key-pair="Lan_Conf:IP_Address_Source=%s"' % source)
97
def get_ipmi_ip_address():
98
(status, output) = commands.getstatusoutput('bmc-config --checkout --key-pair="Lan_Conf:IP_Address"')
99
show_re = re.compile('([0-9]{1,3}[.]?){4}')
100
res = show_re.search(output)
103
def get_ipmi_user_number(user):
104
for i in range(1, 17):
105
ipmi_user_number = "User%s" % i
106
(status, output) = commands.getstatusoutput('bmc-config --checkout --key-pair="%s:Username"' % ipmi_user_number)
108
return ipmi_user_number
111
def commit_ipmi_user_settings(user, password):
112
ipmi_user_number = get_ipmi_user_number(user)
113
if ipmi_user_number is None:
114
(status, output) = commands.getstatusoutput('bmc-config --commit --key-pair="User10:Username=%s"' % user)
115
ipmi_user_number = get_ipmi_user_number(user)
116
(status, output) = commands.getstatusoutput('bmc-config --commit --key-pair="%s:Username=%s"' % (ipmi_user_number, user))
117
(status, output) = commands.getstatusoutput('bmc-config --commit --key-pair="%s:Password=%s"' % (ipmi_user_number, password))
118
(status, output) = commands.getstatusoutput('bmc-config --commit --key-pair="%s:Enable_User=Yes"' % ipmi_user_number)
119
(status, output) = commands.getstatusoutput('bmc-config --commit --key-pair="%s:Lan_Enable_IPMI_Msgs=Yes"' % ipmi_user_number)
120
(status, output) = commands.getstatusoutput('bmc-config --commit --key-pair="%s:Lan_Privilege_Limit=Administrator"' % ipmi_user_number)
122
def commit_ipmi_settings(config):
123
(status, output) = commands.getstatusoutput('bmc-config --commit --filename %s' % config)
125
def get_maas_power_settings(user, password, ipaddress):
126
return "%s,%s,%s" % (user, password, ipaddress)
128
def get_maas_power_settings_json(user, password, ipaddress):
129
power_params = {"power_address": ipaddress, "power_pass": password, "power_user": user}
130
return json.dumps(power_params)
132
def generate_random_password(min=8,max=15):
133
length=random.randint(min,max)
134
letters=string.ascii_letters+string.digits
135
return ''.join([random.choice(letters) for _ in range(length)])
141
parser = argparse.ArgumentParser(
142
description='send config file to modify IPMI settings with')
143
parser.add_argument("--configdir", metavar="folder",
144
help="specify config file directory", default=None)
145
parser.add_argument("--dhcp-if-static", action="store_true",
146
dest="dhcp", help="set network source to DHCP if Static", default=False)
147
parser.add_argument("--commission-creds", action="store_true",
148
dest="commission_creds", help="Create IPMI temporary credentials", default=False)
150
args = parser.parse_args()
152
# Check whether IPMI exists or not.
153
(status, ipmi_version) = detect_ipmi()
155
# if False, then failed to detect ipmi
158
# Check whether IPMI is being set to DHCP. If it is not, and
159
# '--dhcp-if-static' has been passed, Set it to IPMI to DHCP.
160
if not is_ipmi_dhcp() and args.dhcp:
161
set_ipmi_network_source("Use_DHCP")
162
# allow IPMI 120 seconds to obtain an IP address
166
IPMI_MAAS_USER="maas"
167
IPMI_MAAS_PASSWORD=generate_random_password()
169
# Configure IPMI user/password
170
commit_ipmi_user_settings(IPMI_MAAS_USER, IPMI_MAAS_PASSWORD)
172
# Commit other IPMI settings
174
for file in os.listdir(args.configdir):
175
commit_ipmi_settings(os.path.join(args.configdir, file))
178
IPMI_IP_ADDRESS = get_ipmi_ip_address()
179
if IPMI_IP_ADDRESS == "0.0.0.0":
180
# if IPMI_IP_ADDRESS is 0.0.0.0, wait 60 seconds and retry.
181
set_ipmi_network_source("Static")
183
set_ipmi_network_source("Use_DHCP")
185
IPMI_IP_ADDRESS = get_ipmi_ip_address()
187
if IPMI_IP_ADDRESS is None or IPMI_IP_ADDRESS == "0.0.0.0":
188
# Exit (to not set power params in MAAS) if no IPMI_IP_ADDRESS
192
if args.commission_creds:
193
print get_maas_power_settings_json(IPMI_MAAS_USER, IPMI_MAAS_PASSWORD, IPMI_IP_ADDRESS)
195
print get_maas_power_settings(IPMI_MAAS_USER, IPMI_MAAS_PASSWORD, IPMI_IP_ADDRESS)
197
if __name__ == '__main__':
58
{{maas_ipmi_autodetect_py}}
199
59
END_MAAS_IPMI_AUTODETECT
201
61
# we could obtain the interface that booted from the kernel cmdline