~ubuntu-branches/ubuntu/raring/maas/raring-updates

« back to all changes in this revision

Viewing changes to services/run

  • Committer: Package Import Robot
  • Author(s): Andres Rodriguez
  • Date: 2012-07-03 17:42:37 UTC
  • mfrom: (1.1.13)
  • Revision ID: package-import@ubuntu.com-20120703174237-p8l0keuuznfg721k
Tags: 0.1+bzr709+dfsg-0ubuntu1
* New Upstream release
* debian/control:
  - Depends on python-celery, python-tempita, libjs-yui3-{full,min},
    libjs-raphael
* debian/maas.install:
  - Install apiclient, celeryconfig.py, maas-import-pxe-files, preseeds_v2.
  - Update to install various files from chroot, rather tha manually copy
    them from the source.
* debian/maas.links: symlink celeryconfig.py
* debian/maas.maas-celery.upstart: Add job.
* debian/rules:
  - Install celery upstart job.
  - Do not install jslibs as packages are now used.
  - Drop copying of maas_local_settings_sample.py as source now ships
    a maas_local_settings.py
* debian/patches:
  - 04-maas-http-fix.patch: Drop. Merged upstream.
  - 01-fix-database-settings.patch: Refreshed.
  - 99_enums_js.patch: Added until creation of enum.js / build process
    is fixed.
* debian/maas.postinst: Update bzr version to correctly handle upgrades.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python2.7
 
2
# -*- mode: python -*-
 
3
# Copyright 2012 Canonical Ltd.  This software is licensed under the
 
4
# GNU Affero General Public License version 3 (see the file LICENSE).
 
5
 
 
6
"""Script to start services, tail logs, and stop services at the end.
 
7
 
 
8
Service names should be provided on the command-line.
 
9
 
 
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:
 
13
 
 
14
  $ services/run pserv txlongpoll +webapp
 
15
 
 
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.
 
20
 
 
21
If no service has a preceding "+" then all the services named are started and
 
22
their logs tailed.
 
23
"""
 
24
 
 
25
from __future__ import (
 
26
    absolute_import,
 
27
    print_function,
 
28
    unicode_literals,
 
29
    )
 
30
 
 
31
__metaclass__ = type
 
32
 
 
33
from contextlib import contextmanager
 
34
from os.path import join
 
35
from subprocess import (
 
36
    CalledProcessError,
 
37
    check_call,
 
38
    )
 
39
from sys import (
 
40
    argv,
 
41
    stderr,
 
42
    )
 
43
 
 
44
 
 
45
@contextmanager
 
46
def service(services):
 
47
    started = []
 
48
    try:
 
49
        for service in services:
 
50
            control(service, "start")
 
51
            started.append(service)
 
52
        yield
 
53
    finally:
 
54
        for service in started:
 
55
            control(service, "stop")
 
56
 
 
57
 
 
58
def control(service, action):
 
59
    print("--> %s `%s`" % (action.capitalize(), service), file=stderr)
 
60
    return check_call(
 
61
        ("make", "--no-print-directory",
 
62
         "services/%s/@%s" % (service, action)))
 
63
 
 
64
 
 
65
def tail_logs(services):
 
66
    command = ["tail", "--follow=name", "--retry", "--"]
 
67
    command.extend(
 
68
        join("logs", service, "current")
 
69
        for service in services)
 
70
    check_call(command)
 
71
 
 
72
 
 
73
if __name__ == "__main__":
 
74
    if len(argv) == 1:
 
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.")
 
82
    try:
 
83
        with service(services_bg):
 
84
            if service_fg:
 
85
                control(service_fg, "run")
 
86
            else:
 
87
                tail_logs(services_bg)
 
88
    except CalledProcessError, error:
 
89
        raise SystemExit(error.returncode)
 
90
    except KeyboardInterrupt:
 
91
        pass  # Ignore.