Commit 9cd823945709f54db309ff9562f0242d305c8869

Authored by Alexis Koralewski
1 parent f2843971
Exists in dev

Add new option for agentSST : start a specific agent

@@ -1016,8 +1016,9 @@ def start(agent: str, configfile: str, observatory: str, unit: str, computer_hos @@ -1016,8 +1016,9 @@ def start(agent: str, configfile: str, observatory: str, unit: str, computer_hos
1016 @click.option('--observatory', '-o', help='the observatory name to be used') 1016 @click.option('--observatory', '-o', help='the observatory name to be used')
1017 @click.option('--unit', '-u', help='the unit name to be used') 1017 @click.option('--unit', '-u', help='the unit name to be used')
1018 @click.option("--computer_hostname","-cp", help="The name of simulated computer hostname") 1018 @click.option("--computer_hostname","-cp", help="The name of simulated computer hostname")
  1019 +@click.option("--agent","-a", help="The name of agent to be launched")
1019 @click.option("--foreground","-fg", is_flag=True, help="Print stdout and error in terminal") 1020 @click.option("--foreground","-fg", is_flag=True, help="Print stdout and error in terminal")
1020 -def new_start(configfile: str, observatory: str, unit: str, computer_hostname: str,foreground:bool): 1021 +def new_start(configfile: str, observatory: str, unit: str, computer_hostname: str,foreground:bool,agent:str):
1021 log.debug("Running start command") 1022 log.debug("Running start command")
1022 try: 1023 try:
1023 from config.pyros.config_pyros import ConfigPyros 1024 from config.pyros.config_pyros import ConfigPyros
@@ -1116,6 +1117,8 @@ def new_start(configfile: str, observatory: str, unit: str, computer_hostname: s @@ -1116,6 +1117,8 @@ def new_start(configfile: str, observatory: str, unit: str, computer_hostname: s
1116 cmd += " -v" 1117 cmd += " -v"
1117 if computer_hostname: 1118 if computer_hostname:
1118 cmd += f" --computer {computer_hostname}" 1119 cmd += f" --computer {computer_hostname}"
  1120 + if agent:
  1121 + cmd += f" --agent {agent}"
1119 1122
1120 # if not test_mode(): current_processes.append( [execProcessFromVenvAsync(cmd), agent_name, -1] ) 1123 # if not test_mode(): current_processes.append( [execProcessFromVenvAsync(cmd), agent_name, -1] )
1121 # Append this process ( [process id, agent_name, result=failure] ) 1124 # Append this process ( [process id, agent_name, result=failure] )
src/core/pyros_django/agent/Agent.py
@@ -2721,7 +2721,7 @@ def extract_parameters(): @@ -2721,7 +2721,7 @@ def extract_parameters():
2721 2721
2722 #def build_agent(Agent_type:Agent, name="GenericAgent", RUN_IN_THREAD=True): 2722 #def build_agent(Agent_type:Agent, name="GenericAgent", RUN_IN_THREAD=True):
2723 #def build_agent(Agent_type:Agent, RUN_IN_THREAD=True): 2723 #def build_agent(Agent_type:Agent, RUN_IN_THREAD=True):
2724 -def build_agent(Agent_type:Agent) -> Agent : 2724 +def build_agent(Agent_type:Agent,param_constr=None) -> Agent :
2725 #DEBUG_MODE, WITH_SIM, TEST_MODE, VERBOSE_MODE, configfile = extract_parameters() 2725 #DEBUG_MODE, WITH_SIM, TEST_MODE, VERBOSE_MODE, configfile = extract_parameters()
2726 DEBUG_MODE, TEST_MODE, VERBOSE_MODE = extract_parameters() 2726 DEBUG_MODE, TEST_MODE, VERBOSE_MODE = extract_parameters()
2727 log.debug("debug mode is" + os.getenv("PYROS_DEBUG")) 2727 log.debug("debug mode is" + os.getenv("PYROS_DEBUG"))
@@ -2732,11 +2732,16 @@ def build_agent(Agent_type:Agent) -> Agent : @@ -2732,11 +2732,16 @@ def build_agent(Agent_type:Agent) -> Agent :
2732 #agent = Agent("GenericAgent", configfile, RUN_IN_THREAD=True) 2732 #agent = Agent("GenericAgent", configfile, RUN_IN_THREAD=True)
2733 #agent = Agent_type(configfile, RUN_IN_THREAD, DEBUG_MODE=DEBUG_MODE) 2733 #agent = Agent_type(configfile, RUN_IN_THREAD, DEBUG_MODE=DEBUG_MODE)
2734 #agent = Agent_type(RUN_IN_THREAD) 2734 #agent = Agent_type(RUN_IN_THREAD)
2735 -  
2736 - agent = Agent_type() 2735 + if param_constr:
  2736 + if "AgentSST" in str(Agent_type):
  2737 + agent = Agent_type(agent=param_constr.get("agent"))
  2738 + else:
  2739 + agent = Agent_type()
2737 if agent.name == "AgentSST": 2740 if agent.name == "AgentSST":
  2741 + # args are passed two times: first in AgentSST script and then to build_agent function, we need to setup again argparse in order to argparse to allow arguments
2738 parser = argparse.ArgumentParser(description='Start a agentSST.') 2742 parser = argparse.ArgumentParser(description='Start a agentSST.')
2739 parser.add_argument("--computer",dest="computer",help='Launch agent with simulated computer hostname',action="store") 2743 parser.add_argument("--computer",dest="computer",help='Launch agent with simulated computer hostname',action="store")
  2744 + parser.add_argument("--agent",dest="agent",help='Launch an specific agent ',action="store")
2740 parser.add_argument("-t", action="store_true" ) 2745 parser.add_argument("-t", action="store_true" )
2741 2746
2742 # parser.add_argument("-d", action="store_true" ) 2747 # parser.add_argument("-d", action="store_true" )
src/core/pyros_django/agent/AgentSST.py
@@ -2,7 +2,8 @@ @@ -2,7 +2,8 @@
2 2
3 from pathlib import Path 3 from pathlib import Path
4 import subprocess 4 import subprocess
5 -import sys, os 5 +import sys, os, argparse
  6 +
6 from datetime import datetime, timezone, timedelta 7 from datetime import datetime, timezone, timedelta
7 ##import utils.Logger as L 8 ##import utils.Logger as L
8 #import threading #, multiprocessing, os 9 #import threading #, multiprocessing, os
@@ -33,7 +34,7 @@ class AgentSST(Agent): @@ -33,7 +34,7 @@ class AgentSST(Agent):
33 TEST_COMMANDS_LIST = [ 34 TEST_COMMANDS_LIST = [
34 ] 35 ]
35 36
36 - def __init__(self, name:str=None,sim_computer=None): 37 + def __init__(self, name:str=None, sim_computer=None, agent=None):
37 38
38 super().__init__() 39 super().__init__()
39 self.PROJECT_ROOT_PATH = os.environ["PROJECT_ROOT_PATH"] 40 self.PROJECT_ROOT_PATH = os.environ["PROJECT_ROOT_PATH"]
@@ -59,13 +60,20 @@ class AgentSST(Agent): @@ -59,13 +60,20 @@ class AgentSST(Agent):
59 + os.sep 60 + os.sep
60 ) 61 )
61 self.VENV_PYTHON = VENV_BIN + "python3" 62 self.VENV_PYTHON = VENV_BIN + "python3"
  63 + if agent:
  64 + self.init_agent = agent
  65 + else:
  66 + self.init_agent = None
62 #log.info(f"PC hostname is {self.computer}") 67 #log.info(f"PC hostname is {self.computer}")
63 #name_from_config = self.get_config().get_agent_sst_of_current_computer() 68 #name_from_config = self.get_config().get_agent_sst_of_current_computer()
64 69
65 def init(self): 70 def init(self):
66 super().init() 71 super().init()
67 log.info(f"PC hostname is {self.computer}") 72 log.info(f"PC hostname is {self.computer}")
68 - self.start_agents() 73 + if self.init_agent is None:
  74 + self.start_agents()
  75 + else:
  76 + self.start_agents(self.init_agent)
69 self.TEST_MODE = False 77 self.TEST_MODE = False
70 time.sleep(10) 78 time.sleep(10)
71 self.set_delay(3) 79 self.set_delay(3)
@@ -84,6 +92,11 @@ class AgentSST(Agent): @@ -84,6 +92,11 @@ class AgentSST(Agent):
84 92
85 if agent_name: 93 if agent_name:
86 agent = agent_name 94 agent = agent_name
  95 + agents = obs_config.get_agents_per_computer(obs_config.unit_name).get(self.computer)
  96 + if agent not in agents:
  97 + log.info(f"{agent} isn't associated to this computer : {self.computer}")
  98 + log.info(f"Agents associated to this computer : {agents}")
  99 + exit(1)
87 # Start a specific agent of obs_config (restart) 100 # Start a specific agent of obs_config (restart)
88 agent_informations = obs_config.get_agent_information(obs_config.unit_name,agent) 101 agent_informations = obs_config.get_agent_information(obs_config.unit_name,agent)
89 protocol = agent_informations.get("protocol") 102 protocol = agent_informations.get("protocol")
@@ -94,12 +107,13 @@ class AgentSST(Agent): @@ -94,12 +107,13 @@ class AgentSST(Agent):
94 if os.path.exists(protocol_folder_abs_path + os.sep + protocol_script_name): 107 if os.path.exists(protocol_folder_abs_path + os.sep + protocol_script_name):
95 cmd = self.VENV_PYTHON +" "+ protocol_folder_abs_path + os.sep + protocol_script_name 108 cmd = self.VENV_PYTHON +" "+ protocol_folder_abs_path + os.sep + protocol_script_name
96 if not agent in self.agent_in_mode_test: 109 if not agent in self.agent_in_mode_test:
97 - self.agent_in_mode_test = self.TEST_MODE 110 + self.agent_in_mode_test[agent] = self.TEST_MODE
98 if self.agent_in_mode_test[agent]: 111 if self.agent_in_mode_test[agent]:
99 cmd += test_mode 112 cmd += test_mode
100 process = subprocess.Popen(f"{cmd}",shell=True) 113 process = subprocess.Popen(f"{cmd}",shell=True)
101 process.poll() 114 process.poll()
102 - 115 + if agent not in self.subprocess_dict:
  116 + self.subprocess_dict[agent] = {}
103 self.subprocess_dict[agent]["process"] = process 117 self.subprocess_dict[agent]["process"] = process
104 nb_try_restart = self.subprocess_dict[agent].get("nb_try_restart",0) 118 nb_try_restart = self.subprocess_dict[agent].get("nb_try_restart",0)
105 nb_try_restart += 1 119 nb_try_restart += 1
@@ -158,7 +172,7 @@ class AgentSST(Agent): @@ -158,7 +172,7 @@ class AgentSST(Agent):
158 # agent = args[0] 172 # agent = args[0]
159 if agent in self.subprocess_dict.keys(): 173 if agent in self.subprocess_dict.keys():
160 cmd = self.send_cmd_to(agent,"do_exit") 174 cmd = self.send_cmd_to(agent,"do_exit")
161 - #cmd = self.send_cmd_to(agent,"do_exit","asap") 175 + #cmd = self.send_cmd_to(agent,"do_stop","asap")
162 return cmd 176 return cmd
163 177
164 def do_restart_agent(self, agent:str)->None: 178 def do_restart_agent(self, agent:str)->None:
@@ -217,7 +231,9 @@ class AgentSST(Agent): @@ -217,7 +231,9 @@ class AgentSST(Agent):
217 agent = cmd.args[0] 231 agent = cmd.args[0]
218 self.force_kill_agent(agent) 232 self.force_kill_agent(agent)
219 233
220 - # checking status of agent if they are timeout 234 + # checking status of agent if they are timeout if in auto mode
  235 + # soft_mode = Majordome.objects.objects.last().soft_mode
  236 + # if soft_mode == "AUTO":
221 for agent in self.subprocess_dict.keys(): 237 for agent in self.subprocess_dict.keys():
222 try: 238 try:
223 agent_survey = AgentSurvey.objects.get(name=agent) 239 agent_survey = AgentSurvey.objects.get(name=agent)
@@ -275,6 +291,11 @@ class AgentSST(Agent): @@ -275,6 +291,11 @@ class AgentSST(Agent):
275 291
276 292
277 if __name__ == "__main__": 293 if __name__ == "__main__":
278 -  
279 - agent = build_agent(AgentSST) 294 + parser = argparse.ArgumentParser(description='Start a agentSST.')
  295 + parser.add_argument("--computer",dest="computer",help='Launch agent with simulated computer hostname',action="store")
  296 + parser.add_argument("--agent",dest="agent",help='Launch an specific agent ',action="store")
  297 + parser.add_argument("-t", action="store_true" )
  298 + args = vars(parser.parse_args())
  299 + agent = build_agent(AgentSST,param_constr=args)
  300 + # agent = build_agent(AgentSST)
280 agent.run() 301 agent.run()
281 \ No newline at end of file 302 \ No newline at end of file