Blame view

src/core/pyros_django/agent/AgentSST.py 13.1 KB
05316241   Alexis Koralewski   Adding AgentSST, ...
1
2
3
4
#!/usr/bin/env python3

from pathlib import Path
import subprocess
a04e004d   Alexis Koralewski   Fixing AgentCmd c...
5
6
import sys, os
from datetime import datetime, timezone, timedelta
05316241   Alexis Koralewski   Adding AgentSST, ...
7
8
9
10
11
12
13
##import utils.Logger as L
#import threading #, multiprocessing, os
import time

#from django.db import transaction
#from common.models import Command

dd27c2bc   Alexis Koralewski   Updating agent co...
14
from Agent import Agent, build_agent, log, extract_parameters
05316241   Alexis Koralewski   Adding AgentSST, ...
15
import socket
a04e004d   Alexis Koralewski   Fixing AgentCmd c...
16
from common.models import AgentCmd, AgentSurvey
05316241   Alexis Koralewski   Adding AgentSST, ...
17

dd27c2bc   Alexis Koralewski   Updating agent co...
18
from src.core.pyros_django.obsconfig.obsconfig_class import OBSConfig
05316241   Alexis Koralewski   Adding AgentSST, ...
19
20

class AgentSST(Agent):
04b0b442   Alexis Koralewski   Changing computer...
21
    computer = "XCY1"
05316241   Alexis Koralewski   Adding AgentSST, ...
22
23
24
25
    _previous_dir = ""
    PROJECT_ROOT_PATH = ""
    VENV_PYTHON = ""
    subprocess_dict = {}
7fd15ce5   Alexis Koralewski   Adding test mode ...
26
    agent_in_mode_test = {}
05316241   Alexis Koralewski   Adding AgentSST, ...
27

dd27c2bc   Alexis Koralewski   Updating agent co...
28
    AGENT_SPECIFIC_COMMANDS = [
8d9c8345   Alexis Koralewski   Renaming commands...
29
        "do_kill_agent",
2918ab51   Alexis Koralewski   Adding do_start_a...
30
31
        "do_restart_agent",
        "do_start_agent"
dd27c2bc   Alexis Koralewski   Updating agent co...
32
    ]
7fd15ce5   Alexis Koralewski   Adding test mode ...
33
34
    TEST_COMMANDS_LIST = [
    ]
05316241   Alexis Koralewski   Adding AgentSST, ...
35

dd27c2bc   Alexis Koralewski   Updating agent co...
36
    def __init__(self, name:str=None,sim_computer=None):
05316241   Alexis Koralewski   Adding AgentSST, ...
37
        
dd27c2bc   Alexis Koralewski   Updating agent co...
38
        super().__init__()
05316241   Alexis Koralewski   Adding AgentSST, ...
39
40
41
        self.PROJECT_ROOT_PATH = os.environ["PROJECT_ROOT_PATH"]
        if name is None:
            name = self.__class__.__name__
c957b9e1   Alexis Koralewski   fixing restart of...
42
        self.computer = socket.gethostname()        
05316241   Alexis Koralewski   Adding AgentSST, ...
43
44
45
        WITH_DOCKER = False
        if os.environ.get("WITH_DOCKER"):
            WITH_DOCKER = True
99502363   Alexis Koralewski   add message when ...
46
            # if WITH_DOCKER socket.gethostname() bizarre 
05316241   Alexis Koralewski   Adding AgentSST, ...
47
48
49
50
51
52
53
54
55
56
57
58
59
60
        if WITH_DOCKER:
            VENV_ROOT = ""
            VENV = ""
            VENV_BIN = ""
        else:
            VENV_ROOT = "venv"
            VENV = "venv_py3_pyros"
            VENV_BIN = (
                self.PROJECT_ROOT_PATH
                + os.sep + VENV_ROOT
                + os.sep + VENV
                + os.sep + "bin"
                + os.sep
            )
dd27c2bc   Alexis Koralewski   Updating agent co...
61
        self.VENV_PYTHON = VENV_BIN + "python3"
05316241   Alexis Koralewski   Adding AgentSST, ...
62
        log.info(f"PC hostname is {self.computer}")
9bd7ac9e   Alexis Koralewski   Adding timeout co...
63
        #name_from_config = self.get_config().get_agent_sst_of_current_computer()
7fd15ce5   Alexis Koralewski   Adding test mode ...
64
65
66
    
    def init(self):
        super().init()
05316241   Alexis Koralewski   Adding AgentSST, ...
67
        self.start_agents()
7fd15ce5   Alexis Koralewski   Adding test mode ...
68
69
        self.TEST_MODE = False

05316241   Alexis Koralewski   Adding AgentSST, ...
70
71
72
73
74
75
76
77
78

    def start_agents(self,agent_name=None):
        """
        Start all agents or one agent of obs_config

        Args:
            agent_name (_type_, optional): Specific agent name to start. Defaults to None.
        """
        obs_config = self.get_config()
7fd15ce5   Alexis Koralewski   Adding test mode ...
79
80
        test_mode = " -t"

05316241   Alexis Koralewski   Adding AgentSST, ...
81
82
83
84
85
86
87
88
89
90
        if agent_name:
            agent = agent_name
            # Start a specific agent of obs_config (restart)
            agent_informations = obs_config.get_agent_information(obs_config.unit_name,agent)
            protocol = agent_informations.get("protocol")
            if protocol:
                protocol_folder_abs_path = os.path.join(self.PROJECT_ROOT_PATH, os.path.dirname(protocol))
                
                protocol_script_name = protocol.split("/")[-1]
                if os.path.exists(protocol_folder_abs_path + os.sep + protocol_script_name):
dd27c2bc   Alexis Koralewski   Updating agent co...
91
                    cmd = self.VENV_PYTHON +" "+ protocol_folder_abs_path + os.sep + protocol_script_name
7fd15ce5   Alexis Koralewski   Adding test mode ...
92
93
94
95
                    if not agent in self.agent_in_mode_test:
                        self.agent_in_mode_test = self.TEST_MODE
                    if self.agent_in_mode_test[agent]:
                        cmd += test_mode
9bd7ac9e   Alexis Koralewski   Adding timeout co...
96
                    process = subprocess.Popen(f"{cmd}",shell=True)
8d9c8345   Alexis Koralewski   Renaming commands...
97
                    process.poll()
9bd7ac9e   Alexis Koralewski   Adding timeout co...
98
99
100
                    
                    self.subprocess_dict[agent]["process"] = process
                    self.subprocess_dict[agent]["nb_try_restart"] = self.subprocess_dict[agent].get("nb_try_restart",0)+1  
05316241   Alexis Koralewski   Adding AgentSST, ...
101
102
103
                    log.info(f"Start agent {agent} with cmd {cmd}")

        else:
027eaa78   Alexis Koralewski   Fixing AgentSST i...
104
105
106
107
108
109
110
111
112
            agents = obs_config.get_agents_per_computer(obs_config.unit_name).get(self.computer)
            if agents is None:
                available_hostnames = obs_config.get_agents_per_computer(obs_config.unit_name).keys()
                log.info("Computer not found in obs config")
                log.info(f"Available hostnames {available_hostnames}. Current hostname is {self.computer}")
                exit(1)
            #self.change_dir(self.PROJECT_ROOT_PATH)
            else:
                log.info(f"Agents associated to this computer : {agents}")
05316241   Alexis Koralewski   Adding AgentSST, ...
113
114
115
116
117
118
119
120
121
            # Start every agent of obs_config (initial start)
            for agent in agents:
                agent_informations = obs_config.get_agent_information(obs_config.unit_name,agent)
                protocol = agent_informations.get("protocol")
                if protocol:
                    protocol_folder_abs_path = os.path.join(self.PROJECT_ROOT_PATH, os.path.dirname(protocol))
                    
                    protocol_script_name = protocol.split("/")[-1]
                    if os.path.exists(protocol_folder_abs_path + os.sep + protocol_script_name):
05316241   Alexis Koralewski   Adding AgentSST, ...
122
                        
7fd15ce5   Alexis Koralewski   Adding test mode ...
123
124
125
126
127
                        cmd = self.VENV_PYTHON +" "+ protocol_folder_abs_path + os.sep + protocol_script_name
                        if not agent in self.agent_in_mode_test:
                            self.agent_in_mode_test[agent] = self.TEST_MODE
                        if self.agent_in_mode_test[agent]:
                            cmd += test_mode
a04e004d   Alexis Koralewski   Fixing AgentCmd c...
128
129
                        # process = subprocess.Popen(f"{cmd}", shell=True, stdout=subprocess.DEVNULL,stderr=subprocess.STDOUT)
                        process = subprocess.Popen(f"{cmd}", shell=True)
9bd7ac9e   Alexis Koralewski   Adding timeout co...
130
131
                        self.subprocess_dict[agent] = {}
                        self.subprocess_dict[agent]["process"] = process
3449489e   Alexis Koralewski   Fixing current_nb...
132
133
                        # Reset to zero nb_try when AgentSST start (launch all agents)
                        self.subprocess_dict[agent]["nb_try_restart"] = 0
05316241   Alexis Koralewski   Adding AgentSST, ...
134
135
                        log.info(f"Start agent {agent} with cmd {cmd}")

2918ab51   Alexis Koralewski   Adding do_start_a...
136
    def do_start_agent(self, agent_name:str):
05316241   Alexis Koralewski   Adding AgentSST, ...
137
138
139
140
141
142
143
        """
        Start a specific agent of obs_config (Restart)

        Args:
            agent_name (str): Name of agent to start
        """
        self.start_agents(agent_name)
3449489e   Alexis Koralewski   Fixing current_nb...
144
145
146
147
148
        nb_try_restart_agent = self.subprocess_dict[agent_name]["nb_try_restart"]
        agent_survey = AgentSurvey.objects.get(name=agent_name)
        agent_survey.current_nb_restart = nb_try_restart_agent
        
        agent_survey.save()
05316241   Alexis Koralewski   Adding AgentSST, ...
149

dd27c2bc   Alexis Koralewski   Updating agent co...
150

afbc5c95   Alexis Koralewski   Renaming AgentSST...
151
152
    def do_kill_agent(self, agent)->None:
        # agent = args[0]
dd27c2bc   Alexis Koralewski   Updating agent co...
153
        if agent in self.subprocess_dict.keys():
fd880010   Alexis Koralewski   Fixing error in A...
154
            cmd = self.send_cmd_to(agent,"do_abort")
a04e004d   Alexis Koralewski   Fixing AgentCmd c...
155
            return cmd
dd27c2bc   Alexis Koralewski   Updating agent co...
156

afbc5c95   Alexis Koralewski   Renaming AgentSST...
157
158
    def do_restart_agent(self, agent)->None:
        # agent = args[0]
9bd7ac9e   Alexis Koralewski   Adding timeout co...
159
        nb_try_restart_agent = self.subprocess_dict[agent]["nb_try_restart"]
6a41fe96   Alexis Koralewski   Add current_nb_re...
160
161
162
        agent_survey = AgentSurvey.objects.get(name=agent)
        agent_survey.current_nb_restart = nb_try_restart_agent
        agent_survey.save()
c957b9e1   Alexis Koralewski   fixing restart of...
163
        if nb_try_restart_agent < AgentSurvey.objects.get(name=agent).nb_restart_max:
9bd7ac9e   Alexis Koralewski   Adding timeout co...
164
165
166
167
168
169
            self.do_kill_agent(agent)
            self.do_start_agent(agent)
        else:
            #sendmail
            pass

a04e004d   Alexis Koralewski   Fixing AgentCmd c...
170
171
172
173
174
175
        # if agent in self.subprocess_dict.keys():
        #     cmd.set_result(f"Agent {agent} restarted")
        #     cmd.set_as_processed()
        # else:
        #     cmd.set_result(f"Agent {agent} failed to restart")
        #     log.debug(f"Agent {agent} failed to restart")
dd27c2bc   Alexis Koralewski   Updating agent co...
176

a04e004d   Alexis Koralewski   Fixing AgentCmd c...
177
    def force_kill_agent(self, *args)->None:
d4ebe565   Alexis Koralewski   Adding pyros stop...
178
179
180
        if args:
            agent = args[0]
            if self.subprocess_dict.get(agent) is not None:
9bd7ac9e   Alexis Koralewski   Adding timeout co...
181
                process = self.subprocess_dict.get(agent).get("process")
d4ebe565   Alexis Koralewski   Adding pyros stop...
182
183
184
185
186
187
                # process.terminate()
                # process.wait()
                # Kill is better when using Popen(shell=True) because it will remove the created child process
                process.kill()
            else:
                return None
a04e004d   Alexis Koralewski   Fixing AgentCmd c...
188
189
190
191

    def do_things_before_exit(self,abort_cmd_sender):
        kill_agent_commands = {}
        for agent in self.subprocess_dict.keys():
8d9c8345   Alexis Koralewski   Renaming commands...
192
            cmd = self.do_kill_agent(agent)    
a04e004d   Alexis Koralewski   Fixing AgentCmd c...
193
            kill_agent_commands[agent] = cmd
3449489e   Alexis Koralewski   Fixing current_nb...
194
195
196
197
            agent_survey = AgentSurvey.objects.get(name=agent)
            # Reset counter before exiting
            agent_survey.current_nb_restart = 0
            agent_survey.save()
d4ebe565   Alexis Koralewski   Adding pyros stop...
198
199
        # wait 10 seconds in order to agents to exit themselves properly 
        time.sleep(10)
fd880010   Alexis Koralewski   Fixing error in A...
200
201
        # while self.subprocess_dict[agent].poll() is None:
        #     time.sleep(0.5)
a04e004d   Alexis Koralewski   Fixing AgentCmd c...
202

2ac0a02f   Alexis Koralewski   Fixing AgentSST (...
203
    def routine_process_after_body(self):
a04e004d   Alexis Koralewski   Fixing AgentCmd c...
204
        now_time = datetime.now(timezone.utc) 
027eaa78   Alexis Koralewski   Fixing AgentSST i...
205
        last_running_commands = AgentCmd.get_commands_sent_by_agent("AgentSST").filter(state="CMD_RUNNING",recipient__in=list(self.subprocess_dict.keys()))
a04e004d   Alexis Koralewski   Fixing AgentCmd c...
206
        for cmd in last_running_commands:
fd880010   Alexis Koralewski   Fixing error in A...
207
            last_running_cmd = cmd.full_name
a04e004d   Alexis Koralewski   Fixing AgentCmd c...
208
            if last_running_cmd == "KILL_AGENT" and cmd.is_expired():
fd880010   Alexis Koralewski   Fixing error in A...
209
                agent = cmd.args[0]
2918ab51   Alexis Koralewski   Adding do_start_a...
210
                self.force_kill_agent(agent)
a04e004d   Alexis Koralewski   Fixing AgentCmd c...
211

027eaa78   Alexis Koralewski   Fixing AgentSST i...
212
        # checking status of agent if they are timeout
a04e004d   Alexis Koralewski   Fixing AgentCmd c...
213
        for agent in self.subprocess_dict.keys():
fd880010   Alexis Koralewski   Fixing error in A...
214
215
            try:
                agent_survey = AgentSurvey.objects.get(name=agent)
027eaa78   Alexis Koralewski   Fixing AgentSST i...
216
217
218
219
220
221
222
223
224
225
226
            except AgentSurvey.DoesNotExist:
                # If there is no entry in AgentSurvey for this agent go to next iteration (it surely means that the agentSST launched this agent for the first time, and it didn't had enough time to create an entry in AgentSurvey)
                continue
            
            validity_duration = agent_survey.validity_duration
            last_update_from_agent = agent_survey.updated
            validity_duration_timedelta = timedelta(seconds=validity_duration)
            timeout_datetime = last_update_from_agent + validity_duration_timedelta
            timeout_datetime = timeout_datetime.replace(tzinfo=timezone.utc)
            # if agent latest state is timeout, restart it
            if timeout_datetime < now_time:
9bd7ac9e   Alexis Koralewski   Adding timeout co...
227
                if self.subprocess_dict[agent].get("process").poll() != None:
027eaa78   Alexis Koralewski   Fixing AgentSST i...
228
229
                    last_executed_start_agent_cmd =  AgentCmd.objects.filter(state="CMD_EXECUTED",full_name=f"do_start_agent {agent}",recipient=self.name).order_by("-s_deposit_time")
                    if last_executed_start_agent_cmd.exists():
7fd15ce5   Alexis Koralewski   Adding test mode ...
230
231
232
233
234
                        cmd_outdated_datetime_start = datetime.utcnow() - timedelta(seconds=30)
                        cmd_outdated_datetime_end = datetime.utcnow() - timedelta(seconds=25)
                        cmd_outdated_datetime_start = cmd_outdated_datetime_start.replace(tzinfo=timezone.utc)
                        cmd_outdated_datetime_end = cmd_outdated_datetime_end.replace(tzinfo=timezone.utc)
                        # cmd outdated if deposit time was between 25 and 30 seconds ago from now
027eaa78   Alexis Koralewski   Fixing AgentSST i...
235
                        # if last start cmd for this agent was executed and this agent isn't currently running, ask again a start.
3449489e   Alexis Koralewski   Fixing current_nb...
236
                        if cmd_outdated_datetime_start >= last_executed_start_agent_cmd.first().s_deposit_time and cmd_outdated_datetime_end >= last_executed_start_agent_cmd.first().s_deposit_time:
027eaa78   Alexis Koralewski   Fixing AgentSST i...
237
238
239
240
241
242
243
244
245
246
                            self.send_cmd_to("AgentSST","do_start_agent", agent)
                    else:
                        try:
                            # Check if do_start_agent cmd already asked by agentSST in previous iterations and not exectuted. If the query success (no exception raised), we don't send again a cmd
                            AgentCmd.get_pending_and_running_commands_for_agent(self.name).get(full_name=f"do_start_agent {agent}")
                        except:
                            self.send_cmd_to("AgentSST","do_start_agent", agent)
                else:
                    last_executed_start_or_restart_agent_cmd =  AgentCmd.objects.filter(state="CMD_EXECUTED",full_name__in=(f"do_start_agent {agent}",f"do_restart_agent {agent}"),recipient=self.name).order_by("-s_deposit_time")
                    if last_executed_start_or_restart_agent_cmd.exists():
7fd15ce5   Alexis Koralewski   Adding test mode ...
247
248
249
250
251
                        cmd_outdated_datetime_start = datetime.utcnow() - timedelta(seconds=30)
                        cmd_outdated_datetime_end = datetime.utcnow() - timedelta(seconds=25)
                        cmd_outdated_datetime_start = cmd_outdated_datetime_start.replace(tzinfo=timezone.utc)
                        cmd_outdated_datetime_end = cmd_outdated_datetime_end.replace(tzinfo=timezone.utc)
                        # cmd outdated if deposit time was between 25 and 30 seconds ago from now
027eaa78   Alexis Koralewski   Fixing AgentSST i...
252
                        # if last start or restart cmd for this agent was executed and this agent isn't currently running, ask again a restart.
3449489e   Alexis Koralewski   Fixing current_nb...
253
                        if cmd_outdated_datetime_start >= last_executed_start_or_restart_agent_cmd.first().s_deposit_time and cmd_outdated_datetime_end >=  last_executed_start_or_restart_agent_cmd.first().s_deposit_time:
027eaa78   Alexis Koralewski   Fixing AgentSST i...
254
255
256
257
258
259
260
261
                            self.send_cmd_to("AgentSST","do_restart_agent", agent)    
                    else:
                        try:
                            # Check if do_restart_agent cmd already asked by agentSST in previous iterations and not exectuted. If the query success (no exception raised), we don't send again a cmd
                            AgentCmd.get_pending_and_running_commands_for_agent(self.name).get(full_name=f"do_restart_agent {agent}")
                        except:
                            self.send_cmd_to("AgentSST","do_restart_agent", agent)

a04e004d   Alexis Koralewski   Fixing AgentCmd c...
262
263
        log.info("Check status of process")
        for agent in self.subprocess_dict:
9bd7ac9e   Alexis Koralewski   Adding timeout co...
264
            proc = self.subprocess_dict.get(agent).get("process")
dd27c2bc   Alexis Koralewski   Updating agent co...
265
            log.info(f"{agent} poll result is {proc.poll()}")
a04e004d   Alexis Koralewski   Fixing AgentCmd c...
266
     
dd27c2bc   Alexis Koralewski   Updating agent co...
267

05316241   Alexis Koralewski   Adding AgentSST, ...
268
269
270
271
272

if __name__ == "__main__":

    agent = build_agent(AgentSST)
    agent.run()