annotate libervia/backend/core/launcher.py @ 4229:dd9bc7d791d7

core (constants): use `bool_type`: Using `bool` for type hint in `Const.bool_const` if conflicting with `Const.bool` method. To avoid this, `bool_type` is used as an alias to `bool`.
author Goffi <goffi@goffi.org>
date Sat, 06 Apr 2024 12:14:10 +0200
parents b26339343076
children 0d7bb4df2343
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
3480
7550ae9cfbac Renamed the project from "Salut à Toi" to "Libervia":
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
3 # Libervia: an XMPP client
3479
be6d91572633 date update
Goffi <goffi@goffi.org>
parents: 3416
diff changeset
4 # Copyright (C) 2009-2021 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
3281
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
26 from twisted.application import app
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
27 from twisted.python import usage
4071
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 3480
diff changeset
28 from libervia.backend.core.constants import Const as C
3281
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
29
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
30
4071
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 3480
diff changeset
31 class LiberviaLogger(app.AppLogger):
3281
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
32 def start(self, application):
4081
84f6bee6440d installation: moved from `setup.py` to `pyproject.toml`:
Goffi <goffi@goffi.org>
parents: 4075
diff changeset
33 # logging is initialised by libervia.baceknd.core.log_config via the Twisted
84f6bee6440d installation: moved from `setup.py` to `pyproject.toml`:
Goffi <goffi@goffi.org>
parents: 4075
diff changeset
34 # plugin, nothing to do here
3281
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
35 self._initialLog()
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
36
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
37 def stop(self):
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
38 pass
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
39
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
40
3281
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
41 class Launcher:
4194
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
42 APP_NAME = C.APP_NAME
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
43 APP_NAME_FILE = C.APP_NAME_FILE
3281
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
44
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
45 @property
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
46 def NOT_RUNNING_MSG(self):
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
47 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
48
3284
751d8fa45ced core (launcher): fixed launching of twistd when no subparsed is specified
Goffi <goffi@goffi.org>
parents: 3282
diff changeset
49 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
50 """Command launched by default"""
751d8fa45ced core (launcher): fixed launching of twistd when no subparsed is specified
Goffi <goffi@goffi.org>
parents: 3282
diff changeset
51 args.extra_args = []
751d8fa45ced core (launcher): fixed launching of twistd when no subparsed is specified
Goffi <goffi@goffi.org>
parents: 3282
diff changeset
52 self.cmd_background(args)
751d8fa45ced core (launcher): fixed launching of twistd when no subparsed is specified
Goffi <goffi@goffi.org>
parents: 3282
diff changeset
53
3281
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
54 def cmd_background(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)
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_foreground(self, args):
4194
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
58 self.run_twistd(args, twistd_opts=["--nodaemon"])
3281
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
59
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
60 def cmd_debug(self, args):
4194
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
61 self.run_twistd(args, twistd_opts=["--debug"])
463
ac568832a71a fixed lauching script
Goffi <goffi@goffi.org>
parents: 369
diff changeset
62
3281
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
63 def cmd_stop(self, args):
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
64 import signal
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
65 import time
4194
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
66
3281
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
67 config = self.get_config()
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
68 pid_file = self.get_pid_file(config)
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
69 if not pid_file.is_file():
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
70 print(self.NOT_RUNNING_MSG)
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
71 sys.exit(0)
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
72 try:
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
73 pid = int(pid_file.read_text())
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
74 except Exception as e:
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
75 print(f"Can't read PID file at {pid_file}: {e}")
4075
47401850dec6 refactoring: rename `libervia.frontends.jp` to `libervia.cli`
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
76 # we use the same exit code as DATA_ERROR in CLI frontend
3281
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
77 sys.exit(17)
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
78 print(f"Terminating {self.APP_NAME}…")
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
79 os.kill(pid, signal.SIGTERM)
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
80 kill_started = time.time()
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
81 state = "init"
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
82 import errno
4194
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
83
3281
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:
4194
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
91 print(
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
92 f"Can't kill {self.APP_NAME}, the process is owned by an other user",
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
93 file=sys.stderr,
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
94 )
3281
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
95 sys.exit(18)
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
96 else:
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
97 raise e
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
98 time.sleep(0.2)
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
99 now = time.time()
4194
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
100 if state == "init" and now - kill_started > 5:
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
101 if state == "init":
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
102 state = "waiting"
3281
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
103 print(f"Still waiting for {self.APP_NAME} to be terminated…")
4194
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
104 elif state == "waiting" and now - kill_started > 10:
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
105 state == "killing"
3416
d7cfb031e41f core (launcher): exit code can now specified
Goffi <goffi@goffi.org>
parents: 3284
diff changeset
106 print("Waiting for too long, we kill the process")
3281
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
107 os.kill(pid, signal.SIGKILL)
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
108 sys.exit(1)
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
109
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
110 sys.exit(0)
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
111
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
112 def cmd_status(self, args):
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
113 config = self.get_config()
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
114 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
115 if pid_file.is_file():
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
116 import errno
4194
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
117
3281
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
118 try:
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
119 pid = int(pid_file.read_text())
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
120 except Exception as e:
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
121 print(f"Can't read PID file at {pid_file}: {e}")
4075
47401850dec6 refactoring: rename `libervia.frontends.jp` to `libervia.cli`
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
122 # we use the same exit code as DATA_ERROR in CLI frontend
3281
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
123 sys.exit(17)
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
124 # 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
125 # 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
126 try:
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
127 os.kill(pid, 0)
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
128 except OSError as e:
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
129 if e.errno == errno.ESRCH:
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
130 running = False
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
131 elif e.errno == errno.EPERM:
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
132 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
133 running = True
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 running = True
931
3b30e9f83d88 misc: sat stop would not kill all sat instances anymore
souliane <souliane@mailoo.org>
parents: 930
diff changeset
136
3281
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
137 if running:
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
138 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
139 sys.exit(0)
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
140 else:
4194
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
141 print(
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
142 f"{self.NOT_RUNNING_MSG}, but a pid file is present (bad exit ?): {pid_file}"
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
143 )
3281
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
144 sys.exit(2)
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
145 else:
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
146 print(self.NOT_RUNNING_MSG)
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
147 sys.exit(1)
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 def parse_args(self):
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
150 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
151 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
152 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
153 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
154
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
155 bg_parser = subparsers.add_parser(
4194
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
156 "background",
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
157 aliases=["bg"],
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
158 help=f"run {self.APP_NAME} backend in background (as a daemon)",
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
159 )
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
160 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
161 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
162
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
163 fg_parser = subparsers.add_parser(
4194
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
164 "foreground",
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
165 aliases=["fg"],
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
166 help=f"run {self.APP_NAME} backend in foreground",
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
167 )
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
168 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
169 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
170
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
171 dbg_parser = subparsers.add_parser(
4194
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
172 "debug", aliases=["dbg"], help=f"run {self.APP_NAME} backend in debug mode"
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
173 )
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
174 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
175 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
176
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
177 stop_parser = subparsers.add_parser(
4194
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
178 "stop", help=f"stop running {self.APP_NAME} backend"
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
179 )
3281
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
180 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
181
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
182 status_parser = subparsers.add_parser(
4194
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
183 "status", help=f"indicate if {self.APP_NAME} backend is running"
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
184 )
3281
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
185 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
186
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
187 return parser.parse_args()
931
3b30e9f83d88 misc: sat stop would not kill all sat instances anymore
souliane <souliane@mailoo.org>
parents: 930
diff changeset
188
3281
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
189 def get_config(self):
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
190 config = ConfigParser(defaults=C.DEFAULT_CONFIG)
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
191 try:
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
192 config.read(C.CONFIG_FILES)
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
193 except Exception as e:
4194
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
194 print(rf"/!\ Can't read main config! {e}")
3281
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
195 sys.exit(1)
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
196 return config
225
fd9b7834d98a distutils installation script, draft
Goffi <goffi@goffi.org>
parents: 223
diff changeset
197
3281
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
198 def get_pid_file(self, config):
4194
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
199 pid_dir = Path(config.get("DEFAULT", "pid_dir")).expanduser()
4202
b26339343076 core: use a user specific directory for PID file:
Goffi <goffi@goffi.org>
parents: 4194
diff changeset
200 pid_dir.mkdir(parents=True, exist_ok=True)
3281
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
201 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
202
4194
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
203 def wait_for_service(
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
204 self,
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
205 service_host: str,
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
206 service_port: int,
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
207 timeout: int,
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
208 service_name: str
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
209 ) -> None:
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
210 """Waits for a network service to become available.
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
211
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
212 @param service_host: The hostname or IP address of the service.
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
213 @param service_port: The port number of the service.
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
214 @param timeout: The maximum number of seconds to wait for the service.
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
215 @param service_name: The name of the service.
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
216
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
217 @raise TimeoutError: If the service is not available within the specified timeout.
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
218 """
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
219 import socket
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
220 import time
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
221
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
222 start_time = time.time()
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
223 wait_interval = 5
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
224
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
225 while True:
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
226 with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
227 sock.settimeout(1)
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
228 try:
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
229 sock.connect((service_host, service_port))
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
230 return
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
231 except socket.error:
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
232 elapsed_time = time.time() - start_time
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
233 if elapsed_time % wait_interval < 1:
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
234 print(f"Waiting for {service_name}…")
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
235 if elapsed_time > timeout:
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
236 raise TimeoutError(
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
237 f"{service_name} on {service_host}:{service_port} not "
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
238 f"available after {timeout} seconds."
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
239 )
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
240 time.sleep(1)
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
241
3281
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
242 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
243 """Run twistd settings options with args"""
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
244 from twisted.python.runtime import platformType
4194
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
245
3281
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
246 if platformType == "win32":
4194
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
247 from twisted.scripts._twistw import (
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
248 ServerOptions,
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
249 WindowsApplicationRunner as app_runner,
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
250 )
3281
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
251 else:
4194
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
252 from twisted.scripts._twistd_unix import (
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
253 ServerOptions,
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
254 UnixApplicationRunner as app_runner,
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
255 )
225
fd9b7834d98a distutils installation script, draft
Goffi <goffi@goffi.org>
parents: 223
diff changeset
256
4071
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 3480
diff changeset
257 app_runner.loggerFactory = LiberviaLogger
3281
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
258 server_options = ServerOptions()
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
259 config = self.get_config()
4194
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
260
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
261 # wait for a service (e.g. XMPP server)
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
262 wait_for_service_value = config.get(
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
263 "DEFAULT", "init_wait_for_service", fallback=None
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
264 )
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
265
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
266 if wait_for_service_value is not None:
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
267 try:
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
268 # Syntax: [ipv6_address]:port[:timeout][:service_name]
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
269 # or hostname:port[:timeout][:service_name]
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
270 parts = wait_for_service_value.split(":")
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
271 if parts[0] and parts[0][0] == "[" and parts[0][-1] == "]":
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
272 # IPv6 address
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
273 host = parts[0][1:-1]
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
274 else:
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
275 # Hostname or IPv4
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
276 host = parts[0]
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
277
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
278 port = int(parts[1])
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
279
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
280 # Defaults
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
281 timeout = 60
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
282 service_name = "service"
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
283
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
284 if len(parts) > 2:
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
285 timeout_part = parts[2]
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
286 # Check if timeout is skipped (double colon for service_name)
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
287 if timeout_part:
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
288 timeout = int(timeout_part)
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
289 if len(parts) > 3:
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
290 service_name = parts[3]
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
291 except (ValueError, IndexError):
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
292 raise ValueError(
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
293 f'Invalid "init_wait_for_service" value: {wait_for_service_value!r}'
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
294 )
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
295 else:
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
296 self.wait_for_service(host, port, timeout, service_name)
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
297
3281
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
298 pid_file = self.get_pid_file(config)
4194
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
299 log_dir = Path(config.get("DEFAULT", "log_dir")).expanduser()
3281
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
300 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
301 server_opts = [
4194
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
302 "--no_save",
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
303 "--pidfile",
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
304 str(pid_file),
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
305 "--logfile",
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
306 str(log_file),
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
307 ]
3281
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
308 if twistd_opts is not None:
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
309 server_opts.extend(twistd_opts)
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
310 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
311 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
312 try:
4194
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
313 args.extra_args.remove("--")
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
314 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
315 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
316 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
317 try:
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
318 server_options.parseOptions(server_opts)
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
319 except usage.error as ue:
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
320 print(server_options)
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
321 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
322 sys.exit(1)
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
323 else:
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
324 runner = app_runner(server_options)
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
325 runner.run()
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
326 if runner._exitSignal is not None:
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
327 app._exitWithSignal(runner._exitSignal)
3416
d7cfb031e41f core (launcher): exit code can now specified
Goffi <goffi@goffi.org>
parents: 3284
diff changeset
328 try:
d7cfb031e41f core (launcher): exit code can now specified
Goffi <goffi@goffi.org>
parents: 3284
diff changeset
329 sys.exit(app._exitCode)
d7cfb031e41f core (launcher): exit code can now specified
Goffi <goffi@goffi.org>
parents: 3284
diff changeset
330 except AttributeError:
d7cfb031e41f core (launcher): exit code can now specified
Goffi <goffi@goffi.org>
parents: 3284
diff changeset
331 pass
226
d8bb72f00eec distutils install: fixed plugin import and log file path
Goffi <goffi@goffi.org>
parents: 225
diff changeset
332
3281
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
333 @classmethod
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
334 def run(cls):
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
335 args = cls().parse_args()
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
336 args.cmd(args)
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
337
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
338
4194
3dbaf179c50d core (launcher): new `wait_for_service` option:
Goffi <goffi@goffi.org>
parents: 4081
diff changeset
339 if __name__ == "__main__":
3281
a3639d6d9643 core: replaced `sat` shell script by a python script:
Goffi <goffi@goffi.org>
parents: 3263
diff changeset
340 Launcher.run()