Commit be09bdbce773e489d51ac15d0f7d515ad6ed4808

Authored by Alexis Koralewski
1 parent 72519c93
Exists in dev

Check if observatory configuration is valid by instantiating the configuration i…

…n 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
@@ -12,7 +12,7 @@ import subprocess @@ -12,7 +12,7 @@ import subprocess
12 import sys 12 import sys
13 import time 13 import time
14 import re 14 import re
15 - 15 +from src.core.pyros_django.obsconfig.configpyros import ConfigPyros
16 16
17 17
18 18
@@ -308,6 +308,10 @@ def set_environment_variables_if_not_configured(env_path: str,env_sample_path: s @@ -308,6 +308,10 @@ def set_environment_variables_if_not_configured(env_path: str,env_sample_path: s
308 print("Some environment variables are not configured...") 308 print("Some environment variables are not configured...")
309 try: 309 try:
310 with open(env_path,"r") as env_file: 310 with open(env_path,"r") as env_file:
  311 + # env file is empty
  312 + if len(env_file) <= 0:
  313 + raise BaseException()
  314 +
311 print("Reading env file") 315 print("Reading env file")
312 for line in env_file: 316 for line in env_file:
313 if(line.startswith("#") and not line.strip()): 317 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 @@ -317,7 +321,7 @@ def set_environment_variables_if_not_configured(env_path: str,env_sample_path: s
317 # setting variables as environment variables 321 # setting variables as environment variables
318 os.environ[key] = value 322 os.environ[key] = value
319 except: 323 except:
320 - 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 \ 324 + 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 \
321 values from .env-sample will be used as environment variables") 325 values from .env-sample will be used as environment variables")
322 with open(env_sample_path,'r') as env_sample_file: 326 with open(env_sample_path,'r') as env_sample_file:
323 with open(env_path,"w") as env_file: 327 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 @@ -333,7 +337,6 @@ def set_environment_variables_if_not_configured(env_path: str,env_sample_path: s
333 337
334 338
335 339
336 -  
337 """ 340 """
338 ******************************************************************************** 341 ********************************************************************************
339 ******************** CLI COMMANDS DEFINITION (click format) ******************** 342 ******************** CLI COMMANDS DEFINITION (click format) ********************
@@ -598,7 +601,8 @@ def start(agent:str, configfile:str): @@ -598,7 +601,8 @@ def start(agent:str, configfile:str):
598 configfile = '' 601 configfile = ''
599 #if test_mode(): print("in test mode") 602 #if test_mode(): print("in test mode")
600 #if verbose_mode(): print("in verbose mode") 603 #if verbose_mode(): print("in verbose mode")
601 - 604 + # add path to pyros_django folder as the config class is supposed to work within this folder
  605 + ConfigPyros(PYROS_DJANGO_BASE_DIR+"/"+os.environ.get("PATH_TO_OBSCONF_FILE"))
602 # Check each agent in the given list 606 # Check each agent in the given list
603 agents = agent.split(",") if "," in agent else [agent] 607 agents = agent.split(",") if "," in agent else [agent]
604 for a in agents: 608 for a in agents:
src/core/pyros_django/obsconfig/configpyros.py
@@ -5,7 +5,7 @@ from pykwalify.errors import PyKwalifyException,SchemaError @@ -5,7 +5,7 @@ from pykwalify.errors import PyKwalifyException,SchemaError
5 5
6 class ConfigPyros: 6 class ConfigPyros:
7 # (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 7 # (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
8 - # 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 8 +
9 devices_links = {} 9 devices_links = {}
10 current_file = None 10 current_file = None
11 COMPONENT_PATH = "../../../../config/components/" 11 COMPONENT_PATH = "../../../../config/components/"
@@ -24,16 +24,17 @@ class ConfigPyros: @@ -24,16 +24,17 @@ class ConfigPyros:
24 Returns: 24 Returns:
25 bool: [description] 25 bool: [description]
26 """ 26 """
27 - if os.path.isfile(self.pickle_file) == False: 27 + self.CONFIG_PATH = os.path.dirname(observatory_config_file)+"/"
  28 + if os.path.isfile(self.CONFIG_PATH+self.pickle_file) == False:
28 return True 29 return True
29 else: 30 else:
30 - pickle_file_mtime = os.path.getmtime(self.pickle_file) 31 + pickle_file_mtime = os.path.getmtime(self.CONFIG_PATH+self.pickle_file)
31 obs_config_mtime = os.path.getmtime(observatory_config_file) 32 obs_config_mtime = os.path.getmtime(observatory_config_file)
32 if obs_config_mtime > pickle_file_mtime: 33 if obs_config_mtime > pickle_file_mtime:
33 return True 34 return True
34 35
35 obs_config = self.read_and_check_config_file(observatory_config_file) 36 obs_config = self.read_and_check_config_file(observatory_config_file)
36 - if obs_config is None: 37 + if obs_config == None:
37 print(f"Error when trying to read config file (path of config file : {observatory_config_file}") 38 print(f"Error when trying to read config file (path of config file : {observatory_config_file}")
38 return -1 39 return -1
39 self.obs_config = obs_config 40 self.obs_config = obs_config
@@ -53,13 +54,13 @@ class ConfigPyros: @@ -53,13 +54,13 @@ class ConfigPyros:
53 54
54 def load(self,observatory_config_file): 55 def load(self,observatory_config_file):
55 does_pickle_needs_to_be_updated = self.verify_if_pickle_needs_to_be_updated(observatory_config_file) 56 does_pickle_needs_to_be_updated = self.verify_if_pickle_needs_to_be_updated(observatory_config_file)
56 - if os.path.isfile(self.pickle_file) and does_pickle_needs_to_be_updated == False: 57 + if os.path.isfile(self.CONFIG_PATH+self.pickle_file) and does_pickle_needs_to_be_updated == False:
57 print("Reading pickle file") 58 print("Reading pickle file")
58 try: 59 try:
59 can_pickle_file_be_read = False 60 can_pickle_file_be_read = False
60 while can_pickle_file_be_read != True: 61 while can_pickle_file_be_read != True:
61 - if os.access(self.pickle_file, os.R_OK):  
62 - pickle_dict = pickle.load(open(self.pickle_file,"rb")) 62 + if os.access(self.CONFIG_PATH+self.pickle_file, os.R_OK):
  63 + pickle_dict = pickle.load(open(self.CONFIG_PATH+self.pickle_file,"rb"))
63 can_pickle_file_be_read = True 64 can_pickle_file_be_read = True
64 else: 65 else:
65 time.sleep(0.5) 66 time.sleep(0.5)
@@ -80,7 +81,7 @@ class ConfigPyros: @@ -80,7 +81,7 @@ class ConfigPyros:
80 pickle_dict["computers"] = self.get_computers() 81 pickle_dict["computers"] = self.get_computers()
81 pickle_dict["devices_links"] = self.devices_links 82 pickle_dict["devices_links"] = self.devices_links
82 print("Writing pickle file") 83 print("Writing pickle file")
83 - pickle.dump(pickle_dict,open(self.pickle_file,"wb")) 84 + pickle.dump(pickle_dict,open(self.CONFIG_PATH+self.pickle_file,"wb"))
84 85
85 def check_and_return_config(self,yaml_file:str,schema_file:str)->dict: 86 def check_and_return_config(self,yaml_file:str,schema_file:str)->dict:
86 """ 87 """
@@ -143,9 +144,9 @@ class ConfigPyros: @@ -143,9 +144,9 @@ class ConfigPyros:
143 self.COMPONENT_PATH = os.path.join(os.path.abspath(os.path.dirname(yaml_file)),"../../../config/components/") 144 self.COMPONENT_PATH = os.path.join(os.path.abspath(os.path.dirname(yaml_file)),"../../../config/components/")
144 self.GENERIC_DEVICES_PATH = os.path.join(os.path.abspath(os.path.dirname(yaml_file)),"../../../config/devices/") 145 self.GENERIC_DEVICES_PATH = os.path.join(os.path.abspath(os.path.dirname(yaml_file)),"../../../config/devices/")
145 result = self.check_and_return_config(yaml_file,self.SCHEMA_PATH+config_file["schema"]) 146 result = self.check_and_return_config(yaml_file,self.SCHEMA_PATH+config_file["schema"])
146 - if result is None: 147 + if result == None:
147 print("Error when reading and validating config file, please check the errors right above") 148 print("Error when reading and validating config file, please check the errors right above")
148 - 149 + exit(1)
149 return result 150 return result
150 151
151 except yaml.YAMLError as exc: 152 except yaml.YAMLError as exc:
@@ -245,8 +246,9 @@ class ConfigPyros: @@ -245,8 +246,9 @@ class ConfigPyros:
245 self.SCHEMA_PATH = os.path.join(os.path.abspath(os.path.dirname(config_file_name)),"../../../config/schemas/") 246 self.SCHEMA_PATH = os.path.join(os.path.abspath(os.path.dirname(config_file_name)),"../../../config/schemas/")
246 247
247 result = self.check_and_return_config(config_file_name,self.SCHEMA_PATH+config_file["schema"]) 248 result = self.check_and_return_config(config_file_name,self.SCHEMA_PATH+config_file["schema"])
248 - if result is None: 249 + if result == None:
249 print("Error when reading and validating config file, please check the errors right above") 250 print("Error when reading and validating config file, please check the errors right above")
  251 + exit(-1)
250 else: 252 else:
251 device = result["DEVICE"] 253 device = result["DEVICE"]
252 254
@@ -286,7 +288,7 @@ class ConfigPyros: @@ -286,7 +288,7 @@ class ConfigPyros:
286 capabilities_of_attached_device = None 288 capabilities_of_attached_device = None
287 if "CAPABILITIES" in config_of_attached_device["DEVICE"].keys(): 289 if "CAPABILITIES" in config_of_attached_device["DEVICE"].keys():
288 capabilities_of_attached_device = config_of_attached_device["DEVICE"]["CAPABILITIES"] 290 capabilities_of_attached_device = config_of_attached_device["DEVICE"]["CAPABILITIES"]
289 - if capabilities_of_attached_device is not None: 291 + if capabilities_of_attached_device != None:
290 # get name of device corresponding to the config file name 292 # get name of device corresponding to the config file name
291 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] 293 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]
292 294
src/core/pyros_django/obsconfig/templates/obsconfig/device_details.html
@@ -19,7 +19,6 @@ th,td{ @@ -19,7 +19,6 @@ th,td{
19 {% if key == "device_config" %} 19 {% if key == "device_config" %}
20 20
21 {% with device_detail|get_item:key as device_config %} 21 {% with device_detail|get_item:key as device_config %}
22 - {{ devices_links }}  
23 {% for key,value in device_config.items %} 22 {% for key,value in device_config.items %}
24 23
25 {% if key == "ATTACHED_DEVICES" and device_name in active_devices %} 24 {% if key == "ATTACHED_DEVICES" and device_name in active_devices %}
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 @@ -65,6 +65,10 @@ def set_environment_variables_if_not_configured(env_path: str,env_sample_path: s
65 print("Some environment variables are not configured...") 65 print("Some environment variables are not configured...")
66 try: 66 try:
67 with open(env_path,"r") as env_file: 67 with open(env_path,"r") as env_file:
  68 + # env file is empty
  69 + if len(env_file) <= 0:
  70 + raise BaseException()
  71 +
68 print("Reading env file") 72 print("Reading env file")
69 for line in env_file: 73 for line in env_file:
70 if(line.startswith("#") and not line.strip()): 74 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 @@ -74,7 +78,7 @@ def set_environment_variables_if_not_configured(env_path: str,env_sample_path: s
74 # setting variables as environment variables 78 # setting variables as environment variables
75 os.environ[key] = value 79 os.environ[key] = value
76 except: 80 except:
77 - 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 \ 81 + 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 \
78 values from .env-sample will be used as environment variables") 82 values from .env-sample will be used as environment variables")
79 with open(env_sample_path,'r') as env_sample_file: 83 with open(env_sample_path,'r') as env_sample_file:
80 with open(env_path,"w") as env_file: 84 with open(env_path,"w") as env_file: