Commit 80d55816f2f2a464c0cf6506b836008df97d0365

Authored by Etienne Pallier
1 parent 82400cb8
Exists in dev

agent cleanup and comments

@@ -721,8 +721,7 @@ def test(module, function): @@ -721,8 +721,7 @@ def test(module, function):
721 if module is None: 721 if module is None:
722 # apps = ['obs_config','scp_mgmt','common', 'scheduling', 'seq_submit', 'user_mgmt', 'alert_mgmt.tests.TestStrategyChange'] 722 # apps = ['obs_config','scp_mgmt','common', 'scheduling', 'seq_submit', 'user_mgmt', 'alert_mgmt.tests.TestStrategyChange']
723 # Removing alert_mgmt, scheduler from tests 723 # Removing alert_mgmt, scheduler from tests
724 - apps = ['obs_config', "scp_mgmt",  
725 - 'dashboard', 'user_mgmt', 'seq_submit','api'] 724 + apps = ['obs_config', "scp_mgmt", 'dashboard', 'user_mgmt', 'seq_submit', 'api']
726 else: 725 else:
727 os.environ["PATH_TO_OBSCONF_FILE"] = os.path.join(os.path.abspath( 726 os.environ["PATH_TO_OBSCONF_FILE"] = os.path.join(os.path.abspath(
728 PYROS_DJANGO_BASE_DIR), "obs_config/fixtures/observatory_configuration_ok_simple.yml") 727 PYROS_DJANGO_BASE_DIR), "obs_config/fixtures/observatory_configuration_ok_simple.yml")
@@ -754,7 +753,7 @@ def test(module, function): @@ -754,7 +753,7 @@ def test(module, function):
754 # execProcessFromVenv('manage.py test ' + app) or die() 753 # execProcessFromVenv('manage.py test ' + app) or die()
755 # KEEP test_pyros database after tests 754 # KEEP test_pyros database after tests
756 # execProcessFromVenv('manage.py test --keep ' + app) or die() 755 # execProcessFromVenv('manage.py test --keep ' + app) or die()
757 - execProcessFromVenv('manage.py test --keep --noinput ' + app,foreground=True) or die() 756 + execProcessFromVenv('manage.py test --keep --noinput ' + app, foreground=True) or die()
758 change_dir("PREVIOUS") 757 change_dir("PREVIOUS")
759 # execProcess("python install.py install") 758 # execProcess("python install.py install")
760 return True 759 return True
src/core/pyros_django/majordome/agent/A_Basic.py
@@ -43,13 +43,13 @@ from typing import List, Tuple, Union, Any @@ -43,13 +43,13 @@ from typing import List, Tuple, Union, Any
43 class A_Basic(Agent): 43 class A_Basic(Agent):
44 44
45 # FOR TEST ONLY 45 # FOR TEST ONLY
46 - # Run this agent in simulator mode 46 + # - Run this agent in simulator mode
47 TEST_MODE = False 47 TEST_MODE = False
48 - # Run the assertion tests at the end 48 + # - Run the assertion tests at the end
49 TEST_WITH_FINAL_TEST = False 49 TEST_WITH_FINAL_TEST = False
50 #TEST_MAX_DURATION_SEC = None 50 #TEST_MAX_DURATION_SEC = None
51 TEST_MAX_DURATION_SEC = 400 51 TEST_MAX_DURATION_SEC = 400
52 - # Who should I send commands to ? 52 + # - Who should I send commands to ?
53 TEST_COMMANDS_DEST = "myself" 53 TEST_COMMANDS_DEST = "myself"
54 #TEST_COMMANDS_DEST = "AgentB" 54 #TEST_COMMANDS_DEST = "AgentB"
55 # Scenario to be executed 55 # Scenario to be executed
src/core/pyros_django/majordome/agent/Agent.py
@@ -160,6 +160,7 @@ from vendor.guitastro.src.guitastro import Ephemeris @@ -160,6 +160,7 @@ from vendor.guitastro.src.guitastro import Ephemeris
160 import pickle 160 import pickle
161 161
162 162
  163 +# Aliases for Cmd exceptions
163 CmdException = AgentCmd.CmdException 164 CmdException = AgentCmd.CmdException
164 CmdExceptionUnknown = AgentCmd.CmdExceptionUnknown 165 CmdExceptionUnknown = AgentCmd.CmdExceptionUnknown
165 CmdExceptionUnimplemented = AgentCmd.CmdExceptionUnimplemented 166 CmdExceptionUnimplemented = AgentCmd.CmdExceptionUnimplemented
@@ -218,7 +219,7 @@ class Colors: @@ -218,7 +219,7 @@ class Colors:
218 BOLD = "\033[1m" 219 BOLD = "\033[1m"
219 UNDERLINE = "\033[4m" 220 UNDERLINE = "\033[4m"
220 221
221 -def printColor(color: Colors, message, file=sys.stdout, eol=os.linesep, forced=False): 222 +def print_colored(color: Colors, message, file=sys.stdout, eol=os.linesep, forced=False):
222 #system = platform.system() 223 #system = platform.system()
223 """ 224 """
224 if (self.disp == False and forced == False): 225 if (self.disp == False and forced == False):
@@ -237,10 +238,10 @@ def printFullTerm(color: Colors, string: str): @@ -237,10 +238,10 @@ def printFullTerm(color: Colors, string: str):
237 row = 1000 238 row = 1000
238 disp = True 239 disp = True
239 value = int(columns / 2 - len(string) / 2) 240 value = int(columns / 2 - len(string) / 2)
240 - printColor(color, "-" * value, eol="")  
241 - printColor(color, string, eol="") 241 + print_colored(color, "-" * value, eol="")
  242 + print_colored(color, string, eol="")
242 value += len(string) 243 value += len(string)
243 - printColor(color, "-" * (columns - value)) 244 + print_colored(color, "-" * (columns - value))
244 return 0 245 return 0
245 246
246 247
@@ -301,23 +302,22 @@ class Agent: @@ -301,23 +302,22 @@ class Agent:
301 # --- - INSTANCE attributes are accessible via agent.__dict__ 302 # --- - INSTANCE attributes are accessible via agent.__dict__
302 # --- 303 # ---
303 304
304 -  
305 class EXEC_MODE(Enum): 305 class EXEC_MODE(Enum):
306 SEQUENTIAL = 0 306 SEQUENTIAL = 0
307 THREAD = 1 307 THREAD = 1
308 PROCESS = 2 308 PROCESS = 2
309 309
310 # Default modes 310 # Default modes
311 - DEBUG_MODE:bool = False 311 + DEBUG_MODE: bool = False
312 #TEST_MODE = False 312 #TEST_MODE = False
313 313
314 # By default, a command is valid during 5s (and then perempted) 314 # By default, a command is valid during 5s (and then perempted)
315 - DEFAULT_CMD_VALIDITY_DURATION:int = 5 315 + DEFAULT_CMD_VALIDITY_DURATION: int = 5
316 316
317 # Wait a fixed number of seconds before each loop ? 317 # Wait a fixed number of seconds before each loop ?
318 #WITH_RANDOM_WAIT = False 318 #WITH_RANDOM_WAIT = False
319 # 1 sec by default 319 # 1 sec by default
320 - __DELAY_NB_SEC:int = 1 320 + __DELAY_NB_SEC: int = 1
321 # - YES if TEST mode (in init()) 321 # - YES if TEST mode (in init())
322 322
323 # Default LOG level is INFO 323 # Default LOG level is INFO
@@ -332,7 +332,7 @@ class Agent: @@ -332,7 +332,7 @@ class Agent:
332 - exec_mode (EXEC_MODE) : EXEC_MODE.SEQUENTIAL, EXEC_MODE.THREAD, or EXEC_MODE.PROCESS 332 - exec_mode (EXEC_MODE) : EXEC_MODE.SEQUENTIAL, EXEC_MODE.THREAD, or EXEC_MODE.PROCESS
333 - tooltip : description text (displayed on clic) 333 - tooltip : description text (displayed on clic)
334 ''' 334 '''
335 - _AGENT_SPECIFIC_COMMANDS: Dict [ str, Tuple[int, EXEC_MODE, str] ] = { 335 + _AGENT_SPECIFIC_COMMANDS: Dict[str, Tuple[int, EXEC_MODE, str]] = {
336 # Format : โ€œcmd_nameโ€ : (timeout, exec_mode, tooltip) 336 # Format : โ€œcmd_nameโ€ : (timeout, exec_mode, tooltip)
337 337
338 #"do_specific1" : (10, EXEC_MODE.SEQUENTIAL, ''), 338 #"do_specific1" : (10, EXEC_MODE.SEQUENTIAL, ''),
@@ -352,14 +352,15 @@ class Agent: @@ -352,14 +352,15 @@ class Agent:
352 # Maximum duration of this agent (only for SIMULATION mode) 352 # Maximum duration of this agent (only for SIMULATION mode)
353 # If set to 0, it will never exit except if asked (or CTRL-C) 353 # If set to 0, it will never exit except if asked (or CTRL-C)
354 # If set to 20, it will exit after 20s 354 # If set to 20, it will exit after 20s
355 - TEST_MAX_DURATION_SEC :int=0 355 + TEST_MAX_DURATION_SEC: int = 0
356 #TEST_MAX_DURATION_SEC = 30 356 #TEST_MAX_DURATION_SEC = 30
357 # Run this agent in simulator mode 357 # Run this agent in simulator mode
358 #TEST_MODE = True 358 #TEST_MODE = True
359 - WITH_SIMULATOR:bool = False 359 + WITH_SIMULATOR: bool = False
360 # Run the assertion tests at the end 360 # Run the assertion tests at the end
361 - TEST_WITH_FINAL_TEST:bool = False 361 + TEST_WITH_FINAL_TEST: bool = False
362 362
  363 + # Aliases
363 CMD_STATUS = AgentCmd.CMD_STATUS_CODES 364 CMD_STATUS = AgentCmd.CMD_STATUS_CODES
364 AGT_STATUS = AgentSurvey.STATUS_CHOICES 365 AGT_STATUS = AgentSurvey.STATUS_CHOICES
365 366
@@ -393,8 +394,8 @@ class Agent: @@ -393,8 +394,8 @@ class Agent:
393 ##_TEST_COMMANDS_LIST: List[ Tuple[ bool, str, int, Optional[str], AgentCmd.CMD_STATUS_CODES ] ] = [ 394 ##_TEST_COMMANDS_LIST: List[ Tuple[ bool, str, int, Optional[str], AgentCmd.CMD_STATUS_CODES ] ] = [
394 395
395 # Alias type for _TEST_COMMANDS_LIST (for more readability) 396 # Alias type for _TEST_COMMANDS_LIST (for more readability)
396 - TestCommand = Tuple[ bool, str, Optional[int], Optional[str], Optional[int]]  
397 - _TEST_COMMANDS_LIST: List[ TestCommand ] = [ 397 + TestCommand = Tuple[bool, str, Optional[int], Optional[str], Optional[int]]
  398 + _TEST_COMMANDS_LIST: List[TestCommand] = [
398 # Format : (DO_IT, "self cmd_name cmd_args", validity, "expected_result", expected_status), 399 # Format : (DO_IT, "self cmd_name cmd_args", validity, "expected_result", expected_status),
399 400
400 #("self do_stop now", 200, '15.5', None), 401 #("self do_stop now", 200, '15.5', None),
@@ -591,8 +592,8 @@ class Agent: @@ -591,8 +592,8 @@ class Agent:
591 ##_cmdts: AgentCmd = None 592 ##_cmdts: AgentCmd = None
592 ##_next_cmdts = None 593 ##_next_cmdts = None
593 594
594 - __agent_survey:AgentSurvey = None  
595 - __pending_commands:QuerySet = None # [] 595 + __agent_survey: AgentSurvey = None
  596 + __pending_commands: QuerySet = None # []
596 597
597 # List of agents I will send commands to 598 # List of agents I will send commands to
598 _my_client_agents_aliases = [] 599 _my_client_agents_aliases = []
@@ -610,28 +611,28 @@ class Agent: @@ -610,28 +611,28 @@ class Agent:
610 611
611 # new obsconfig init for agent: 612 # new obsconfig init for agent:
612 ##def __init__(self, RUN_IN_THREAD=True): 613 ##def __init__(self, RUN_IN_THREAD=True):
613 - def __init__(self,simulated_computer=None): 614 + def __init__(self, simulated_computer=None):
614 615
615 # Instance attributes declaration (with default values, or None) 616 # Instance attributes declaration (with default values, or None)
616 - self.__UP_SINCE : Final = datetime.now(tz=timezone.utc) 617 + self.__UP_SINCE: Final = datetime.now(tz=timezone.utc)
617 #self.UP_SINCE = datetime.utcnow() 618 #self.UP_SINCE = datetime.utcnow()
618 - self.__ROUTINE_ITER_START_IS_RUNNING:bool = False  
619 - self.__ROUTINE_ITER_END_IS_RUNNING:bool = False  
620 - self.__test_cmd_received_num:int = 0 # only for tests 619 + self.__ROUTINE_ITER_START_IS_RUNNING: bool = False
  620 + self.__ROUTINE_ITER_END_IS_RUNNING: bool = False
  621 + self.__test_cmd_received_num: int = 0 # only for tests
621 # Current Command running 622 # Current Command running
622 - self.__CC :Optional[AgentCmd] #= None  
623 - self.__CC_thread :Union[StoppableThreadEvenWhenSleeping, multiprocessing.Process] #= None 623 + self.__CC: Optional[AgentCmd] # = None
  624 + self.__CC_thread: Union[StoppableThreadEvenWhenSleeping, multiprocessing.Process] #= None
624 # Previous Command running 625 # Previous Command running
625 ##self.__CC_prev :Optional[AgentCmd] = None 626 ##self.__CC_prev :Optional[AgentCmd] = None
626 # Current Command exception (if occurs) 627 # Current Command exception (if occurs)
627 - self.__CCE :Optional[Exception] #= None 628 + self.__CCE: Optional[Exception] #= None
628 self.name = "Generic Agent" 629 self.name = "Generic Agent"
629 - self.__status :str #= None  
630 - self.__mode :str #= None  
631 - self.unit :str #= None 630 + self.__status: str # = None
  631 + self.__mode: str # = None
  632 + self.unit: str # = None
632 #self.TEST_COMMANDS :List #= None 633 #self.TEST_COMMANDS :List #= None
633 - self.TEST_COMMANDS :Iterable[Agent.TestCommand] #= None  
634 - self.__iter_num :int = 0 634 + self.TEST_COMMANDS: Iterable[Agent.TestCommand] # = None
  635 + self.__iter_num: int = 0
635 #print(AgentSurvey.MODE_CHOICES.IDLE) 636 #print(AgentSurvey.MODE_CHOICES.IDLE)
636 #sys.exit() 637 #sys.exit()
637 638
@@ -639,18 +640,21 @@ class Agent: @@ -639,18 +640,21 @@ class Agent:
639 #self.__mode = self.MODE_ATTENTIVE 640 #self.__mode = self.MODE_ATTENTIVE
640 self.set_mode_attentive() 641 self.set_mode_attentive()
641 #self._set_mode(MODES.) 642 #self._set_mode(MODES.)
  643 +
  644 + # Set Obs config
642 obs_config_file_path = os.environ["PATH_TO_OBSCONF_FILE"] 645 obs_config_file_path = os.environ["PATH_TO_OBSCONF_FILE"]
643 path_to_obs_config_folder = os.environ["PATH_TO_OBSCONF_FOLDER"] 646 path_to_obs_config_folder = os.environ["PATH_TO_OBSCONF_FOLDER"]
644 unit = os.environ["unit_name"] 647 unit = os.environ["unit_name"]
645 - oc = OBSConfig(obs_config_file_path,unit) 648 + oc = OBSConfig(obs_config_file_path, unit)
646 pyros_yaml_path = os.environ["pyros_config_file"] 649 pyros_yaml_path = os.environ["pyros_config_file"]
647 pyros_config = ConfigPyros(pyros_yaml_path) 650 pyros_config = ConfigPyros(pyros_yaml_path)
648 self.set_config(oc, obs_config_file_path, path_to_obs_config_folder, unit, pyros_config, pyros_yaml_path) 651 self.set_config(oc, obs_config_file_path, path_to_obs_config_folder, unit, pyros_config, pyros_yaml_path)
  652 +
  653 + # Agent name
649 agent_name_from_config = self.get_config().get_agent_name_from_config(self.__class__.__name__,simulated_computer) 654 agent_name_from_config = self.get_config().get_agent_name_from_config(self.__class__.__name__,simulated_computer)
650 - if agent_name_from_config:  
651 - self.name = agent_name_from_config  
652 - else:  
653 - self.name = self.__class__.__name__ 655 + self.name = agent_name_from_config if agent_name_from_config else self.__class__.__name__
  656 +
  657 + # LOG
654 log.addHandler(handler_filebyagent(logging.INFO, self.name)) 658 log.addHandler(handler_filebyagent(logging.INFO, self.name))
655 #log.addHandler(handler_filebyagent(logging.INFO, self.name)) 659 #log.addHandler(handler_filebyagent(logging.INFO, self.name))
656 log.debug("start Agent init") 660 log.debug("start Agent init")
@@ -670,10 +674,11 @@ class Agent: @@ -670,10 +674,11 @@ class Agent:
670 674
671 self.TEST_COMMANDS = iter(self._TEST_COMMANDS_LIST) 675 self.TEST_COMMANDS = iter(self._TEST_COMMANDS_LIST)
672 ##self.RUN_IN_THREAD = RUN_IN_THREAD 676 ##self.RUN_IN_THREAD = RUN_IN_THREAD
  677 +
673 self.__set_status(self.AGT_STATUS.LAUNCHED) 678 self.__set_status(self.AGT_STATUS.LAUNCHED)
674 ####self._set_idle() 679 ####self._set_idle()
675 680
676 - # Create 1st survey if none 681 + # Get survey or create 1st one if none
677 #tmp = AgentSurvey.objects.filter(name=self.name) 682 #tmp = AgentSurvey.objects.filter(name=self.name)
678 #if len(tmp) == 0: 683 #if len(tmp) == 0:
679 #nb_agents = AgentSurvey.objects.filter(name=self.name).count() 684 #nb_agents = AgentSurvey.objects.filter(name=self.name).count()
@@ -857,18 +862,16 @@ class Agent: @@ -857,18 +862,16 @@ class Agent:
857 @property 862 @property
858 def ROUTINE_ITER_START_IS_RUNNING(self): 863 def ROUTINE_ITER_START_IS_RUNNING(self):
859 return self.__ROUTINE_ITER_START_IS_RUNNING 864 return self.__ROUTINE_ITER_START_IS_RUNNING
  865 +
860 @property 866 @property
861 def ROUTINE_ITER_END_IS_RUNNING(self): 867 def ROUTINE_ITER_END_IS_RUNNING(self):
862 return self.__ROUTINE_ITER_END_IS_RUNNING 868 return self.__ROUTINE_ITER_END_IS_RUNNING
863 869
864 -  
865 -  
866 -  
867 def set_config(self, oc: OBSConfig, obs_config_file_path: str, path_to_obs_config_folder: str, unit: str, pyros_config: str, pyros_yaml_path: str): 870 def set_config(self, oc: OBSConfig, obs_config_file_path: str, path_to_obs_config_folder: str, unit: str, pyros_config: str, pyros_yaml_path: str):
868 self._oc = { 871 self._oc = {
869 - 'config' : oc,  
870 - 'pyros_config' : pyros_config,  
871 - 'env' : [ 872 + 'config': oc,
  873 + 'pyros_config': pyros_config,
  874 + 'env': [
872 obs_config_file_path, 875 obs_config_file_path,
873 path_to_obs_config_folder, 876 path_to_obs_config_folder,
874 unit, 877 unit,
@@ -962,10 +965,6 @@ class Agent: @@ -962,10 +965,6 @@ class Agent:
962 print("\n") 965 print("\n")
963 966
964 967
965 -  
966 -  
967 -  
968 -  
969 def get_config_filename(self, config_filename: str): 968 def get_config_filename(self, config_filename: str):
970 if not config_filename: 969 if not config_filename:
971 #config_filename = self.DEFAULT_CONFIG_FILE_NAME 970 #config_filename = self.DEFAULT_CONFIG_FILE_NAME
@@ -1013,7 +1012,7 @@ class Agent: @@ -1013,7 +1012,7 @@ class Agent:
1013 1012
1014 1013
1015 1014
1016 - def _get_real_agent_name(self, agent_alias_name:str)->str: 1015 + def _get_real_agent_name(self, agent_alias_name: str) -> str:
1017 #self.printd("key is", agent_alias_name) 1016 #self.printd("key is", agent_alias_name)
1018 ''' 1017 '''
1019 if not self._my_client_agents: return agent_alias_name 1018 if not self._my_client_agents: return agent_alias_name
@@ -1023,7 +1022,7 @@ class Agent: @@ -1023,7 +1022,7 @@ class Agent:
1023 1022
1024 1023
1025 1024
1026 - def run(self, nb_iter:int=None, FOR_REAL:bool=True): 1025 + def run(self, nb_iter: int = None, FOR_REAL: bool = True):
1027 """ 1026 """
1028 FOR_REAL: set to False if you don't want Agent to send commands to devices but just print messages without really doing anything 1027 FOR_REAL: set to False if you don't want Agent to send commands to devices but just print messages without really doing anything
1029 """ 1028 """