Commit f4fd20f9661e96d6402cf973ff03984c03664880

Authored by Etienne Pallier
1 parent 01348735
Exists in dev

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
README.md
... ... @@ -67,14 +67,23 @@ This software has been tested and validated with the following configurations :
67 67 --------------------------------------------------------------------------------------------
68 68 ## LAST VERSION
69 69  
70   -Date: 05/02/2019
  70 +Date: 27/02/2019
71 71  
72 72 Author: E. Pallier
73 73  
74   -VERSION: 20190205.0.000.0 (0.20)
75   -
76   -Comment:
77   - Update README and REDMINE
  74 +VERSION: 20190227.0.000.0 (0.20)
  75 +
  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
  86 +
78 87  
79 88 --------------------------------------------------------------------------------------------
80 89 - TECHNICAL DOC: tinyurl.com/pyros-doc
... ... @@ -95,6 +104,8 @@ Comment:
95 104  
96 105 Full list of commits: https://gitlab.irap.omp.eu/epallier/pyros/commits/master
97 106  
  107 +The list below is obsolete. The new official changes history is here : https://tinyurl.com/pyros-dev#heading=h.2r55bflctpt5
  108 +
98 109 **17/01/19 : Version 20190117.0.000.0 (EP)**
99 110 - NO MORE CELERY (removed from everywhere)
100 111  
... ...
install/install.py
... ... @@ -332,14 +332,14 @@ def install_database(venv):
332 332 #TODO: from venv !!!
333 333 try:
334 334 #os.system(GLOBAL_PYTHON+" pyros.py init_database")
335   - os.system(GLOBAL_PYTHON+" pyros.py initdatabase")
  335 + os.system(GLOBAL_PYTHON+" pyros.py initdb")
336 336 '''
337 337 process = subprocess.Popen(GLOBAL_PYTHON + " pyros.py init_database" , shell=True)
338 338 process.wait()
339 339 '''
340 340 except Exception as e:
341 341 print("Exception ", e)
342   - print(Colors.ERROR + "Error while initialising database :" + Colors.END)
  342 + print(Colors.ERROR + "Error while initializing database :" + Colors.END)
343 343 return -1
344 344  
345 345 print(Colors.LOG_BLUE + "\r\n\r\n-----------------------------Install successfull !-----------------------------" + Colors.END)
... ...
pyros.py
... ... @@ -123,10 +123,11 @@ def execProcess(command, from_venv=False, is_async=False):
123 123 else:
124 124 printFullTerm(Colors.WARNING, "Process execution failed")
125 125 # self.addError(self.current_command, command)
126   - return process.returncode
  126 + #return process.returncode
  127 + return True if process.returncode==0 else False
127 128  
128 129 def execProcessFromVenv(command:str):
129   - execProcess(command, from_venv=True)
  130 + return execProcess(command, from_venv=True)
130 131  
131 132 #TODO: fusionner dans execProcess avec param is_async
132 133 def execProcessFromVenvAsync(command:str):
... ... @@ -169,6 +170,13 @@ def printFullTerm(color: Colors, string: str):
169 170 return 0
170 171  
171 172  
  173 +def die():
  174 + print()
  175 + print("...ERROR...")
  176 + print()
  177 + exit(1)
  178 +
  179 +
172 180  
173 181 """
174 182 ********************************************************************************
... ... @@ -244,63 +252,48 @@ def install():
244 252 # return 0
245 253 return True
246 254  
  255 +
  256 +
  257 +
247 258 '''
248 259 TODO:
249 260 '''
250 261 @pyros_launcher.command(help="Update the pyros software (git pull + update DB if necessary)")
251 262 def update():
252 263 print("Running update command")
253   - print("git pull")
254   - print("update db (make migrations + migrate)")
  264 + #res = _gitpull()
  265 + _gitpull() or die()
  266 + #if not res: return False
  267 + _updatedb() or die()
255 268 return True
256 269  
257   -@pyros_launcher.command(help="Init the database with initial fixture data")
258   -def initdatabase():
259   - makemigrations()
260   - migrate()
261   - loaddata()
262   - return 0
263   -
264   -def migrate():
265   - changeDirectory("src")
266   - execProcessFromVenv("manage.py migrate")
267   - changeDirectory("..")
268   - return 0
269   -
270   -def makemigrations():
271   - changeDirectory("src")
272   - #execProcessFromVenv(self.venv_bin + " manage.py makemigrations")
273   - execProcessFromVenv("manage.py makemigrations")
274   - changeDirectory("..")
275   - return 0
276   -
277   -#TODO: mettre la fixture en date naive (sans time zone)
278   -def loaddata():
279   - changeDirectory("src")
280   - #execProcessFromVenv(self.venv_bin + " manage.py loaddata misc" + os.sep + "fixtures" + os.sep + self.init_fixture)
281   - execProcessFromVenv("manage.py loaddata misc" + os.sep + "fixtures" + os.sep + init_fixture)
282   - changeDirectory("..")
283   - return 0
284 270  
285   -def changeDirectory(path):
286   - if DEBUG: print("Moving to : " + path)
287   - os.chdir(path)
288   - if DEBUG: print("Current directory : " + str(os.getcwd()))
289   - return 0
  271 +def _gitpull():
  272 + print("-- running git pull")
  273 + GIT = "git.exe" if IS_WINDOWS else "git"
  274 + if not test_mode(): return execProcess(f"{GIT} pull")
  275 + return True
290 276  
  277 +#@pyros_launcher.command(help="Update the pyros database")
  278 +def _updatedb():
  279 + print("-- update db (make migrations + migrate)")
  280 + if not test_mode() :
  281 + _makemigrations() or die()
  282 + _migrate() or die()
  283 + return True
291 284  
292 285  
293   -def check_agent(agent):
294   - # Check that agent exists
295   - if agent not in AGENTS.keys() and agent != "all":
296   - print("This agent does not exist")
297   - print("Here is the allowed list of agents:")
298   - print("- all => will launch ALL agents")
299   - for agent_name in AGENTS.keys(): print('-',agent_name)
300   - return False
  286 +@pyros_launcher.command(help="Update the pyros database and fill it with initial fixture data")
  287 +def initdb():
  288 + if not test_mode():
  289 + #updatedb()
  290 + _makemigrations()
  291 + _migrate()
  292 + _loaddata()
301 293 return True
302 294  
303 295  
  296 +
304 297 @pyros_launcher.command(help="Launch an agent")
305 298 #@global_test_options
306 299 @click.argument('agent')
... ... @@ -316,7 +309,7 @@ def start(agent:str, configfile:str):
316 309 configfile = ''
317 310 #if test_mode(): print("in test mode")
318 311 #if verbose_mode(): print("in verbose mode")
319   - if not check_agent(agent): return
  312 + if not _check_agent(agent): return
320 313 # VENV_BIN = 'private/venv_py3_pyros' + os.sep + self.bin_dir + os.sep + self.bin_name
321 314  
322 315  
... ... @@ -347,13 +340,13 @@ def start(agent:str, configfile:str):
347 340 os.chdir("src")
348 341 #if not test_mode(): execProcessFromVenv("start_agent.py " + agent_name + " " + configfile)
349 342 if not test_mode(): execProcessFromVenv(cmd)
350   - # self.changeDirectory("..")
  343 + # self._change_dir("..")
351 344  
352 345 '''
353 346 # Any other agent
354 347 else:
355 348 # Go into src/
356   - # self.changeDirectory("src")
  349 + # self._change_dir("src")
357 350 os.chdir("src")
358 351 # print("Current directory : " + str(os.getcwd()))
359 352 if agent_name != "webserver": os.chdir(agent_folder)
... ... @@ -369,12 +362,12 @@ def start(agent:str, configfile:str):
369 362 if agent_name == "webserver": cmd = "manage.py runserver"
370 363 if not test_mode(): execProcessFromVenv(cmd)
371 364 # Go back to src/
372   - # self.changeDirectory('..')
  365 + # self._change_dir('..')
373 366 os.chdir("..")
374 367 '''
375 368  
376 369 # Go back to root folder (/)
377   - # self.changeDirectory('..')
  370 + # self._change_dir('..')
378 371 os.chdir("..")
379 372 return True
380 373  
... ... @@ -385,10 +378,62 @@ def start(agent:str, configfile:str):
385 378 @click.argument('agent')
386 379 def stop(agent):
387 380 print("Running stop command")
388   - if not check_agent(agent): return
  381 + if not _check_agent(agent): return
389 382  
390 383  
391 384  
  385 +"""
  386 +********************************************************************************
  387 +******************** PRIVATE FUNCTIONS DEFINITION ******************************
  388 +********************************************************************************
  389 +"""
  390 +
  391 +def _migrate():
  392 + _change_dir("src")
  393 + res = execProcessFromVenv("manage.py migrate")
  394 + _change_dir("..")
  395 + return res
  396 +
  397 +def _makemigrations():
  398 + _change_dir("src")
  399 + #execProcessFromVenv(self.venv_bin + " manage.py makemigrations")
  400 + res = execProcessFromVenv("manage.py makemigrations")
  401 + print("res is", res)
  402 + _change_dir("..")
  403 + return res
  404 +
  405 +#TODO: mettre la fixture en date naive (sans time zone)
  406 +def _loaddata():
  407 + _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)
  410 + _change_dir("..")
  411 + return res
  412 +
  413 +def _change_dir(path):
  414 + if DEBUG: print("Moving to : " + path)
  415 + os.chdir(path)
  416 + if DEBUG: print("Current directory : " + str(os.getcwd()))
  417 +
  418 +
  419 +
  420 +def _check_agent(agent):
  421 + # Check that agent exists
  422 + if agent not in AGENTS.keys() and agent != "all":
  423 + print("This agent does not exist")
  424 + print("Here is the allowed list of agents:")
  425 + print("- all => will launch ALL agents")
  426 + for agent_name in AGENTS.keys(): print('-',agent_name)
  427 + return False
  428 + return True
  429 +
  430 +
  431 +
  432 +"""
  433 +********************************************************************************
  434 +********************************* main() FUNCTION ******************************
  435 +********************************************************************************
  436 +"""
392 437 def main():
393 438 pyros_launcher()
394 439  
... ...
src/agent/Agent.py
... ... @@ -105,15 +105,18 @@ class Agent:
105 105 if not config_filename:
106 106 #config_filename = '/PROJECTS/GFT/SOFT/PYROS_SOFT/CURRENT/config/config_unit_simulunit1.xml'
107 107 config_filename = self.DEFAULT_CONFIG_FILE_NAME
108   - config_abs_filename = os.path.abspath(self.CONFIG_DIR + os.sep + config_filename)
  108 + #config_file_path, _ = os.path.split(config_filename)
  109 + if config_filename == os.path.basename(config_filename):
  110 + config_filename = os.path.abspath(self.CONFIG_DIR + os.sep + config_filename)
  111 + print("Config file used is", config_filename)
109 112 #print("current path", os.getcwd())
110 113 #print("this file path :", __file__)
111 114 #print("config file path is", config_filename)
112 115 # Instantiate an object for configuration
113 116 #print("config file path is ", config_abs_filename)
114   - self.config = ConfigPyros(config_abs_filename)
  117 + self.config = ConfigPyros(config_filename)
115 118 if self.config.get_last_errno() != self.config.NO_ERROR:
116   - raise Exception(f"Bad config file name '{config_abs_filename}', error {str(self.config.get_last_errno())}: {str(self.config.get_last_errmsg())}")
  119 + raise Exception(f"Bad config file name '{config_filename}', error {str(self.config.get_last_errno())}: {str(self.config.get_last_errmsg())}")
117 120 tmp = AgentsSurvey.objects.filter(name=self.name)
118 121 if len(tmp) == 0:
119 122 self._agents_survey = AgentsSurvey(name=self.name, mode=self.mode, status=self.status)
... ...
src/pyros/settings.py
... ... @@ -73,7 +73,7 @@ INSTALLED_APPS = [
73 73 'django.contrib.sessions',
74 74 'django.contrib.messages',
75 75  
76   - # For debug_toolbar
  76 + # (EP) For debug_toolbar
77 77 'django.contrib.staticfiles',
78 78 'debug_toolbar',
79 79  
... ... @@ -82,6 +82,8 @@ INSTALLED_APPS = [
82 82 'django_extensions',
83 83 'test_without_migrations',
84 84 'bootstrap3',
  85 +
  86 + # PYROS APPS
85 87 'dashboard',
86 88 'scheduler',
87 89 'common',
... ...
start_agent.py
... ... @@ -25,8 +25,8 @@ agent_name = sys.argv[1]
25 25  
26 26 # arg 2 : config file
27 27 if len(sys.argv) == 3:
28   - configfile = sys.argv[1]
29   -
  28 + configfile = sys.argv[2]
  29 +
30 30 # Conseil sur le net:
31 31 #https://stackoverflow.com/questions/16853649/how-to-execute-a-python-script-from-the-django-shell
32 32 """
... ...