Mercurial > libervia-backend
annotate sat/core/launcher.py @ 3281:a3639d6d9643
core: replaced `sat` shell script by a python script:
the backend is now launched by `sat.core.launcher`, and the script is generated during
installation with a new entry point in `setup.py`.
author | Goffi <goffi@goffi.org> |
---|---|
date | Mon, 25 May 2020 15:50:01 +0200 |
parents | bin/sat@e81ad34e8af8 |
children | e7e7be79fbcd |
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 |
3281
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
3 # SàT: an XMPP client |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
4 # Copyright (C) 2009-2020 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 |
369
e83d0c21d64d
launching script now read config files
Goffi <goffi@goffi.org>
parents:
358
diff
changeset
|
26 from os.path import expanduser, join |
3281
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
27 from twisted.application import app |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
28 from twisted.python import usage |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
29 from sat.core.constants import Const as C |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
30 |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
31 |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
32 class SatLogger(app.AppLogger): |
369
e83d0c21d64d
launching script now read config files
Goffi <goffi@goffi.org>
parents:
358
diff
changeset
|
33 |
3281
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
34 def start(self, application): |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
35 # logging is initialised by sat.core.log_config via the Twisted plugin, nothing |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
36 # to do here |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
37 self._initialLog() |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
38 |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
39 def stop(self): |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
40 pass |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
41 |
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
|
42 |
3281
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
43 class Launcher: |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
44 APP_NAME=C.APP_NAME |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
45 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
|
46 |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
47 @property |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
48 def NOT_RUNNING_MSG(self): |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
49 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
|
50 |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
51 def cmd_background(self, args): |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
52 self.run_twistd(args) |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
53 |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
54 def cmd_foreground(self, args): |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
55 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
|
56 |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
57 def cmd_debug(self, args): |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
58 self.run_twistd(args, twistd_opts=['--debug']) |
463 | 59 |
3281
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
60 def cmd_stop(self, args): |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
61 import signal |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
62 import time |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
63 config = self.get_config() |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
64 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
|
65 if not pid_file.is_file(): |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
66 print(self.NOT_RUNNING_MSG) |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
67 sys.exit(0) |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
68 try: |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
69 pid = int(pid_file.read_text()) |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
70 except Exception as e: |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
71 print(f"Can't read PID file at {pid_file}: {e}") |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
72 # we use the same exit code as DATA_ERROR in jp |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
73 sys.exit(17) |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
74 print(f"Terminating {self.APP_NAME}…") |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
75 os.kill(pid, signal.SIGTERM) |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
76 kill_started = time.time() |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
77 state = "init" |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
78 import errno |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
79 while True: |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
80 try: |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
81 os.kill(pid, 0) |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
82 except OSError as e: |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
83 if e.errno == errno.ESRCH: |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
84 break |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
85 elif e.errno == errno.EPERM: |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
86 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
|
87 sys.exit(18) |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
88 else: |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
89 raise e |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
90 time.sleep(0.2) |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
91 now = time.time() |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
92 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
|
93 if state == 'init': |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
94 state = 'waiting' |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
95 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
|
96 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
|
97 state == 'killing' |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
98 print(f"Waiting for too long, we kill the process") |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
99 os.kill(pid, signal.SIGKILL) |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
100 sys.exit(1) |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
101 |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
102 sys.exit(0) |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
103 |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
104 def cmd_status(self, args): |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
105 config = self.get_config() |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
106 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
|
107 if pid_file.is_file(): |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
108 import errno |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
109 try: |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
110 pid = int(pid_file.read_text()) |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
111 except Exception as e: |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
112 print(f"Can't read PID file at {pid_file}: {e}") |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
113 # we use the same exit code as DATA_ERROR in jp |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
114 sys.exit(17) |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
115 # 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
|
116 # 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
|
117 try: |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
118 os.kill(pid, 0) |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
119 except OSError as e: |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
120 if e.errno == errno.ESRCH: |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
121 running = False |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
122 elif e.errno == errno.EPERM: |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
123 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
|
124 running = True |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
125 else: |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
126 running = True |
931
3b30e9f83d88
misc: sat stop would not kill all sat instances anymore
souliane <souliane@mailoo.org>
parents:
930
diff
changeset
|
127 |
3281
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
128 if running: |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
129 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
|
130 sys.exit(0) |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
131 else: |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
132 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
|
133 sys.exit(2) |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
134 else: |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
135 print(self.NOT_RUNNING_MSG) |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
136 sys.exit(1) |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
137 |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
138 def parse_args(self): |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
139 parser = argparse.ArgumentParser(description=f"Launch {self.APP_NAME} backend") |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
140 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
|
141 subparsers = parser.add_subparsers() |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
142 |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
143 bg_parser = subparsers.add_parser( |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
144 'background', |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
145 aliases=['bg'], |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
146 help=f"run {self.APP_NAME} backend in background (as a daemon)") |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
147 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
|
148 |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
149 fg_parser = subparsers.add_parser( |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
150 'foreground', |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
151 aliases=['fg'], |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
152 help=f"run {self.APP_NAME} backend in foreground") |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
153 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
|
154 |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
155 dbg_parser = subparsers.add_parser( |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
156 'debug', |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
157 help=f"run {self.APP_NAME} backend in debug mode") |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
158 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
|
159 |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
160 stop_parser = subparsers.add_parser( |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
161 'stop', |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
162 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
|
163 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
|
164 |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
165 status_parser = subparsers.add_parser( |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
166 'status', |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
167 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
|
168 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
|
169 |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
170 return parser.parse_args() |
931
3b30e9f83d88
misc: sat stop would not kill all sat instances anymore
souliane <souliane@mailoo.org>
parents:
930
diff
changeset
|
171 |
3281
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
172 def get_config(self): |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
173 config = ConfigParser(defaults=C.DEFAULT_CONFIG) |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
174 try: |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
175 config.read(C.CONFIG_FILES) |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
176 except Exception as e: |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
177 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
|
178 sys.exit(1) |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
179 return config |
225
fd9b7834d98a
distutils installation script, draft
Goffi <goffi@goffi.org>
parents:
223
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_pid_file(self, config): |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
182 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
|
183 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
|
184 |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
185 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
|
186 """Run twistd settings options with args""" |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
187 from twisted.python.runtime import platformType |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
188 if platformType == "win32": |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
189 from twisted.scripts._twistw import (ServerOptions, |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
190 WindowsApplicationRunner as app_runner) |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
191 else: |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
192 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
|
193 UnixApplicationRunner as app_runner) |
225
fd9b7834d98a
distutils installation script, draft
Goffi <goffi@goffi.org>
parents:
223
diff
changeset
|
194 |
3281
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
195 app_runner.loggerFactory = SatLogger |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
196 server_options = ServerOptions() |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
197 config = self.get_config() |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
198 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
|
199 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
|
200 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
|
201 server_opts = [ |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
202 '--no_save', |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
203 '--pidfile', str(pid_file), |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
204 '--logfile', str(log_file), |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
205 ] |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
206 if twistd_opts is not None: |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
207 server_opts.extend(twistd_opts) |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
208 server_opts.append(self.APP_NAME_FILE) |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
209 try: |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
210 server_options.parseOptions(server_opts) |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
211 except usage.error as ue: |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
212 print(server_options) |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
213 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
|
214 sys.exit(1) |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
215 else: |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
216 runner = app_runner(server_options) |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
217 runner.run() |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
218 if runner._exitSignal is not None: |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
219 app._exitWithSignal(runner._exitSignal) |
226
d8bb72f00eec
distutils install: fixed plugin import and log file path
Goffi <goffi@goffi.org>
parents:
225
diff
changeset
|
220 |
3281
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
221 @classmethod |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
222 def run(cls): |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
223 args = cls().parse_args() |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
224 args.cmd(args) |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
225 |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
226 |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
227 if __name__ == '__main__': |
a3639d6d9643
core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents:
3263
diff
changeset
|
228 Launcher.run() |