iLO-manager first main commitgit status
This commit is contained in:
1 parent
e6f4cc04ca
commit
599d8da562
3271 files changed
+550655
No files matched your search
@@ -0,0 +1 @@
|
||||
"""Config parsing for RDMC"""
|
||||
Binary file not shown.
Binary file not shown.
BIN
Binary file not shown.
@@ -0,0 +1,159 @@
|
||||
###
|
||||
# Copyright 2016-2021 Hewlett Packard Enterprise, Inc. All rights reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
###
|
||||
|
||||
# -*- coding: utf-8 -*-
|
||||
"""Module for working with global configuration options."""
|
||||
|
||||
# ---------Imports---------
|
||||
|
||||
import logging
|
||||
import os
|
||||
import re
|
||||
|
||||
import six
|
||||
from six.moves import configparser
|
||||
|
||||
# ---------End of imports---------
|
||||
|
||||
|
||||
# ---------Debug logger---------
|
||||
|
||||
LOGGER = logging.getLogger(__name__)
|
||||
|
||||
# ---------End of debug logger---------
|
||||
|
||||
|
||||
class AutoConfigParser(object):
|
||||
"""Auto configuration parser. Properties starting with _ac__ are automatically
|
||||
serialized to config file"""
|
||||
|
||||
_config_pattern = re.compile(r"_ac__(?P<confkey>.*)")
|
||||
|
||||
def __init__(self, filename=None):
|
||||
"""Initialize AutoConfigParser
|
||||
|
||||
:param filename: file name to be used for config loading.
|
||||
:type filename: str.
|
||||
|
||||
"""
|
||||
self._sectionname = "globals"
|
||||
self._configfile = filename
|
||||
|
||||
@property
|
||||
def configfile(self):
|
||||
"""The current configuration file location"""
|
||||
return self._configfile
|
||||
|
||||
def _get_ac_keys(self):
|
||||
"""Retrieve parse option keys"""
|
||||
result = []
|
||||
for key in six.iterkeys(self.__dict__):
|
||||
match = AutoConfigParser._config_pattern.search(key)
|
||||
if match:
|
||||
result.append(match.group("confkey"))
|
||||
return result
|
||||
|
||||
def _get(self, key):
|
||||
"""Retrieve parse option key
|
||||
|
||||
:param key: key to retrieve.
|
||||
:type key: str.
|
||||
|
||||
"""
|
||||
ackey = "_ac__%s" % key.replace("-", "_")
|
||||
if ackey in self.__dict__:
|
||||
return self.__dict__[ackey]
|
||||
return None
|
||||
|
||||
def _set(self, key, value):
|
||||
"""Set parse option key
|
||||
|
||||
:param key: key to be set.
|
||||
:type key: str.
|
||||
:param value: value to be given to key.
|
||||
:type value: str.
|
||||
|
||||
"""
|
||||
ackey = "_ac__%s" % key.replace("-", "_")
|
||||
if ackey in self.__dict__:
|
||||
self.__dict__[ackey] = value
|
||||
return None
|
||||
|
||||
def load(self, filename=None):
|
||||
"""Load configuration settings from the file filename, if filename
|
||||
is None then the value from configfile property is used
|
||||
|
||||
:param filename: file name to be used for config loading.
|
||||
:type filename: str.
|
||||
|
||||
"""
|
||||
fname = self.configfile
|
||||
if filename:
|
||||
fname = filename
|
||||
|
||||
if not fname or not os.path.isfile(fname):
|
||||
return
|
||||
|
||||
try:
|
||||
config = configparser.RawConfigParser()
|
||||
config.read(fname)
|
||||
for key in self._get_ac_keys():
|
||||
configval = None
|
||||
try:
|
||||
configval = config.get(self._sectionname, key)
|
||||
except configparser.NoOptionError:
|
||||
# also try with - instead of _
|
||||
try:
|
||||
configval = config.get(self._sectionname, key.replace("_", "-"))
|
||||
except configparser.NoOptionError:
|
||||
pass
|
||||
|
||||
if configval:
|
||||
ackey = "_ac__%s" % key
|
||||
self.__dict__[ackey] = configval
|
||||
except configparser.NoOptionError:
|
||||
pass
|
||||
except configparser.NoSectionError:
|
||||
pass
|
||||
|
||||
def save(self, filename=None):
|
||||
"""Save configuration settings from the file filename, if filename
|
||||
is None then the value from configfile property is used
|
||||
|
||||
:param filename: file name to be used for config saving.
|
||||
:type filename: str.
|
||||
|
||||
"""
|
||||
fname = self.configfile
|
||||
if filename:
|
||||
fname = filename
|
||||
|
||||
if fname:
|
||||
return
|
||||
|
||||
config = configparser.RawConfigParser()
|
||||
try:
|
||||
config.add_section(self._sectionname)
|
||||
except configparser.DuplicateSectionError:
|
||||
pass # ignored
|
||||
|
||||
for key in self._get_ac_keys():
|
||||
ackey = "_ac__%s" % key
|
||||
config.set(self._sectionname, key, str(self.__dict__[ackey]))
|
||||
|
||||
fileh = open(self._configfile, "wb")
|
||||
config.write(fileh)
|
||||
fileh.close()
|
||||
@@ -0,0 +1,288 @@
|
||||
###
|
||||
# Copyright 2016-2021 Hewlett Packard Enterprise, Inc. All rights reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
###
|
||||
|
||||
# -*- coding: utf-8 -*-
|
||||
"""Rdmc config"""
|
||||
|
||||
import os
|
||||
import json
|
||||
|
||||
try:
|
||||
from config.config import AutoConfigParser
|
||||
from logging_config_path import get_logging_config_path
|
||||
except ImportError:
|
||||
from ilorest.config.config import AutoConfigParser
|
||||
from ilorest.logging_config_path import get_logging_config_path
|
||||
|
||||
|
||||
class RdmcConfig(AutoConfigParser):
|
||||
"""Rdmc config class for loading and parsing the .conf file global configuration options.
|
||||
Uses the AutoConfigParser."""
|
||||
|
||||
def _load_logdir_from_json(self):
|
||||
"""Load logdir from logging configuration JSON file.
|
||||
|
||||
:returns: logdir path from JSON config, or current directory as fallback
|
||||
:rtype: str
|
||||
"""
|
||||
config_path = get_logging_config_path()
|
||||
try:
|
||||
if os.path.isfile(config_path):
|
||||
with open(config_path, "r", encoding="utf-8") as f:
|
||||
config = json.load(f)
|
||||
handlers = config.get("handlers", {})
|
||||
file_handler = handlers.get("file", {})
|
||||
filename = file_handler.get("filename", "")
|
||||
if "%(logdir)s" in filename:
|
||||
logdir = file_handler.get("filename").split("%(logdir)s")[0].rstrip("/\\")
|
||||
return logdir if logdir else os.getcwd()
|
||||
else:
|
||||
# If filename does not contain %(logdir)s, extract directory from filename
|
||||
return os.path.dirname(filename) if os.path.dirname(filename) else os.getcwd()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# Fallback to current working directory if any issues
|
||||
return os.getcwd()
|
||||
|
||||
def __init__(self, filename=None):
|
||||
"""Initialize RdmcConfig
|
||||
|
||||
:param filename: file name to be used for Rdmcconfig loading.
|
||||
:type filename: str
|
||||
|
||||
"""
|
||||
AutoConfigParser.__init__(self, filename=filename)
|
||||
self._sectionname = "redfish"
|
||||
self._configfile = filename
|
||||
self._ac__logdir = self._load_logdir_from_json()
|
||||
self._ac__cache = True
|
||||
self._ac__url = ""
|
||||
self._ac__username = ""
|
||||
self._ac__password = ""
|
||||
self._ac__sslcert = ""
|
||||
self._ac__commit = ""
|
||||
self._ac__format = ""
|
||||
self._ac__cachedir = ""
|
||||
self._ac__savefile = ""
|
||||
self._ac__loadfile = ""
|
||||
self._ac__user_cert = ""
|
||||
self._ac__user_root_ca_key = ""
|
||||
self._ac__user_root_ca_password = ""
|
||||
|
||||
@property
|
||||
def configfile(self):
|
||||
"""The current configuration file"""
|
||||
return self._configfile
|
||||
|
||||
@configfile.setter
|
||||
def configfile(self, config_file):
|
||||
"""Set the current configuration file
|
||||
|
||||
:param config_file: file name to be used for Rmcconfig loading.
|
||||
:type config_file: str
|
||||
"""
|
||||
self._configfile = config_file
|
||||
|
||||
@property
|
||||
def logdir(self):
|
||||
"""Get the current log directory"""
|
||||
return self._get("logdir")
|
||||
|
||||
@logdir.setter
|
||||
def logdir(self, value):
|
||||
"""Set the current log directory
|
||||
:param value: current working directory for logging
|
||||
:type value: str
|
||||
"""
|
||||
return self._set("logdir", value)
|
||||
|
||||
@property
|
||||
def cache(self):
|
||||
"""Get the config file cache status"""
|
||||
|
||||
if isinstance(self._get("cache"), bool):
|
||||
return self._get("cache")
|
||||
|
||||
return self._get("cache").lower() in ("yes", "true", "t", "1")
|
||||
|
||||
@cache.setter
|
||||
def cache(self, value):
|
||||
"""Get the config file cache status
|
||||
|
||||
:param value: status of config file cache
|
||||
:type value: bool
|
||||
"""
|
||||
return self._set("cache", value)
|
||||
|
||||
@property
|
||||
def url(self):
|
||||
"""Get the config file URL"""
|
||||
url = self._get("url")
|
||||
url = url[:-1] if url.endswith("/") else url
|
||||
|
||||
return url
|
||||
|
||||
@url.setter
|
||||
def url(self, value):
|
||||
"""Set the config file URL
|
||||
|
||||
:param value: URL path for the config file
|
||||
:type value: str
|
||||
"""
|
||||
return self._set("url", value)
|
||||
|
||||
@property
|
||||
def username(self):
|
||||
"""Get the config file user name"""
|
||||
return self._get("username")
|
||||
|
||||
@username.setter
|
||||
def username(self, value):
|
||||
"""Set the config file user name
|
||||
|
||||
:param value: user name for config file
|
||||
:type value: str
|
||||
"""
|
||||
return self._set("username", value)
|
||||
|
||||
@property
|
||||
def password(self):
|
||||
"""Get the config file password"""
|
||||
return self._get("password")
|
||||
|
||||
@password.setter
|
||||
def password(self, value):
|
||||
"""Set the config file password
|
||||
|
||||
:param value: password for config file
|
||||
:type value: str
|
||||
"""
|
||||
return self._set("password", value)
|
||||
|
||||
@property
|
||||
def commit(self):
|
||||
"""Get the config file commit status"""
|
||||
return self._get("commit")
|
||||
|
||||
@commit.setter
|
||||
def commit(self, value):
|
||||
"""Set the config file commit status
|
||||
|
||||
:param value: commit status
|
||||
:type value: str
|
||||
"""
|
||||
return self._set("commit", value)
|
||||
|
||||
@property
|
||||
def format(self):
|
||||
"""Get the config file default format"""
|
||||
return self._get("format")
|
||||
|
||||
@format.setter
|
||||
def format(self, value):
|
||||
"""Set the config file default format
|
||||
|
||||
:param value: set the config file format
|
||||
:type value: str
|
||||
"""
|
||||
return self._set("format", value)
|
||||
|
||||
@property
|
||||
def cachedir(self):
|
||||
"""Get the config file cache directory"""
|
||||
return self._get("cachedir")
|
||||
|
||||
@cachedir.setter
|
||||
def cachedir(self, value):
|
||||
"""Set the config file cache directory
|
||||
|
||||
:param value: config file cache directory
|
||||
:type value: str
|
||||
"""
|
||||
return self._set("cachedir", value)
|
||||
|
||||
@property
|
||||
def defaultsavefilename(self):
|
||||
"""Get the config file default save name"""
|
||||
return self._get("savefile")
|
||||
|
||||
@defaultsavefilename.setter
|
||||
def defaultsavefilename(self, value):
|
||||
"""Set the config file default save name
|
||||
|
||||
:param value: config file save name
|
||||
:type value: str
|
||||
"""
|
||||
return self._set("savefile", value)
|
||||
|
||||
@property
|
||||
def defaultloadfilename(self):
|
||||
"""Get the config file default load name"""
|
||||
return self._get("loadfile")
|
||||
|
||||
@defaultloadfilename.setter
|
||||
def defaultloadfilename(self, value):
|
||||
"""Set the config file default load name
|
||||
|
||||
:param value: name of config file to load by default
|
||||
:type value: str
|
||||
"""
|
||||
return self._set("loadfile", value)
|
||||
|
||||
@property
|
||||
def proxy(self):
|
||||
"""Get proxy value to be set for communication"""
|
||||
return self._get("proxy")
|
||||
|
||||
@proxy.setter
|
||||
def proxy(self, value):
|
||||
"""Set proxy value for communication"""
|
||||
return self._set("proxy", value)
|
||||
|
||||
@property
|
||||
def ssl_cert(self):
|
||||
"""Get proxy value to be set for communication"""
|
||||
return self._get("sslcert")
|
||||
|
||||
@ssl_cert.setter
|
||||
def ssl_cert(self, value):
|
||||
"""Set proxy value for communication"""
|
||||
return self._set("sslcert", value)
|
||||
|
||||
@property
|
||||
def user_cert(self):
|
||||
return self._get("usercert")
|
||||
|
||||
@user_cert.setter
|
||||
def user_cert(self, value):
|
||||
return self._set("usercert", value)
|
||||
|
||||
@property
|
||||
def user_root_ca_key(self):
|
||||
return self._get("user_root_ca_key")
|
||||
|
||||
@user_root_ca_key.setter
|
||||
def user_root_ca_key(self, value):
|
||||
return self._set("user_root_ca_key", value)
|
||||
|
||||
@property
|
||||
def user_root_ca_password(self):
|
||||
return self._get("user_root_ca_password")
|
||||
|
||||
@user_root_ca_password.setter
|
||||
def user_root_ca_password(self, value):
|
||||
return self._set("user_root_ca_password", value)
|
||||
Reference in new issue
Block a user