~allenap/maas/pypy-compatibility

« back to all changes in this revision

Viewing changes to src/maasserver/provisioning.py

  • Committer: Tarmac
  • Author(s): Gavin Panella
  • Date: 2012-02-10 12:50:33 UTC
  • mfrom: (87.9.20 maas-pserv-add-node)
  • Revision ID: ed@carob-20120210125033-235n3q8t6dsmk5c5
[r=julian-edwards][bug=][author=allenap] Adding a node to the UI results in a new node in Cobbler.

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
"""Interact with the Provisioning API."""
 
5
 
 
6
from __future__ import (
 
7
    print_function,
 
8
    unicode_literals,
 
9
    )
 
10
 
 
11
__metaclass__ = type
 
12
__all__ = []
 
13
 
 
14
from uuid import uuid1
 
15
import xmlrpclib
 
16
 
 
17
from django.conf import settings
 
18
from django.db.models.signals import (
 
19
    post_delete,
 
20
    post_save,
 
21
    )
 
22
from django.dispatch import receiver
 
23
from maasserver.models import (
 
24
    MACAddress,
 
25
    Node,
 
26
    )
 
27
 
 
28
 
 
29
def get_provisioning_api_proxy():
 
30
    """Return a proxy to the Provisioning API."""
 
31
    # FIXME: This is a little ugly.
 
32
    url = settings.PSERV_URL
 
33
    if url is None:
 
34
        from provisioningserver.testing.fakeapi import (
 
35
            FakeSynchronousProvisioningAPI,
 
36
            )
 
37
        return FakeSynchronousProvisioningAPI()
 
38
    else:
 
39
        return xmlrpclib.ServerProxy(
 
40
            url, allow_none=True, use_datetime=True)
 
41
 
 
42
 
 
43
@receiver(post_save, sender=Node)
 
44
def provision_post_save_Node(sender, instance, created, **kwargs):
 
45
    """Create or update nodes in the provisioning server."""
 
46
    papi = get_provisioning_api_proxy()
 
47
    nodes = papi.get_nodes_by_name([instance.system_id])
 
48
    if instance.system_id in nodes:
 
49
        profile = nodes[instance.system_id]["profile"]
 
50
    else:
 
51
        # TODO: Get these from somewhere.
 
52
        distro = papi.add_distro(
 
53
            "distro-%s" % uuid1().get_hex(),
 
54
            "initrd", "kernel")
 
55
        profile = papi.add_profile(
 
56
            "profile-%s" % uuid1().get_hex(),
 
57
            distro)
 
58
    papi.add_node(instance.system_id, profile)
 
59
 
 
60
 
 
61
@receiver(post_save, sender=MACAddress)
 
62
def provision_post_save_MACAddress(sender, instance, created, **kwargs):
 
63
    """Create or update MACs in the provisioning server."""
 
64
    # TODO
 
65
 
 
66
 
 
67
@receiver(post_delete, sender=Node)
 
68
def provision_post_delete_Node(sender, instance, **kwargs):
 
69
    """Delete nodes in the provisioning server."""
 
70
    papi = get_provisioning_api_proxy()
 
71
    papi.delete_nodes_by_name([instance.system_id])
 
72
 
 
73
 
 
74
@receiver(post_delete, sender=MACAddress)
 
75
def provision_post_delete_MACAddress(sender, instance, **kwargs):
 
76
    """Delete MACs in the provisioning server."""
 
77
    # TODO