Commit a97898806e31f324d5c3e725ac2e6e8d3c29e2c2
1 parent
35daee3f
Exists in
dev
Add new way to start agent in pyros (option 'observatory'), Pyros will automatic…
…ally search for an observatory configuration file starting with 'observatory'
Showing
5 changed files
with
44 additions
and
12 deletions
Show diff stats
.gitignore
docker/PYROS_DOCKER_RUN
pyros.py
... | ... | @@ -12,7 +12,7 @@ import subprocess |
12 | 12 | import sys |
13 | 13 | import time |
14 | 14 | import re |
15 | - | |
15 | +import glob | |
16 | 16 | |
17 | 17 | |
18 | 18 | |
... | ... | @@ -27,7 +27,6 @@ import re |
27 | 27 | UPDATE = False |
28 | 28 | |
29 | 29 | PYROS_DJANGO_BASE_DIR = "src/core/pyros_django" |
30 | - | |
31 | 30 | INIT_FIXTURE = "initial_fixture_TZ.json" |
32 | 31 | |
33 | 32 | _previous_dir = None |
... | ... | @@ -512,6 +511,8 @@ def install_or_update(UPDATE:bool=False, packages_only:bool=False, database_only |
512 | 511 | @pyros_launcher.command(help="Run some tests") |
513 | 512 | def test(): |
514 | 513 | print("Running tests") |
514 | + os.environ["PATH_TO_OBSCONF_FOLDER"] = os.path.join(os.path.abspath(PYROS_DJANGO_BASE_DIR),"obsconfig/fixtures/") | |
515 | + | |
515 | 516 | #start_dir = os.getcwd() |
516 | 517 | apps = ['obsconfig','common', 'scheduler', 'routine_manager', 'user_manager', 'alert_manager.tests.TestStrategyChange'] |
517 | 518 | for app in apps: |
... | ... | @@ -599,10 +600,11 @@ def initdb(): |
599 | 600 | #@global_test_options |
600 | 601 | @click.argument('agent') |
601 | 602 | @click.option('--configfile', '-c', help='the configuration file to be used') |
603 | +@click.option('--observatory', '-o', help='the observatory name to be used') | |
602 | 604 | #@click.option('--format', '-f', type=click.Choice(['html', 'xml', 'text']), default='html', show_default=True) |
603 | 605 | #@click.option('--port', default=8000) |
604 | 606 | #def start(agent:str, configfile:str, test, verbosity): |
605 | -def start(agent:str, configfile:str): | |
607 | +def start(agent:str, configfile:str,observatory:str): | |
606 | 608 | printd("Running start command") |
607 | 609 | if configfile: |
608 | 610 | printd("With config file", configfile) |
... | ... | @@ -610,8 +612,28 @@ def start(agent:str, configfile:str): |
610 | 612 | configfile = '' |
611 | 613 | #if test_mode(): print("in test mode") |
612 | 614 | #if verbose_mode(): print("in verbose mode") |
615 | + if observatory == None or len(observatory) == 0: | |
616 | + observatory = "default" | |
617 | + observatories_configuration_folder = os.path.join(os.path.abspath(PYROS_DJANGO_BASE_DIR),"../../../privatedev/config/") | |
618 | + if len(glob.glob(observatories_configuration_folder+observatory+"/")) != 1: | |
619 | + # Observatory configuration folder not found | |
620 | + print(f"Observatory configuration folder for observatory '{observatory}' not found in {observatories_configuration_folder}") | |
621 | + print("Available observatories configuration :") | |
622 | + for obs_conf_folder in os.listdir(observatories_configuration_folder): | |
623 | + print(obs_conf_folder) | |
624 | + exit(1) | |
625 | + | |
626 | + path_to_obs_config_folder = observatories_configuration_folder+"/"+observatory+"/" | |
627 | + obs_config_file_name = "" | |
628 | + # Search for observatory config file | |
629 | + obs_config_file_name = glob.glob(path_to_obs_config_folder+"/observatory*.yml")[0] | |
630 | + | |
631 | + obs_config_file_path = os.path.join(path_to_obs_config_folder,obs_config_file_name) | |
632 | + os.environ["PATH_TO_OBSCONF_FILE"] = obs_config_file_path | |
633 | + os.environ["PATH_TO_OBSCONF_FOLDER"] = path_to_obs_config_folder | |
613 | 634 | # add path to pyros_django folder as the config class is supposed to work within this folder |
614 | - cmd_test_obs_config = f"-c \"from src.core.pyros_django.obsconfig.configpyros import ConfigPyros\nConfigPyros('{os.path.join(PYROS_DJANGO_BASE_DIR,os.environ.get('PATH_TO_OBSCONF_FILE'))}')\"" | |
635 | + #cmd_test_obs_config = f"-c \"from src.core.pyros_django.obsconfig.configpyros import ConfigPyros\nConfigPyros('{os.path.join(PYROS_DJANGO_BASE_DIR,os.environ.get('PATH_TO_OBSCONF_FILE'))}')\"" | |
636 | + cmd_test_obs_config = f"-c \"from src.core.pyros_django.obsconfig.configpyros import ConfigPyros\nConfigPyros('{obs_config_file_path}')\"" | |
615 | 637 | if not execProcessFromVenv(cmd_test_obs_config): |
616 | 638 | # Observatory configuration has an issue |
617 | 639 | exit(1) | ... | ... |
src/core/pyros_django/obsconfig/configpyros.py
1 | 1 | #!/usr/bin/env python3 |
2 | 2 | import pykwalify.core |
3 | 3 | import yaml,sys,logging,os, pickle, time |
4 | +from datetime import datetime | |
4 | 5 | from pykwalify.errors import PyKwalifyException,SchemaError |
5 | - | |
6 | +from pathlib import Path | |
6 | 7 | class ConfigPyros: |
7 | 8 | # (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 | 9 | devices_links = {} |
... | ... | @@ -14,6 +15,7 @@ class ConfigPyros: |
14 | 15 | devices = None |
15 | 16 | computers = None |
16 | 17 | agents = None |
18 | + obs_config_path = os.environ["PATH_TO_OBSCONF_FOLDER"] | |
17 | 19 | def verify_if_pickle_needs_to_be_updated(self,observatory_config_file)->bool: |
18 | 20 | """[summary] |
19 | 21 | |
... | ... | @@ -29,10 +31,17 @@ class ConfigPyros: |
29 | 31 | else: |
30 | 32 | pickle_file_mtime = os.path.getmtime(self.CONFIG_PATH+self.pickle_file) |
31 | 33 | obs_config_mtime = os.path.getmtime(observatory_config_file) |
32 | - if obs_config_mtime > pickle_file_mtime: | |
33 | - return True | |
34 | 34 | |
35 | 35 | obs_config = self.read_and_check_config_file(observatory_config_file) |
36 | + if obs_config_mtime > pickle_file_mtime: | |
37 | + # create obs file (yaml) from pickle["obsconfig"] with date of pickle within history folder-> nom ficher + année + mois + jour + datetime (avec secondes) -> YYYY/MM/DD H:m:s | |
38 | + pickle_datetime = datetime.fromtimestamp(pickle_file_mtime).strftime("%Y%m%d_%H%M%S") | |
39 | + # Create history folder if doesn't exist | |
40 | + Path(self.obs_config_path+"/history/").mkdir(parents=True, exist_ok=True) | |
41 | + file_name = f"{self.obs_config_path}/history/observatory_{pickle_datetime}.yml" | |
42 | + with open(file_name, 'w') as f: | |
43 | + data = yaml.dump(obs_config, f) | |
44 | + return True | |
36 | 45 | if obs_config == None: |
37 | 46 | print(f"Error when trying to read config file (path of config file : {observatory_config_file}") |
38 | 47 | return -1 |
... | ... | @@ -374,7 +383,6 @@ class ConfigPyros: |
374 | 383 | print(f"device name: '{device_name}', device filename: '{file_name}'") |
375 | 384 | exit(1) |
376 | 385 | # associate attached device to his 'parent' device (parent device is the currently read device) |
377 | - print(self.devices_links) | |
378 | 386 | self.devices_links[attached_device_name] = parent_device_name |
379 | 387 | for capability in capabilities_of_attached_device: |
380 | 388 | # add capabilities of attached device to current device | ... | ... |