19
19
# You should have received a copy of the GNU Affero General Public License
20
20
# along with this program. If not, see <http://www.gnu.org/licenses/>.
22
from __future__ import (
32
# XXX: andreserl 2013-04-09 bug=1064527: Try to detect if node
33
# is a Virtual Machine. If it is, do not try to detect IPMI.
34
with open('/proc/cpuinfo', 'r') as cpuinfo:
36
if line.startswith('model name') and 'QEMU' in line:
39
(status, output) = commands.getstatusoutput('ipmi-locate')
40
show_re = re.compile('(IPMI\ Version:) (\d\.\d)')
41
res = show_re.search(output)
43
found = glob.glob("/dev/ipmi[0-9]")
45
return (True, "UNKNOWN: %s" % " ".join(found))
47
return (True, res.group(2))
49
41
def is_ipmi_dhcp():
50
(status, output) = commands.getstatusoutput('bmc-config --checkout --key-pair="Lan_Conf:IP_Address_Source"')
42
status, output = commands.getstatusoutput(
43
'bmc-config --checkout --key-pair="Lan_Conf:IP_Address_Source"')
51
44
show_re = re.compile('IP_Address_Source\s+Use_DHCP')
52
res = show_re.search(output)
45
return show_re.search(output) is not None
57
48
def set_ipmi_network_source(source):
58
(status, output) = commands.getstatusoutput('bmc-config --commit --key-pair="Lan_Conf:IP_Address_Source=%s"' % source)
49
status, output = commands.getstatusoutput(
50
'bmc-config --commit --key-pair="Lan_Conf:IP_Address_Source=%s"'
60
54
def get_ipmi_ip_address():
61
(status, output) = commands.getstatusoutput('bmc-config --checkout --key-pair="Lan_Conf:IP_Address"')
55
status, output = commands.getstatusoutput(
56
'bmc-config --checkout --key-pair="Lan_Conf:IP_Address"')
62
57
show_re = re.compile('([0-9]{1,3}[.]?){4}')
63
58
res = show_re.search(output)
66
62
def get_ipmi_user_number(user):
67
63
for i in range(1, 17):
68
64
ipmi_user_number = "User%s" % i
69
(status, output) = commands.getstatusoutput('bmc-config --checkout --key-pair="%s:Username"' % ipmi_user_number)
65
status, output = commands.getstatusoutput(
66
'bmc-config --checkout --key-pair="%s:Username"'
71
69
return ipmi_user_number
74
73
def commit_ipmi_user_settings(user, password):
75
74
ipmi_user_number = get_ipmi_user_number(user)
76
75
if ipmi_user_number is None:
77
(status, output) = commands.getstatusoutput('bmc-config --commit --key-pair="User10:Username=%s"' % user)
76
status, output = commands.getstatusoutput(
77
'bmc-config --commit --key-pair="User10:Username=%s"' % user)
78
78
ipmi_user_number = get_ipmi_user_number(user)
79
(status, output) = commands.getstatusoutput('bmc-config --commit --key-pair="%s:Username=%s"' % (ipmi_user_number, user))
80
(status, output) = commands.getstatusoutput('bmc-config --commit --key-pair="%s:Password=%s"' % (ipmi_user_number, password))
81
(status, output) = commands.getstatusoutput('bmc-config --commit --key-pair="%s:Enable_User=Yes"' % ipmi_user_number)
82
(status, output) = commands.getstatusoutput('bmc-config --commit --key-pair="%s:Lan_Enable_IPMI_Msgs=Yes"' % ipmi_user_number)
83
(status, output) = commands.getstatusoutput('bmc-config --commit --key-pair="%s:Lan_Privilege_Limit=Administrator"' % ipmi_user_number)
79
status, output = commands.getstatusoutput(
80
'bmc-config --commit --key-pair="%s:Username=%s"'
81
% (ipmi_user_number, user))
82
status, output = commands.getstatusoutput(
83
'bmc-config --commit --key-pair="%s:Password=%s"'
84
% (ipmi_user_number, password))
85
status, output = commands.getstatusoutput(
86
'bmc-config --commit --key-pair="%s:Enable_User=Yes"'
88
status, output = commands.getstatusoutput(
89
'bmc-config --commit --key-pair="%s:Lan_Enable_IPMI_Msgs=Yes"'
91
status, output = commands.getstatusoutput(
92
'bmc-config --commit --key-pair="%s:Lan_Privilege_Limit=Administrator"'
85
96
def commit_ipmi_settings(config):
86
(status, output) = commands.getstatusoutput('bmc-config --commit --filename %s' % config)
88
def get_maas_power_settings(user, password, ipaddress):
89
return "%s,%s,%s" % (user, password, ipaddress)
91
def get_maas_power_settings_json(user, password, ipaddress):
92
power_params = {"power_address": ipaddress, "power_pass": password, "power_user": user}
93
return json.dumps(power_params)
95
def generate_random_password(min=8,max=15):
96
length=random.randint(min,max)
97
letters=string.ascii_letters+string.digits
97
status, output = commands.getstatusoutput(
98
'bmc-config --commit --filename %s' % config)
101
def get_maas_power_settings(user, password, ipaddress, version):
102
return "%s,%s,%s,%s" % (user, password, ipaddress, version)
105
def get_maas_power_settings_json(user, password, ipaddress, version):
107
"power_address": ipaddress,
108
"power_pass": password,
110
"power_driver": version,
112
return json.dumps(power_params)
115
def generate_random_password(min_length=8, max_length=15):
116
length = random.randint(min_length, max_length)
117
letters = string.ascii_letters + string.digits
98
118
return ''.join([random.choice(letters) for _ in range(length)])
121
def get_ipmi_version():
122
(status, output) = commands.getstatusoutput('ipmi-locate')
124
#IPMI locate driver: SMBIOS
127
#BMC I/O base address: 0xCA2
129
#show_re = re.compile('(IPMI\ Version:) (\d\.\d)')
130
show_re = re.compile(
131
'(IPMI\ Version:) (\d\.\d)(\n)(.*)(\n)(.*)(\n)(.*)(\n)'
132
'(BMC\ I\/O\ base\ address:) (0xCA2)')
133
res = show_re.search(output)
104
142
parser = argparse.ArgumentParser(
105
143
description='send config file to modify IPMI settings with')
106
parser.add_argument("--configdir", metavar="folder",
107
help="specify config file directory", default=None)
108
parser.add_argument("--dhcp-if-static", action="store_true",
109
dest="dhcp", help="set network source to DHCP if Static", default=False)
110
parser.add_argument("--commission-creds", action="store_true",
111
dest="commission_creds", help="Create IPMI temporary credentials", default=False)
145
"--configdir", metavar="folder", help="specify config file directory",
148
"--dhcp-if-static", action="store_true", dest="dhcp",
149
help="set network source to DHCP if Static", default=False)
151
"--commission-creds", action="store_true", dest="commission_creds",
152
help="Create IPMI temporary credentials", default=False)
113
154
args = parser.parse_args()
115
# Check whether IPMI exists or not.
116
(status, ipmi_version) = detect_ipmi()
118
# if False, then failed to detect ipmi
121
156
# Check whether IPMI is being set to DHCP. If it is not, and
122
157
# '--dhcp-if-static' has been passed, Set it to IPMI to DHCP.
123
158
if not is_ipmi_dhcp() and args.dhcp: