Debian Codegraph @

tests/xml/fetch_responses

raw
1 #!/usr/bin/python
2
3 import ConfigParser
4 import hpilo
5 import optparse
6 import os
7 import pprint
8 import sys
9 import time
10 import types
11
12 # Almost all methods that fetch data use the _info_tag function
13 methods = [meth for meth in dir(hpilo.Ilo)
14 if isinstance(getattr(hpilo.Ilo, meth), types.MethodType)
15 and '_info_tag' in getattr(hpilo.Ilo, meth).__code__.co_names]
16 # Except xmldata (raw http call) and certificate_signing_request
17 methods += ['xmldata', 'certificate_signing_request']
18 methods.sort()
19 method_args = {
20 'get_user': ['Administrator'],
21 'get_federation_group': ['slartibartfast'],
22 }
23
24 def main():
25 usage = """
26 %prog [options] hostname"""
27
28 p = optparse.OptionParser(usage=usage, add_help_option=False)
29 p.add_option("-l", "--login", dest="login", default=None,
30 help="Username to access the iLO")
31 p.add_option("-p", "--password", dest="password", default=None,
32 help="Password to access the iLO")
33 p.add_option("-i", "--interactive", action="store_true", default=False,
34 help="Prompt for username and/or password if they are not specified.")
35 p.add_option("-c", "--config", dest="config", default="~/.ilo.conf",
36 help="File containing authentication and config details", metavar="FILE")
37 p.add_option("-t", "--timeout", dest="timeout", type="int", default=60,
38 help="Timeout for iLO connections")
39 p.add_option("-P", "--protocol", dest="protocol", choices=("http","raw","local"), default=None,
40 help="Use the specified protocol instead of autodetecting")
41 p.add_option("-d", "--debug", dest="debug", action="count", default=0,
42 help="Output debug information, repeat to see all XML data")
43 p.add_option("-o", "--port", dest="port", type="int", default=443,
44 help="SSL port to connect to")
45
46 opts, args = p.parse_args()
47
48 if len(args) != 1:
49 p.error("No hostname given")
50
51 hostname = args[0]
52
53 config = ConfigParser.ConfigParser()
54 if os.path.exists(os.path.expanduser(opts.config)):
55 config.read(os.path.expanduser(opts.config))
56
57 login = None
58 password = None
59 needs_login = True
60 if hostname == 'localhost':
61 opts.protocol = 'local'
62 needs_login = False
63 if needs_login:
64 if config.has_option('ilo', 'login'):
65 login = config.get('ilo', 'login')
66 if config.has_option('ilo', 'password'):
67 password = config.get('ilo', 'password')
68 if opts.login:
69 login = opts.login
70 if opts.password:
71 password = opts.password
72 if not login or not password:
73 if opts.interactive:
74 while not login:
75 login = input('Login for iLO at %s: ' % hostname)
76 while not password:
77 password = getpass.getpass('Password for %s@%s:' % (login, hostname))
78 else:
79 p.error("No login details provided")
80
81 opts.protocol = {
82 'http': hpilo.ILO_HTTP,
83 'raw': hpilo.ILO_RAW,
84 'local': hpilo.ILO_LOCAL,
85 }.get(opts.protocol, None)
86
87 ilo = hpilo.Ilo(hostname, login, password, opts.timeout, opts.port, opts.protocol)
88 ilo.debug = opts.debug
89 if config.has_option('ilo', 'hponcfg'):
90 ilo.hponcfg = config.get('ilo', 'hponcfg')
91 fw_version = ilo.get_fw_version()
92 host_data = ilo.get_host_data()
93 product_name = None
94 for data in host_data:
95 if 'Product Name' in data:
96 product_name = data['Product Name']
97 break
98 else:
99 raise RuntimeError("No product name found")
100 output_dir = '%s - %s %s' % (product_name, fw_version['management_processor'], fw_version['firmware_version'])
101 if os.path.exists(output_dir):
102 answer = raw_input("I already have output for %s, rescan? [(y)es/(m)issing only/(N)o] " % output_dir).lower()
103 if answer == 'n':
104 sys.exit(0)
105 missing_only = answer == 'm'
106 else:
107 os.makedirs(output_dir)
108 print("Fetching data for %s" % output_dir)
109 output_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), output_dir)
110 for number, meth in enumerate(methods):
111 sys.stdout.write('\r\033[K%s (%d/%d)' % (meth, number+1, len(methods)))
112 sys.stdout.flush()
113 args = method_args.get(meth, [])
114 output_file_raw = os.path.join(output_dir, meth + '.raw')
115 output_file_parsed = os.path.join(output_dir, meth + '.parsed')
116 if os.path.exists(output_file_raw):
117 if missing_only:
118 continue
119 else:
120 os.unlink(output_file_raw)
121 ilo.save_response = output_file_raw
122 try:
123 data = getattr(ilo, meth)(*args)
124 except hpilo.IloFeatureNotSupported:
125 continue
126 except hpilo.IloError as e:
127 if 'syntax error' in e.message:
128 continue
129 with open(output_file_parsed, 'w') as fd:
130 pprint.pprint(data, stream=fd)
131 print("")
132
133 if __name__ == '__main__':
134 main()