This file is indexed.

/usr/lib/python2.7/dist-packages/maasserver/models/nodegroup.py is in python-django-maas 1.5.4+bzr2294-0ubuntu1.2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
# Copyright 2012-2014 Canonical Ltd.  This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).

"""Model definition for NodeGroup which models a collection of Nodes."""

from __future__ import (
    absolute_import,
    print_function,
    unicode_literals,
    )

str = None

__metaclass__ = type
__all__ = [
    'NodeGroup',
    'NODEGROUP_CLUSTER_NAME_TEMPLATE',
    ]


from django.db.models import (
    CharField,
    ForeignKey,
    IntegerField,
    Manager,
    )
from maasserver import DefaultMeta
from maasserver.enum import (
    NODEGROUP_STATUS,
    NODEGROUP_STATUS_CHOICES,
    NODEGROUPINTERFACE_MANAGEMENT,
    )
from maasserver.models.nodegroupinterface import NodeGroupInterface
from maasserver.models.timestampedmodel import TimestampedModel
from maasserver.refresh_worker import refresh_worker
from piston.models import (
    KEY_SIZE,
    Token,
    )
from provisioningserver.omshell import generate_omapi_key
from provisioningserver.tasks import (
    add_new_dhcp_host_map,
    add_seamicro15k,
    add_virsh,
    enlist_nodes_from_mscm,
    enlist_nodes_from_ucsm,
    import_boot_images,
    report_boot_images,
    )


class NodeGroupManager(Manager):
    """Manager for the NodeGroup class.

    Don't import or instantiate this directly; access as `<Class>.objects` on
    the model class it manages.
    """

    def new(self, name, uuid, ip, subnet_mask=None,
            broadcast_ip=None, router_ip=None, ip_range_low=None,
            ip_range_high=None, dhcp_key='', interface='',
            status=NODEGROUP_STATUS.DEFAULT,
            management=NODEGROUPINTERFACE_MANAGEMENT.DEFAULT,
            cluster_name=None, maas_url=''):
        """Create a :class:`NodeGroup` with the given parameters.

        This method will:
        - create the related NodeGroupInterface if `interface` is provided
        - generate API credentials for the nodegroup's worker to use.
        """
        dhcp_values = [
            interface,
            subnet_mask,
            router_ip,
            ip_range_low,
            ip_range_high,
            ]
        assert all(dhcp_values) or not any(dhcp_values), (
            "Provide all DHCP settings, or none at all. "
            "Only the broadcast address is optional.")

        if cluster_name is None:
            cluster_name = NODEGROUP_CLUSTER_NAME_TEMPLATE % {'uuid': uuid}
        nodegroup = NodeGroup(
            name=name, uuid=uuid, cluster_name=cluster_name, dhcp_key=dhcp_key,
            status=status, maas_url=maas_url)
        nodegroup.save()
        if interface != '':
            nginterface = NodeGroupInterface(
                nodegroup=nodegroup, ip=ip, subnet_mask=subnet_mask,
                broadcast_ip=broadcast_ip, router_ip=router_ip,
                interface=interface, ip_range_low=ip_range_low,
                ip_range_high=ip_range_high, management=management)
            nginterface.save()
        return nodegroup

    def ensure_master(self):
        """Obtain the master node group, creating it first if needed."""
        # Avoid circular imports.
        from maasserver.models import Node
        from maasserver.forms import DEFAULT_DNS_ZONE_NAME

        try:
            # Get the first created nodegroup if it exists.
            master = self.all().order_by('id')[0:1].get()
        except NodeGroup.DoesNotExist:
            # The master did not exist yet; create it on demand.
            master = self.new(
                DEFAULT_DNS_ZONE_NAME, 'master', '127.0.0.1',
                dhcp_key=generate_omapi_key(),
                status=NODEGROUP_STATUS.ACCEPTED)

            # If any legacy nodes were still not associated with a node
            # group, enroll them in the master node group.
            Node.objects.filter(nodegroup=None).update(nodegroup=master)

        return master

    def get_by_natural_key(self, uuid):
        """For Django, a node group's uuid is a natural key."""
        return self.get(uuid=uuid)

    def refresh_workers(self):
        """Send refresh tasks to all node-group workers."""
        for nodegroup in self.filter(status=NODEGROUP_STATUS.ACCEPTED):
            refresh_worker(nodegroup)

    def _mass_change_status(self, old_status, new_status):
        nodegroups = self.filter(status=old_status)
        nodegroups_count = nodegroups.count()
        # Change the nodegroups one by one in order to trigger the
        # post_save signals.
        for nodegroup in nodegroups:
            nodegroup.status = new_status
            nodegroup.save()
        return nodegroups_count

    def reject_all_pending(self):
        """Change the status of the 'PENDING' nodegroup to 'REJECTED."""
        return self._mass_change_status(
            NODEGROUP_STATUS.PENDING, NODEGROUP_STATUS.REJECTED)

    def accept_all_pending(self):
        """Change the status of the 'PENDING' nodegroup to 'ACCEPTED."""
        return self._mass_change_status(
            NODEGROUP_STATUS.PENDING, NODEGROUP_STATUS.ACCEPTED)

    def import_boot_images_accepted_clusters(self):
        """Import the boot images on all the accepted cluster controllers."""
        accepted_nodegroups = NodeGroup.objects.filter(
            status=NODEGROUP_STATUS.ACCEPTED)
        for nodegroup in accepted_nodegroups:
            nodegroup.import_boot_images()


NODEGROUP_CLUSTER_NAME_TEMPLATE = "Cluster %(uuid)s"


class NodeGroup(TimestampedModel):

    class Meta(DefaultMeta):
        """Needed for South to recognize this model."""

    objects = NodeGroupManager()

    cluster_name = CharField(
        max_length=100, unique=True, editable=True, blank=True, null=False)

    # A node group's name is also used for the group's DNS zone.
    name = CharField(
        max_length=80, unique=False, editable=True, blank=True, null=False)

    status = IntegerField(
        choices=NODEGROUP_STATUS_CHOICES, editable=True,
        default=NODEGROUP_STATUS.DEFAULT)

    # Credentials for the worker to access the API with.
    api_token = ForeignKey(Token, null=False, editable=False, unique=True)
    api_key = CharField(
        max_length=KEY_SIZE, null=False, blank=False, editable=False,
        unique=True)

    dhcp_key = CharField(
        blank=True, editable=False, max_length=255, default='')

    # Unique identifier of the worker.
    uuid = CharField(
        max_length=36, unique=True, null=False, blank=False, editable=True)

    # The URL where the cluster controller can access the region
    # controller.
    maas_url = CharField(
        blank=True, editable=False, max_length=255, default='')

    def __repr__(self):
        return "<NodeGroup %s>" % self.uuid

    def accept(self):
        """Accept this nodegroup's enlistment."""
        self.status = NODEGROUP_STATUS.ACCEPTED
        self.save()

    def reject(self):
        """Reject this nodegroup's enlistment."""
        self.status = NODEGROUP_STATUS.REJECTED
        self.save()

    def save(self, *args, **kwargs):
        if self.api_token_id is None:
            # Avoid circular imports.
            from maasserver.models.user import create_auth_token
            from maasserver.worker_user import get_worker_user

            api_token = create_auth_token(get_worker_user())
            self.api_token = api_token
            self.api_key = api_token.key
        return super(NodeGroup, self).save(*args, **kwargs)

    def get_managed_interfaces(self):
        """Return the list of interfaces for which MAAS manages DHCP."""
        # Filter in python instead of in SQL.  This will use the cached
        # version of self.nodegroupinterface_set if present.
        return [
            itf
            for itf in self.nodegroupinterface_set.all()
            if itf.management != NODEGROUPINTERFACE_MANAGEMENT.UNMANAGED
            ]

    def manages_dns(self):
        """Does this `NodeGroup` manage DNS on any interfaces?

        This returns `True` when the `NodeGroup` is accepted, and has a
        `NodeGroupInterface` that's set to manage both DHCP and DNS.
        """
        if self.status != NODEGROUP_STATUS.ACCEPTED:
            return False
        # Filter in python instead of in SQL.  This will use the cached
        # version of self.nodegroupinterface_set if present.
        for itf in self.nodegroupinterface_set.all():
            if itf.management == NODEGROUPINTERFACE_MANAGEMENT.DHCP_AND_DNS:
                return True
        return False

    def ensure_dhcp_key(self):
        """Ensure that this nodegroup has a dhcp key.

        This method persists the dhcp key without triggering the model
        signals (pre_save/post_save/etc) because it's called from
        dhcp.configure_dhcp which, in turn, it called from the post_save
        signal of NodeGroup."""
        if self.dhcp_key == '':
            dhcp_key = generate_omapi_key()
            self.dhcp_key = dhcp_key
            # Persist the dhcp_key without triggering the signals.
            NodeGroup.objects.filter(id=self.id).update(dhcp_key=dhcp_key)

    @property
    def work_queue(self):
        """The name of the queue for tasks specific to this nodegroup."""
        return self.uuid

    def import_boot_images(self):
        """Import the pxe files on this cluster controller.

        The files are downloaded through the proxy defined in the config
        setting 'http_proxy' if defined.
        """
        # Avoid circular imports.
        from maasserver.models import Config
        task_kwargs = {
            'callback': report_boot_images.subtask(
                options={'queue': self.uuid}),
            }
        http_proxy = Config.objects.get_config('http_proxy')
        if http_proxy is not None:
            task_kwargs['http_proxy'] = http_proxy
        import_boot_images.apply_async(queue=self.uuid, kwargs=task_kwargs)

    def add_seamicro15k(self, mac, username, password, power_control=None):
        """ Add all of the specified cards the Seamicro SM15000 chassis at the
        specified MAC.

        :param mac: MAC address of the card.
        :param username: username for power controller
        :param password: password for power controller
        :param power_control: optional specify the power control method,
            either ipmi (default), restapi, or restapi2.
        """
        args = (mac, username, password, power_control)
        add_seamicro15k.apply_async(queue=self.uuid, args=args)

    def add_virsh(self, poweraddr, password=None):
        """ Add all of the virtual machines inside a virsh controller.

        :param poweraddr: virsh connection string
        :param password: ssh password
        """
        args = (poweraddr, password)
        add_virsh.apply_async(queue=self.uuid, args=args)

    def enlist_nodes_from_ucsm(self, url, username, password):
        """ Add the servers from a Cicso UCS Manager.

        :param URL: URL of the Cisco UCS Manager HTTP-XML API.
        :param username: username for UCS Manager.
        :param password: password for UCS Manager.
        """
        args = (url, username, password)
        enlist_nodes_from_ucsm.apply_async(queue=self.uuid, args=args)

    def enlist_nodes_from_mscm(self, host, username, password):
        """ Add the servers from a Moonshot HP iLO Chassis Manager.

        :param host: IP address for the MSCM.
        :param username: username for MSCM.
        :param password: password for MSCM.
        """
        args = (host, username, password)
        enlist_nodes_from_mscm.apply_async(queue=self.uuid, args=args)

    def add_dhcp_host_maps(self, new_leases):
        if len(new_leases) > 0 and len(self.get_managed_interfaces()) > 0:
            # XXX JeroenVermeulen 2012-08-21, bug=1039362: the DHCP
            # server is currently always local to the worker system, so
            # use 127.0.0.1 as the DHCP server address.
            task_kwargs = dict(
                mappings=new_leases, server_address='127.0.0.1',
                shared_key=self.dhcp_key)
            add_new_dhcp_host_map.apply_async(
                queue=self.uuid, kwargs=task_kwargs)