annotate sat/core/launcher.py @ 3355:33d9b38b5890

plugin XEP-0329: better error handling in `onComponentAffiliationsGet`
author Goffi <goffi@goffi.org>
date Mon, 14 Sep 2020 09:23:23 +0200
parents 751d8fa45ced
children d7cfb031e41f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
0b6d56a8f7e3 bin: look for twistd3 first:
Goffi <goffi@goffi.org>
parents: 3053
diff changeset
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
3c1cefd41186 added sat executable
Goffi <goffi@goffi.org>
parents:
diff changeset
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
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
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
3284
751d8fa45ced core (launcher): fixed launching of twistd when no subparsed is specified
Goffi <goffi@goffi.org>
parents: 3282
diff changeset
51 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
52 """Command launched by default"""
751d8fa45ced core (launcher): fixed launching of twistd when no subparsed is specified
Goffi <goffi@goffi.org>
parents: 3282
diff changeset
53 args.extra_args = []
751d8fa45ced core (launcher): fixed launching of twistd when no subparsed is specified
Goffi <goffi@goffi.org>
parents: 3282
diff changeset
54 self.cmd_background(args)
751d8fa45ced core (launcher): fixed launching of twistd when no subparsed is specified
Goffi <goffi@goffi.org>
parents: 3282
diff changeset
55
3281
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
56 def cmd_background(self, args):
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
57 self.run_twistd(args)
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
58
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
59 def cmd_foreground(self, args):
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
60 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
61
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
62 def cmd_debug(self, args):
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
63 self.run_twistd(args, twistd_opts=['--debug'])
463
ac568832a71a fixed lauching script
Goffi <goffi@goffi.org>
parents: 369
diff changeset
64
3281
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
65 def cmd_stop(self, args):
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
66 import signal
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
67 import time
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
68 config = self.get_config()
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
69 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
70 if not pid_file.is_file():
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
71 print(self.NOT_RUNNING_MSG)
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
72 sys.exit(0)
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
73 try:
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
74 pid = int(pid_file.read_text())
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
75 except Exception as e:
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
76 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
77 # 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
78 sys.exit(17)
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
79 print(f"Terminating {self.APP_NAME}…")
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
80 os.kill(pid, signal.SIGTERM)
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
81 kill_started = time.time()
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
82 state = "init"
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
83 import errno
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
84 while True:
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
85 try:
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
86 os.kill(pid, 0)
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
87 except OSError as e:
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
88 if e.errno == errno.ESRCH:
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
89 break
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
90 elif e.errno == errno.EPERM:
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
91 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
92 sys.exit(18)
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
93 else:
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
94 raise e
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
95 time.sleep(0.2)
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
96 now = time.time()
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
97 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
98 if state == 'init':
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
99 state = 'waiting'
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
100 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
101 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
102 state == 'killing'
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
103 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
104 os.kill(pid, signal.SIGKILL)
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
105 sys.exit(1)
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
106
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
107 sys.exit(0)
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
108
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
109 def cmd_status(self, args):
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
110 config = self.get_config()
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
111 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
112 if pid_file.is_file():
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
113 import errno
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
114 try:
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
115 pid = int(pid_file.read_text())
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
116 except Exception as e:
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
117 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
118 # 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
119 sys.exit(17)
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
120 # 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
121 # 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
122 try:
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
123 os.kill(pid, 0)
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
124 except OSError as e:
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
125 if e.errno == errno.ESRCH:
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
126 running = False
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
127 elif e.errno == errno.EPERM:
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
128 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
129 running = True
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
130 else:
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
131 running = True
931
3b30e9f83d88 misc: sat stop would not kill all sat instances anymore
souliane <souliane@mailoo.org>
parents: 930
diff changeset
132
3281
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
133 if running:
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
134 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
135 sys.exit(0)
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
136 else:
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
137 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
138 sys.exit(2)
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
139 else:
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
140 print(self.NOT_RUNNING_MSG)
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
141 sys.exit(1)
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 def parse_args(self):
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
144 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
145 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
146 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
147 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
148
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
149 bg_parser = subparsers.add_parser(
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
150 'background',
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
151 aliases=['bg'],
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 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
153 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
154 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
155
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
156 fg_parser = subparsers.add_parser(
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
157 'foreground',
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
158 aliases=['fg'],
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
159 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
160 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
161 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
162
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
163 dbg_parser = subparsers.add_parser(
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
164 '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
165 aliases=['dbg'],
3281
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
166 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
167 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
168 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
169
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
170 stop_parser = subparsers.add_parser(
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
171 'stop',
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
172 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
173 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
174
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
175 status_parser = subparsers.add_parser(
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
176 'status',
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
177 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
178 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
179
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
180 return parser.parse_args()
931
3b30e9f83d88 misc: sat stop would not kill all sat instances anymore
souliane <souliane@mailoo.org>
parents: 930
diff changeset
181
3281
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
182 def get_config(self):
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
183 config = ConfigParser(defaults=C.DEFAULT_CONFIG)
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
184 try:
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
185 config.read(C.CONFIG_FILES)
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
186 except Exception as e:
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
187 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
188 sys.exit(1)
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
189 return config
225
fd9b7834d98a distutils installation script, draft
Goffi <goffi@goffi.org>
parents: 223
diff changeset
190
3281
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
191 def get_pid_file(self, config):
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
192 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
193 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
194
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
195 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
196 """Run twistd settings options with args"""
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
197 from twisted.python.runtime import platformType
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
198 if platformType == "win32":
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
199 from twisted.scripts._twistw import (ServerOptions,
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
200 WindowsApplicationRunner as app_runner)
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
201 else:
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
202 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
203 UnixApplicationRunner as app_runner)
225
fd9b7834d98a distutils installation script, draft
Goffi <goffi@goffi.org>
parents: 223
diff changeset
204
3281
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
205 app_runner.loggerFactory = SatLogger
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
206 server_options = ServerOptions()
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
207 config = self.get_config()
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
208 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
209 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
210 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
211 server_opts = [
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
212 '--no_save',
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
213 '--pidfile', str(pid_file),
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
214 '--logfile', str(log_file),
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
215 ]
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
216 if twistd_opts is not None:
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
217 server_opts.extend(twistd_opts)
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
218 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
219 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
220 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
221 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
222 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
223 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
224 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
225 try:
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
226 server_options.parseOptions(server_opts)
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
227 except usage.error as ue:
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
228 print(server_options)
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
229 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
230 sys.exit(1)
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
231 else:
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
232 runner = app_runner(server_options)
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
233 runner.run()
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
234 if runner._exitSignal is not None:
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
235 app._exitWithSignal(runner._exitSignal)
226
d8bb72f00eec distutils install: fixed plugin import and log file path
Goffi <goffi@goffi.org>
parents: 225
diff changeset
236
3281
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
237 @classmethod
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
238 def run(cls):
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
239 args = cls().parse_args()
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
240 args.cmd(args)
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
241
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
242
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
243 if __name__ == '__main__':
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
244 Launcher.run()