1
#!/usr/bin/env python2.7
3
# Copyright 2012 Canonical Ltd. This software is licensed under the
4
# GNU Affero General Public License version 3 (see the file LICENSE).
6
"""Script to start services, tail logs, and stop services at the end.
8
Service names should be provided on the command-line.
10
If a service name is preceded by a "+" (plus) it is started in the
11
foreground. For example, to start the `pserv` and `txlongpoll` services in the
12
background and `webapp` in the foreground:
14
$ services/run pserv txlongpoll +webapp
16
It does not matter if a service name appears twice in the list. If the service
17
name appears once with a preceding "+" and once without, the "+" one takes
18
precedence, and that service will be started in the foreground. Only one
19
service may be started in the foreground.
21
If no service has a preceding "+" then all the services named are started and
25
from __future__ import (
33
from contextlib import contextmanager
34
from os.path import join
35
from subprocess import (
46
def service(services):
49
for service in services:
50
control(service, "start")
51
started.append(service)
54
for service in started:
55
control(service, "stop")
58
def control(service, action):
59
print("--> %s `%s`" % (action.capitalize(), service), file=stderr)
61
("make", "--no-print-directory",
62
"services/%s/@%s" % (service, action)))
65
def tail_logs(services):
66
command = ["tail", "--follow=name", "--retry", "--"]
68
join("logs", service, "current")
69
for service in services)
73
if __name__ == "__main__":
75
raise SystemExit(__doc__)
76
services_bg = {name for name in argv[1:] if not name.startswith("+")}
77
services_fg = {name[1:] for name in argv[1:] if name.startswith("+")}
78
services_bg = services_bg - services_fg
79
service_fg = services_fg.pop() if services_fg else None
80
if len(services_fg) != 0:
81
raise SystemExit("Only zero or one foreground services permitted.")
83
with service(services_bg):
85
control(service_fg, "run")
87
tail_logs(services_bg)
88
except CalledProcessError, error:
89
raise SystemExit(error.returncode)
90
except KeyboardInterrupt: