~ubuntu-branches/ubuntu/saucy/maas/saucy-updates

« back to all changes in this revision

Viewing changes to src/provisioningserver/start_cluster_controller.py

  • Committer: Package Import Robot
  • Author(s): Andres Rodriguez, Julian Edwards, Raphaël Badin, Jeroen Vermeulen, Andres Rodriguez, Robie Basak, Scott Moser, Diogo Matsubara
  • Date: 2012-10-08 13:10:23 UTC
  • mfrom: (1.1.18)
  • mto: This revision was merged to the branch mainline in revision 21.
  • Revision ID: package-import@ubuntu.com-20121008131023-68glibh0pbqrgtfq
Tags: 0.1+bzr1223+dfsg-0ubuntu1
* New upstream release. (LP: #1062518)

[ Julian Edwards ]
* Split packaging of 'maas' into maas-{region,cluster}-controller
  - debian/control: Update accordingly.
  - debian/*.install: Move files accordingly
  - debian/*.{postinst,postrm,preinst}: Move files accordingly.
* Ensure isc-dhcp-server is disabled when installing maas-dhcp.
* Ensure maas-dns creates the maas user before trying to chown files.
* Make maas-cluster-controller autoconfigure itself when upgrading from the
  old maas package. (LP: #1059416)
* Add missing prerm file for maas-cluster-controller so that .pyc files
  are cleaned up. (LP: #1059973)

[ Raphaël Badin ]
* Install maas_local_celeryconfig.py in /etc/maas and symlink to
  /usr/share/maas.
* debian/maas.postinst: Create rabbitmq celery user/vhost.
* debian/maas.postinst: Update BROKER_URL in maas_local_celeryconfig.py.
* Use maas_local_celeryconfig_cluster.py as the local celery
  configuration file for the cluster worker.
* debian/maas-region-controller.maas-region-celery.upstart: Add region
  worker upstart script.
* Rename cluster worker upstart script into
  maas-cluster-controller.maas-clluster-celery.upstart.
* maas-cluster-controller.maas-celery.upstart: use "celeryconfig_cluster"
  as the Celery config module.
* debian/maas-common.install: Install celeryconfig_common.py.
* debian/maas-cluster-controller.install: Install celeryconfig_cluster.py.
* debian/maas-region-controller.install: Install celeryconfig.py.
* Split celery config into cluster and region configs.
* Add region celeryd upstart config.
* Define CELERY_CONFIG_MODULE in
  maas-cluster-controller.maas-cluster-celery.upstart

[ Jeroen Vermeulen ]
* Make maas_local_celery_config.py non-world readable.
* Make maas_local_celeryconfig_cluster.py non-world readable.
* Set root:maas ownership of local cluster config only *after*
  the maas user/group have been created

[ Andres Rodriguez ]
* debian/maas.postinst:
  - Always restart apache2.
  - Handle upgrades for new upstream release.
  - Handle upgrades for celery rabbitmq worker.
* Add binary package to install client tool.
  - debian/extras/maas-cli: Add binary.
  - debian/maas-cli.install: Add. Install maascli and apiclient.
  - debian/control: Add binary package.
* debian/control:
  - Depends on freeipmi-tools instead of ipmitool.
  - Conflicts/Replaces on maas for python-maas-client.
  - Depends on python-netifaces, python-lxml.
* Add python-maas-client binary package:
  - debian/python-maas-client.install: Add. Install 'apiclient' python module.
  - debian/control: Add package. python-django-maas and maas-cli now
    Depend on it.
* debian/rules: Install maas-dhcp-server upstart job.
* debian/maas.postrm: Remove celery worker rabbitmq user and host.
* debian/extras/99-maas-sudoers: Add for maas-dhcp-server upstart job
  instead of isc-dhcp-server (LP: #1055951)
* debian/maas-region-controller.postinst: Cleanup upgrade rules.
* debian/maas-cluster-controller.postinst: Fix 'local' usage.
* debian/maas-common.install: Install celeryconfig in appropriate location.
* debian/maas-cluster-controller.postrm: Add and delete maas user.
* debian/maas-dhcp.postinst: Stop isc-dhcp-server not isc_dhcp_server.
* debian/maas-region-controller.postinst:
  - Always update passwords on upgrade. No longer check versioning. (LP: #1060094).
  - Add MAAS server to allowed mirror in squid-deb-proxy
  - Source dbconfig conf file for maas-region-controller on upgrade because
    it writes a new config file and no longer preservers the previous password.
* debian/maas-cluster-controller.maas-cluster-celery.upstart: Remove set{uid/gid}.
* debian/maas-region-controller.install: Install maas-import-squashfs
* Handle removal of non existant files (LP: #1059556):
  - debian/maintscript: Added to handle removal of conffiles.
  - debian/control: Add Pre-depends and bump debhelper version.
* update po files for the templates.
* debian/extras/99-maas: Install in usr/share/maas/conf and symlink to the
  appropriate etc dir.
* debian/maas-cluster-controller.config: Source debconf at the beginning of the
  script. (LP: #1063857)
* debian/patches/99-temporary-fix-constraints.patch: Fix constraints maaping
  when deploying with juju. Temporary until it gets merged upstream

[ Robie Basak ]
* Add maas-cluster-controller dependency on uuid-runtime, needed for uuidgen
  by postinst.

[ Scott Moser ]
* debian/maas-dhcp.{install,apparmor,postrm} install apparmor profile into
  /etc/apparmor.d/dhcpd.d (LP: #1049177), and update apparmor profile for
  /usr/sbin/dhcpd on install/remove
* get the ip address for the dhcp server in config

[ Diogo Matsubara ]
* Add initial tests to be run by autotests:
  - debian/control: XS-Testsuite: autopkgtest
  - debian/tests/control: Add
  - debian/tests/maas-package-test: Add
  - tests/maas-integration.py: Add

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2012 Canonical Ltd.  This software is licensed under the
 
2
# GNU Affero General Public License version 3 (see the file LICENSE).
 
3
 
 
4
"""Command: start the cluster controller."""
 
5
 
 
6
from __future__ import (
 
7
    absolute_import,
 
8
    print_function,
 
9
    unicode_literals,
 
10
    )
 
11
 
 
12
__metaclass__ = type
 
13
__all__ = [
 
14
    'add_arguments',
 
15
    'run',
 
16
    ]
 
17
 
 
18
from grp import getgrnam
 
19
import httplib
 
20
import json
 
21
import os
 
22
from pwd import getpwnam
 
23
from time import sleep
 
24
from urllib2 import (
 
25
    HTTPError,
 
26
    URLError,
 
27
    )
 
28
 
 
29
from apiclient.maas_client import (
 
30
    MAASClient,
 
31
    MAASDispatcher,
 
32
    NoAuth,
 
33
    )
 
34
from celery.app import app_or_default
 
35
from provisioningserver.logging import task_logger
 
36
from provisioningserver.network import discover_networks
 
37
 
 
38
 
 
39
class ClusterControllerRejected(Exception):
 
40
    """Request to become a cluster controller has been rejected."""
 
41
 
 
42
 
 
43
def add_arguments(parser):
 
44
    """For use by :class:`MainScript`."""
 
45
    parser.add_argument(
 
46
        'server_url', metavar='URL', help="URL to the MAAS region controller.")
 
47
    parser.add_argument(
 
48
        '--user', '-u', metavar='USER', default='maas',
 
49
        help="System user identity that should run the cluster controller.")
 
50
    parser.add_argument(
 
51
        '--group', '-g', metavar='GROUP', default='maas',
 
52
        help="System group that should run the cluster controller.")
 
53
 
 
54
 
 
55
def log_error(exception):
 
56
    task_logger.info(
 
57
        "Could not register with region controller: %s."
 
58
        % exception.reason)
 
59
 
 
60
 
 
61
def make_anonymous_api_client(server_url):
 
62
    """Create an unauthenticated API client."""
 
63
    return MAASClient(NoAuth(), MAASDispatcher(), server_url)
 
64
 
 
65
 
 
66
def get_cluster_uuid():
 
67
    """Read this cluster's UUID from the config."""
 
68
    return app_or_default().conf.CLUSTER_UUID
 
69
 
 
70
 
 
71
def get_maas_celery_log():
 
72
    """Read location for MAAS Celery log file from the config."""
 
73
    return app_or_default().conf.MAAS_CELERY_LOG
 
74
 
 
75
 
 
76
def get_maas_celerybeat_db():
 
77
    """Read location for MAAS Celery schedule file from the config."""
 
78
    return app_or_default().conf.MAAS_CLUSTER_CELERY_DB
 
79
 
 
80
 
 
81
def register(server_url):
 
82
    """Request Rabbit connection details from the domain controller.
 
83
 
 
84
    Offers this machine to the region controller as a potential cluster
 
85
    controller.
 
86
 
 
87
    :param server_url: URL to the region controller's MAAS API.
 
88
    :return: A dict of connection details if this cluster controller has been
 
89
        accepted, or `None` if there is no definite response yet.  If there
 
90
        is no definite response, retry this call later.
 
91
    :raise ClusterControllerRejected: if this system has been rejected as a
 
92
        cluster controller.
 
93
    """
 
94
    known_responses = {httplib.OK, httplib.FORBIDDEN, httplib.ACCEPTED}
 
95
 
 
96
    interfaces = json.dumps(discover_networks())
 
97
    client = make_anonymous_api_client(server_url)
 
98
    cluster_uuid = get_cluster_uuid()
 
99
    try:
 
100
        response = client.post(
 
101
            'api/1.0/nodegroups/', 'register',
 
102
            interfaces=interfaces, uuid=cluster_uuid)
 
103
    except HTTPError as e:
 
104
        status_code = e.code
 
105
        if e.code not in known_responses:
 
106
            log_error(e)
 
107
            # Unknown error.  Keep trying.
 
108
            return None
 
109
    except URLError as e:
 
110
        log_error(e)
 
111
        # Unknown error.  Keep trying.
 
112
        return None
 
113
    else:
 
114
        status_code = response.getcode()
 
115
 
 
116
    if status_code == httplib.OK:
 
117
        # Our application has been approved.  Proceed.
 
118
        return json.loads(response.read())
 
119
    elif status_code == httplib.ACCEPTED:
 
120
        # Our application is still waiting for approval.  Keep trying.
 
121
        return None
 
122
    elif status_code == httplib.FORBIDDEN:
 
123
        # Our application has been rejected.  Give up.
 
124
        raise ClusterControllerRejected(
 
125
            "This system has been rejected as a cluster controller.")
 
126
    else:
 
127
        raise AssertionError("Unexpected return code: %r" % status_code)
 
128
 
 
129
 
 
130
def start_celery(connection_details, user, group):
 
131
    broker_url = connection_details['BROKER_URL']
 
132
    uid = getpwnam(user).pw_uid
 
133
    gid = getgrnam(group).gr_gid
 
134
 
 
135
    # Copy environment, but also tell celeryd what broker to listen to.
 
136
    env = dict(os.environ, CELERY_BROKER_URL=broker_url)
 
137
 
 
138
    command = [
 
139
        'celeryd',
 
140
        '--logfile=%s' % get_maas_celery_log(),
 
141
        '--schedule=%s' % get_maas_celerybeat_db(),
 
142
        '--loglevel=INFO',
 
143
        '--beat',
 
144
        '-Q', get_cluster_uuid(),
 
145
        ]
 
146
 
 
147
    # Change gid first, just in case changing the uid might deprive
 
148
    # us of the privileges required to setgid.
 
149
    os.setgid(gid)
 
150
    os.setuid(uid)
 
151
 
 
152
    os.execvpe(command[0], command, env=env)
 
153
 
 
154
 
 
155
def request_refresh(server_url):
 
156
    client = make_anonymous_api_client(server_url)
 
157
    try:
 
158
        client.post('api/1.0/nodegroups/', 'refresh_workers')
 
159
    except URLError as e:
 
160
        task_logger.warn(
 
161
            "Could not request secrets from region controller: %s"
 
162
            % e.reason)
 
163
 
 
164
 
 
165
def start_up(server_url, connection_details, user, group):
 
166
    """We've been accepted as a cluster controller; start doing the job.
 
167
 
 
168
    This starts up celeryd, listening to the broker that the region
 
169
    controller pointed us to, and on the appropriate queue.
 
170
    """
 
171
    # Get the region controller to send out credentials.  If it arrives
 
172
    # before celeryd has started up, we should find the message waiting
 
173
    # in our queue.  Even if we're new and the queue did not exist yet,
 
174
    # the arriving task will create the queue.
 
175
    request_refresh(server_url)
 
176
    start_celery(connection_details, user=user, group=group)
 
177
 
 
178
 
 
179
def run(args):
 
180
    """Start the cluster controller.
 
181
 
 
182
    If this system is still awaiting approval as a cluster controller, this
 
183
    command will keep looping until it gets a definite answer.
 
184
    """
 
185
    connection_details = register(args.server_url)
 
186
    while connection_details is None:
 
187
        sleep(60)
 
188
        connection_details = register(args.server_url)
 
189
    start_up(
 
190
        args.server_url, connection_details, user=args.user, group=args.group)