Commit 4816e86b4ccffc646d41d1776a06c4b1be08a5a1
1 parent
124965dc
Exists in
master
and in
1 other branch
removed *.sh *.bat now the manager is pyros.py
Showing
18 changed files
with
609 additions
and
25 deletions
Show diff stats
install/install.py
... | ... | @@ -146,22 +146,30 @@ class AInstaller(Utils): |
146 | 146 | def setConfig(self, config): |
147 | 147 | self.config = config |
148 | 148 | |
149 | + def addExecuted(self, src, message): | |
150 | + if (src in self.executed): | |
151 | + self.executed[src].append(str(message)) | |
152 | + else: | |
153 | + self.executed[src] = [str(message)] | |
154 | + return 0 | |
155 | + | |
156 | + def addError(self, src, message): | |
157 | + if (src in self.errors): | |
158 | + self.errors[src].append(str(message)) | |
159 | + else: | |
160 | + self.errors[src] = [str(message)] | |
161 | + return 0 | |
162 | + | |
149 | 163 | def execProcess(self, command): |
150 | 164 | self.printFullTerm(Colors.BLUE, "Executing command [" + command + "]") |
151 | 165 | process = subprocess.Popen(command, shell=True) |
152 | 166 | process.wait() |
153 | 167 | if process.returncode == 0: |
154 | 168 | self.printFullTerm(Colors.GREEN, "Process executed successfully") |
155 | - if (self.current_command in self.executed): | |
156 | - self.executed[self.current_command].append(command) | |
157 | - else: | |
158 | - self.executed[self.current_command] = [command] | |
169 | + self.addExecuted(self.current_command, command) | |
159 | 170 | else: |
160 | 171 | self.printFullTerm(Colors.WARNING, "Process execution failed") |
161 | - if (self.current_command in self.errors): | |
162 | - self.errors[self.current_command].append(command) | |
163 | - else: | |
164 | - self.errors[self.current_command] = [command] | |
172 | + self.addError(self.current_command, command) | |
165 | 173 | return process.returncode |
166 | 174 | |
167 | 175 | def execProcessFromVenv(self, command): |
... | ... | @@ -171,16 +179,10 @@ class AInstaller(Utils): |
171 | 179 | process.wait() |
172 | 180 | if process.returncode == 0: |
173 | 181 | self.printFullTerm(Colors.GREEN, "Process executed successfully") |
174 | - if (self.current_command in self.executed): | |
175 | - self.executed[self.current_command].append(str(' '.join(args[1:]))) | |
176 | - else: | |
177 | - self.executed[self.current_command] = [str(' '.join(args[1:]))] | |
182 | + self.addExecuted(self.current_command, str(' '.join(args[1:]))) | |
178 | 183 | else: |
179 | 184 | self.printFullTerm(Colors.WARNING, "Process execution failed") |
180 | - if (self.current_command in self.errors): | |
181 | - self.errors[self.current_command].append(str(' '.join(args[1:]))) | |
182 | - else: | |
183 | - self.errors[self.current_command] = [str(' '.join(args[1:]))] | |
185 | + self.addError(self.current_command, str(' '.join(args[1:]))) | |
184 | 186 | return process.returncode |
185 | 187 | |
186 | 188 | def askYesNoQuestion(self, message, question_tag, default=True): |
... | ... | @@ -389,7 +391,7 @@ class Config: |
389 | 391 | res = self.__parser.parse_args(), self.__parser.format_usage() |
390 | 392 | return (res) |
391 | 393 | except SystemExit as e: |
392 | - print(e, file=sys.stderr) | |
394 | + # print(e, file=sys.stderr) | |
393 | 395 | sys.exit(1) |
394 | 396 | |
395 | 397 | def parseConf(self): | ... | ... |
pyrosrun.bat renamed to obsolete/pyrosrun.bat
pyrosrun.sh renamed to obsolete/pyrosrun.sh
src/scripts/celery_test.sh renamed to obsolete/scripts/celery_test.sh
src/scripts/delete_all_requests.sh renamed to obsolete/scripts/delete_all_requests.sh
src/scripts/kill_celery_workers.bat renamed to obsolete/scripts/kill_celery_workers.bat
src/scripts/kill_celery_workers.sh renamed to obsolete/scripts/kill_celery_workers.sh
src/scripts/kill_simulation.sh renamed to obsolete/scripts/kill_simulation.sh
src/scripts/start_celery_single_worker.sh renamed to obsolete/scripts/start_celery_single_worker.sh
src/scripts/start_celery_workers.bat renamed to obsolete/scripts/start_celery_workers.bat
src/scripts/start_celery_workers.sh renamed to obsolete/scripts/start_celery_workers.sh
src/scripts/test_all_usual.sh renamed to obsolete/scripts/test_all_usual.sh
simulators/scripts/kill_all.sh renamed to obsolete/sim_scripts/kill_all.sh
simulators/scripts/run_all.sh renamed to obsolete/sim_scripts/run_all.sh
simulators/scripts/simulator_launch.sh renamed to obsolete/sim_scripts/simulator_launch.sh
simulators/scripts/start_simulator_worker.sh renamed to obsolete/sim_scripts/start_simulator_worker.sh
... | ... | @@ -0,0 +1,576 @@ |
1 | +import sys | |
2 | +import os | |
3 | +import subprocess | |
4 | +import platform | |
5 | +import fileinput | |
6 | +import argparse | |
7 | +import time | |
8 | + | |
9 | +DEBUG = False | |
10 | + | |
11 | + | |
12 | +class Utils: | |
13 | + system = platform.system() | |
14 | + columns = 100 | |
15 | + row = 1000 | |
16 | + disp = True | |
17 | + | |
18 | + def __init__(self): | |
19 | + if (platform.system() != 'Windows'): | |
20 | + try: | |
21 | + rows, columns = os.popen('stty size', 'r').read().split() | |
22 | + self.columns = int(columns) | |
23 | + except: | |
24 | + self.columns = 100 | |
25 | + if DEBUG: | |
26 | + print("Could not get terminal size") | |
27 | + | |
28 | + def printFullTerm(self, color, string): | |
29 | + value = int(self.columns / 2 - len(string) / 2) | |
30 | + self.printColor(color, "-" * value, eol='') | |
31 | + self.printColor(color, string, eol='') | |
32 | + value += len(string) | |
33 | + self.printColor(color, "-" * (self.columns - value)) | |
34 | + return 0 | |
35 | + | |
36 | + def changeDirectory(self, path): | |
37 | + if DEBUG: | |
38 | + print("Moving to : " + path) | |
39 | + os.chdir(path) | |
40 | + if DEBUG: | |
41 | + print("Current directory : " + str(os.getcwd())) | |
42 | + return 0 | |
43 | + | |
44 | + def replacePatternInFile(self, pattern, replace, file_path): | |
45 | + try: | |
46 | + with fileinput.FileInput(file_path, inplace=True, backup='.bak') as file: | |
47 | + for line in file: | |
48 | + print(line.replace(pattern, replace), end='') | |
49 | + except: | |
50 | + return 1 | |
51 | + return 0 | |
52 | + | |
53 | + def printColor(self, color, message, file=sys.stdout, eol=os.linesep, forced=False): | |
54 | + if (self.disp == False and forced == False): | |
55 | + return 0 | |
56 | + if (self.system == 'Windows'): | |
57 | + print(message, file=file, end=eol) | |
58 | + else: | |
59 | + print(color + message + Colors.ENDC, file=file, end=eol) | |
60 | + return 0 | |
61 | + | |
62 | + def askQuestion(self, message, default = ""): | |
63 | + self.printColor(Colors.BLUE, message, forced=True) | |
64 | + self.printColor(Colors.BOLD, "Answer (default="+default+"): ", eol='', forced=True) | |
65 | + sys.stdout.flush() | |
66 | + ret = sys.stdin.readline().replace(os.linesep, '') | |
67 | + if ret == "": | |
68 | + return default | |
69 | + return ret | |
70 | + | |
71 | + def sleep(self, t): | |
72 | + time.sleep(t) | |
73 | + return 0 | |
74 | + | |
75 | +''' | |
76 | + Manager class : manager of your project | |
77 | +''' | |
78 | + | |
79 | + | |
80 | +class AManager(Utils): | |
81 | + path = os.path.realpath(__file__) | |
82 | + path_dir = os.getcwd() | |
83 | + path_dir_file = os.path.dirname(os.path.realpath(__file__)) | |
84 | + python_path = sys.executable | |
85 | + python_version = sys.version_info | |
86 | + | |
87 | + bin_dir = "" | |
88 | + celery = "celery" | |
89 | + venv_pip = "pip" | |
90 | + venv_bin = "python" | |
91 | + wait = True | |
92 | + current_command = "" | |
93 | + config = None | |
94 | + commandMatcher = {} | |
95 | + commandDescription = {} | |
96 | + commands = [] | |
97 | + errors = {} | |
98 | + executed = {} | |
99 | + | |
100 | + def __init__(self, param): | |
101 | + super(AManager, self).__init__() | |
102 | + self.wait = param.getWait() | |
103 | + self.commands = param.getCommandList() | |
104 | + self.disp = param.getPrint() | |
105 | + config = param.getConfig() | |
106 | + self.config = config | |
107 | + | |
108 | + self.changeDirectory(self.path_dir_file) | |
109 | + | |
110 | + if self.system == 'Windows': | |
111 | + self.bin_dir = "Scripts" | |
112 | + self.bin_name = "python.exe" | |
113 | + self.pip_name = "pip.exe" | |
114 | + self.celery = "celery.exe" | |
115 | + else: | |
116 | + self.bin_dir = "bin" | |
117 | + self.bin_name = "python" | |
118 | + self.pip_name = "pip" | |
119 | + self.celery = "celery" | |
120 | + 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 | |
121 | + 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 | |
122 | + self.venv_cel = self.path_dir_file + os.sep + config["path"] + os.sep + config["env"] + os.sep + self.bin_dir + os.sep + self.celery | |
123 | + | |
124 | + def help(self): | |
125 | + print("This function must be implemented") | |
126 | + raise(NotImplementedError("Function not implemented")) | |
127 | + | |
128 | + def addExecuted(self, src, message): | |
129 | + if (src in self.executed): | |
130 | + self.executed[src].append(str(message)) | |
131 | + else: | |
132 | + self.executed[src] = [str(message)] | |
133 | + return 0 | |
134 | + | |
135 | + def addError(self, src, message): | |
136 | + if (src in self.errors): | |
137 | + self.errors[src].append(str(message)) | |
138 | + else: | |
139 | + self.errors[src] = [str(message)] | |
140 | + return 0 | |
141 | + | |
142 | + def execProcess(self, command): | |
143 | + self.printFullTerm(Colors.BLUE, "Executing command [" + command + "]") | |
144 | + process = subprocess.Popen(command, shell=True) | |
145 | + process.wait() | |
146 | + if process.returncode == 0: | |
147 | + self.printFullTerm(Colors.GREEN, "Process executed successfully") | |
148 | + self.addExecuted(self.current_command, command) | |
149 | + else: | |
150 | + self.printFullTerm(Colors.WARNING, "Process execution failed") | |
151 | + self.addError(self.current_command, command) | |
152 | + return process.returncode | |
153 | + | |
154 | + def execProcessFromVenv(self, command): | |
155 | + args = command.split() | |
156 | + self.printFullTerm(Colors.BLUE, "Executing command from venv [" + str(' '.join(args[1:])) + "]") | |
157 | + process = subprocess.Popen(args) | |
158 | + process.wait() | |
159 | + if process.returncode == 0: | |
160 | + self.printFullTerm(Colors.GREEN, "Process executed successfully") | |
161 | + self.addExecuted(self.current_command, str(' '.join(args[1:]))) | |
162 | + else: | |
163 | + self.printFullTerm(Colors.WARNING, "Process execution failed") | |
164 | + self.addError(self.current_command, str(' '.join(args[1:]))) | |
165 | + return process.returncode | |
166 | + | |
167 | + def execProcessAsync(self, command): | |
168 | + self.printFullTerm(Colors.BLUE, "Executing command [" + command + "]") | |
169 | + subprocess.Popen(command, shell=True) | |
170 | + self.printFullTerm(Colors.GREEN, "Process launched successfully") | |
171 | + self.addExecuted(self.current_command, command) | |
172 | + return 0 | |
173 | + | |
174 | + def execProcessFromVenvAsync(self, command: str) -> int: | |
175 | + args = command.split() | |
176 | + self.printFullTerm(Colors.BLUE, "Executing command from venv [" + str(' '.join(args[1:])) + "]") | |
177 | + subprocess.Popen(args) | |
178 | + self.printFullTerm(Colors.GREEN, "Process launched successfully") | |
179 | + self.addExecuted(self.current_command, str(' '.join(args[1:]))) | |
180 | + return 0 | |
181 | + | |
182 | + def end(self): | |
183 | + count = 0 | |
184 | + | |
185 | + self.printFullTerm(Colors.WARNING, "Summary") | |
186 | + self.printColor(Colors.GREEN, "Success : ") | |
187 | + for command, valid in self.executed.items(): | |
188 | + if not valid: | |
189 | + self.printColor(Colors.BLUE, "\t- Command : " + command + " success !") | |
190 | + else: | |
191 | + self.printColor(Colors.WARNING, "\t- In commmand : " + command) | |
192 | + for exe in valid: | |
193 | + self.printColor(Colors.GREEN, "\t\t - Command : " + exe + " success !") | |
194 | + self.printColor(Colors.FAIL, "Errors : ") | |
195 | + if not self.errors: | |
196 | + self.printColor(Colors.GREEN, "\tNone") | |
197 | + for command, items in self.errors.items(): | |
198 | + count += 1 | |
199 | + if (not items): | |
200 | + self.printColor(Colors.FAIL, "Command : " + command + " failed !") | |
201 | + else: | |
202 | + self.printColor(Colors.WARNING, "\t- In commmand : " + command) | |
203 | + for exe in items: | |
204 | + self.printColor(Colors.FAIL, "\t\t - Command : " + exe) | |
205 | + return count | |
206 | + | |
207 | + def exec(self): | |
208 | + if (not self.commands): | |
209 | + self.commandMatcher["help"]() | |
210 | + return 0 | |
211 | + for command in self.commands: | |
212 | + self.current_command = command | |
213 | + if command in self.commandMatcher: | |
214 | + self.commandMatcher[command]() | |
215 | + else: | |
216 | + self.addError(str(command), "invalid command") | |
217 | + return self.end() | |
218 | + | |
219 | + def logError(self, message): | |
220 | + self.printColor(Colors.FAIL, "Pyros : An error occurred [" + message + "]", file=sys.stderr) | |
221 | + return 0 | |
222 | + | |
223 | + | |
224 | +class Config: | |
225 | + __parser = argparse.ArgumentParser("Installer parser") | |
226 | + __content = { | |
227 | + "path": "private", | |
228 | + "env": "venv_py3_pyros" | |
229 | + } | |
230 | + __wait = True | |
231 | + __print = True | |
232 | + usage = "" | |
233 | + __command_list = [] | |
234 | + | |
235 | + def __init__(self): | |
236 | + self.__parser.add_argument("command", nargs='?', default="help", help="The command you want to execute (default=help)") | |
237 | + self.__parser.add_argument("--env", help="Your environment directory name default=venv") | |
238 | + self.__parser.add_argument("--path", help="Path to the virtual env (from the source file directory) (default=private)") | |
239 | + self.__parser.add_argument("--nowait", action='store_true', help="Don't wait the end of a program") | |
240 | + self.__parser.add_argument("--noprint", action='store_true', help="Won't print") | |
241 | + self.usage = self.__parser.format_usage() | |
242 | + | |
243 | + def parse(self): | |
244 | + try: | |
245 | + res = self.__parser.parse_args(), self.__parser.format_usage() | |
246 | + return (res) | |
247 | + except SystemExit as e: | |
248 | + # print(e, file=sys.stderr) | |
249 | + sys.exit(1) | |
250 | + | |
251 | + def parseConf(self): | |
252 | + res, usage = self.parse() | |
253 | + try: | |
254 | + if (res.env): | |
255 | + self.__content["env"] = res.env | |
256 | + if (res.path): | |
257 | + self.__content["path"] = res.path | |
258 | + if (res.nowait): | |
259 | + self.__wait = False | |
260 | + if (res.noprint): | |
261 | + self.__print = False | |
262 | + self.__command_list.append(res.command) | |
263 | + return 0 | |
264 | + except Exception as e: | |
265 | + print(e, file=sys.stderr) | |
266 | + return 1 | |
267 | + | |
268 | + def getPath(self): | |
269 | + return self.__content["path"] | |
270 | + | |
271 | + def getEnv(self): | |
272 | + return self.__content["env"] | |
273 | + | |
274 | + def getWait(self): | |
275 | + return self.__wait | |
276 | + | |
277 | + def getPrint(self): | |
278 | + return self.__print | |
279 | + | |
280 | + def setPrint(self, value): | |
281 | + self.__print = value | |
282 | + return 0 | |
283 | + | |
284 | + def setWait(self, value): | |
285 | + self.__wait = value | |
286 | + return 0 | |
287 | + | |
288 | + def getCommandList(self): | |
289 | + return self.__command_list | |
290 | + | |
291 | + def printUsage(self): | |
292 | + print(self.usage, file=sys.stderr) | |
293 | + | |
294 | + def addConf(self, key, value): | |
295 | + if isinstance(key, str) and isinstance(value, str): | |
296 | + self.__content[key] = value | |
297 | + return 0 | |
298 | + return 1 | |
299 | + | |
300 | + def setPath(self, path): | |
301 | + if (os.path.isdir(path)): | |
302 | + if (path == ""): | |
303 | + path = "." | |
304 | + self.__content["path"] = path | |
305 | + return 0 | |
306 | + return 1 | |
307 | + | |
308 | + def setEnvName(self, name): | |
309 | + self.__content["env"] = name | |
310 | + return 0 | |
311 | + | |
312 | + def getConfig(self): | |
313 | + return self.__content | |
314 | + | |
315 | + | |
316 | +''' | |
317 | + Color class | |
318 | +''' | |
319 | + | |
320 | + | |
321 | +class Colors: | |
322 | + HEADER = '\033[95m' | |
323 | + BLUE = '\033[94m' | |
324 | + GREEN = '\033[92m' | |
325 | + WARNING = '\033[93m' | |
326 | + FAIL = '\033[91m' | |
327 | + ENDC = '\033[0m' | |
328 | + BOLD = '\033[1m' | |
329 | + UNDERLINE = '\033[4m' | |
330 | + | |
331 | + | |
332 | +''' | |
333 | + Pyros class | |
334 | +''' | |
335 | + | |
336 | + | |
337 | +class Pyros(AManager): | |
338 | + help_message = "python neo.py" | |
339 | + init_fixture = "initial_fixture.json" | |
340 | + | |
341 | + def install(self): | |
342 | + if (self.system == "Windows"): | |
343 | + self.execProcess("python install/install.py install") | |
344 | + else: | |
345 | + self.execProcess("python3 install/install.py install") | |
346 | + return 0 | |
347 | + | |
348 | + def update(self): | |
349 | + if (self.system == "Windows"): | |
350 | + self.execProcess("python install/install.py update") | |
351 | + else: | |
352 | + self.execProcess("python3 install/install.py update") | |
353 | + return 0 | |
354 | + | |
355 | + def server(self): | |
356 | + self.changeDirectory("src") | |
357 | + self.execProcessFromVenvAsync(self.venv_bin + " manage.py runserver") | |
358 | + self.changeDirectory("..") | |
359 | + return 0 | |
360 | + | |
361 | + def clean(self): | |
362 | + return self.clean_logs() | |
363 | + | |
364 | + def clean_logs(self): | |
365 | + return self.execProcess("rm logs/*.log") | |
366 | + | |
367 | + def test(self): | |
368 | + self.changeDirectory("src") | |
369 | + self.execProcessFromVenvAsync(self.venv_bin + " manage.py test") | |
370 | + self.changeDirectory("..") | |
371 | + return 0 | |
372 | + | |
373 | + def migrate(self): | |
374 | + self.changeDirectory("src") | |
375 | + self.execProcessFromVenv(self.venv_bin + " manage.py migrate") | |
376 | + self.changeDirectory("..") | |
377 | + return 0 | |
378 | + | |
379 | + def makemigrations(self): | |
380 | + self.changeDirectory("src") | |
381 | + self.execProcessFromVenv(self.venv_bin + " manage.py makemigrations") | |
382 | + self.changeDirectory("..") | |
383 | + return 0 | |
384 | + | |
385 | + def help(self): | |
386 | + for command, message in self.commandDescription.items(): | |
387 | + self.printColor(Colors.BLUE, "-> " + command + ": ", eol='') | |
388 | + self.printColor(Colors.GREEN, message) | |
389 | + return 0 | |
390 | + | |
391 | + def updatedb(self): | |
392 | + self.migrate() | |
393 | + self.makemigrations() | |
394 | + return 0 | |
395 | + | |
396 | + def unittest(self): | |
397 | + self.changeDirectory("src") | |
398 | + self.execProcessFromVenv(self.venv_bin + " manage.py test common scheduler routine_manager user_manager alert_manager.tests.TestStrategyChange") | |
399 | + self.changeDirectory("..") | |
400 | + return 0 | |
401 | + | |
402 | + def test_all(self): | |
403 | + self.unittest() | |
404 | + | |
405 | + self.changeDirectory("src") | |
406 | + self.replacePatternInFile("CELERY_TEST = False", "CELERY_TEST = True", "pyros/settings.py") | |
407 | + self.execProcess("rm -f testdb.sqlite3") | |
408 | + self.changeDirectory("..") | |
409 | + | |
410 | + self.migrate() | |
411 | + self.loaddata() | |
412 | + self.sims_launch() | |
413 | + self.singleWorker("alert_manager") | |
414 | + self.changeDirectory("src") | |
415 | + self.execProcessFromVenv(self.venv_bin + " manage.py test alert_manager.tests.AlertListenerTestsCelery --keepdb --nomigrations") | |
416 | + self.replacePatternInFile("CELERY_TEST = True", "CELERY_TEST = False", "pyros/settings.py") | |
417 | + self.execProcess("rm -f testdb.sqlite3") | |
418 | + self.changeDirectory("..") | |
419 | + self.kill_simulation() | |
420 | + return 0 | |
421 | + | |
422 | + def loaddata(self): | |
423 | + self.changeDirectory("src") | |
424 | + self.execProcessFromVenv(self.venv_bin + " manage.py loaddata misc/fixtures/" + self.init_fixture) | |
425 | + self.changeDirectory("..") | |
426 | + return 0 | |
427 | + | |
428 | + def celery_on(self): | |
429 | + self.changeDirectory("src") | |
430 | + self.execProcessFromVenvAsync(self.venv_cel + " worker -A pyros -Q alert_listener_q -n pyros@alert_listener -c 1") | |
431 | + self.execProcessFromVenvAsync(self.venv_cel + " worker -A pyros -Q monitoring_q -n pyros@monitoring -c 1") | |
432 | + self.execProcessFromVenvAsync(self.venv_cel + " worker -A pyros -Q majordome_q -n pyros@majordome -c 1") | |
433 | + | |
434 | + self.execProcessFromVenvAsync(self.venv_cel + " worker -A pyros -Q scheduling_q --purge -n pyros@scheduling -c 1") | |
435 | + self.execProcessFromVenvAsync(self.venv_cel + " worker -A pyros -Q execute_sequence_q --purge -n pyros@execute_sequence -c 1") | |
436 | + self.execProcessFromVenvAsync(self.venv_cel + " worker -A pyros -Q execute_plan_vis_q --purge -n pyros@execute_plan_vis -c 1") | |
437 | + self.execProcessFromVenvAsync(self.venv_cel + " worker -A pyros -Q execute_plan_vis_q --purge -n pyros@execute_plan_nir -c 1") | |
438 | + self.execProcessFromVenvAsync(self.venv_cel + " worker -A pyros -Q create_calibrations_q --purge -n pyros@create_calibrations -c 1") | |
439 | + self.execProcessFromVenvAsync(self.venv_cel + " worker -A pyros -Q analysis_q --purge -n pyros@analysis -c 1") | |
440 | + self.execProcessFromVenvAsync(self.venv_cel + " worker -A pyros -Q system_status_q --purge -n pyros@system_status -c 1") | |
441 | + self.execProcessFromVenvAsync(self.venv_cel + " worker -A pyros -Q change_obs_conditions_q --purge -n pyros@change_obs_conditions -c 1") | |
442 | + self.changeDirectory("..") | |
443 | + return 0 | |
444 | + | |
445 | + def start(self): | |
446 | + self.stop() | |
447 | + self.celery_on() | |
448 | + return 0 | |
449 | + | |
450 | + def stop(self): | |
451 | + self.execProcessAsync("ps aux | grep \"celery worker\" | awk '{print $2}' | xargs kill -9") | |
452 | + return 0 | |
453 | + | |
454 | + def simulator(self): | |
455 | + self.changeDirectory("src") | |
456 | + self.replacePatternInFile("CELERY_TEST = True", "CELERY_TEST = False", "pyros/settings.py") | |
457 | + self.execProcess("rm -f testdb.sqlite3") | |
458 | + self.changeDirectory("..") | |
459 | + self.migrate() | |
460 | + self.loaddata() | |
461 | + self.server() | |
462 | + self.sleep(2) | |
463 | + self.printFullTerm(Colors.WARNING, "SUMMARY") | |
464 | + self.printColor(Colors.GREEN, "The simulator has been successfully initialised") | |
465 | + self.printColor(Colors.GREEN, "The simulator run on a temp database : src/testdb.sqlite3") | |
466 | + self.printColor(Colors.GREEN, "The simulation will be ended by the task 'simulator herself'") | |
467 | + self.printColor(Colors.GREEN, "If you want to shutdown the simulation, please run :") | |
468 | + self.printColor(Colors.GREEN, "CTRL-C or ./pyrosrun.sh kill_simulation") | |
469 | + self.printColor(Colors.GREEN, "If the simulation isn't correctly killed, please switch the variable") | |
470 | + self.printColor(Colors.GREEN, "CELERY_TEST in src/pyros/settings.py to false") | |
471 | + self.printFullTerm(Colors.WARNING, "SUMMARY") | |
472 | + self.celery_on() | |
473 | + self.sleep(3) | |
474 | + self.sims_launch() | |
475 | + return 0 | |
476 | + | |
477 | + def kill_simulation(self): | |
478 | + self.changeDirectory("src") | |
479 | + self.replacePatternInFile("CELERY_TEST = True", "CELERY_TEST = False", "pyros/settings.py") | |
480 | + self.execProcessAsync("fuser -k 8000/tcp") | |
481 | + self.execProcessAsync("rm -f testdb.sqlite3") | |
482 | + self.execProcessAsync("ps aux | grep \" userSimulator.py\" | awk '{ print $2 }' | xargs kill") | |
483 | + self.execProcessAsync("ps aux | grep \" alertSimulator.py\" | awk '{ print $2 }' | xargs kill") | |
484 | + self.execProcessAsync("ps aux | grep \" plcSimulator.py\" | awk '{ print $2 }' | xargs kill") | |
485 | + self.execProcessAsync("ps aux | grep \" telescopeSimulator.py\" | awk '{ print $2 }' | xargs kill") | |
486 | + self.execProcessAsync("ps aux | grep \" cameraNIRSimulator.py\" | awk '{ print $2 }' | xargs kill") | |
487 | + self.execProcessAsync("ps aux | grep \" cameraVISSimulator.py\" | awk '{ print $2 }' | xargs kill") | |
488 | + self.changeDirectory("..") | |
489 | + self.stop() | |
490 | + self.printFullTerm(Colors.GREEN, "simulation ended") | |
491 | + return 0 | |
492 | + | |
493 | + def sims_launch(self): | |
494 | + self.changeDirectory("simulators/config") | |
495 | + self.execProcess("ls conf*.json") | |
496 | + conf = self.askQuestion("Which simulation do you want to use", default="conf.json") | |
497 | + if not os.path.isfile(conf): | |
498 | + self.printColor(Colors.FAIL, "The simulation file " + conf + " does not exist") | |
499 | + return 1 | |
500 | + self.changeDirectory("..") | |
501 | + self.changeDirectory("user") | |
502 | + self.execProcessFromVenvAsync(self.venv_bin + " userSimulator.py " + conf) | |
503 | + self.changeDirectory("..") | |
504 | + self.changeDirectory("alert") | |
505 | + self.execProcessFromVenvAsync(self.venv_bin + " alertSimulator.py " + conf) | |
506 | + self.changeDirectory("..") | |
507 | + self.changeDirectory("plc") | |
508 | + self.execProcessFromVenvAsync(self.venv_bin + " plcSimulator.py " + conf) | |
509 | + self.changeDirectory("..") | |
510 | + self.changeDirectory("camera") | |
511 | + self.execProcessFromVenvAsync(self.venv_bin + " cameraVISSimulator.py " + conf) | |
512 | + self.execProcessFromVenvAsync(self.venv_bin + " cameraNIRSimulator.py " + conf) | |
513 | + self.changeDirectory("..") | |
514 | + self.changeDirectory("telescope") | |
515 | + self.execProcessFromVenvAsync(self.venv_bin + " telescopeSimulator.py " + conf) | |
516 | + self.changeDirectory("..") | |
517 | + self.changeDirectory("..") | |
518 | + return 0 | |
519 | + | |
520 | + def singleWorker(self, worker): | |
521 | + self.changeDirectory("src") | |
522 | + self.execProcessFromVenvAsync(self.venv_cel + " worker -A pyros -Q "+ worker +"_q -n pyros@"+worker+" -c 1") | |
523 | + self.changeDirectory("..") | |
524 | + return 0 | |
525 | + | |
526 | + def __init__(self, argv): | |
527 | + super(Pyros, self).__init__(argv) | |
528 | + self.commandMatcher = { | |
529 | + "install": self.install, | |
530 | + "update": self.update, | |
531 | + "server": self.server, | |
532 | + "clean": self.clean, | |
533 | + "clean_logs": self.clean_logs, | |
534 | + "test": self.test, | |
535 | + "migrate": self.migrate, | |
536 | + "makemigrations": self.makemigrations, | |
537 | + "updatedb": self.updatedb, | |
538 | + "unittest": self.unittest, | |
539 | + "test_all": self.test_all, | |
540 | + "celery_on": self.celery_on, | |
541 | + "loaddata": self.loaddata, | |
542 | + "start": self.start, | |
543 | + "stop": self.stop, | |
544 | + "simulator": self.simulator, | |
545 | + "kill_simulation": self.kill_simulation, | |
546 | + "sims_launch": self.sims_launch, | |
547 | + "help": self.help, | |
548 | + } | |
549 | + self.commandDescription = { | |
550 | + "install": "Launch the server installation", | |
551 | + "update": "Update the server", | |
552 | + "server": "Launch the web server", | |
553 | + "loaddata": "Load the initial fixture in database", | |
554 | + "clean": "clean the repository", | |
555 | + "clean_logs": "clean the log directory", | |
556 | + "test": "launch the server tests", | |
557 | + "migrate": "execute migrations", | |
558 | + "makemigrations": "create new migrations", | |
559 | + "help": "Help message", | |
560 | + "updatedb": "Update the database", | |
561 | + "unittest": "Runs the tests thaht don't need celery", | |
562 | + "test_all": "Run all the existing tests (this command needs to be updated when tests are added in the project", | |
563 | + "celery_on": "Starts celery workers", | |
564 | + "start": "Stop the celery workers then the web server", | |
565 | + "stop": "stops the celery workers", | |
566 | + "simulator": "Launch a simulation", | |
567 | + "kill_simulation": "kill the simulators / celery workers / web server", | |
568 | + "sims_launch": "Launch only the simulators", | |
569 | + } | |
570 | + | |
571 | +if __name__ == "__main__": | |
572 | + conf = Config() | |
573 | + if conf.parseConf(): | |
574 | + sys.exit(1) | |
575 | + pyros = Pyros(conf) | |
576 | + sys.exit(pyros.exec()) | |
0 | 577 | \ No newline at end of file | ... | ... |
simulators/user/userSimulator.py
... | ... | @@ -10,7 +10,7 @@ class UserSimulator(): |
10 | 10 | conf_file = "../config/conf.json" |
11 | 11 | conf_path = "../config/" |
12 | 12 | request_path = "../resources/" |
13 | - login = "http://localhost:8000/user_manager/login" # | |
13 | + login = "http://localhost:8000/user_manager/login" | |
14 | 14 | url = "http://localhost:8000/routine_manager/import_request" |
15 | 15 | get_url = "http://localhost:8000/routine_manager/" |
16 | 16 | sims = [] |
... | ... | @@ -51,16 +51,22 @@ class UserSimulator(): |
51 | 51 | |
52 | 52 | def authenticate(self): |
53 | 53 | self.client = requests.session() |
54 | - self.client.get("http://localhost:8000/user_manager/login") | |
55 | - csrftoken = self.client.cookies['csrftoken'] | |
56 | - self.logindata = dict(email="pyros", password="DjangoPyros", csrfmiddlewaretoken=csrftoken) | |
57 | - resp = self.client.post(self.login, data=self.logindata, headers=dict(Referer=self.login)) | |
58 | - self.userPrint("Connection status : %d"%(resp.status_code)) | |
54 | + try: | |
55 | + self.client.get("http://localhost:8000/user_manager/login") | |
56 | + csrftoken = self.client.cookies['csrftoken'] | |
57 | + self.logindata = dict(email="pyros", password="DjangoPyros", csrfmiddlewaretoken=csrftoken) | |
58 | + resp = self.client.post(self.login, data=self.logindata, headers=dict(Referer=self.login)) | |
59 | + self.userPrint("Connection status : %d"%(resp.status_code)) | |
60 | + except Exception as e: | |
61 | + print(e, file=sys.stderr) | |
62 | + return 1 | |
63 | + return 0 | |
59 | 64 | |
60 | 65 | def run(self): |
61 | 66 | i = 0 |
62 | 67 | self.parse() |
63 | - self.authenticate() | |
68 | + if self.authenticate(): | |
69 | + return 1 | |
64 | 70 | self.userPrint("The simulator will end in %d seconds"%(int(self.ended))) |
65 | 71 | while (i < self.ended): |
66 | 72 | for dic in self.sims: |
... | ... | @@ -72,4 +78,4 @@ class UserSimulator(): |
72 | 78 | |
73 | 79 | if (__name__ == "__main__"): |
74 | 80 | sim = UserSimulator(sys.argv) |
75 | - sim.run() | |
81 | + sys.exit(sim.run()) | ... | ... |