Blame view

pyros.py 39.2 KB
d0b663be   Etienne Pallier   refactorized majo...
1
#!/usr/bin/env python
20ed1e7a   Etienne Pallier   bugfixed time sav...
2

4816e86b   Jeremy   removed *.sh *.ba...
3
4
5
6
7
8
9
import sys
import os
import subprocess
import platform
import fileinput
import argparse
import time
bca9a283   Jeremy   Reworked the sche...
10
import signal
4816e86b   Jeremy   removed *.sh *.ba...
11

cd0149f5   Etienne Pallier   progress made for...
12
13
# For USE_CELERY global variable
#from src.pyros import settings
f3534c59   Etienne Pallier   Simulators web pa...
14
15
USE_CELERY = False
#USE_CELERY = True
cd0149f5   Etienne Pallier   progress made for...
16

d1e6236c   Etienne Pallier   Refactorized pyro...
17
DEBUG = True
a64f430a   Unknown   Adding argument t...
18
SIMULATOR_CONFIG_FILE = "conf.json" #default config file
4816e86b   Jeremy   removed *.sh *.ba...
19

257abe9b   Jeremy   Added comments
20
21
22
23
24
'''
    Pyros Manager, able to launch processes and handle all project commands
'''


4816e86b   Jeremy   removed *.sh *.ba...
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
class Utils:
    system = platform.system()
    columns = 100
    row = 1000
    disp = True

    def __init__(self):
        if (platform.system() != 'Windows'):
            try:
                rows, columns = os.popen('stty size', 'r').read().split()
                self.columns = int(columns)
            except:
                self.columns = 100
                if DEBUG:
                    print("Could not get terminal size")

    def printFullTerm(self, color, string):
        value = int(self.columns / 2 - len(string) / 2)
        self.printColor(color, "-" * value, eol='')
        self.printColor(color, string, eol='')
        value += len(string)
        self.printColor(color, "-" * (self.columns - value))
        return 0

    def changeDirectory(self, path):
        if DEBUG:
            print("Moving to : " + path)
        os.chdir(path)
        if DEBUG:
            print("Current directory : " + str(os.getcwd()))
        return 0

    def replacePatternInFile(self, pattern, replace, file_path):
737529b0   Etienne Pallier   bugfixes and impr...
58
59
60
61
62
        #try:
        with fileinput.FileInput(file_path, inplace=True, backup='.bak') as file:
            for line in file:
                print(line.replace(pattern, replace), end='')
        '''
4816e86b   Jeremy   removed *.sh *.ba...
63
64
65
        except:
            return 1
        return 0
737529b0   Etienne Pallier   bugfixes and impr...
66
67
68
69
70
71
72
73
74
75
76
77
78
79
        '''
        # Now, check that replacement has been done or else ERROR !!!
        #pattern = "CELERY_TEST = "+str(set_it)
        FOUND=False
        #with fileinput.FileInput(file_path) as file:
        with open(file_path) as file:
            for line in file:
                #if replace in line:
                if line.startswith(replace):
                    FOUND = True
                    break
        #if FOUND: print("pattern "+pattern+" found in file "+file_path)
        if not FOUND: raise(Exception("pattern "+replace+" not found in file "+file_path))

4816e86b   Jeremy   removed *.sh *.ba...
80
81
82
83
84
85
86
87
88
89
90
91
92
93

    def printColor(self, color, message, file=sys.stdout, eol=os.linesep, forced=False):
        if (self.disp == False and forced == False):
            return 0
        if (self.system == 'Windows'):
            print(message, file=file, end=eol)
        else:
            print(color + message + Colors.ENDC, file=file, end=eol)
        return 0

    def askQuestion(self, message, default = ""):
        self.printColor(Colors.BLUE, message, forced=True)
        self.printColor(Colors.BOLD, "Answer (default="+default+"): ", eol='', forced=True)
        sys.stdout.flush()
27e81f38   jeremy   Update windows
94
        ret = sys.stdin.readline().replace('\n', '').replace('\r', '')
4816e86b   Jeremy   removed *.sh *.ba...
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
        if ret == "":
            return default
        return ret

    def sleep(self, t):
        time.sleep(t)
        return 0

'''
    Manager class : manager of your project
'''


class AManager(Utils):
    path = os.path.realpath(__file__)
    path_dir = os.getcwd()
    path_dir_file = os.path.dirname(os.path.realpath(__file__))
    python_path = sys.executable
    python_version = sys.version_info

    bin_dir = ""
    celery = "celery"
    venv_pip = "pip"
    venv_bin = "python"
    wait = True
    current_command = ""
    config = None
    commandMatcher = {}
    commandDescription = {}
    commands = []
    errors = {}
    executed = {}
bca9a283   Jeremy   Reworked the sche...
127
    subproc = []
4816e86b   Jeremy   removed *.sh *.ba...
128
129
130
131
132
133
134
135

    def __init__(self, param):
        super(AManager, self).__init__()
        self.wait = param.getWait()
        self.commands = param.getCommandList()
        self.disp = param.getPrint()
        config = param.getConfig()
        self.config = config
bca9a283   Jeremy   Reworked the sche...
136
        signal.signal(signal.SIGINT, self.signal_handler)
4816e86b   Jeremy   removed *.sh *.ba...
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157

        self.changeDirectory(self.path_dir_file)

        if self.system == 'Windows':
            self.bin_dir = "Scripts"
            self.bin_name = "python.exe"
            self.pip_name = "pip.exe"
            self.celery = "celery.exe"
        else:
            self.bin_dir = "bin"
            self.bin_name = "python"
            self.pip_name = "pip"
            self.celery = "celery"
        self.venv_pip = self.path_dir_file + os.sep + config["path"] + os.sep + config["env"] + os.sep + self.bin_dir + os.sep + self.pip_name
        self.venv_bin = self.path_dir_file + os.sep + config["path"] + os.sep + config["env"] + os.sep + self.bin_dir + os.sep + self.bin_name
        self.venv_cel = self.path_dir_file + os.sep + config["path"] + os.sep + config["env"] + os.sep + self.bin_dir + os.sep + self.celery

    def help(self):
        print("This function must be implemented")
        raise(NotImplementedError("Function not implemented"))

54467c54   Etienne Pallier   refactorisation
158

737529b0   Etienne Pallier   bugfixes and impr...
159
    def set_celery_test_and_simulator_modes_to(self, set_it:bool, simulator:bool=True):
54467c54   Etienne Pallier   refactorisation
160
161
162
163
164
165
166
        file_path = "pyros/settings.py"
        '''
        self.replacePatternInFile("CELERY_TEST = False", "CELERY_TEST = True", file_path)
        if simulator: self.replacePatternInFile("SIMULATOR = False", "SIMULATOR = True", file_path)
        '''
        self.replacePatternInFile("CELERY_TEST = "+str(not set_it), "CELERY_TEST = "+str(set_it), file_path)
        if simulator: self.replacePatternInFile("SIMULATOR = "+str(not set_it), "SIMULATOR = "+str(set_it), file_path)
54467c54   Etienne Pallier   refactorisation
167
168


bca9a283   Jeremy   Reworked the sche...
169
170
    def signal_handler(self, signal, frame):
        self.printFullTerm(Colors.WARNING, "Ctrl-c catched")
54467c54   Etienne Pallier   refactorisation
171
        self.set_celery_test_and_simulator_modes_to(False)
bca9a283   Jeremy   Reworked the sche...
172
173
174
175
176
177
178
        for p in self.subproc:
            proc, name = p
            self.printColor(Colors.BLUE, "Killing process " + str(name))
            proc.kill()
        self.printFullTerm(Colors.WARNING, "Exiting")
        sys.exit(0)

4816e86b   Jeremy   removed *.sh *.ba...
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
    def addExecuted(self, src, message):
        if (src in self.executed):
            self.executed[src].append(str(message))
        else:
            self.executed[src] = [str(message)]
        return 0

    def addError(self, src, message):
        if (src in self.errors):
            self.errors[src].append(str(message))
        else:
            self.errors[src] = [str(message)]
        return 0

    def execProcess(self, command):
        self.printFullTerm(Colors.BLUE, "Executing command [" + command + "]")
        process = subprocess.Popen(command, shell=True)
        process.wait()
        if process.returncode == 0:
            self.printFullTerm(Colors.GREEN, "Process executed successfully")
            self.addExecuted(self.current_command, command)
        else:
            self.printFullTerm(Colors.WARNING, "Process execution failed")
            self.addError(self.current_command, command)
        return process.returncode

bca9a283   Jeremy   Reworked the sche...
205
206
207
208
209
    def execProcessSilent(self, command):
        process = subprocess.Popen(command, shell=True)
        process.wait()
        return process.returncode

4816e86b   Jeremy   removed *.sh *.ba...
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
    def execProcessFromVenv(self, command):
        args = command.split()
        self.printFullTerm(Colors.BLUE, "Executing command from venv [" + str(' '.join(args[1:])) + "]")
        process = subprocess.Popen(args)
        process.wait()
        if process.returncode == 0:
            self.printFullTerm(Colors.GREEN, "Process executed successfully")
            self.addExecuted(self.current_command, str(' '.join(args[1:])))
        else:
            self.printFullTerm(Colors.WARNING, "Process execution failed")
            self.addError(self.current_command, str(' '.join(args[1:])))
        return process.returncode

    def execProcessAsync(self, command):
        self.printFullTerm(Colors.BLUE, "Executing command [" + command + "]")
bca9a283   Jeremy   Reworked the sche...
225
226
        p = subprocess.Popen(command, shell=True)
        self.subproc.append((p, command))
4816e86b   Jeremy   removed *.sh *.ba...
227
228
        self.printFullTerm(Colors.GREEN, "Process launched successfully")
        self.addExecuted(self.current_command, command)
bca9a283   Jeremy   Reworked the sche...
229
        return p
4816e86b   Jeremy   removed *.sh *.ba...
230

bca9a283   Jeremy   Reworked the sche...
231
    def execProcessFromVenvAsync(self, command: str):
4816e86b   Jeremy   removed *.sh *.ba...
232
233
        args = command.split()
        self.printFullTerm(Colors.BLUE, "Executing command from venv [" + str(' '.join(args[1:])) + "]")
bca9a283   Jeremy   Reworked the sche...
234
235
        p = subprocess.Popen(args)
        self.subproc.append((p, ' '.join(args[1:])))
4816e86b   Jeremy   removed *.sh *.ba...
236
237
        self.printFullTerm(Colors.GREEN, "Process launched successfully")
        self.addExecuted(self.current_command, str(' '.join(args[1:])))
bca9a283   Jeremy   Reworked the sche...
238
239
240
241
242
243
244
        return p

    def waitProcesses(self):
        if (self.wait):
            for p in self.subproc:
                proc, name = p
                proc.wait()
4816e86b   Jeremy   removed *.sh *.ba...
245
246
247
248
        return 0

    def end(self):
        count = 0
bca9a283   Jeremy   Reworked the sche...
249
        self.waitProcesses()
4816e86b   Jeremy   removed *.sh *.ba...
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
        self.printFullTerm(Colors.WARNING, "Summary")
        self.printColor(Colors.GREEN, "Success : ")
        for command, valid in self.executed.items():
            if not valid:
                self.printColor(Colors.BLUE, "\t- Command : " + command + " success !")
            else:
                self.printColor(Colors.WARNING, "\t- In commmand : " + command)
                for exe in valid:
                    self.printColor(Colors.GREEN, "\t\t - Command : " + exe + " success !")
        self.printColor(Colors.FAIL, "Errors : ")
        if not self.errors:
            self.printColor(Colors.GREEN, "\tNone")
        for command, items in self.errors.items():
            count += 1
            if (not items):
                self.printColor(Colors.FAIL, "Command : " + command + " failed !")
            else:
                self.printColor(Colors.WARNING, "\t- In commmand : " + command)
                for exe in items:
                    self.printColor(Colors.FAIL, "\t\t - Command : " + exe)
        return count

    def exec(self):
        if (not self.commands):
            self.commandMatcher["help"]()
            return 0
        for command in self.commands:
            self.current_command = command
            if command in self.commandMatcher:
                self.commandMatcher[command]()
            else:
                self.addError(str(command), "invalid command")
        return self.end()

    def logError(self, message):
        self.printColor(Colors.FAIL, "Pyros : An error occurred [" + message + "]", file=sys.stderr)
        return 0

257abe9b   Jeremy   Added comments
288
289
290
291
'''
    Config file representation (able to parse and give informations)
'''

4816e86b   Jeremy   removed *.sh *.ba...
292
293

class Config:
7db4ab5f   Unknown   small change in h...
294
    __parser = argparse.ArgumentParser("Project Pyros")
4816e86b   Jeremy   removed *.sh *.ba...
295
296
297
298
299
300
301
302
303
304
    __content = {
        "path": "private",
        "env": "venv_py3_pyros"
    }
    __wait = True
    __print = True
    usage = ""
    __command_list = []

    def __init__(self):
a64f430a   Unknown   Adding argument t...
305
        self.__parser.add_argument("command", nargs='*', default="help", help="The command you want to execute (default=help)")
4816e86b   Jeremy   removed *.sh *.ba...
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
        self.__parser.add_argument("--env", help="Your environment directory name default=venv")
        self.__parser.add_argument("--path", help="Path to the virtual env (from the source file directory) (default=private)")
        self.__parser.add_argument("--nowait", action='store_true', help="Don't wait the end of a program")
        self.__parser.add_argument("--noprint", action='store_true', help="Won't print")
        self.usage = self.__parser.format_usage()

    def parse(self):
        try:
            res = self.__parser.parse_args(), self.__parser.format_usage()
            return (res)
        except SystemExit as e:
            # print(e, file=sys.stderr)
            sys.exit(1)

    def parseConf(self):
        res, usage = self.parse()
        try:
            if (res.env):
                self.__content["env"] = res.env
            if (res.path):
                self.__content["path"] = res.path
            if (res.nowait):
                self.__wait = False
            if (res.noprint):
                self.__print = False
a64f430a   Unknown   Adding argument t...
331
            self.__command_list.append(res.command[0])
b4e8963c   Etienne Pallier   Each agent (envmo...
332
333
            #return 
            return True
4816e86b   Jeremy   removed *.sh *.ba...
334
335
        except Exception as e:
            print(e, file=sys.stderr)
b4e8963c   Etienne Pallier   Each agent (envmo...
336
337
            #return 1
            return False
4816e86b   Jeremy   removed *.sh *.ba...
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411

    def getPath(self):
        return self.__content["path"]

    def getEnv(self):
        return self.__content["env"]

    def getWait(self):
        return self.__wait

    def getPrint(self):
        return self.__print

    def setPrint(self, value):
        self.__print = value
        return 0

    def setWait(self, value):
        self.__wait = value
        return 0

    def getCommandList(self):
        return self.__command_list

    def printUsage(self):
        print(self.usage, file=sys.stderr)

    def addConf(self, key, value):
        if isinstance(key, str) and isinstance(value, str):
            self.__content[key] = value
            return 0
        return 1

    def setPath(self, path):
        if (os.path.isdir(path)):
            if (path == ""):
                path = "."
            self.__content["path"] = path
            return 0
        return 1

    def setEnvName(self, name):
        self.__content["env"] = name
        return 0

    def getConfig(self):
        return self.__content


'''
    Color class
'''


class Colors:
    HEADER = '\033[95m'
    BLUE = '\033[94m'
    GREEN = '\033[92m'
    WARNING = '\033[93m'
    FAIL = '\033[91m'
    ENDC = '\033[0m'
    BOLD = '\033[1m'
    UNDERLINE = '\033[4m'


'''
    Pyros class
'''


class Pyros(AManager):
    help_message = "python neo.py"
    init_fixture = "initial_fixture.json"

ff448d43   Jeremy   Update
412
413
414
415
416
417
418
419
420
421
422
423
    def signal_handler(self, signal, frame):
        self.printFullTerm(Colors.WARNING, "Ctrl-c catched")
        for p in self.subproc:
            proc, name = p
            self.printColor(Colors.BLUE, "Killing process " + str(name))
            proc.kill()
        if self.current_command == "simulator" or self.current_command == "simulator_development":
            self.changeDirectory(self.path_dir_file)
            self.kill_simulation()
        self.printFullTerm(Colors.WARNING, "Exiting")
        sys.exit(0)

4816e86b   Jeremy   removed *.sh *.ba...
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
    def install(self):
        if (self.system == "Windows"):
            self.execProcess("python install/install.py install")
        else:
            self.execProcess("python3 install/install.py install")
        return 0

    def update(self):
        if (self.system == "Windows"):
            self.execProcess("python install/install.py update")
        else:
            self.execProcess("python3 install/install.py update")
        return 0

    def server(self):
        self.changeDirectory("src")
        self.execProcessFromVenvAsync(self.venv_bin + " manage.py runserver")
        self.changeDirectory("..")
        return 0

    def clean(self):
        return self.clean_logs()

    def clean_logs(self):
        return self.execProcess("rm logs/*.log")

    def test(self):
e573c1f1   Etienne Pallier   All unit tests no...
451
        ##self.singleWorker("scheduling")
4816e86b   Jeremy   removed *.sh *.ba...
452
        self.changeDirectory("src")
e573c1f1   Etienne Pallier   All unit tests no...
453
        ##self.execProcessFromVenv(self.venv_bin + " manage.py test")
4816e86b   Jeremy   removed *.sh *.ba...
454
455
        self.execProcessFromVenvAsync(self.venv_bin + " manage.py test")
        self.changeDirectory("..")
e573c1f1   Etienne Pallier   All unit tests no...
456
        ##self.stop_workers()
4816e86b   Jeremy   removed *.sh *.ba...
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
        return 0

    def migrate(self):
        self.changeDirectory("src")
        self.execProcessFromVenv(self.venv_bin + " manage.py migrate")
        self.changeDirectory("..")
        return 0

    def makemigrations(self):
        self.changeDirectory("src")
        self.execProcessFromVenv(self.venv_bin + " manage.py makemigrations")
        self.changeDirectory("..")
        return 0

    def help(self):
3b0ef1c7   Jeremy   Beautify help mes...
472
473
        count = 0
        self.printFullTerm(Colors.WARNING, "Help Message")
4816e86b   Jeremy   removed *.sh *.ba...
474
        for command, message in self.commandDescription.items():
3b0ef1c7   Jeremy   Beautify help mes...
475
476
477
478
479
            count += 1
            if (self.columns > 100):
                self.printColor(Colors.BLUE, "\t"+str(count)+(' ' if count < 10 else '')+": " + command + ": ", eol='')
            else:
                self.printColor(Colors.BLUE, "-> " + command + ": ", eol='')
4816e86b   Jeremy   removed *.sh *.ba...
480
481
482
483
            self.printColor(Colors.GREEN, message)
        return 0

    def updatedb(self):
4816e86b   Jeremy   removed *.sh *.ba...
484
        self.makemigrations()
3b0ef1c7   Jeremy   Beautify help mes...
485
        self.migrate()
4816e86b   Jeremy   removed *.sh *.ba...
486
487
        return 0

c53a13e0   Jeremy   Updating a lot of...
488
489
    def reset_config(self):
        self.changeDirectory("src")
54467c54   Etienne Pallier   refactorisation
490
        self.set_celery_test_and_simulator_modes_to(False)
c53a13e0   Jeremy   Updating a lot of...
491
492
493
494
        self.addExecuted(self.current_command, "reset configuration")
        self.changeDirectory("..")
        return 0

f4dd5b4a   Unknown   merging pyros.py
495
496
497
498
499
500
501
502
    def unittest(self):
        #self.changeDirectory("src")
        apps = ['common', 'scheduler', 'routine_manager', 'user_manager', 'alert_manager.tests.TestStrategyChange']
        for app in apps:
            self.loaddata()
            self.changeDirectory("src")
            self.execProcessFromVenv(self.venv_bin + ' manage.py test ' + app)
            self.changeDirectory("..")
4816e86b   Jeremy   removed *.sh *.ba...
503
504
505
506
        return 0

    def test_all(self):
        self.unittest()
4816e86b   Jeremy   removed *.sh *.ba...
507
508
        return 0

d1e6236c   Etienne Pallier   Refactorized pyro...
509
    #TODO: mettre la fixture en date naive (sans time zone)
4816e86b   Jeremy   removed *.sh *.ba...
510
511
    def loaddata(self):
        self.changeDirectory("src")
3185a08a   Unknown   updating init_dat...
512
        self.execProcessFromVenv(self.venv_bin + " manage.py loaddata misc" + os.sep + "fixtures" + os.sep + self.init_fixture)
4816e86b   Jeremy   removed *.sh *.ba...
513
514
515
        self.changeDirectory("..")
        return 0

d1e6236c   Etienne Pallier   Refactorized pyro...
516

fd782a47   Etienne Pallier   refactorisations ...
517
    def celery_on(self, TOTAL=True):
4816e86b   Jeremy   removed *.sh *.ba...
518
        self.changeDirectory("src")
fd782a47   Etienne Pallier   refactorisations ...
519
520
521
522
523

        if TOTAL: self.execProcessFromVenvAsync(self.venv_cel + " worker -A pyros -Q alert_listener_q -n pyros@alert_listener -c 1")
        if TOTAL: self.execProcessFromVenvAsync(self.venv_cel + " worker -A pyros -Q monitoring_q -n pyros@monitoring -c 1")

        #self.singleWorker("majordome")
4816e86b   Jeremy   removed *.sh *.ba...
524
525
        self.execProcessFromVenvAsync(self.venv_cel + " worker -A pyros -Q majordome_q -n pyros@majordome -c 1")

fd782a47   Etienne Pallier   refactorisations ...
526
        #self.singleWorker("scheduling")
4816e86b   Jeremy   removed *.sh *.ba...
527
        self.execProcessFromVenvAsync(self.venv_cel + " worker -A pyros -Q scheduling_q --purge -n pyros@scheduling -c 1")
fd782a47   Etienne Pallier   refactorisations ...
528
529
        
        #self.singleWorker("execute_plan_vis")
4816e86b   Jeremy   removed *.sh *.ba...
530
        self.execProcessFromVenvAsync(self.venv_cel + " worker -A pyros -Q execute_plan_vis_q --purge -n pyros@execute_plan_vis -c 1")
fd782a47   Etienne Pallier   refactorisations ...
531
532
533
534

        if TOTAL: self.execProcessFromVenvAsync(self.venv_cel + " worker -A pyros -Q night_calibrations_q --purge -n pyros@night_calibrations -c 1")
        
        #self.singleWorker("execute_plan_nir")
c53a13e0   Jeremy   Updating a lot of...
535
        self.execProcessFromVenvAsync(self.venv_cel + " worker -A pyros -Q execute_plan_nir_q --purge -n pyros@execute_plan_nir -c 1")
fd782a47   Etienne Pallier   refactorisations ...
536
537
        
        #self.singleWorker("create_calibrations")
4816e86b   Jeremy   removed *.sh *.ba...
538
        self.execProcessFromVenvAsync(self.venv_cel + " worker -A pyros -Q create_calibrations_q --purge -n pyros@create_calibrations -c 1")
fd782a47   Etienne Pallier   refactorisations ...
539
540

        #self.singleWorker("analysis")
4816e86b   Jeremy   removed *.sh *.ba...
541
        self.execProcessFromVenvAsync(self.venv_cel + " worker -A pyros -Q analysis_q --purge -n pyros@analysis -c 1")
fd782a47   Etienne Pallier   refactorisations ...
542
        
4816e86b   Jeremy   removed *.sh *.ba...
543
544
545
        self.changeDirectory("..")
        return 0

b4e8963c   Etienne Pallier   Each agent (envmo...
546
    # ex start()
fd782a47   Etienne Pallier   refactorisations ...
547
548
    def start_workers(self):
        self.stop_workers()
4816e86b   Jeremy   removed *.sh *.ba...
549
550
551
        self.celery_on()
        return 0

b4e8963c   Etienne Pallier   Each agent (envmo...
552
    # ex stop()
fd782a47   Etienne Pallier   refactorisations ...
553
    def stop_workers(self):
27e81f38   jeremy   Update windows
554
555
556
557
558
        if (self.system == "Windows"):
            self.execProcessAsync("taskkill /f /im celery.exe")
            self.execProcessAsync("taskkill /f /im python.exe")
        else:
            self.execProcessAsync("ps aux | grep \"celery worker\" | awk '{print $2}' | xargs kill -9")
4816e86b   Jeremy   removed *.sh *.ba...
559
560
        return 0

3b0ef1c7   Jeremy   Beautify help mes...
561
562
563
564
    def init_database(self):
        self.makemigrations()
        self.migrate()
        self.loaddata()
675fb3d5   Jeremy   Update scheduler ...
565
566
        return 0

3b0ef1c7   Jeremy   Beautify help mes...
567

b4e8963c   Etienne Pallier   Each agent (envmo...
568
569
570
571
572
573
574
575
576
577
578
579
    # Start the PyROS software

    # Start only the (django) Web server (pyros website)
    def start_web(self):
        self.start("web")
        #self.start(3)

    # Start only the agents (all of them)
    def start_agents(self):
        self.start("agents")

    # Start only 1 agent
40227fbd   Etienne Pallier   changed monitorin...
580
    def start_agent_monitoring(self):
b4e8963c   Etienne Pallier   Each agent (envmo...
581
582
583
584
585
586
587
588
589
        self.start("monitoring")
    def start_agent_majordome(self):
        self.start("majordome")
    def start_agent_alertmanager(self):
        self.start("alert_manager")
        
    # Start the PyROS software
    # By default (what=None), start everything : all the agents and also the web server (website, web viewing, for global monitoring)
    def start(self, what:str = None):
b70385ab   Etienne Pallier   bugfixing pyros s...
590
591
        #agents = ("alert_manager", "majordome", "monitoring")
        agents = ("majordome", "monitoring")
b4e8963c   Etienne Pallier   Each agent (envmo...
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
        '''
        what="monitoring"
        what="majordome"
        what="alert_manager"
        what="all"
        '''

        ''' OLD CODE
        self.stop_workers()
        #self.celery_on()
        # Start the 3 main agents (via their celery worker) :
        self.startAgent("monitoring")
        self.startAgent("majordome")
        self.startAgent("alert_listener")
        '''

        # Go into src/
        self.changeDirectory("src")
        #print("Current directory : " + str(os.getcwd()))

        # Start (Django) Web server (pyros website)
        if what=='web' or what is None:
            self.execProcessFromVenvAsync(self.venv_bin + " manage.py runserver")
            #self.changeDirectory("..")
40227fbd   Etienne Pallier   changed monitorin...
616

b4e8963c   Etienne Pallier   Each agent (envmo...
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
        # DJANGO setup
        ''' (optional)
        import sys
        sys.path.append('/PROJECTS/GFT/SOFT/PYROS_SOFT/PYROS201802/src')
        '''
        '''
        os.environ.setdefault("DJANGO_SETTINGS_MODULE", "src.pyros.settings")
        #os.environ['SECRET_KEY'] = 'abc'
        #os.environ['ENVIRONMENT'] = 'production'
        import django
        django.setup()
        '''

        # Start Agents
        '''            
        if agent=="majordome" or agent=="all":
            from majordome.tasks import Majordome
            Majordome().run()
        if agent=="alert_manager" or agent=="all":
            from alert_manager.tasks import AlertListener
            AlertListener().run()
        '''
        for agent in agents:
            if what==agent or what=="agents" or what is None:
                #os.chdir('src')
                '''
                from monitoring.tasks import Monitoring
                Monitoring().run()
                '''
                #self.changeDirectory("src/monitoring")
                #self.changeDirectory("src")
                self.changeDirectory(agent)
                self.execProcessFromVenvAsync(self.venv_bin + ' start_agent_'+agent+'.py')
737529b0   Etienne Pallier   bugfixes and impr...
650
651
652
653
654
                # Go back to src/
                self.changeDirectory('..')
                #os.chdir('..')
        # Go back to root folder (/)
        self.changeDirectory('..')
b4e8963c   Etienne Pallier   Each agent (envmo...
655
656
657
658
659
660


            
    def startAgent(self, agent_name:str):
        # Start Celery Worker
        self.singleWorker(agent_name)
a64f430a   Unknown   Adding argument t...
661
        time.sleep(3)
b4e8963c   Etienne Pallier   Each agent (envmo...
662
663
664
        # Put the agent task run() into the Worker queue

        
fd782a47   Etienne Pallier   refactorisations ...
665
    # Reset the database content
c72eb17a   Jeremy   Update celery task
666
667
    def reset_database_sim(self):
        self.changeDirectory("src")
54467c54   Etienne Pallier   refactorisation
668
        self.set_celery_test_and_simulator_modes_to(True, False)
cd0149f5   Etienne Pallier   progress made for...
669
670
671
672
673
674
        '''
        Supprime toutes les données de la base de données 
        et réexécute tout gestionnaire de post-synchronisation. 
        La table contenant les informations d’application des migrations n’est pas effacée
        '''
        #TODO: remplacer par manage.py --noinput flush pour eviter le "echo yes"...
f79c72e7   Unknown   quickfix --noinpu...
675
        self.execProcess( self.venv_bin + " manage.py flush  --noinput")
54467c54   Etienne Pallier   refactorisation
676
        self.set_celery_test_and_simulator_modes_to(True, False)
c72eb17a   Jeremy   Update celery task
677
678
        self.changeDirectory("..")

d1e6236c   Etienne Pallier   Refactorized pyro...
679

fd782a47   Etienne Pallier   refactorisations ...
680
681
682
    # Simulation for the scheduler ONLY (and only some celery workers, not all of them)
    def simulator_development(self):
        self.simulator(False)
d1e6236c   Etienne Pallier   Refactorized pyro...
683

05bdcc44   Etienne Pallier   BIG DEMO tests (s...
684
685
686
687
688
689
690
691
692
    '''
    (EP) utile pour le debug avec phpmyadmin pour remettre à zéro la bd pyros_test
    delete from plan;
    delete from album;
    delete from schedule_has_sequences;
    delete from schedule;
    delete from sequence;
    delete from request;
    '''
d1e6236c   Etienne Pallier   Refactorized pyro...
693

fd782a47   Etienne Pallier   refactorisations ...
694
695
    # Simulation (by default, with ALL simulators and ALL celery workers)
    def simulator(self, TOTAL=True):
4816e86b   Jeremy   removed *.sh *.ba...
696
        self.changeDirectory("src")
cd0149f5   Etienne Pallier   progress made for...
697
698

        # Set CELERY_TEST mode ON
cfc9d09c   Jeremy   Added dome simula...
699
        self.replacePatternInFile("CELERY_TEST = False", "CELERY_TEST = True", "pyros/settings.py")
ff448d43   Jeremy   Update
700
        self.replacePatternInFile("SIMULATOR = False", "SIMULATOR = True", "pyros/settings.py")
cd0149f5   Etienne Pallier   progress made for...
701

075082e1   Etienne Pallier   added start of ag...
702
        # 0) Reset the database (Empty the database from any data)
cd0149f5   Etienne Pallier   progress made for...
703
        #TODO: je crois qu'on n'utilise plus sqlite... donc ce code est inutile ?
2ce20c50   Quentin Durand   applying workshop...
704
705
        if self.system == "Windows" : self.execProcess("del /f testdb.sqlite3")
        else : self.execProcess("rm -f testdb.sqlite3")
4816e86b   Jeremy   removed *.sh *.ba...
706
        self.changeDirectory("..")
fd782a47   Etienne Pallier   refactorisations ...
707
        if TOTAL: self.reset_database_sim()
05bdcc44   Etienne Pallier   BIG DEMO tests (s...
708
709
        # EP added:
        if not TOTAL: self.reset_database_sim()
cd0149f5   Etienne Pallier   progress made for...
710
        # Actualise l’état de la DB en accord avec l’ensemble des modèles et des migrations actuels
03814884   Etienne Pallier   launch majordome ...
711
        self.makemigrations()
4816e86b   Jeremy   removed *.sh *.ba...
712
        self.migrate()
cd0149f5   Etienne Pallier   progress made for...
713
        # Load fixture initial_fixture.json
4816e86b   Jeremy   removed *.sh *.ba...
714
        self.loaddata()
d1e6236c   Etienne Pallier   Refactorized pyro...
715
716
717
718

        #
        # 1) Launch Django web server
        #
4816e86b   Jeremy   removed *.sh *.ba...
719
720
        self.server()
        self.sleep(2)
075082e1   Etienne Pallier   added start of ag...
721

4816e86b   Jeremy   removed *.sh *.ba...
722
723
        self.printFullTerm(Colors.WARNING, "SUMMARY")
        self.printColor(Colors.GREEN, "The simulator has been successfully initialised")
cd0149f5   Etienne Pallier   progress made for...
724
725
        #TODO: (EP) verifier mais je pense que cette info est obsolete, c'est MySQL qui est utilisé, pas sqlite
        #self.printColor(Colors.GREEN, "The simulator runs on a temp database : src/testdb.sqlite3")
4816e86b   Jeremy   removed *.sh *.ba...
726
727
        self.printColor(Colors.GREEN, "The simulation will be ended by the task 'simulator herself'")
        self.printColor(Colors.GREEN, "If you want to shutdown the simulation, please run :")
ed1bf194   Etienne Pallier   bugfix task major...
728
        self.printColor(Colors.GREEN, "CTRL-C or pyros.py kill_simulation")
4816e86b   Jeremy   removed *.sh *.ba...
729
        self.printColor(Colors.GREEN, "If the simulation isn't correctly killed, please switch the variable")
fd782a47   Etienne Pallier   refactorisations ...
730
        self.printColor(Colors.GREEN, "CELERY_TEST in src/pyros/settings.py to False")
4816e86b   Jeremy   removed *.sh *.ba...
731
        self.printFullTerm(Colors.WARNING, "SUMMARY")
d1e6236c   Etienne Pallier   Refactorized pyro...
732
733

        #
cd0149f5   Etienne Pallier   progress made for...
734
        # 2) (if USE_CELERY) Start Celery workers
d1e6236c   Etienne Pallier   Refactorized pyro...
735
        #
cd0149f5   Etienne Pallier   progress made for...
736
737
738
        if USE_CELERY:
            self.celery_on(TOTAL)
            self.sleep(3)
d1e6236c   Etienne Pallier   Refactorized pyro...
739
740
741
742
743
744
745

        #
        # 3) Start simulator(s) :
        #
        self.sims_launch(TOTAL)

        '''
fd782a47   Etienne Pallier   refactorisations ...
746
        # (DEFAULT NORMAL) TOTAL RUN
d1e6236c   Etienne Pallier   Refactorized pyro...
747
        # - Launch ALL simulators
fd782a47   Etienne Pallier   refactorisations ...
748
        if TOTAL:
fd782a47   Etienne Pallier   refactorisations ...
749
            # Launch ALL simulators and WAIT until finished:
d1e6236c   Etienne Pallier   Refactorized pyro...
750
751
752
            #self.sims_launch(conf)
            self.sims_launch(TOTAL)

fd782a47   Etienne Pallier   refactorisations ...
753
        # (DEV) PARTIAL RUN
d1e6236c   Etienne Pallier   Refactorized pyro...
754
        # - Launch only USER simulator
fd782a47   Etienne Pallier   refactorisations ...
755
        else:
d1e6236c   Etienne Pallier   Refactorized pyro...
756
757
758
759
760
761
762
763
764
765
            # EP: bugfix: inutile si TOTAL, car refait dans sims_launch !!!
            self.changeDirectory("simulators/config")
            self.printColor(Colors.BOLD, "Existing simulations : ", eol='')
            sys.stdout.flush()
            if (self.system == "Windows") : self.execProcessSilent("dir /B conf*.json")
            else: self.execProcessSilent("ls conf*.json")
            self.changeDirectory("..")
            conf = self.askQuestion("Which simulation do you want to use", default="conf.json")
            self.changeDirectory("..")

fd782a47   Etienne Pallier   refactorisations ...
766
767
768
769
770
771
772
773
774
            procs = []
            self.changeDirectory("simulators")
            self.changeDirectory("user")
            # Launch only 1 simulator : UserSimulator
            procs.append(self.execProcessFromVenvAsync(self.venv_bin + " userSimulator.py " + conf))
            self.changeDirectory("..")
            for p in procs:
                p.wait()
            self.changeDirectory("..")
d1e6236c   Etienne Pallier   Refactorized pyro...
775
776
        '''

075082e1   Etienne Pallier   added start of ag...
777
778
779
780
781
782
783
784
785
786
787
788
789
790
        '''
        #
        # 2) (if not USE_CELERY) Start Agents
        #
        if TOTAL and not USE_CELERY :
            #pass
            # Start Environment Monitoring Agent
            # Start Majordome Agent
            # Start Alert Manager Agent
            self.start_agents()
            # Needed ?
            self.sleep(3)
        '''

fd782a47   Etienne Pallier   refactorisations ...
791
        # When simulators are finished:
d1e6236c   Etienne Pallier   Refactorized pyro...
792
        #self.kill_simulation()
4816e86b   Jeremy   removed *.sh *.ba...
793
        return 0
fd782a47   Etienne Pallier   refactorisations ...
794
    
4816e86b   Jeremy   removed *.sh *.ba...
795

03814884   Etienne Pallier   launch majordome ...
796
797
798
799
800
801
802


    # Test of the Majordome agent
    def test_majordome(self):
        self.changeDirectory("src")

        # Set CELERY_TEST mode ON
54467c54   Etienne Pallier   refactorisation
803
        self.set_celery_test_and_simulator_modes_to(True)
03814884   Etienne Pallier   launch majordome ...
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854

        # 0) Reset the database (Empty the database from any data)
        self.changeDirectory("..")
        self.reset_database_sim()
        # Actualise l’état de la DB en accord avec l’ensemble des modèles et des migrations actuels
        self.migrate()
        # Load fixture initial_fixture.json
        self.loaddata()
        return

        #
        # 1) Launch Django web server
        #
        '''
        self.server()
        self.sleep(2)
        '''

        self.printFullTerm(Colors.WARNING, "SUMMARY")
        self.printColor(Colors.GREEN, "The simulation will be ended by the task 'simulator herself'")
        self.printColor(Colors.GREEN, "If you want to shutdown the simulation, please run :")
        self.printColor(Colors.GREEN, "CTRL-C or pyros.py kill_simulation")
        self.printColor(Colors.GREEN, "If the simulation isn't correctly killed, please switch the variable")
        self.printColor(Colors.GREEN, "CELERY_TEST in src/pyros/settings.py to False")
        self.printFullTerm(Colors.WARNING, "SUMMARY")

        # 2) (if USE_CELERY) Start Celery workers
        #
        '''
        if USE_CELERY:
            self.celery_on(TOTAL)
            self.sleep(3)
        '''

        #
        # 3) Start simulator(s) :
        #
        #self.sims_launch(True)
        agent='majordome'
        self.changeDirectory(agent)
        p = self.execProcessFromVenvAsync(self.venv_bin + ' start_agent_'+agent+'.py')
        os.chdir('..')
        time.sleep(5)


        # Wait for end of simulators :
        #p.wait()

        # When simulators are finished:
        #self.kill_simulation()
        #self.changeDirectory("src")
54467c54   Etienne Pallier   refactorisation
855
        self.set_celery_test_and_simulator_modes_to(False)
03814884   Etienne Pallier   launch majordome ...
856
857
858
859
860
861
862
863
864
865
866
        self.changeDirectory("..")
        self.execProcessAsync("ps aux | grep \"start_agent_majordome.py\" | awk '{ print $2 }' | xargs kill")

        return 0







3b0ef1c7   Jeremy   Beautify help mes...
867
868
869
870
    def kill_server(self):
        self.execProcessAsync("fuser -k 8000/tcp")
        return 0

4816e86b   Jeremy   removed *.sh *.ba...
871
    def kill_simulation(self):
9f3db1a3   Quentin Durand   plc_mode in progr...
872
        self.changeDirectory("src")
54467c54   Etienne Pallier   refactorisation
873
        self.set_celery_test_and_simulator_modes_to(False)
9f3db1a3   Quentin Durand   plc_mode in progr...
874
        self.changeDirectory("..")
27e81f38   jeremy   Update windows
875
876
        if (self.system == "Windows"):
            self.execProcessAsync("taskkill /f /im python.exe")
58f35996   Unknown   Fixing process bu...
877
878
            #self.execProcessAsync("del /f testdb.sqlite3") obsolete
            #self.changeDirectory("..") POURQUOI ?
27e81f38   jeremy   Update windows
879
            return 0
58f35996   Unknown   Fixing process bu...
880
        #else: self.execProcessAsync("rm -f testdb.sqlite3")
075082e1   Etienne Pallier   added start of ag...
881

fd782a47   Etienne Pallier   refactorisations ...
882
883
        # Kill web server (processes using TCP port 8000) :
        #self.execProcessAsync("fuser -k 8000/tcp")
9f3db1a3   Quentin Durand   plc_mode in progr...
884

fd782a47   Etienne Pallier   refactorisations ...
885
        self.kill_server()
075082e1   Etienne Pallier   added start of ag...
886
887
888
889
890
891

        # (if not using celery) Kill all agents :
        if not USE_CELERY:
            self.execProcessAsync("ps aux | grep \"start_agent_alert_manager.py\" | awk '{ print $2 }' | xargs kill")
            self.execProcessAsync("ps aux | grep \"start_agent_monitoring.py\" | awk '{ print $2 }' | xargs kill")
            self.execProcessAsync("ps aux | grep \"start_agent_majordome.py\" | awk '{ print $2 }' | xargs kill")
58f35996   Unknown   Fixing process bu...
892
        #self.execProcessAsync("ps aux | grep \"start_agent_majordome.py\" | awk '{ print $2 }' | xargs kill")
fd782a47   Etienne Pallier   refactorisations ...
893
        # Kill all simulators :
cfc9d09c   Jeremy   Added dome simula...
894
        self.execProcessAsync("ps aux | grep \" domeSimulator.py\" | awk '{ print $2 }' | xargs kill")
4816e86b   Jeremy   removed *.sh *.ba...
895
896
897
898
899
900
901
        self.execProcessAsync("ps aux | grep \" userSimulator.py\" | awk '{ print $2 }' | xargs kill")
        self.execProcessAsync("ps aux | grep \" alertSimulator.py\" | awk '{ print $2 }' | xargs kill")
        self.execProcessAsync("ps aux | grep \" plcSimulator.py\" | awk '{ print $2 }' | xargs kill")
        self.execProcessAsync("ps aux | grep \" telescopeSimulator.py\" | awk '{ print $2 }' | xargs kill")
        self.execProcessAsync("ps aux | grep \" cameraNIRSimulator.py\" | awk '{ print $2 }' | xargs kill")
        self.execProcessAsync("ps aux | grep \" cameraVISSimulator.py\" | awk '{ print $2 }' | xargs kill")
        self.changeDirectory("..")
03814884   Etienne Pallier   launch majordome ...
902
        if USE_CELERY: self.stop_workers()
58f35996   Unknown   Fixing process bu...
903

4816e86b   Jeremy   removed *.sh *.ba...
904
905
906
        self.printFullTerm(Colors.GREEN, "simulation ended")
        return 0

d1e6236c   Etienne Pallier   Refactorized pyro...
907

f3534c59   Etienne Pallier   Simulators web pa...
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950

    # EP 25/7/18
    # Start one, some, or all agents and simulators
    
    def start_agents_and_simulators_for_majordome(self):
        self.start_agents_and_simulators("majordome")
        
    def start_agents_and_simulators(self, agent:str=None):
        
        simulators = ('user', 'plc', 'dome', 'alert', 'cameraVIS', 'cameraNIR', 'telescope')
        #simulators = ('plc', 'user')
        #simulators = ()

        agents = ("alert_manager", "majordome", "monitoring")

        if agent=="majordome": 
            simulators = ("plc",)
            agents = (agent, "monitoring")

        # Start simulators
        p_sims = []
        self.changeDirectory("simulators/")
        for simulator in simulators:
            print()
            print("Start simulator", ' '+simulator+"Simulator.py ")
            self.changeDirectory(simulator)
            #p_sims.append( self.execProcessFromVenvAsync(self.venv_bin + ' '+simulator+"Simulator.py " + conf) )
            p_sims.append( self.execProcessFromVenvAsync(self.venv_bin + ' '+simulator+"Simulator.py ") )
            self.changeDirectory("../")

        # Start agents
        #self.start_agents()
        p_agents = []
        self.changeDirectory("../src/")
        for agent in agents:
            print()
            print("Start agent", 'start_agent_'+agent+'.py')
            self.changeDirectory(agent)
            p_agents.append( self.execProcessFromVenvAsync(self.venv_bin + ' start_agent_'+agent+'.py') )
            self.changeDirectory('..')



fd782a47   Etienne Pallier   refactorisations ...
951
    # Simulators only
a64f430a   Unknown   Adding argument t...
952
953
    def sims_launch(self, TOTAL=True):
        conf = SIMULATOR_CONFIG_FILE
d1e6236c   Etienne Pallier   Refactorized pyro...
954
        # Read simulators scenario file
4816e86b   Jeremy   removed *.sh *.ba...
955
        self.changeDirectory("simulators/config")
a64f430a   Unknown   Adding argument t...
956
957
958
959
960
961
962
963
        # if (conf == ""):
        #     self.printColor(Colors.BOLD, "Existing simulations : ", eol='')
        #     sys.stdout.flush()
        #     if (self.system == "Windows"):
        #         self.execProcessSilent("dir /B conf*.json")
        #     else:
        #         self.execProcessSilent("ls conf*.json")
        #     conf = self.askQuestion("Which simulation do you want to use ?", default="conf.json")
4816e86b   Jeremy   removed *.sh *.ba...
964
965
966
        if not os.path.isfile(conf):
            self.printColor(Colors.FAIL, "The simulation file " + conf + " does not exist")
            return 1
075082e1   Etienne Pallier   added start of ag...
967
968
        # Go back to simulators/
        self.changeDirectory("..")
d1e6236c   Etienne Pallier   Refactorized pyro...
969
970
971
972

        procs = []

        # Launch the User simulator:
4816e86b   Jeremy   removed *.sh *.ba...
973
        self.changeDirectory("user")
bca9a283   Jeremy   Reworked the sche...
974
        procs.append(self.execProcessFromVenvAsync(self.venv_bin + " userSimulator.py " + conf))
075082e1   Etienne Pallier   added start of ag...
975
        self.changeDirectory("..")
d1e6236c   Etienne Pallier   Refactorized pyro...
976
977
978
979

        # (if TOTAL) Launch all other simulators
        if TOTAL:

d1e6236c   Etienne Pallier   Refactorized pyro...
980
981
            self.changeDirectory("dome")
            procs.append(self.execProcessFromVenvAsync(self.venv_bin + " domeSimulator.py " + conf))
d1e6236c   Etienne Pallier   Refactorized pyro...
982
            self.changeDirectory("..")
075082e1   Etienne Pallier   added start of ag...
983

d1e6236c   Etienne Pallier   Refactorized pyro...
984
985
            self.changeDirectory("alert")
            procs.append(self.execProcessFromVenvAsync(self.venv_bin + " alertSimulator.py " + conf))
d1e6236c   Etienne Pallier   Refactorized pyro...
986
            self.changeDirectory("..")
075082e1   Etienne Pallier   added start of ag...
987

d1e6236c   Etienne Pallier   Refactorized pyro...
988
989
            self.changeDirectory("plc")
            procs.append(self.execProcessFromVenvAsync(self.venv_bin + " plcSimulator.py " + conf))
d1e6236c   Etienne Pallier   Refactorized pyro...
990
            self.changeDirectory("..")
075082e1   Etienne Pallier   added start of ag...
991

d1e6236c   Etienne Pallier   Refactorized pyro...
992
993
994
            self.changeDirectory("camera")
            procs.append(self.execProcessFromVenvAsync(self.venv_bin + " cameraVISSimulator.py " + conf))
            procs.append(self.execProcessFromVenvAsync(self.venv_bin + " cameraNIRSimulator.py " + conf))
d1e6236c   Etienne Pallier   Refactorized pyro...
995
            self.changeDirectory("..")
075082e1   Etienne Pallier   added start of ag...
996

d1e6236c   Etienne Pallier   Refactorized pyro...
997
998
            self.changeDirectory("telescope")
            procs.append(self.execProcessFromVenvAsync(self.venv_bin + " telescopeSimulator.py " + conf))
075082e1   Etienne Pallier   added start of ag...
999
            self.changeDirectory("..")
d1e6236c   Etienne Pallier   Refactorized pyro...
1000

075082e1   Etienne Pallier   added start of ag...
1001
        # Get back to project root folder
4816e86b   Jeremy   removed *.sh *.ba...
1002
        self.changeDirectory("..")
075082e1   Etienne Pallier   added start of ag...
1003
1004

        # (if not celery) Launch agents (env monitor, major, alert mgr)
f3534c59   Etienne Pallier   Simulators web pa...
1005
        if not USE_CELERY: self.start_agents()
075082e1   Etienne Pallier   added start of ag...
1006
1007

        # Wait for end of simulators :
f3534c59   Etienne Pallier   Simulators web pa...
1008
        for p in procs: p.wait()
075082e1   Etienne Pallier   added start of ag...
1009
1010

        # Kill all processes
d1e6236c   Etienne Pallier   Refactorized pyro...
1011
        self.kill_simulation()
4816e86b   Jeremy   removed *.sh *.ba...
1012
1013
        return 0

d1e6236c   Etienne Pallier   Refactorized pyro...
1014

4816e86b   Jeremy   removed *.sh *.ba...
1015
1016
1017
1018
1019
1020
    def singleWorker(self, worker):
        self.changeDirectory("src")
        self.execProcessFromVenvAsync(self.venv_cel + " worker -A pyros -Q "+ worker +"_q -n pyros@"+worker+" -c 1")
        self.changeDirectory("..")
        return 0

c53a13e0   Jeremy   Updating a lot of...
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
    def mysql_on(self):
        self.changeDirectory("src")
        self.replacePatternInFile("MYSQL = False", "MYSQL = True", "pyros/settings.py")
        self.addExecuted(self.current_command, "Switch to mysql")
        self.changeDirectory("..")
        return 0

    def mysql_off(self):
        self.changeDirectory("src")
        self.replacePatternInFile("MYSQL = True", "MYSQL = False", "pyros/settings.py")
        self.addExecuted(self.current_command, "Switch to sqlite")
        self.changeDirectory("..")
        return 0

4816e86b   Jeremy   removed *.sh *.ba...
1035
1036
1037
1038
1039
1040
1041
    def __init__(self, argv):
        super(Pyros, self).__init__(argv)
        self.commandMatcher = {
            "install": self.install,
            "update": self.update,
            "server": self.server,
            "clean": self.clean,
ff448d43   Jeremy   Update
1042
            "simulator_development": self.simulator_development,
4816e86b   Jeremy   removed *.sh *.ba...
1043
1044
1045
            "clean_logs": self.clean_logs,
            "test": self.test,
            "migrate": self.migrate,
c53a13e0   Jeremy   Updating a lot of...
1046
1047
            "mysql_on": self.mysql_on,
            "mysql_off": self.mysql_off,
4816e86b   Jeremy   removed *.sh *.ba...
1048
1049
1050
            "makemigrations": self.makemigrations,
            "updatedb": self.updatedb,
            "unittest": self.unittest,
c53a13e0   Jeremy   Updating a lot of...
1051
            "reset_config": self.reset_config,
4816e86b   Jeremy   removed *.sh *.ba...
1052
1053
            "test_all": self.test_all,
            "celery_on": self.celery_on,
c72eb17a   Jeremy   Update celery task
1054
            "reset_database_sim": self.reset_database_sim,
3b0ef1c7   Jeremy   Beautify help mes...
1055
1056
            "init_database": self.init_database,
            "kill_server": self.kill_server,
4816e86b   Jeremy   removed *.sh *.ba...
1057
            "loaddata": self.loaddata,
b4e8963c   Etienne Pallier   Each agent (envmo...
1058
1059
1060
            "start": self.start,
            "start_web": self.start_web,
            "start_agents": self.start_agents,
f3534c59   Etienne Pallier   Simulators web pa...
1061
1062
            "start_agents_and_simulators": self.start_agents_and_simulators,
            "start_agents_and_simulators_for_majordome": self.start_agents_and_simulators_for_majordome,
40227fbd   Etienne Pallier   changed monitorin...
1063
            "start_agent_monitoring": self.start_agent_monitoring,
b4e8963c   Etienne Pallier   Each agent (envmo...
1064
1065
            "start_agent_majordome": self.start_agent_majordome,
            "start_agent_alertmanager": self.start_agent_alertmanager,
03814884   Etienne Pallier   launch majordome ...
1066
            "test_majordome": self.test_majordome,
b4e8963c   Etienne Pallier   Each agent (envmo...
1067
1068
            
            # ex start
fd782a47   Etienne Pallier   refactorisations ...
1069
            "start_workers": self.start_workers,
b4e8963c   Etienne Pallier   Each agent (envmo...
1070
            # ex stop
fd782a47   Etienne Pallier   refactorisations ...
1071
            "stop_workers": self.stop_workers,
4816e86b   Jeremy   removed *.sh *.ba...
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
            "simulator": self.simulator,
            "kill_simulation": self.kill_simulation,
            "sims_launch": self.sims_launch,
            "help": self.help,
        }
        self.commandDescription = {
            "install": "Launch the server installation",
            "update": "Update the server",
            "server": "Launch the web server",
            "loaddata": "Load the initial fixture in database",
            "clean": "clean the repository",
            "clean_logs": "clean the log directory",
            "test": "launch the server tests",
            "migrate": "execute migrations",
c53a13e0   Jeremy   Updating a lot of...
1086
1087
            "mysql_on": "switch the database to be used to MYSQL",
            "mysql_off": "switch the database to be used usage to SQLITE",
4816e86b   Jeremy   removed *.sh *.ba...
1088
            "makemigrations": "create new migrations",
c53a13e0   Jeremy   Updating a lot of...
1089
            "reset_config": "Reset the configuration in settings.py",
c72eb17a   Jeremy   Update celery task
1090
            "reset_database_sim": "Reset the database content",
4816e86b   Jeremy   removed *.sh *.ba...
1091
1092
            "help": "Help message",
            "updatedb": "Update the database",
3b0ef1c7   Jeremy   Beautify help mes...
1093
1094
            "kill_server": "Kill the web server on port 8000",
            "init_database": "Create a standard context for pyros in db",
2e0c8f94   Unknown   New install scrip...
1095
            "unittest": "Runs the tests that don't need celery",
4816e86b   Jeremy   removed *.sh *.ba...
1096
1097
            "test_all": "Run all the existing tests (this command needs to be updated when tests are added in the project",
            "celery_on": "Starts celery workers",
b4e8963c   Etienne Pallier   Each agent (envmo...
1098
1099
            "start_workers": "Starts the celery workers (then the web server ???)",
            "stop_workers": "Stops the celery workers",
4816e86b   Jeremy   removed *.sh *.ba...
1100
            "simulator": "Launch a simulation",
ff448d43   Jeremy   Update
1101
            "simulator_development": "Simulation for the scheduler only",
4816e86b   Jeremy   removed *.sh *.ba...
1102
1103
1104
1105
1106
            "kill_simulation": "kill the simulators / celery workers / web server",
            "sims_launch": "Launch only the simulators",
        }

if __name__ == "__main__":
a64f430a   Unknown   Adding argument t...
1107
1108
    if len(sys.argv) == 3 and sys.argv[1].startswith("simulator"):
        SIMULATOR_CONFIG_FILE = sys.argv[2]
4816e86b   Jeremy   removed *.sh *.ba...
1109
    conf = Config()
b4e8963c   Etienne Pallier   Each agent (envmo...
1110
    if not conf.parseConf(): sys.exit(1)
4816e86b   Jeremy   removed *.sh *.ba...
1111
    pyros = Pyros(conf)
2ce20c50   Quentin Durand   applying workshop...
1112
    sys.exit(pyros.exec())