annotate sat/core/launcher.py @ 3282:e7e7be79fbcd

core (launcher): extra arguments can now be passed the service with `bg`, `fg` and `dbg`: - `dbg` is an alias for `debug` - extra arguments can be passed to the service, use `-- --help` to see them`
author Goffi <goffi@goffi.org>
date Mon, 25 May 2020 16:15:05 +0200
parents a3639d6d9643
children 751d8fa45ced
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
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
ac568832a71a fixed lauching script
Goffi <goffi@goffi.org>
parents: 369
diff changeset
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()
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
142 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
143
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
144 bg_parser = subparsers.add_parser(
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
145 'background',
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
146 aliases=['bg'],
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
147 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
148 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
149 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
150
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
151 fg_parser = subparsers.add_parser(
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
152 'foreground',
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
153 aliases=['fg'],
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
154 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
155 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
156 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
157
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
158 dbg_parser = subparsers.add_parser(
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
159 '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
160 aliases=['dbg'],
3281
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
161 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
162 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
163 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
164
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
165 stop_parser = subparsers.add_parser(
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
166 'stop',
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
167 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
168 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
169
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
170 status_parser = subparsers.add_parser(
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
171 'status',
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
172 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
173 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
174
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
175 return parser.parse_args()
931
3b30e9f83d88 misc: sat stop would not kill all sat instances anymore
souliane <souliane@mailoo.org>
parents: 930
diff changeset
176
3281
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
177 def get_config(self):
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
178 config = ConfigParser(defaults=C.DEFAULT_CONFIG)
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
179 try:
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
180 config.read(C.CONFIG_FILES)
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
181 except Exception as e:
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
182 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
183 sys.exit(1)
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
184 return config
225
fd9b7834d98a distutils installation script, draft
Goffi <goffi@goffi.org>
parents: 223
diff changeset
185
3281
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
186 def get_pid_file(self, config):
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
187 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
188 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
189
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
190 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
191 """Run twistd settings options with args"""
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
192 from twisted.python.runtime import platformType
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
193 if platformType == "win32":
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
194 from twisted.scripts._twistw import (ServerOptions,
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
195 WindowsApplicationRunner as app_runner)
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
196 else:
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
197 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
198 UnixApplicationRunner as app_runner)
225
fd9b7834d98a distutils installation script, draft
Goffi <goffi@goffi.org>
parents: 223
diff changeset
199
3281
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
200 app_runner.loggerFactory = SatLogger
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
201 server_options = ServerOptions()
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
202 config = self.get_config()
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
203 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
204 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
205 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
206 server_opts = [
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
207 '--no_save',
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
208 '--pidfile', str(pid_file),
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
209 '--logfile', str(log_file),
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
210 ]
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
211 if twistd_opts is not None:
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
212 server_opts.extend(twistd_opts)
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
213 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
214 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
215 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
216 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
217 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
218 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
219 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
220 try:
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
221 server_options.parseOptions(server_opts)
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
222 except usage.error as ue:
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
223 print(server_options)
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
224 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
225 sys.exit(1)
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
226 else:
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
227 runner = app_runner(server_options)
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
228 runner.run()
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
229 if runner._exitSignal is not None:
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
230 app._exitWithSignal(runner._exitSignal)
226
d8bb72f00eec distutils install: fixed plugin import and log file path
Goffi <goffi@goffi.org>
parents: 225
diff changeset
231
3281
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
232 @classmethod
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
233 def run(cls):
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
234 args = cls().parse_args()
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
235 args.cmd(args)
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
236
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
237
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
238 if __name__ == '__main__':
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
239 Launcher.run()