| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
import ConfigParser |
| 4 |
import hashlib |
| 5 |
import hpilo |
| 6 |
import optparse |
| 7 |
import os |
| 8 |
import pprint |
| 9 |
import sys |
| 10 |
import time |
| 11 |
import types |
| 12 |
|
| 13 |
# All methods that fetch data use the _info_tag or _control_tag function |
| 14 |
methods = [meth for meth in dir(hpilo.Ilo) |
| 15 |
if isinstance(getattr(hpilo.Ilo, meth), types.MethodType) |
| 16 |
and ('_info_tag' in getattr(hpilo.Ilo, meth).__code__.co_names |
| 17 |
or '_control_tag' in getattr(hpilo.Ilo, meth).__code__.co_names)] |
| 18 |
methods.sort() |
| 19 |
method_args = { |
| 20 |
'activate_license': [['XXXXX-XXXXX-XXXXX-XXXXX-XXXXX']], |
| 21 |
'add_federation_group': [['TestGroup', 'TestKey', False, False, True]], |
| 22 |
'add_user': [['TestUser', 'TestPass', 'Test User', True, False]], |
| 23 |
'cert_fqdn': [[True]], |
| 24 |
'certificate_signing_request': [[],['NL', 'Flevoland']], |
| 25 |
'computer_lock_config': [[], [None, 'X']], |
| 26 |
'delete_federation_group': [['TestGroup']], |
| 27 |
'delete_user': [['TestUser']], |
| 28 |
'ers_ahs_submit': [['TestMessage', 'bb days']], |
| 29 |
'get_federation_group': [['slartibartfast']], |
| 30 |
'get_user': [['TestUser']], |
| 31 |
'hotkey_config': [['something', 'something else']], |
| 32 |
'import_certificate': [['-----BEGIN CERTIFICATE-----\nMIIEJjCCAw6gAwIBAgIBADANBgkqhkiG9w0BAQsFADBhMQswCQYDVQQGEwJOTDES\n-----END CERTIFICATE-----']], |
| 33 |
'import_ssh_key': [['TestUser', 'ssh-rsa VGVzdCBrZXk= Test Key']], |
| 34 |
'delete_ssh_key': [['TestUser']], |
| 35 |
'insert_virtual_media': [['CDROM', 'http://localhost/os.iso']], |
| 36 |
'mod_dir_config': [[True, True, 'ldap.example.com', 389]], |
| 37 |
'mod_dir_config': [[True, True, '']], |
| 38 |
'mod_federation_group': [['TestGroup', None, None, False, False, True]], |
| 39 |
'mod_global_settings': [ |
| 40 |
{'remote_syslog_enable': True, 'remote_syslog_server_address': '10.1.1.1', 'remote_syslog_port': 514}, |
| 41 |
{'http_port': 80, 'https_port': 443, 'snmp_access_enabled': False}, |
| 42 |
], |
| 43 |
'mod_network_settings': [ |
| 44 |
{'dhcp_enable': True, 'shared_network_port_vlan_id': 42}, |
| 45 |
{'dhcp_enable': False, 'dns_name': 'example-server', 'domain_name': 'ilos.example.com', |
| 46 |
'ip_address': '10.1.1.1', 'subnet_mask': '255.255.255.0', 'gateway_ip_address': '10.1.1.254'} |
| 47 |
], |
| 48 |
'mod_snmp_im_settings': [[True, '10.1.1.1', '10.1.1.2', 'rocomm', {'value': 'trapcomm', 'version': '2c'}]], |
| 49 |
'mod_sso_settings': [['TrustMode', True, False, True]], |
| 50 |
'mod_user': [['TestUser', 'TestUser2', 'TestPassword2', False, True]], |
| 51 |
'profile_apply': [['TestProfile', 'Action']], |
| 52 |
'profile_delete': [['TestProfile']], |
| 53 |
'profile_desc_download': [['TestProfileDesc', 'TestProfile', 'Description']], |
| 54 |
'set_ahs_status': [[True]], |
| 55 |
'set_asset_tag': 'NL000001', |
| 56 |
'set_ers_direct_connect': [['ErsUser', 'ErsPassword', 'ErsProxyHost', 3128]], |
| 57 |
'set_ers_irs_connect': [['ErsUrl', 9999]], |
| 58 |
'set_ers_web_proxy': [['ErsProxy', 3128]], |
| 59 |
'set_federation_multicast': [[False]], |
| 60 |
'set_language': [['en']], |
| 61 |
'set_host_power': [[False]], |
| 62 |
'set_host_power_saver': [['PowerSaver']], |
| 63 |
'set_one_time_boot': [['BootDevice'], ['floppy']], |
| 64 |
'set_pending_boot_mode': [['UEFI']], |
| 65 |
'set_persistent_boot': [['cdrom,floppy,boot_device']], |
| 66 |
'set_pers_mouse_keyboard_enabled': [[False]], |
| 67 |
'set_pwreg': [['disabled'],['enabled', 'threshold']], |
| 68 |
'set_power_cap': [['CAP']], |
| 69 |
'set_security_msg': [[True, 'Security Message Goes Here']], |
| 70 |
'set_server_auto_pwr': [[True],[45]], |
| 71 |
'set_server_fqdn': [['example-server.example.com']], |
| 72 |
'set_server_name': [['example-server']], |
| 73 |
'trigger_l2_collection': [['TestMessage']], |
| 74 |
'trigger_test_event': [['TestMessage']], |
| 75 |
} |
| 76 |
|
| 77 |
def main(): |
| 78 |
usage = """ |
| 79 |
%prog [options] hostname""" |
| 80 |
|
| 81 |
p = optparse.OptionParser(usage=usage, add_help_option=False) |
| 82 |
p.add_option("-l", "--login", dest="login", default=None, |
| 83 |
help="Username to access the iLO") |
| 84 |
p.add_option("-p", "--password", dest="password", default=None, |
| 85 |
help="Password to access the iLO") |
| 86 |
p.add_option("-i", "--interactive", action="store_true", default=False, |
| 87 |
help="Prompt for username and/or password if they are not specified.") |
| 88 |
p.add_option("-c", "--config", dest="config", default="~/.ilo.conf", |
| 89 |
help="File containing authentication and config details", metavar="FILE") |
| 90 |
p.add_option("-t", "--timeout", dest="timeout", type="int", default=60, |
| 91 |
help="Timeout for iLO connections") |
| 92 |
p.add_option("-P", "--protocol", dest="protocol", choices=("http","raw","local"), default=None, |
| 93 |
help="Use the specified protocol instead of autodetecting") |
| 94 |
p.add_option("-d", "--debug", dest="debug", action="count", default=0, |
| 95 |
help="Output debug information, repeat to see all XML data") |
| 96 |
p.add_option("-o", "--port", dest="port", type="int", default=443, |
| 97 |
help="SSL port to connect to") |
| 98 |
|
| 99 |
opts, args = p.parse_args() |
| 100 |
|
| 101 |
if len(args) != 1: |
| 102 |
p.error("No hostname given") |
| 103 |
|
| 104 |
hostname = args[0] |
| 105 |
|
| 106 |
config = ConfigParser.ConfigParser() |
| 107 |
if os.path.exists(os.path.expanduser(opts.config)): |
| 108 |
config.read(os.path.expanduser(opts.config)) |
| 109 |
|
| 110 |
login = None |
| 111 |
password = None |
| 112 |
needs_login = True |
| 113 |
if hostname == 'localhost': |
| 114 |
opts.protocol = 'local' |
| 115 |
needs_login = False |
| 116 |
if needs_login: |
| 117 |
if config.has_option('ilo', 'login'): |
| 118 |
login = config.get('ilo', 'login') |
| 119 |
if config.has_option('ilo', 'password'): |
| 120 |
password = config.get('ilo', 'password') |
| 121 |
if opts.login: |
| 122 |
login = opts.login |
| 123 |
if opts.password: |
| 124 |
password = opts.password |
| 125 |
if not login or not password: |
| 126 |
if opts.interactive: |
| 127 |
while not login: |
| 128 |
login = input('Login for iLO at %s: ' % hostname) |
| 129 |
while not password: |
| 130 |
password = getpass.getpass('Password for %s@%s:' % (login, hostname)) |
| 131 |
else: |
| 132 |
p.error("No login details provided") |
| 133 |
|
| 134 |
opts.protocol = { |
| 135 |
'http': hpilo.ILO_HTTP, |
| 136 |
'raw': hpilo.ILO_RAW, |
| 137 |
'local': hpilo.ILO_LOCAL, |
| 138 |
}.get(opts.protocol, None) |
| 139 |
|
| 140 |
ilo = hpilo.Ilo(hostname, login, password, opts.timeout, opts.port, opts.protocol) |
| 141 |
ilo.debug = opts.debug |
| 142 |
if config.has_option('ilo', 'hponcfg'): |
| 143 |
ilo.hponcfg = config.get('ilo', 'hponcfg') |
| 144 |
fw_version = ilo.get_fw_version() |
| 145 |
host_data = ilo.get_host_data() |
| 146 |
product_name = None |
| 147 |
for data in host_data: |
| 148 |
if 'Product Name' in data: |
| 149 |
product_name = data['Product Name'] |
| 150 |
break |
| 151 |
else: |
| 152 |
raise RuntimeError("No product name found") |
| 153 |
ilo.login = 'Administrator' |
| 154 |
ilo.password = 'TestPassword' |
| 155 |
output_dir = '%s - %s %s' % (product_name, fw_version['management_processor'], fw_version['firmware_version']) |
| 156 |
missing_only = False |
| 157 |
if os.path.exists(output_dir): |
| 158 |
answer = raw_input("I already have output for %s, regenerate? [(y)es/(m)issing only/(N)o] " % output_dir).lower() |
| 159 |
if answer == 'n': |
| 160 |
sys.exit(0) |
| 161 |
missing_only = answer == 'm' |
| 162 |
else: |
| 163 |
os.makedirs(output_dir) |
| 164 |
print("Generating data for %s" % output_dir) |
| 165 |
output_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), output_dir) |
| 166 |
for number, meth in enumerate(methods): |
| 167 |
sys.stdout.write('\r\033[K%s (%d/%d)' % (meth, number+1, len(methods))) |
| 168 |
sys.stdout.flush() |
| 169 |
for args in method_args.get(meth, [[]]): |
| 170 |
argsf = pprint.pformat(args) |
| 171 |
sha1 = hashlib.new('sha1') |
| 172 |
sha1.update(argsf) |
| 173 |
ident = sha1.hexdigest()[:8] |
| 174 |
output_file_args = os.path.join(output_dir, '%s-%s.args' % (meth, ident)) |
| 175 |
output_file_req = os.path.join(output_dir, '%s-%s.request' % (meth, ident)) |
| 176 |
if os.path.exists(output_file_req): |
| 177 |
if missing_only: |
| 178 |
continue |
| 179 |
else: |
| 180 |
os.unlink(output_file_req) |
| 181 |
with open(output_file_args, 'w') as fd: |
| 182 |
fd.write(argsf) |
| 183 |
ilo.save_request = output_file_req |
| 184 |
try: |
| 185 |
if isinstance(args, dict): |
| 186 |
data = getattr(ilo, meth)(**args) |
| 187 |
else: |
| 188 |
data = getattr(ilo, meth)(*args) |
| 189 |
except ValueError: |
| 190 |
if meth.startswith('get'): |
| 191 |
raise |
| 192 |
continue |
| 193 |
print("") |
| 194 |
|
| 195 |
if __name__ == '__main__': |
| 196 |
main() |