From 378b688e4893ca9e6f4272b2660910e92c15b0af Mon Sep 17 00:00:00 2001 From: uggla Date: Sat, 11 Jul 2015 22:34:12 +0200 Subject: [PATCH] TODO and logger modifications. Add TODO ideas in the code. Create a config.logger global object to log from anywhere in the code. --- alexandria/app.py | 31 +++++++++++++++++++------------ alexandria/config.py | 19 +++++++++++-------- alexandria/configuration_item.py | 3 +++ alexandria/drivers.py | 8 ++++++++ alexandria/models.py | 13 ++++++++----- 5 files changed, 49 insertions(+), 25 deletions(-) diff --git a/alexandria/app.py b/alexandria/app.py index 2908e2b..c7aafc9 100644 --- a/alexandria/app.py +++ b/alexandria/app.py @@ -14,6 +14,9 @@ import configuration_item app = Flask(__name__) app.debug = True +# Affect app logger to a global variable so logger can be used elsewhere. +config.logger = app.logger + @app.route("/drivers", methods = ["GET"]) def api_drivers(): @@ -39,13 +42,12 @@ def api_driver(driver_name): @app.route('/shutdown', methods=['POST']) def shutdown(): shutdown_server() - app.logger.info("Stopping Alexandria...") + app.logger.info("Stopping %s...", config.alexandria.NAME) return 'Server shutting down...' @app.route('/bruno', methods=['POST']) def bruno(): coucou = "Coucou " + request.json["Server"] - #coucou = "Coucou" return coucou @app.route('/ci', methods=['POST']) @@ -53,7 +55,7 @@ def create_ci(): pp = pprint.PrettyPrinter(indent=4) ci = configuration_item.ConfigurationItem(request.json["uuid"], - request.json["ip_mgmt"], + request.json["url_mgmt"], request.json["login"], request.json["password"]) @@ -86,7 +88,9 @@ def synchronize_ci(ci): for driver in config.alexandria.drivers: app.logger.info("Get information from {} driver.".format(driver.get_driver_type())) driver.get_ci(ci) - + + # TODO : implement checksum to not push data if there is no change. + # Push the data provided above to all our drivers for driver in config.alexandria.drivers: app.logger.info("Push information to {} driver.".format(driver.get_driver_type())) @@ -100,8 +104,8 @@ def update_ci(): @app.route("/", methods = ["GET"]) def api_root(): data = { - "Service" : config.alexandria.name, - "Version" : config.alexandria.version + "Service" : config.alexandria.NAME, + "Version" : config.alexandria.VERSION } resp = jsonify(data) @@ -138,11 +142,16 @@ if __name__ == "__main__": pp = pprint.PrettyPrinter(indent=4) # Define a structure to handle ci - alexandria_cis = {} - + # TODO : derivate a ci_collection class from dict. + # (same mechanism as drivers) + alexandria_cis = {} + + # Initialise, so create a global config.alexandria object. + config.initialise_alexandria() + # Configure Flask logger configure_logger(app.logger, app_logfile) - + config.alexandria.model.logger = app.logger # TODO : Debugging stuff to remove later. print config.alexandria.model.reference_items @@ -151,8 +160,6 @@ if __name__ == "__main__": print config.alexandria.drivers.itop.driver_type #pp.pprint(models.EthernetInterface) # debugging example. #pp.pprint(models.Manager) # debugging example. - app.logger.info("Starting Alexandria...") + app.logger.info("Starting %s...", config.alexandria.NAME) app.run(port=int(config.alexandria.conf_file.get_alexandria_port())) - - \ No newline at end of file diff --git a/alexandria/config.py b/alexandria/config.py index 88fcd13..dbe4ff8 100644 --- a/alexandria/config.py +++ b/alexandria/config.py @@ -5,11 +5,20 @@ import ConfigParser import models import drivers +# Initialise global variable +logger = None +alexandria = None + +def initialise_alexandria(): + """Define alexandria global object so it can be called from anywhere.""" + global alexandria + # TODO : at a protection to not initialise twice. + alexandria = Alexandria() class Alexandria(object): def __init__(self): - self.name = "Alexandria" - self.version = "0.1" + self.NAME = "Alexandria" + self.VERSION = "0.1" # Model self.model = models.Model() @@ -51,9 +60,3 @@ class AlexandriaConfiguration(object): def get_alexandria_port(self): return self.config.get("alexandria", "port") - - -# Initialise global variable -# Define alexandria global object so it can be called from anywhere. -alexandria = Alexandria() - diff --git a/alexandria/configuration_item.py b/alexandria/configuration_item.py index b67207b..7647653 100644 --- a/alexandria/configuration_item.py +++ b/alexandria/configuration_item.py @@ -14,6 +14,9 @@ class ConfigurationItem(object): self.ci_parents = [] # List to store parents ci self.ci_children = [] # List to store children ci + # TODO : Maintain a structure to query only the drivers that make + # sens for the CI. + @property def ci_type(self): return self.__ci_type diff --git a/alexandria/drivers.py b/alexandria/drivers.py index 295764a..a48a37d 100644 --- a/alexandria/drivers.py +++ b/alexandria/drivers.py @@ -76,11 +76,19 @@ class Fakeprovider(Driver): ci.data = config.alexandria.model.get_model("Manager") # Update the structure with data + # TODO : think to encapsulate to not edit ci.data directly. + # This could be also a way to check source of truth. + # If data provided by our driver is not the source of truth + # then discard it. + + ci.data["ManagerType"] = "BMC" ci.data["Model"] = "Néné Manager" ci.data["FirmwareVersion"] = "1.00" + + #if ci.data is config.alexandria.model.Manager: # print "identical" diff --git a/alexandria/models.py b/alexandria/models.py index 7b8ad97..3b80fc3 100644 --- a/alexandria/models.py +++ b/alexandria/models.py @@ -4,23 +4,26 @@ import json import glob import os import re - +import config class Model(object): """Implements Alexandria reference model.""" def __init__(self): - + self.logger = "" self.reference_items = [] - self.read_ref_files() - pass + def read_ref_files(self): cwd = os.getcwd() # Hierarchy model/std/*.json + # TODO : Manage several version of redfish. + # Load the latest model in memory by default. + # Update get_model with a version parameter + # so if an older version is required load it. model_files = glob.glob(cwd + "/model/redfish/*.json") for file in model_files: - print("Loading model file : {}".format(file)) + config.logger.debug("Loading model file : {}".format(file)) # Derive attribute name from file. attr_name = os.path.basename(file) attr_name = re.sub(r"\..*$", "", attr_name)