Update redfish-client.py

This commit is contained in:
Uggla 2015-10-16 00:40:36 +02:00
parent 6eaccc3ad2
commit 065a61442a

93
redfish-client/redfish-client.py Normal file → Executable file
View File

@ -1,3 +1,5 @@
#!/usr/bin/python
# coding=utf-8
"""
@ -19,7 +21,7 @@ Options:
--conf_file FILE Configuration file [default: ~/.redfish.conf].
config commands manage the configuration file.
config commands manage the configuration file.
"""
@ -32,17 +34,65 @@ import docopt
class ConfigFile(object):
def __init__(self, config_file):
self._config_file = config_file
# read json file
try:
with open(config_file) as json_data:
try:
with open(self._config_file) as json_data:
self.data = json.load(json_data)
json_data.close()
except IOError as e:
self.data = {"Nodes":{}}
except (ValueError, IOError):
self.data = {"Managers":{}}
def save(self):
try:
with open(self._config_file , 'w') as json_data:
json.dump(self.data, json_data)
json_data.close()
except IOError as e:
print(e.msg)
sys.exit(1)
def add_manager(self, manager_name, url, login, password):
self.data['Managers'][manager_name] = {}
self.data['Managers'][manager_name]['url'] = url
if login != None:
self.data['Managers'][manager_name]['login'] = login
if password != None:
self.data['Managers'][manager_name]['password'] = password
def get_managers(self):
managers = []
for manager in self.data['Managers']:
managers += [manager]
return(managers)
def get_manager_info(self, manager):
info = {}
url=self.data['Managers'][manager]['url']
login=self.data['Managers'][manager]['login']
password=self.data['Managers'][manager]['password']
info={'url':url, 'login':login, 'password':password}
return(info)
class RedfishClientException(Exception):
"""Base class for redfish client exceptions"""
def __init__(self, message=None, **kwargs):
self.kwargs = kwargs
self.message = message
if __name__ == '__main__':
# Functions
def show_manager(all=False):
print("Managers configured :")
for manager in conf_file.get_managers():
print(manager)
if all == True:
info = conf_file.get_manager_info(manager)
print("\tUrl : {}".format(info['url']))
print("\tLogin : {}".format(info['login']))
print("\tPassword : {}".format(info['password']))
# Get $HOME environment.
HOME = os.getenv('HOME')
@ -52,22 +102,25 @@ if __name__ == '__main__':
arguments = docopt.docopt(__doc__, version='redfish-client 0.1')
print(arguments)
arguments['--conf_file'] = arguments['--conf_file'].replace('~', HOME)
conf_file = ConfigFile(arguments['--conf_file'])
if arguments['config'] == True:
if arguments['showall'] == True:
pprint.pprint(conf_file.data)
if arguments['show'] == True:
show_manager()
elif arguments['showall'] == True:
show_manager(True)
elif arguments['add'] == True:
conf_file.data['Nodes'][arguments['<manager_name>']] = {}
conf_file.data['Nodes'][arguments['<manager_name>']]['url'] = arguments['<manager_url>']
if arguments['<login>'] != None:
conf_file.data['Nodes'][arguments['<manager_name>']]['login'] = arguments['<login>']
if arguments['<login>'] != None:
conf_file.data['Nodes'][arguments['<manager_name>']]['password'] = arguments['<password>']
conf_file.add_manager(arguments['<manager_name>'],
arguments['<manager_url>'],
arguments['<login>'],
arguments['password'])
pprint.pprint(conf_file.data)
conf_file.save()
sys.exit(0)