2014-11-13 14:39:11 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
2016-01-07 04:26:29 +00:00
|
|
|
# Copyright 2014-2016 OpenMarket Ltd
|
2014-11-13 14:39:11 +00:00
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
2016-06-21 15:38:05 +00:00
|
|
|
import argparse
|
|
|
|
import collections
|
|
|
|
import glob
|
2014-11-13 14:39:11 +00:00
|
|
|
import os
|
2015-09-24 21:50:20 +00:00
|
|
|
import os.path
|
2014-11-13 14:39:11 +00:00
|
|
|
import signal
|
2016-06-21 15:38:05 +00:00
|
|
|
import subprocess
|
|
|
|
import sys
|
2015-04-30 16:21:21 +00:00
|
|
|
import yaml
|
2014-11-13 14:39:11 +00:00
|
|
|
|
2016-10-08 21:55:20 +00:00
|
|
|
SYNAPSE = [sys.executable, "-B", "-m", "synapse.app.homeserver"]
|
2014-11-13 14:39:11 +00:00
|
|
|
|
2014-11-20 17:26:36 +00:00
|
|
|
GREEN = "\x1b[1;32m"
|
2015-09-22 11:57:40 +00:00
|
|
|
RED = "\x1b[1;31m"
|
2014-11-20 17:26:36 +00:00
|
|
|
NORMAL = "\x1b[m"
|
2014-11-13 14:39:11 +00:00
|
|
|
|
|
|
|
|
2016-06-21 15:38:05 +00:00
|
|
|
def write(message, colour=NORMAL, stream=sys.stdout):
|
|
|
|
if colour == NORMAL:
|
|
|
|
stream.write(message + "\n")
|
|
|
|
else:
|
|
|
|
stream.write(colour + message + NORMAL + "\n")
|
|
|
|
|
|
|
|
|
2015-09-24 21:50:20 +00:00
|
|
|
def start(configfile):
|
2016-06-21 15:38:05 +00:00
|
|
|
write("Starting ...")
|
2014-11-13 14:39:11 +00:00
|
|
|
args = SYNAPSE
|
2015-09-24 21:50:20 +00:00
|
|
|
args.extend(["--daemonize", "-c", configfile])
|
2015-10-01 08:21:27 +00:00
|
|
|
|
2015-09-22 11:57:40 +00:00
|
|
|
try:
|
2015-10-01 08:21:27 +00:00
|
|
|
subprocess.check_call(args)
|
2016-06-21 15:38:05 +00:00
|
|
|
write("started synapse.app.homeserver(%r)" % (configfile,), colour=GREEN)
|
|
|
|
except subprocess.CalledProcessError as e:
|
|
|
|
write(
|
|
|
|
"error starting (exit code: %d); see above for logs" % e.returncode,
|
|
|
|
colour=RED,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def start_worker(app, configfile, worker_configfile):
|
|
|
|
args = [
|
|
|
|
"python", "-B",
|
|
|
|
"-m", app,
|
|
|
|
"-c", configfile,
|
|
|
|
"-c", worker_configfile
|
|
|
|
]
|
|
|
|
|
|
|
|
try:
|
|
|
|
subprocess.check_call(args)
|
|
|
|
write("started %s(%r)" % (app, worker_configfile), colour=GREEN)
|
2015-09-22 11:57:40 +00:00
|
|
|
except subprocess.CalledProcessError as e:
|
2016-06-21 15:38:05 +00:00
|
|
|
write(
|
|
|
|
"error starting %s(%r) (exit code: %d); see above for logs" % (
|
|
|
|
app, worker_configfile, e.returncode,
|
|
|
|
),
|
|
|
|
colour=RED,
|
2015-09-22 11:57:40 +00:00
|
|
|
)
|
2014-11-13 14:39:11 +00:00
|
|
|
|
2014-11-20 17:26:36 +00:00
|
|
|
|
2016-06-21 15:38:05 +00:00
|
|
|
def stop(pidfile, app):
|
2015-09-24 21:50:20 +00:00
|
|
|
if os.path.exists(pidfile):
|
|
|
|
pid = int(open(pidfile).read())
|
2014-11-13 14:39:11 +00:00
|
|
|
os.kill(pid, signal.SIGTERM)
|
2016-06-21 15:38:05 +00:00
|
|
|
write("stopped %s" % (app,), colour=GREEN)
|
|
|
|
|
|
|
|
|
|
|
|
Worker = collections.namedtuple("Worker", [
|
|
|
|
"app", "configfile", "pidfile", "cache_factor"
|
|
|
|
])
|
2014-11-13 14:39:11 +00:00
|
|
|
|
2014-11-20 17:26:36 +00:00
|
|
|
|
2014-11-13 14:39:11 +00:00
|
|
|
def main():
|
2016-06-21 15:38:05 +00:00
|
|
|
|
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
|
|
|
|
parser.add_argument(
|
|
|
|
"action",
|
|
|
|
choices=["start", "stop", "restart"],
|
|
|
|
help="whether to start, stop or restart the synapse",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"configfile",
|
|
|
|
nargs="?",
|
|
|
|
default="homeserver.yaml",
|
|
|
|
help="the homeserver config file, defaults to homserver.yaml",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"-w", "--worker",
|
|
|
|
metavar="WORKERCONFIG",
|
|
|
|
help="start or stop a single worker",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"-a", "--all-processes",
|
|
|
|
metavar="WORKERCONFIGDIR",
|
|
|
|
help="start or stop all the workers in the given directory"
|
|
|
|
" and the main synapse process",
|
|
|
|
)
|
|
|
|
|
|
|
|
options = parser.parse_args()
|
|
|
|
|
|
|
|
if options.worker and options.all_processes:
|
|
|
|
write(
|
|
|
|
'Cannot use "--worker" with "--all-processes"',
|
|
|
|
stream=sys.stderr
|
|
|
|
)
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
configfile = options.configfile
|
2015-09-24 21:50:20 +00:00
|
|
|
|
|
|
|
if not os.path.exists(configfile):
|
2016-06-21 15:38:05 +00:00
|
|
|
write(
|
2015-09-24 21:50:20 +00:00
|
|
|
"No config file found\n"
|
|
|
|
"To generate a config file, run '%s -c %s --generate-config"
|
|
|
|
" --server-name=<server name>'\n" % (
|
2016-06-21 15:38:05 +00:00
|
|
|
" ".join(SYNAPSE), options.configfile
|
|
|
|
),
|
|
|
|
stream=sys.stderr,
|
2015-09-24 21:50:20 +00:00
|
|
|
)
|
|
|
|
sys.exit(1)
|
|
|
|
|
2016-06-21 15:38:05 +00:00
|
|
|
with open(configfile) as stream:
|
|
|
|
config = yaml.load(stream)
|
|
|
|
|
2015-09-24 21:50:20 +00:00
|
|
|
pidfile = config["pid_file"]
|
2016-06-21 15:38:05 +00:00
|
|
|
cache_factor = config.get("synctl_cache_factor")
|
|
|
|
start_stop_synapse = True
|
2016-05-16 16:01:57 +00:00
|
|
|
|
|
|
|
if cache_factor:
|
2016-05-16 16:21:30 +00:00
|
|
|
os.environ["SYNAPSE_CACHE_FACTOR"] = str(cache_factor)
|
2015-09-24 21:50:20 +00:00
|
|
|
|
2016-06-21 15:38:05 +00:00
|
|
|
worker_configfiles = []
|
|
|
|
if options.worker:
|
|
|
|
start_stop_synapse = False
|
|
|
|
worker_configfile = options.worker
|
|
|
|
if not os.path.exists(worker_configfile):
|
|
|
|
write(
|
|
|
|
"No worker config found at %r" % (worker_configfile,),
|
|
|
|
stream=sys.stderr,
|
|
|
|
)
|
|
|
|
sys.exit(1)
|
|
|
|
worker_configfiles.append(worker_configfile)
|
|
|
|
|
|
|
|
if options.all_processes:
|
|
|
|
worker_configdir = options.all_processes
|
|
|
|
if not os.path.isdir(worker_configdir):
|
|
|
|
write(
|
|
|
|
"No worker config directory found at %r" % (worker_configdir,),
|
|
|
|
stream=sys.stderr,
|
|
|
|
)
|
|
|
|
sys.exit(1)
|
|
|
|
worker_configfiles.extend(sorted(glob.glob(
|
|
|
|
os.path.join(worker_configdir, "*.yaml")
|
|
|
|
)))
|
|
|
|
|
|
|
|
workers = []
|
|
|
|
for worker_configfile in worker_configfiles:
|
|
|
|
with open(worker_configfile) as stream:
|
|
|
|
worker_config = yaml.load(stream)
|
|
|
|
worker_app = worker_config["worker_app"]
|
|
|
|
worker_pidfile = worker_config["worker_pid_file"]
|
|
|
|
worker_daemonize = worker_config["worker_daemonize"]
|
2017-03-29 12:47:42 +00:00
|
|
|
assert worker_daemonize, "In config %r: expected '%s' to be True" % (
|
|
|
|
worker_configfile, "worker_daemonize")
|
2016-06-21 15:38:05 +00:00
|
|
|
worker_cache_factor = worker_config.get("synctl_cache_factor")
|
|
|
|
workers.append(Worker(
|
|
|
|
worker_app, worker_configfile, worker_pidfile, worker_cache_factor,
|
|
|
|
))
|
|
|
|
|
|
|
|
action = options.action
|
|
|
|
|
|
|
|
if action == "stop" or action == "restart":
|
|
|
|
for worker in workers:
|
|
|
|
stop(worker.pidfile, worker.app)
|
|
|
|
|
|
|
|
if start_stop_synapse:
|
|
|
|
stop(pidfile, "synapse.app.homeserver")
|
|
|
|
|
|
|
|
# TODO: Wait for synapse to actually shutdown before starting it again
|
|
|
|
|
|
|
|
if action == "start" or action == "restart":
|
|
|
|
if start_stop_synapse:
|
|
|
|
start(configfile)
|
|
|
|
|
|
|
|
for worker in workers:
|
|
|
|
if worker.cache_factor:
|
|
|
|
os.environ["SYNAPSE_CACHE_FACTOR"] = str(worker.cache_factor)
|
|
|
|
|
|
|
|
start_worker(worker.app, configfile, worker.configfile)
|
|
|
|
|
|
|
|
if cache_factor:
|
|
|
|
os.environ["SYNAPSE_CACHE_FACTOR"] = str(cache_factor)
|
|
|
|
else:
|
|
|
|
os.environ.pop("SYNAPSE_CACHE_FACTOR", None)
|
2014-11-13 14:39:11 +00:00
|
|
|
|
2014-11-20 17:26:36 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2014-11-13 14:39:11 +00:00
|
|
|
main()
|