Commit 630dd6e4b4703c014779bd80da68cc00ba03a8b2
1 parent
a5f1e984
Exists in
dev
Renommé script start_agent_X en start_agent générique pour tout agent
Il n'y a plus besoin des scripts start_agent de chaque app
Showing
6 changed files
with
60 additions
and
87 deletions
Show diff stats
pyros2
... | ... | @@ -285,14 +285,22 @@ def start(agent:str, configfile:str): |
285 | 285 | if agent in ("all", agent_name) : |
286 | 286 | |
287 | 287 | # Default case, launch agentX |
288 | - if agent_name == "agentX": | |
289 | - # execProcessFromVenvAsync(VENV_BIN + " manage.py runserver") | |
290 | - print(VENV_BIN) | |
291 | - print("Launching agent", agent_name, "...") | |
292 | - #if not test_mode(): execProcess(VENV_BIN + " manage.py runserver") | |
293 | - if not test_mode(): execProcessFromVenv("start_agent_" + agent_name + ".py " + configfile) | |
294 | - # self.changeDirectory("..") | |
288 | + #if agent_name == "agentX": | |
289 | + | |
290 | + # execProcessFromVenvAsync(VENV_BIN + " manage.py runserver") | |
291 | + print(VENV_BIN) | |
292 | + print("Launching agent", agent_name, "...") | |
293 | + #if not test_mode(): execProcess(VENV_BIN + " manage.py runserver") | |
294 | + #if not test_mode(): execProcessFromVenv("start_agent_" + agent_name + ".py " + configfile) | |
295 | + cmd = "start_agent.py " + agent_name + " " + configfile | |
296 | + if agent_name == "webserver": | |
297 | + cmd = "manage.py runserver" | |
298 | + os.chdir("src") | |
299 | + #if not test_mode(): execProcessFromVenv("start_agent.py " + agent_name + " " + configfile) | |
300 | + if not test_mode(): execProcessFromVenv(cmd) | |
301 | + # self.changeDirectory("..") | |
295 | 302 | |
303 | + ''' | |
296 | 304 | # Any other agent |
297 | 305 | else: |
298 | 306 | # Go into src/ |
... | ... | @@ -314,6 +322,7 @@ def start(agent:str, configfile:str): |
314 | 322 | # Go back to src/ |
315 | 323 | # self.changeDirectory('..') |
316 | 324 | os.chdir("..") |
325 | + ''' | |
317 | 326 | |
318 | 327 | # Go back to root folder (/) |
319 | 328 | # self.changeDirectory('..') | ... | ... |
src/devices/Device.py
... | ... | @@ -20,7 +20,8 @@ class DeviceController(): |
20 | 20 | __metaclass__ = abc.ABCMeta |
21 | 21 | logger = L.setupLogger("DeviceLogger", "Devices") |
22 | 22 | name = "" |
23 | - config_file = "../config/socket_config.ini" | |
23 | + #config_file = "../config/socket_config.ini" | |
24 | + config_file = "config/socket_config.ini" | |
24 | 25 | config = None |
25 | 26 | connected = False |
26 | 27 | sock = None |
... | ... | @@ -28,7 +29,10 @@ class DeviceController(): |
28 | 29 | port = None |
29 | 30 | |
30 | 31 | def __init__(self, device_name): |
32 | + print("DeviceController:__init__()") | |
31 | 33 | self.name = device_name |
34 | + print("device name", device_name) | |
35 | + print(os.getcwd()) | |
32 | 36 | self.getConfig() |
33 | 37 | self.getConnection(device_name) |
34 | 38 | self.connect() |
... | ... | @@ -142,6 +146,7 @@ class DeviceController(): |
142 | 146 | return message |
143 | 147 | |
144 | 148 | def getConnection(self, device_name): |
149 | + #print("device_name", device_name) | |
145 | 150 | self.ip = self.config.get(device_name, "ip") |
146 | 151 | self.port = int(self.config.get(device_name, "port")) |
147 | 152 | return (0) | ... | ... |
src/majordome/start_agent_majordome.py deleted
... | ... | @@ -1,41 +0,0 @@ |
1 | -#!/usr/bin/env python | |
2 | - | |
3 | -import os | |
4 | -import sys | |
5 | -from django.conf import settings as djangosettings | |
6 | - | |
7 | -FOR_REAL = True | |
8 | -if len(sys.argv) == 2 and sys.argv[1] == "test": | |
9 | - FOR_REAL = False | |
10 | -print("FOR_REAL is", FOR_REAL) | |
11 | - | |
12 | -# To avoid a "ModuleNotFoundError: No module named 'dashboard'"... (not even 1 app found) : | |
13 | -sys.path.insert(0, os.path.abspath("..")) | |
14 | -# To avoid a "ModuleNotFoundError: No module named 'src'" | |
15 | -sys.path.append("..") | |
16 | -print("Starting with this sys.path", sys.path) | |
17 | - | |
18 | -# DJANGO setup | |
19 | -# print("file is", __file__) | |
20 | -# mypath = os.getcwd() | |
21 | -# Go into src/ | |
22 | -os.chdir("..") | |
23 | -# print("Current directory : " + str(os.getcwd())) | |
24 | -os.environ.setdefault("DJANGO_SETTINGS_MODULE", "src.pyros.settings") | |
25 | -# os.environ['SECRET_KEY'] = 'abc' | |
26 | -# os.environ['ENVIRONMENT'] = 'production' | |
27 | -import django | |
28 | - | |
29 | -django.setup() | |
30 | - | |
31 | -print("DB2 used is:", djangosettings.DATABASES["default"]["NAME"]) | |
32 | - | |
33 | - | |
34 | -# MONITORING AGENT setup | |
35 | -agent = "majordome" | |
36 | - | |
37 | -if agent == "majordome": | |
38 | - from majordome.tasks import Majordome | |
39 | - | |
40 | - # Run agent Majordome without actual commands sent to devices (FOR_REAL=False) | |
41 | - Majordome().run(FOR_REAL) |
src/monitoring/start_agent_monitoring.py deleted
... | ... | @@ -1,29 +0,0 @@ |
1 | -#!/usr/bin/env python3 | |
2 | - | |
3 | -import os | |
4 | -import sys | |
5 | - | |
6 | -# To avoid a "ModuleNotFoundError: No module named 'dashboard'"... (not even 1 app found) : | |
7 | -sys.path.insert(0, os.path.abspath("..")) | |
8 | -# To avoid a "ModuleNotFoundError: No module named 'src'" | |
9 | -sys.path.append('..') | |
10 | -print("Starting with this sys.path", sys.path) | |
11 | - | |
12 | -# DJANGO SETUP | |
13 | -# Go into src/ | |
14 | -os.chdir('..') | |
15 | -#print("Current directory : " + str(os.getcwd())) | |
16 | -os.environ.setdefault("DJANGO_SETTINGS_MODULE", "src.pyros.settings") | |
17 | -#os.environ['SECRET_KEY'] = 'abc' | |
18 | -#os.environ['ENVIRONMENT'] = 'production' | |
19 | -import django | |
20 | -django.setup() | |
21 | - | |
22 | -#import datetime | |
23 | -#print(datetime.datetime.now()) | |
24 | - | |
25 | -# MONITORING agent setup | |
26 | -#agent="monitoring" | |
27 | -#if agent=="monitoring": | |
28 | -from monitoring.tasks import Monitoring | |
29 | -Monitoring().run() |
src/monitoring/tasks.py
... | ... | @@ -45,14 +45,14 @@ class Monitoring(): |
45 | 45 | # (non blocking if could not connect) |
46 | 46 | self.setContext() |
47 | 47 | print ("AGENT ENV: config PLC is (ip={}, port={})".format(self.plcController.ip, self.plcController.port)) |
48 | - | |
48 | + | |
49 | 49 | # (EP) self.setTime() |
50 | 50 | self.setTimers() |
51 | 51 | print("AGENT ENV: my timers (check env status every {}s, check other agents every {}s)".format(self.timer_status, self.timer_tasks)) |
52 | - | |
52 | + | |
53 | 53 | self.setTasks() |
54 | 54 | print("AGENT ENV: Other Agents id read from DB (majordome={}, alert={})".format(self.majordome_task, self.alert_task)) |
55 | - | |
55 | + | |
56 | 56 | self.loop() |
57 | 57 | |
58 | 58 | ... | ... |
start_agent_agentX.py renamed to start_agent.py
... | ... | @@ -5,15 +5,29 @@ import sys |
5 | 5 | |
6 | 6 | from django.conf import settings as djangosettings |
7 | 7 | |
8 | +AGENTS = { | |
9 | + "agentX" : "AgentX", | |
10 | + "webserver" : "webserver", | |
11 | + "monitoring" : "monitoring", | |
12 | + "majordome" : "majordome", | |
13 | + "scheduler" : "scheduler", | |
14 | + "alert_manager" : "alert_manager" | |
15 | +} | |
8 | 16 | |
9 | 17 | configfile = None |
10 | -if len(sys.argv) == 2: | |
11 | - configfile = sys.argv[1] | |
12 | 18 | |
13 | -# from AgentX import AgentX | |
19 | +if len(sys.argv) < 2: | |
20 | + print("Error: you must give an agent name to start") | |
21 | + exit(1) | |
22 | + | |
23 | +# arg 1 : agent name | |
24 | +agent_name = sys.argv[1] | |
14 | 25 | |
26 | +# arg 2 : config file | |
27 | +if len(sys.argv) == 3: | |
28 | + configfile = sys.argv[1] | |
15 | 29 | |
16 | -# Préconisé sur le net: | |
30 | +# Conseil sur le net: | |
17 | 31 | #https://stackoverflow.com/questions/16853649/how-to-execute-a-python-script-from-the-django-shell |
18 | 32 | """ |
19 | 33 | import sys, os |
... | ... | @@ -31,7 +45,7 @@ TODO: |
31 | 45 | - thread de run |
32 | 46 | - thread de wrapper |
33 | 47 | - gestion des commandes dans le run |
34 | -- table dédiée à l'agent, infos dynamiques volatiles | |
48 | +- table dediee à l agent, infos dynamiques volatiles | |
35 | 49 | """ |
36 | 50 | |
37 | 51 | |
... | ... | @@ -64,8 +78,23 @@ print("DB2 used is:", djangosettings.DATABASES["default"]["NAME"]) |
64 | 78 | print() |
65 | 79 | |
66 | 80 | |
67 | -from src.agent.AgentX import AgentX | |
81 | +if agent_name not in AGENTS: | |
82 | + print("Error: this agent does not exists") | |
83 | + sys.exit(1) | |
68 | 84 | |
85 | +if agent_name == "majordome": | |
86 | + from src.majordome.tasks import Majordome | |
87 | + Majordome().run(FOR_REAL=True) | |
88 | + sys.exit(0) | |
89 | + | |
90 | +if agent_name == "monitoring": | |
91 | + from src.monitoring.tasks import Monitoring | |
92 | + Monitoring().run() | |
93 | + sys.exit(0) | |
94 | + | |
95 | +# Default agent is AgentX | |
96 | + | |
97 | +from src.agent.AgentX import AgentX | |
69 | 98 | # AgentX().run(FOR_REAL=False) |
70 | 99 | agentx = AgentX(name="agentX", config_filename=configfile) |
71 | 100 | ... | ... |