Commit be09bdbce773e489d51ac15d0f7d515ad6ed4808
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
Showing
4 changed files
with
27 additions
and
18 deletions
Show diff stats
pyros.py
... | ... | @@ -12,7 +12,7 @@ import subprocess |
12 | 12 | import sys |
13 | 13 | import time |
14 | 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 | 308 | print("Some environment variables are not configured...") |
309 | 309 | try: |
310 | 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 | 315 | print("Reading env file") |
312 | 316 | for line in env_file: |
313 | 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 | 321 | # setting variables as environment variables |
318 | 322 | os.environ[key] = value |
319 | 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 | 325 | values from .env-sample will be used as environment variables") |
322 | 326 | with open(env_sample_path,'r') as env_sample_file: |
323 | 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 | 337 | |
334 | 338 | |
335 | 339 | |
336 | - | |
337 | 340 | """ |
338 | 341 | ******************************************************************************** |
339 | 342 | ******************** CLI COMMANDS DEFINITION (click format) ******************** |
... | ... | @@ -598,7 +601,8 @@ def start(agent:str, configfile:str): |
598 | 601 | configfile = '' |
599 | 602 | #if test_mode(): print("in test mode") |
600 | 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 | 606 | # Check each agent in the given list |
603 | 607 | agents = agent.split(",") if "," in agent else [agent] |
604 | 608 | for a in agents: | ... | ... |
src/core/pyros_django/obsconfig/configpyros.py
... | ... | @@ -5,7 +5,7 @@ from pykwalify.errors import PyKwalifyException,SchemaError |
5 | 5 | |
6 | 6 | class ConfigPyros: |
7 | 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 | 9 | devices_links = {} |
10 | 10 | current_file = None |
11 | 11 | COMPONENT_PATH = "../../../../config/components/" |
... | ... | @@ -24,16 +24,17 @@ class ConfigPyros: |
24 | 24 | Returns: |
25 | 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 | 29 | return True |
29 | 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 | 32 | obs_config_mtime = os.path.getmtime(observatory_config_file) |
32 | 33 | if obs_config_mtime > pickle_file_mtime: |
33 | 34 | return True |
34 | 35 | |
35 | 36 | obs_config = self.read_and_check_config_file(observatory_config_file) |
36 | - if obs_config is None: | |
37 | + if obs_config == None: | |
37 | 38 | print(f"Error when trying to read config file (path of config file : {observatory_config_file}") |
38 | 39 | return -1 |
39 | 40 | self.obs_config = obs_config |
... | ... | @@ -53,13 +54,13 @@ class ConfigPyros: |
53 | 54 | |
54 | 55 | def load(self,observatory_config_file): |
55 | 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 | 58 | print("Reading pickle file") |
58 | 59 | try: |
59 | 60 | can_pickle_file_be_read = False |
60 | 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 | 64 | can_pickle_file_be_read = True |
64 | 65 | else: |
65 | 66 | time.sleep(0.5) |
... | ... | @@ -80,7 +81,7 @@ class ConfigPyros: |
80 | 81 | pickle_dict["computers"] = self.get_computers() |
81 | 82 | pickle_dict["devices_links"] = self.devices_links |
82 | 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 | 86 | def check_and_return_config(self,yaml_file:str,schema_file:str)->dict: |
86 | 87 | """ |
... | ... | @@ -143,9 +144,9 @@ class ConfigPyros: |
143 | 144 | self.COMPONENT_PATH = os.path.join(os.path.abspath(os.path.dirname(yaml_file)),"../../../config/components/") |
144 | 145 | self.GENERIC_DEVICES_PATH = os.path.join(os.path.abspath(os.path.dirname(yaml_file)),"../../../config/devices/") |
145 | 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 | 148 | print("Error when reading and validating config file, please check the errors right above") |
148 | - | |
149 | + exit(1) | |
149 | 150 | return result |
150 | 151 | |
151 | 152 | except yaml.YAMLError as exc: |
... | ... | @@ -245,8 +246,9 @@ class ConfigPyros: |
245 | 246 | self.SCHEMA_PATH = os.path.join(os.path.abspath(os.path.dirname(config_file_name)),"../../../config/schemas/") |
246 | 247 | |
247 | 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 | 250 | print("Error when reading and validating config file, please check the errors right above") |
251 | + exit(-1) | |
250 | 252 | else: |
251 | 253 | device = result["DEVICE"] |
252 | 254 | |
... | ... | @@ -286,7 +288,7 @@ class ConfigPyros: |
286 | 288 | capabilities_of_attached_device = None |
287 | 289 | if "CAPABILITIES" in config_of_attached_device["DEVICE"].keys(): |
288 | 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 | 292 | # get name of device corresponding to the config file name |
291 | 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 | 19 | {% if key == "device_config" %} |
20 | 20 | |
21 | 21 | {% with device_detail|get_item:key as device_config %} |
22 | - {{ devices_links }} | |
23 | 22 | {% for key,value in device_config.items %} |
24 | 23 | |
25 | 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 | 65 | print("Some environment variables are not configured...") |
66 | 66 | try: |
67 | 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 | 72 | print("Reading env file") |
69 | 73 | for line in env_file: |
70 | 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 | 78 | # setting variables as environment variables |
75 | 79 | os.environ[key] = value |
76 | 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 | 82 | values from .env-sample will be used as environment variables") |
79 | 83 | with open(env_sample_path,'r') as env_sample_file: |
80 | 84 | with open(env_path,"w") as env_file: | ... | ... |