From a3015e31435321ff50b8026e8a2611985f2814af Mon Sep 17 00:00:00 2001 From: Etienne Pallier Date: Thu, 28 Feb 2019 14:13:01 +0100 Subject: [PATCH] table des commandes --- README.md | 16 +++++----------- pyros.py | 45 ++++++++++++++++++++++----------------------- src/agent/Agent.py | 7 +++---- src/common/models.py | 27 ++++++++++++++++++++++++++- 4 files changed, 56 insertions(+), 39 deletions(-) diff --git a/README.md b/README.md index cd06cb1..19def63 100644 --- a/README.md +++ b/README.md @@ -67,22 +67,16 @@ This software has been tested and validated with the following configurations : -------------------------------------------------------------------------------------------- ## LAST VERSION -Date: 27/02/2019 +Date: 28/02/2019 Author: E. Pallier -VERSION: 20190227.0.000.0 (0.20) +VERSION: 0.20.2 Comment: - pyros.py script improvements - - config relative ou absolue - - all private methods with "_" prefix - - die on any error - - added commands : - - pyros update (= git pull + update db) - - pyros initdb - - cleanup - - bugfix start_agent.py + Agent.py improvements : + - update_db_survey() : ajouté date created + - read_db_commands() : ajout table AgentsCommand -------------------------------------------------------------------------------------------- diff --git a/pyros.py b/pyros.py index a053b87..2acd8f4 100755 --- a/pyros.py +++ b/pyros.py @@ -11,7 +11,6 @@ import sys import time -DEBUG = True """ ***************************************************************** @@ -19,7 +18,9 @@ DEBUG = True ***************************************************************** """ -init_fixture = "initial_fixture.json" +DEBUG = True + +INIT_FIXTURE = "initial_fixture.json" AGENTS = { #"agentX" : "majordome", @@ -39,12 +40,12 @@ IS_WINDOWS = platform.system() == "Windows" my_abs_path = os.path.dirname(os.path.realpath(__file__)) if IS_WINDOWS: - bin_dir = "Scripts" + b_in_dir = "Scripts" PYTHON = "python.exe" # should also be ok: #PYTHON = "python" else: - bin_dir = "bin" + b_in_dir = "bin" PYTHON = "python3" # ok only from venv: #PYTHON = "python" @@ -52,7 +53,7 @@ VENV_BIN = ( my_abs_path + os.sep + "private" + os.sep + "venv_py3_pyros" - + os.sep + bin_dir + + os.sep + b_in_dir + os.sep + PYTHON ) @@ -97,18 +98,21 @@ except: ************************************************************************** """ -def in_dir(dirname: str = ""): +def _in_dir(dirname: str = ""): return os.path.basename(os.getcwd()) == dirname -def in_abs_dir(dirname: str = ""): +def _in_abs_dir(dirname: str = ""): return os.getcwd() == dirname -def abort(msg: str = ""): - print("ERROR") +def die(msg: str = ""): + print() + print("...ERROR...") print(msg) + print() exit(1) + #TODO: implement is_async def execProcess(command, from_venv=False, is_async=False): @@ -170,11 +174,6 @@ def printFullTerm(color: Colors, string: str): return 0 -def die(): - print() - print("...ERROR...") - print() - exit(1) @@ -243,12 +242,12 @@ def install(): # self.execProcess("python3 install/install.py install") # if (os.path.basename(os.getcwd()) != "private"): start_dir = os.getcwd() - os.chdir("install/") ; in_dir("install") or abort("Bad dir") + os.chdir("install/") ; _in_dir("install") or die("Bad dir") # execProcess("python install.py install") if not test_mode(): execProcess(PYTHON + " install.py") # cd - # os.chdir("-") - os.chdir(start_dir) ; in_abs_dir(start_dir) or abort("Bad dir") + os.chdir(start_dir) ; _in_abs_dir(start_dir) or die("Bad dir") # return 0 return True @@ -310,7 +309,7 @@ def start(agent:str, configfile:str): #if test_mode(): print("in test mode") #if verbose_mode(): print("in verbose mode") if not _check_agent(agent): return - # VENV_BIN = 'private/venv_py3_pyros' + os.sep + self.bin_dir + os.sep + self.bin_name + # VENV_BIN = 'private/venv_py3_pyros' + os.sep + self.b_in_dir + os.sep + self.bin_name # Start Agents @@ -405,8 +404,8 @@ def _makemigrations(): #TODO: mettre la fixture en date naive (sans time zone) def _loaddata(): _change_dir("src") - #execProcessFromVenv(self.venv_bin + " manage.py loaddata misc" + os.sep + "fixtures" + os.sep + self.init_fixture) - res = execProcessFromVenv("manage.py loaddata misc" + os.sep + "fixtures" + os.sep + init_fixture) + #execProcessFromVenv(self.venv_bin + " manage.py loaddata misc" + os.sep + "fixtures" + os.sep + self.INIT_FIXTURE) + res = execProcessFromVenv("manage.py loaddata misc" + os.sep + "fixtures" + os.sep + INIT_FIXTURE) _change_dir("..") return res @@ -461,20 +460,20 @@ def oldmain(): # Read command if len(sys.argv) <= 1: - abort("You must give a command name") + die("You must give a command name") command = sys.argv[1] if not command in COMMANDS.keys(): - abort("This command does not exist") + die("This command does not exist") command_all_args = COMMANDS[command] # Read command args if should be command_arg = None if command_all_args: if len(sys.argv) <= 2: - abort("This command should be given an argument") + die("This command should be given an argument") command_arg = sys.argv[2] if not command_arg in command_all_args: - abort("This argument does not exist for command " + command) + die("This argument does not exist for command " + command) print("Executing command", command) if command_arg: diff --git a/src/agent/Agent.py b/src/agent/Agent.py index 2dd1c2e..c6c7344 100644 --- a/src/agent/Agent.py +++ b/src/agent/Agent.py @@ -13,8 +13,6 @@ import os - 1 table par agent - table agents_log avec log minimum pour affichage dans dashboard, et ordre chrono intéressant pour suivi activité : nom agent, timestamp, message - table agent__vars : nom variable, value, desc -- table agents_commands : agent demandeur, agent dest, deposit_time (timestamp), durée validité (valeur par défaut), commande (format à définir), ack_time (timestamp) -(l'agent destinataire en profite pour supprimer les commandes périmées qui le concernent) """ @@ -119,8 +117,9 @@ class Agent: raise Exception(f"Bad config file name '{config_filename}', error {str(self.config.get_last_errno())}: {str(self.config.get_last_errmsg())}") tmp = AgentsSurvey.objects.filter(name=self.name) if len(tmp) == 0: - self._agents_survey = AgentsSurvey(name=self.name, mode=self.mode, status=self.status) - self._agents_survey.save() + self._agents_survey = AgentsSurvey.objects.create(name=self.name, validity_duration_sec=60, mode=self.mode, status=self.status) + #self._agents_survey = AgentsSurvey(name=self.name, validity_duration_sec=60, mode=self.mode, status=self.status) + #self._agents_survey.save() print("agent is", self._agents_survey) diff --git a/src/common/models.py b/src/common/models.py index c175238..99992e6 100644 --- a/src/common/models.py +++ b/src/common/models.py @@ -198,9 +198,32 @@ class Request(models.Model): ------------------------ """ +class AgentsCommand(models.Model): + """ + | id | sender | receiver | command | validity_duration_sec (default=60) | sender_deposit_time | receiver_read_time + (l'agent destinataire en profite pour supprimer les commandes périmées qui le concernent) + """ + #sender = models.CharField(max_length=50, blank=True, null=True, unique=True) + sender = models.CharField(max_length=50, unique=True) + receiver = models.CharField(max_length=50, unique=True) + command = models.CharField(max_length=100) + validity_duration_sec = models.PositiveIntegerField(blank=True, default=60) + sender_deposit_time = models.DateTimeField(blank=True, null=True, auto_now_add=True) + receiver_read_time = models.DateTimeField(blank=True, null=True, auto_now=True) + + class Meta: + managed = True + db_table = 'agents_command' + #verbose_name = "agent survey" + #verbose_name_plural = "agents survey" + + def __str__(self): + return (f"Agent {self.sender} sent commmand {self.command} to {self.receiver} at {self.sender_deposit_time}") + + class AgentsSurvey(models.Model): """ - | id | name | created | validity_duration_sec (default=1mn) | mode (active/idle) | status (launch/init/loop/exit/...) | + | id | name | created | updated | validity_duration_sec (default=1mn) | mode (active/idle) | status (launch/init/loop/exit/...) | """ # Statuses STATUS_LAUNCH = "LAUNCHED" @@ -227,6 +250,8 @@ class AgentsSurvey(models.Model): ) name = models.CharField(max_length=50, blank=True, null=True, unique=True) + #created = models.DateTimeField(blank=True, null=True, auto_now_add=True) + created = models.DateTimeField(blank=True, null=True, auto_now_add=True) updated = models.DateTimeField(blank=True, null=True, auto_now=True) validity_duration_sec = models.PositiveIntegerField(blank=True, null=True) mode = models.CharField('agent mode', max_length=15, blank=True, null=True, choices=MODE_CHOICES) -- libgit2 0.21.2