Mercurial > libervia-backend
annotate libervia/backend/core/launcher.py @ 4149:c36295487082
core: introduce Pydantic based models in `libervia.backend.models.core`
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 22 Nov 2023 14:31:05 +0100 |
parents | 84f6bee6440d |
children | 3dbaf179c50d |
rev | line source |
---|---|
3281
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
1 #!/usr/bin/env python3 |
225
fd9b7834d98a
distutils installation script, draft
Goffi <goffi@goffi.org>
parents:
223
diff
changeset
|
2 |
3480
7550ae9cfbac
Renamed the project from "Salut à Toi" to "Libervia":
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
3 # Libervia: an XMPP client |
3479 | 4 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org) |
3099 | 5 |
3281
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
6 # This program is free software: you can redistribute it and/or modify |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
7 # it under the terms of the GNU Affero General Public License as published by |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
8 # the Free Software Foundation, either version 3 of the License, or |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
9 # (at your option) any later version. |
234
a7079e835432
added stop command in sat.sh launching script
Goffi <goffi@goffi.org>
parents:
226
diff
changeset
|
10 |
3281
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
11 # This program is distributed in the hope that it will be useful, |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
14 # GNU Affero General Public License for more details. |
4 | 15 |
3281
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
16 # You should have received a copy of the GNU Affero General Public License |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
17 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
18 |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
19 """Script launching SàT backend""" |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
20 |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
21 import sys |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
22 import os |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
23 import argparse |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
24 from pathlib import Path |
3028 | 25 from configparser import ConfigParser |
3281
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
26 from twisted.application import app |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
27 from twisted.python import usage |
4071
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
28 from libervia.backend.core.constants import Const as C |
3281
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
29 |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
30 |
4071
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
31 class LiberviaLogger(app.AppLogger): |
369
e83d0c21d64d
launching script now read config files
Goffi <goffi@goffi.org>
parents:
358
diff
changeset
|
32 |
3281
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
33 def start(self, application): |
4081
84f6bee6440d
installation: moved from `setup.py` to `pyproject.toml`:
Goffi <goffi@goffi.org>
parents:
4075
diff
changeset
|
34 # logging is initialised by libervia.baceknd.core.log_config via the Twisted |
84f6bee6440d
installation: moved from `setup.py` to `pyproject.toml`:
Goffi <goffi@goffi.org>
parents:
4075
diff
changeset
|
35 # plugin, nothing to do here |
3281
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
36 self._initialLog() |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
37 |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
38 def stop(self): |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
39 pass |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
40 |
1003
52ec79aa5bbe
memory: fixes the method fixLocalDir for pid_dir and log_dir to use local_dir as default
souliane <souliane@mailoo.org>
parents:
931
diff
changeset
|
41 |
3281
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
42 class Launcher: |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
43 APP_NAME=C.APP_NAME |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
44 APP_NAME_FILE=C.APP_NAME_FILE |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
45 |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
46 @property |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
47 def NOT_RUNNING_MSG(self): |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
48 return f"{self.APP_NAME} is *NOT* running" |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
49 |
3284
751d8fa45ced
core (launcher): fixed launching of twistd when no subparsed is specified
Goffi <goffi@goffi.org>
parents:
3282
diff
changeset
|
50 def cmd_no_subparser(self, args): |
751d8fa45ced
core (launcher): fixed launching of twistd when no subparsed is specified
Goffi <goffi@goffi.org>
parents:
3282
diff
changeset
|
51 """Command launched by default""" |
751d8fa45ced
core (launcher): fixed launching of twistd when no subparsed is specified
Goffi <goffi@goffi.org>
parents:
3282
diff
changeset
|
52 args.extra_args = [] |
751d8fa45ced
core (launcher): fixed launching of twistd when no subparsed is specified
Goffi <goffi@goffi.org>
parents:
3282
diff
changeset
|
53 self.cmd_background(args) |
751d8fa45ced
core (launcher): fixed launching of twistd when no subparsed is specified
Goffi <goffi@goffi.org>
parents:
3282
diff
changeset
|
54 |
3281
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
55 def cmd_background(self, args): |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
56 self.run_twistd(args) |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
57 |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
58 def cmd_foreground(self, args): |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
59 self.run_twistd(args, twistd_opts=['--nodaemon']) |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
60 |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
61 def cmd_debug(self, args): |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
62 self.run_twistd(args, twistd_opts=['--debug']) |
463 | 63 |
3281
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
64 def cmd_stop(self, args): |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
65 import signal |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
66 import time |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
67 config = self.get_config() |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
68 pid_file = self.get_pid_file(config) |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
69 if not pid_file.is_file(): |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
70 print(self.NOT_RUNNING_MSG) |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
71 sys.exit(0) |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
72 try: |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
73 pid = int(pid_file.read_text()) |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
74 except Exception as e: |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
75 print(f"Can't read PID file at {pid_file}: {e}") |
4075
47401850dec6
refactoring: rename `libervia.frontends.jp` to `libervia.cli`
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
76 # we use the same exit code as DATA_ERROR in CLI frontend |
3281
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
77 sys.exit(17) |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
78 print(f"Terminating {self.APP_NAME}…") |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
79 os.kill(pid, signal.SIGTERM) |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
80 kill_started = time.time() |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
81 state = "init" |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
82 import errno |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
83 while True: |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
84 try: |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
85 os.kill(pid, 0) |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
86 except OSError as e: |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
87 if e.errno == errno.ESRCH: |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
88 break |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
89 elif e.errno == errno.EPERM: |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
90 print(f"Can't kill {self.APP_NAME}, the process is owned by an other user", file=sys.stderr) |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
91 sys.exit(18) |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
92 else: |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
93 raise e |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
94 time.sleep(0.2) |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
95 now = time.time() |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
96 if state == 'init' and now - kill_started > 5: |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
97 if state == 'init': |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
98 state = 'waiting' |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
99 print(f"Still waiting for {self.APP_NAME} to be terminated…") |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
100 elif state == 'waiting' and now - kill_started > 10: |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
101 state == 'killing' |
3416
d7cfb031e41f
core (launcher): exit code can now specified
Goffi <goffi@goffi.org>
parents:
3284
diff
changeset
|
102 print("Waiting for too long, we kill the process") |
3281
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
103 os.kill(pid, signal.SIGKILL) |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
104 sys.exit(1) |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
105 |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
106 sys.exit(0) |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
107 |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
108 def cmd_status(self, args): |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
109 config = self.get_config() |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
110 pid_file = self.get_pid_file(config) |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
111 if pid_file.is_file(): |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
112 import errno |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
113 try: |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
114 pid = int(pid_file.read_text()) |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
115 except Exception as e: |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
116 print(f"Can't read PID file at {pid_file}: {e}") |
4075
47401850dec6
refactoring: rename `libervia.frontends.jp` to `libervia.cli`
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
117 # we use the same exit code as DATA_ERROR in CLI frontend |
3281
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
118 sys.exit(17) |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
119 # we check if there is a process |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
120 # inspired by https://stackoverflow.com/a/568285 and https://stackoverflow.com/a/6940314 |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
121 try: |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
122 os.kill(pid, 0) |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
123 except OSError as e: |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
124 if e.errno == errno.ESRCH: |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
125 running = False |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
126 elif e.errno == errno.EPERM: |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
127 print("Process {pid} is run by an other user") |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
128 running = True |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
129 else: |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
130 running = True |
931
3b30e9f83d88
misc: sat stop would not kill all sat instances anymore
souliane <souliane@mailoo.org>
parents:
930
diff
changeset
|
131 |
3281
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
132 if running: |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
133 print(f"{self.APP_NAME} is running (pid: {pid})") |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
134 sys.exit(0) |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
135 else: |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
136 print(f"{self.NOT_RUNNING_MSG}, but a pid file is present (bad exit ?): {pid_file}") |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
137 sys.exit(2) |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
138 else: |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
139 print(self.NOT_RUNNING_MSG) |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
140 sys.exit(1) |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
141 |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
142 def parse_args(self): |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
143 parser = argparse.ArgumentParser(description=f"Launch {self.APP_NAME} backend") |
3284
751d8fa45ced
core (launcher): fixed launching of twistd when no subparsed is specified
Goffi <goffi@goffi.org>
parents:
3282
diff
changeset
|
144 parser.set_defaults(cmd=self.cmd_no_subparser) |
3281
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
145 subparsers = parser.add_subparsers() |
3282
e7e7be79fbcd
core (launcher): extra arguments can now be passed the service with `bg`, `fg` and `dbg`:
Goffi <goffi@goffi.org>
parents:
3281
diff
changeset
|
146 extra_help = f"arguments to pass to {self.APP_NAME} service" |
3281
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
147 |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
148 bg_parser = subparsers.add_parser( |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
149 'background', |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
150 aliases=['bg'], |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
151 help=f"run {self.APP_NAME} backend in background (as a daemon)") |
3282
e7e7be79fbcd
core (launcher): extra arguments can now be passed the service with `bg`, `fg` and `dbg`:
Goffi <goffi@goffi.org>
parents:
3281
diff
changeset
|
152 bg_parser.add_argument('extra_args', nargs=argparse.REMAINDER, help=extra_help) |
3281
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
153 bg_parser.set_defaults(cmd=self.cmd_background) |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
154 |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
155 fg_parser = subparsers.add_parser( |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
156 'foreground', |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
157 aliases=['fg'], |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
158 help=f"run {self.APP_NAME} backend in foreground") |
3282
e7e7be79fbcd
core (launcher): extra arguments can now be passed the service with `bg`, `fg` and `dbg`:
Goffi <goffi@goffi.org>
parents:
3281
diff
changeset
|
159 fg_parser.add_argument('extra_args', nargs=argparse.REMAINDER, help=extra_help) |
3281
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
160 fg_parser.set_defaults(cmd=self.cmd_foreground) |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
161 |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
162 dbg_parser = subparsers.add_parser( |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
163 'debug', |
3282
e7e7be79fbcd
core (launcher): extra arguments can now be passed the service with `bg`, `fg` and `dbg`:
Goffi <goffi@goffi.org>
parents:
3281
diff
changeset
|
164 aliases=['dbg'], |
3281
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
165 help=f"run {self.APP_NAME} backend in debug mode") |
3282
e7e7be79fbcd
core (launcher): extra arguments can now be passed the service with `bg`, `fg` and `dbg`:
Goffi <goffi@goffi.org>
parents:
3281
diff
changeset
|
166 dbg_parser.add_argument('extra_args', nargs=argparse.REMAINDER, help=extra_help) |
3281
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
167 dbg_parser.set_defaults(cmd=self.cmd_debug) |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
168 |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
169 stop_parser = subparsers.add_parser( |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
170 'stop', |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
171 help=f"stop running {self.APP_NAME} backend") |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
172 stop_parser.set_defaults(cmd=self.cmd_stop) |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
173 |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
174 status_parser = subparsers.add_parser( |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
175 'status', |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
176 help=f"indicate if {self.APP_NAME} backend is running") |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
177 status_parser.set_defaults(cmd=self.cmd_status) |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
178 |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
179 return parser.parse_args() |
931
3b30e9f83d88
misc: sat stop would not kill all sat instances anymore
souliane <souliane@mailoo.org>
parents:
930
diff
changeset
|
180 |
3281
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
181 def get_config(self): |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
182 config = ConfigParser(defaults=C.DEFAULT_CONFIG) |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
183 try: |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
184 config.read(C.CONFIG_FILES) |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
185 except Exception as e: |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
186 print (rf"/!\ Can't read main config! {e}") |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
187 sys.exit(1) |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
188 return config |
225
fd9b7834d98a
distutils installation script, draft
Goffi <goffi@goffi.org>
parents:
223
diff
changeset
|
189 |
3281
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
190 def get_pid_file(self, config): |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
191 pid_dir = Path(config.get('DEFAULT', 'pid_dir')).expanduser() |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
192 return pid_dir / f"{self.APP_NAME_FILE}.pid" |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
193 |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
194 def run_twistd(self, args, twistd_opts=None): |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
195 """Run twistd settings options with args""" |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
196 from twisted.python.runtime import platformType |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
197 if platformType == "win32": |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
198 from twisted.scripts._twistw import (ServerOptions, |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
199 WindowsApplicationRunner as app_runner) |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
200 else: |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
201 from twisted.scripts._twistd_unix import (ServerOptions, |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
202 UnixApplicationRunner as app_runner) |
225
fd9b7834d98a
distutils installation script, draft
Goffi <goffi@goffi.org>
parents:
223
diff
changeset
|
203 |
4071
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
204 app_runner.loggerFactory = LiberviaLogger |
3281
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
205 server_options = ServerOptions() |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
206 config = self.get_config() |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
207 pid_file = self.get_pid_file(config) |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
208 log_dir = Path(config.get('DEFAULT', 'log_dir')).expanduser() |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
209 log_file = log_dir / f"{self.APP_NAME_FILE}.log" |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
210 server_opts = [ |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
211 '--no_save', |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
212 '--pidfile', str(pid_file), |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
213 '--logfile', str(log_file), |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
214 ] |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
215 if twistd_opts is not None: |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
216 server_opts.extend(twistd_opts) |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
217 server_opts.append(self.APP_NAME_FILE) |
3282
e7e7be79fbcd
core (launcher): extra arguments can now be passed the service with `bg`, `fg` and `dbg`:
Goffi <goffi@goffi.org>
parents:
3281
diff
changeset
|
218 if args.extra_args: |
e7e7be79fbcd
core (launcher): extra arguments can now be passed the service with `bg`, `fg` and `dbg`:
Goffi <goffi@goffi.org>
parents:
3281
diff
changeset
|
219 try: |
e7e7be79fbcd
core (launcher): extra arguments can now be passed the service with `bg`, `fg` and `dbg`:
Goffi <goffi@goffi.org>
parents:
3281
diff
changeset
|
220 args.extra_args.remove('--') |
e7e7be79fbcd
core (launcher): extra arguments can now be passed the service with `bg`, `fg` and `dbg`:
Goffi <goffi@goffi.org>
parents:
3281
diff
changeset
|
221 except ValueError: |
e7e7be79fbcd
core (launcher): extra arguments can now be passed the service with `bg`, `fg` and `dbg`:
Goffi <goffi@goffi.org>
parents:
3281
diff
changeset
|
222 pass |
e7e7be79fbcd
core (launcher): extra arguments can now be passed the service with `bg`, `fg` and `dbg`:
Goffi <goffi@goffi.org>
parents:
3281
diff
changeset
|
223 server_opts.extend(args.extra_args) |
3281
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
224 try: |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
225 server_options.parseOptions(server_opts) |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
226 except usage.error as ue: |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
227 print(server_options) |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
228 print("%s: %s" % (sys.argv[0], ue)) |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
229 sys.exit(1) |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
230 else: |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
231 runner = app_runner(server_options) |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
232 runner.run() |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
233 if runner._exitSignal is not None: |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
234 app._exitWithSignal(runner._exitSignal) |
3416
d7cfb031e41f
core (launcher): exit code can now specified
Goffi <goffi@goffi.org>
parents:
3284
diff
changeset
|
235 try: |
d7cfb031e41f
core (launcher): exit code can now specified
Goffi <goffi@goffi.org>
parents:
3284
diff
changeset
|
236 sys.exit(app._exitCode) |
d7cfb031e41f
core (launcher): exit code can now specified
Goffi <goffi@goffi.org>
parents:
3284
diff
changeset
|
237 except AttributeError: |
d7cfb031e41f
core (launcher): exit code can now specified
Goffi <goffi@goffi.org>
parents:
3284
diff
changeset
|
238 pass |
226
d8bb72f00eec
distutils install: fixed plugin import and log file path
Goffi <goffi@goffi.org>
parents:
225
diff
changeset
|
239 |
3281
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
240 @classmethod |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
241 def run(cls): |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
242 args = cls().parse_args() |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
243 args.cmd(args) |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
244 |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
245 |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
246 if __name__ == '__main__': |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
247 Launcher.run() |