Commit a3015e31435321ff50b8026e8a2611985f2814af
1 parent
0e94cc21
Exists in
dev
table des commandes
Showing
4 changed files
with
56 additions
and
39 deletions
Show diff stats
README.md
... | ... | @@ -67,22 +67,16 @@ This software has been tested and validated with the following configurations : |
67 | 67 | -------------------------------------------------------------------------------------------- |
68 | 68 | ## LAST VERSION |
69 | 69 | |
70 | -Date: 27/02/2019 | |
70 | +Date: 28/02/2019 | |
71 | 71 | |
72 | 72 | Author: E. Pallier |
73 | 73 | |
74 | -VERSION: 20190227.0.000.0 (0.20) | |
74 | +VERSION: 0.20.2 | |
75 | 75 | |
76 | 76 | Comment: |
77 | - pyros.py script improvements | |
78 | - - config relative ou absolue | |
79 | - - all private methods with "_" prefix | |
80 | - - die on any error | |
81 | - - added commands : | |
82 | - - pyros update (= git pull + update db) | |
83 | - - pyros initdb | |
84 | - - cleanup | |
85 | - - bugfix start_agent.py | |
77 | + Agent.py improvements : | |
78 | + - update_db_survey() : ajouté date created | |
79 | + - read_db_commands() : ajout table AgentsCommand | |
86 | 80 | |
87 | 81 | |
88 | 82 | -------------------------------------------------------------------------------------------- | ... | ... |
pyros.py
... | ... | @@ -11,7 +11,6 @@ import sys |
11 | 11 | import time |
12 | 12 | |
13 | 13 | |
14 | -DEBUG = True | |
15 | 14 | |
16 | 15 | """ |
17 | 16 | ***************************************************************** |
... | ... | @@ -19,7 +18,9 @@ DEBUG = True |
19 | 18 | ***************************************************************** |
20 | 19 | """ |
21 | 20 | |
22 | -init_fixture = "initial_fixture.json" | |
21 | +DEBUG = True | |
22 | + | |
23 | +INIT_FIXTURE = "initial_fixture.json" | |
23 | 24 | |
24 | 25 | AGENTS = { |
25 | 26 | #"agentX" : "majordome", |
... | ... | @@ -39,12 +40,12 @@ IS_WINDOWS = platform.system() == "Windows" |
39 | 40 | |
40 | 41 | my_abs_path = os.path.dirname(os.path.realpath(__file__)) |
41 | 42 | if IS_WINDOWS: |
42 | - bin_dir = "Scripts" | |
43 | + b_in_dir = "Scripts" | |
43 | 44 | PYTHON = "python.exe" |
44 | 45 | # should also be ok: |
45 | 46 | #PYTHON = "python" |
46 | 47 | else: |
47 | - bin_dir = "bin" | |
48 | + b_in_dir = "bin" | |
48 | 49 | PYTHON = "python3" |
49 | 50 | # ok only from venv: |
50 | 51 | #PYTHON = "python" |
... | ... | @@ -52,7 +53,7 @@ VENV_BIN = ( |
52 | 53 | my_abs_path |
53 | 54 | + os.sep + "private" |
54 | 55 | + os.sep + "venv_py3_pyros" |
55 | - + os.sep + bin_dir | |
56 | + + os.sep + b_in_dir | |
56 | 57 | + os.sep + PYTHON |
57 | 58 | ) |
58 | 59 | |
... | ... | @@ -97,18 +98,21 @@ except: |
97 | 98 | ************************************************************************** |
98 | 99 | """ |
99 | 100 | |
100 | -def in_dir(dirname: str = ""): | |
101 | +def _in_dir(dirname: str = ""): | |
101 | 102 | return os.path.basename(os.getcwd()) == dirname |
102 | 103 | |
103 | 104 | |
104 | -def in_abs_dir(dirname: str = ""): | |
105 | +def _in_abs_dir(dirname: str = ""): | |
105 | 106 | return os.getcwd() == dirname |
106 | 107 | |
107 | 108 | |
108 | -def abort(msg: str = ""): | |
109 | - print("ERROR") | |
109 | +def die(msg: str = ""): | |
110 | + print() | |
111 | + print("...ERROR...") | |
110 | 112 | print(msg) |
113 | + print() | |
111 | 114 | exit(1) |
115 | + | |
112 | 116 | |
113 | 117 | #TODO: implement is_async |
114 | 118 | def execProcess(command, from_venv=False, is_async=False): |
... | ... | @@ -170,11 +174,6 @@ def printFullTerm(color: Colors, string: str): |
170 | 174 | return 0 |
171 | 175 | |
172 | 176 | |
173 | -def die(): | |
174 | - print() | |
175 | - print("...ERROR...") | |
176 | - print() | |
177 | - exit(1) | |
178 | 177 | |
179 | 178 | |
180 | 179 | |
... | ... | @@ -243,12 +242,12 @@ def install(): |
243 | 242 | # self.execProcess("python3 install/install.py install") |
244 | 243 | # if (os.path.basename(os.getcwd()) != "private"): |
245 | 244 | start_dir = os.getcwd() |
246 | - os.chdir("install/") ; in_dir("install") or abort("Bad dir") | |
245 | + os.chdir("install/") ; _in_dir("install") or die("Bad dir") | |
247 | 246 | # execProcess("python install.py install") |
248 | 247 | if not test_mode(): execProcess(PYTHON + " install.py") |
249 | 248 | # cd - |
250 | 249 | # os.chdir("-") |
251 | - os.chdir(start_dir) ; in_abs_dir(start_dir) or abort("Bad dir") | |
250 | + os.chdir(start_dir) ; _in_abs_dir(start_dir) or die("Bad dir") | |
252 | 251 | # return 0 |
253 | 252 | return True |
254 | 253 | |
... | ... | @@ -310,7 +309,7 @@ def start(agent:str, configfile:str): |
310 | 309 | #if test_mode(): print("in test mode") |
311 | 310 | #if verbose_mode(): print("in verbose mode") |
312 | 311 | if not _check_agent(agent): return |
313 | - # VENV_BIN = 'private/venv_py3_pyros' + os.sep + self.bin_dir + os.sep + self.bin_name | |
312 | + # VENV_BIN = 'private/venv_py3_pyros' + os.sep + self.b_in_dir + os.sep + self.bin_name | |
314 | 313 | |
315 | 314 | |
316 | 315 | # Start Agents |
... | ... | @@ -405,8 +404,8 @@ def _makemigrations(): |
405 | 404 | #TODO: mettre la fixture en date naive (sans time zone) |
406 | 405 | def _loaddata(): |
407 | 406 | _change_dir("src") |
408 | - #execProcessFromVenv(self.venv_bin + " manage.py loaddata misc" + os.sep + "fixtures" + os.sep + self.init_fixture) | |
409 | - res = execProcessFromVenv("manage.py loaddata misc" + os.sep + "fixtures" + os.sep + init_fixture) | |
407 | + #execProcessFromVenv(self.venv_bin + " manage.py loaddata misc" + os.sep + "fixtures" + os.sep + self.INIT_FIXTURE) | |
408 | + res = execProcessFromVenv("manage.py loaddata misc" + os.sep + "fixtures" + os.sep + INIT_FIXTURE) | |
410 | 409 | _change_dir("..") |
411 | 410 | return res |
412 | 411 | |
... | ... | @@ -461,20 +460,20 @@ def oldmain(): |
461 | 460 | |
462 | 461 | # Read command |
463 | 462 | if len(sys.argv) <= 1: |
464 | - abort("You must give a command name") | |
463 | + die("You must give a command name") | |
465 | 464 | command = sys.argv[1] |
466 | 465 | if not command in COMMANDS.keys(): |
467 | - abort("This command does not exist") | |
466 | + die("This command does not exist") | |
468 | 467 | command_all_args = COMMANDS[command] |
469 | 468 | |
470 | 469 | # Read command args if should be |
471 | 470 | command_arg = None |
472 | 471 | if command_all_args: |
473 | 472 | if len(sys.argv) <= 2: |
474 | - abort("This command should be given an argument") | |
473 | + die("This command should be given an argument") | |
475 | 474 | command_arg = sys.argv[2] |
476 | 475 | if not command_arg in command_all_args: |
477 | - abort("This argument does not exist for command " + command) | |
476 | + die("This argument does not exist for command " + command) | |
478 | 477 | |
479 | 478 | print("Executing command", command) |
480 | 479 | if command_arg: | ... | ... |
src/agent/Agent.py
... | ... | @@ -13,8 +13,6 @@ import os |
13 | 13 | - 1 table par agent |
14 | 14 | - table agents_log avec log minimum pour affichage dans dashboard, et ordre chrono intéressant pour suivi activité : nom agent, timestamp, message |
15 | 15 | - table agent_<agent-name>_vars : nom variable, value, desc |
16 | -- table agents_commands : agent demandeur, agent dest, deposit_time (timestamp), durée validité (valeur par défaut), commande (format à définir), ack_time (timestamp) | |
17 | -(l'agent destinataire en profite pour supprimer les commandes périmées qui le concernent) | |
18 | 16 | |
19 | 17 | """ |
20 | 18 | |
... | ... | @@ -119,8 +117,9 @@ class Agent: |
119 | 117 | raise Exception(f"Bad config file name '{config_filename}', error {str(self.config.get_last_errno())}: {str(self.config.get_last_errmsg())}") |
120 | 118 | tmp = AgentsSurvey.objects.filter(name=self.name) |
121 | 119 | if len(tmp) == 0: |
122 | - self._agents_survey = AgentsSurvey(name=self.name, mode=self.mode, status=self.status) | |
123 | - self._agents_survey.save() | |
120 | + self._agents_survey = AgentsSurvey.objects.create(name=self.name, validity_duration_sec=60, mode=self.mode, status=self.status) | |
121 | + #self._agents_survey = AgentsSurvey(name=self.name, validity_duration_sec=60, mode=self.mode, status=self.status) | |
122 | + #self._agents_survey.save() | |
124 | 123 | print("agent is", self._agents_survey) |
125 | 124 | |
126 | 125 | ... | ... |
src/common/models.py
... | ... | @@ -198,9 +198,32 @@ class Request(models.Model): |
198 | 198 | ------------------------ |
199 | 199 | """ |
200 | 200 | |
201 | +class AgentsCommand(models.Model): | |
202 | + """ | |
203 | + | id | sender | receiver | command | validity_duration_sec (default=60) | sender_deposit_time | receiver_read_time | |
204 | + (l'agent destinataire en profite pour supprimer les commandes périmées qui le concernent) | |
205 | + """ | |
206 | + #sender = models.CharField(max_length=50, blank=True, null=True, unique=True) | |
207 | + sender = models.CharField(max_length=50, unique=True) | |
208 | + receiver = models.CharField(max_length=50, unique=True) | |
209 | + command = models.CharField(max_length=100) | |
210 | + validity_duration_sec = models.PositiveIntegerField(blank=True, default=60) | |
211 | + sender_deposit_time = models.DateTimeField(blank=True, null=True, auto_now_add=True) | |
212 | + receiver_read_time = models.DateTimeField(blank=True, null=True, auto_now=True) | |
213 | + | |
214 | + class Meta: | |
215 | + managed = True | |
216 | + db_table = 'agents_command' | |
217 | + #verbose_name = "agent survey" | |
218 | + #verbose_name_plural = "agents survey" | |
219 | + | |
220 | + def __str__(self): | |
221 | + return (f"Agent {self.sender} sent commmand {self.command} to {self.receiver} at {self.sender_deposit_time}") | |
222 | + | |
223 | + | |
201 | 224 | class AgentsSurvey(models.Model): |
202 | 225 | """ |
203 | - | id | name | created | validity_duration_sec (default=1mn) | mode (active/idle) | status (launch/init/loop/exit/...) | | |
226 | + | id | name | created | updated | validity_duration_sec (default=1mn) | mode (active/idle) | status (launch/init/loop/exit/...) | | |
204 | 227 | """ |
205 | 228 | # Statuses |
206 | 229 | STATUS_LAUNCH = "LAUNCHED" |
... | ... | @@ -227,6 +250,8 @@ class AgentsSurvey(models.Model): |
227 | 250 | ) |
228 | 251 | |
229 | 252 | name = models.CharField(max_length=50, blank=True, null=True, unique=True) |
253 | + #created = models.DateTimeField(blank=True, null=True, auto_now_add=True) | |
254 | + created = models.DateTimeField(blank=True, null=True, auto_now_add=True) | |
230 | 255 | updated = models.DateTimeField(blank=True, null=True, auto_now=True) |
231 | 256 | validity_duration_sec = models.PositiveIntegerField(blank=True, null=True) |
232 | 257 | mode = models.CharField('agent mode', max_length=15, blank=True, null=True, choices=MODE_CHOICES) | ... | ... |