From be09bdbce773e489d51ac15d0f7d515ad6ed4808 Mon Sep 17 00:00:00 2001 From: Alexis Koralewski Date: Fri, 16 Jul 2021 18:10:12 +0200 Subject: [PATCH] Check if observatory configuration is valid by instantiating the configuration in pyros.py (when starting an agent), better handling of environment variables (for example : if the .env exists but is empty, pyros will act like it doesn't exist), the pickle of a configuration is stored in the same folder of the corresponding observatory configuration file --- pyros.py | 12 ++++++++---- src/core/pyros_django/obsconfig/configpyros.py | 26 ++++++++++++++------------ src/core/pyros_django/obsconfig/templates/obsconfig/device_details.html | 1 - src/core/pyros_django/pyros/settings.py | 6 +++++- 4 files changed, 27 insertions(+), 18 deletions(-) diff --git a/pyros.py b/pyros.py index 192f8d6..bfcb29b 100755 --- a/pyros.py +++ b/pyros.py @@ -12,7 +12,7 @@ import subprocess import sys import time import re - +from src.core.pyros_django.obsconfig.configpyros import ConfigPyros @@ -308,6 +308,10 @@ def set_environment_variables_if_not_configured(env_path: str,env_sample_path: s print("Some environment variables are not configured...") try: with open(env_path,"r") as env_file: + # env file is empty + if len(env_file) <= 0: + raise BaseException() + print("Reading env file") for line in env_file: if(line.startswith("#") and not line.strip()): @@ -317,7 +321,7 @@ def set_environment_variables_if_not_configured(env_path: str,env_sample_path: s # setting variables as environment variables os.environ[key] = value except: - print(f".env not found at {ENV_PATH}, creating a file at this path from the .env-sample file stored at {ENV_SAMPLE_PATH}\n \ + print(f".env not found at {ENV_PATH} or is empty, creating a file at this path from the .env-sample file stored at {ENV_SAMPLE_PATH}\n \ values from .env-sample will be used as environment variables") with open(env_sample_path,'r') as env_sample_file: with open(env_path,"w") as env_file: @@ -333,7 +337,6 @@ def set_environment_variables_if_not_configured(env_path: str,env_sample_path: s - """ ******************************************************************************** ******************** CLI COMMANDS DEFINITION (click format) ******************** @@ -598,7 +601,8 @@ def start(agent:str, configfile:str): configfile = '' #if test_mode(): print("in test mode") #if verbose_mode(): print("in verbose mode") - + # add path to pyros_django folder as the config class is supposed to work within this folder + ConfigPyros(PYROS_DJANGO_BASE_DIR+"/"+os.environ.get("PATH_TO_OBSCONF_FILE")) # Check each agent in the given list agents = agent.split(",") if "," in agent else [agent] for a in agents: diff --git a/src/core/pyros_django/obsconfig/configpyros.py b/src/core/pyros_django/obsconfig/configpyros.py index 85f838c..e6bdc4c 100644 --- a/src/core/pyros_django/obsconfig/configpyros.py +++ b/src/core/pyros_django/obsconfig/configpyros.py @@ -5,7 +5,7 @@ from pykwalify.errors import PyKwalifyException,SchemaError class ConfigPyros: # (AKo) : Config file path is checked on the settings file, if the file isn't valid (i.e not found) the error will be launched by the settings file when starting the application - # read_files is an history of which config files has been read, where the key is the source file which is linked to a list of files associated to this key + devices_links = {} current_file = None COMPONENT_PATH = "../../../../config/components/" @@ -24,16 +24,17 @@ class ConfigPyros: Returns: bool: [description] """ - if os.path.isfile(self.pickle_file) == False: + self.CONFIG_PATH = os.path.dirname(observatory_config_file)+"/" + if os.path.isfile(self.CONFIG_PATH+self.pickle_file) == False: return True else: - pickle_file_mtime = os.path.getmtime(self.pickle_file) + pickle_file_mtime = os.path.getmtime(self.CONFIG_PATH+self.pickle_file) obs_config_mtime = os.path.getmtime(observatory_config_file) if obs_config_mtime > pickle_file_mtime: return True obs_config = self.read_and_check_config_file(observatory_config_file) - if obs_config is None: + if obs_config == None: print(f"Error when trying to read config file (path of config file : {observatory_config_file}") return -1 self.obs_config = obs_config @@ -53,13 +54,13 @@ class ConfigPyros: def load(self,observatory_config_file): does_pickle_needs_to_be_updated = self.verify_if_pickle_needs_to_be_updated(observatory_config_file) - if os.path.isfile(self.pickle_file) and does_pickle_needs_to_be_updated == False: + if os.path.isfile(self.CONFIG_PATH+self.pickle_file) and does_pickle_needs_to_be_updated == False: print("Reading pickle file") try: can_pickle_file_be_read = False while can_pickle_file_be_read != True: - if os.access(self.pickle_file, os.R_OK): - pickle_dict = pickle.load(open(self.pickle_file,"rb")) + if os.access(self.CONFIG_PATH+self.pickle_file, os.R_OK): + pickle_dict = pickle.load(open(self.CONFIG_PATH+self.pickle_file,"rb")) can_pickle_file_be_read = True else: time.sleep(0.5) @@ -80,7 +81,7 @@ class ConfigPyros: pickle_dict["computers"] = self.get_computers() pickle_dict["devices_links"] = self.devices_links print("Writing pickle file") - pickle.dump(pickle_dict,open(self.pickle_file,"wb")) + pickle.dump(pickle_dict,open(self.CONFIG_PATH+self.pickle_file,"wb")) def check_and_return_config(self,yaml_file:str,schema_file:str)->dict: """ @@ -143,9 +144,9 @@ class ConfigPyros: self.COMPONENT_PATH = os.path.join(os.path.abspath(os.path.dirname(yaml_file)),"../../../config/components/") self.GENERIC_DEVICES_PATH = os.path.join(os.path.abspath(os.path.dirname(yaml_file)),"../../../config/devices/") result = self.check_and_return_config(yaml_file,self.SCHEMA_PATH+config_file["schema"]) - if result is None: + if result == None: print("Error when reading and validating config file, please check the errors right above") - + exit(1) return result except yaml.YAMLError as exc: @@ -245,8 +246,9 @@ class ConfigPyros: self.SCHEMA_PATH = os.path.join(os.path.abspath(os.path.dirname(config_file_name)),"../../../config/schemas/") result = self.check_and_return_config(config_file_name,self.SCHEMA_PATH+config_file["schema"]) - if result is None: + if result == None: print("Error when reading and validating config file, please check the errors right above") + exit(-1) else: device = result["DEVICE"] @@ -286,7 +288,7 @@ class ConfigPyros: capabilities_of_attached_device = None if "CAPABILITIES" in config_of_attached_device["DEVICE"].keys(): capabilities_of_attached_device = config_of_attached_device["DEVICE"]["CAPABILITIES"] - if capabilities_of_attached_device is not None: + if capabilities_of_attached_device != None: # get name of device corresponding to the config file name parent_device_name = [device for device,file_name in devices_name_and_file.items() if file_name == config_file_name[len(self.CONFIG_PATH):] ][0] diff --git a/src/core/pyros_django/obsconfig/templates/obsconfig/device_details.html b/src/core/pyros_django/obsconfig/templates/obsconfig/device_details.html index 4458958..8659100 100644 --- a/src/core/pyros_django/obsconfig/templates/obsconfig/device_details.html +++ b/src/core/pyros_django/obsconfig/templates/obsconfig/device_details.html @@ -19,7 +19,6 @@ th,td{ {% if key == "device_config" %} {% with device_detail|get_item:key as device_config %} - {{ devices_links }} {% for key,value in device_config.items %} {% if key == "ATTACHED_DEVICES" and device_name in active_devices %} diff --git a/src/core/pyros_django/pyros/settings.py b/src/core/pyros_django/pyros/settings.py index ffa98a7..2c1e93d 100644 --- a/src/core/pyros_django/pyros/settings.py +++ b/src/core/pyros_django/pyros/settings.py @@ -65,6 +65,10 @@ def set_environment_variables_if_not_configured(env_path: str,env_sample_path: s print("Some environment variables are not configured...") try: with open(env_path,"r") as env_file: + # env file is empty + if len(env_file) <= 0: + raise BaseException() + print("Reading env file") for line in env_file: if(line.startswith("#") and not line.strip()): @@ -74,7 +78,7 @@ def set_environment_variables_if_not_configured(env_path: str,env_sample_path: s # setting variables as environment variables os.environ[key] = value except: - print(f".env not found at {ENV_PATH}, creating a file at this path from the .env-sample file stored at {ENV_SAMPLE_PATH}\n \ + print(f".env not found at {ENV_PATH} or is empty, creating a file at this path from the .env-sample file stored at {ENV_SAMPLE_PATH}\n \ values from .env-sample will be used as environment variables") with open(env_sample_path,'r') as env_sample_file: with open(env_path,"w") as env_file: -- libgit2 0.21.2