Merge lp:~blake-rouse/maas/fix-1516722-1.9 into lp:maas/1.9

Proposed by Blake Rouse
Status: Merged
Approved by: Andres Rodriguez
Approved revision: no longer in the source branch.
Merged at revision: 4503
Proposed branch: lp:~blake-rouse/maas/fix-1516722-1.9
Merge into: lp:maas/1.9
Diff against target: 98 lines (+67/-13)
1 file modified
src/maasserver/migrations/0182_initial_networking_layout.py (+67/-13)
To merge this branch: bzr merge lp:~blake-rouse/maas/fix-1516722-1.9
Reviewer Review Type Date Requested Status
Blake Rouse (community) Approve
Review via email: mp+277899@code.launchpad.net

Commit message

Modify the initial networking migration to use the orm only from the migration, instead of using the current node model from source.

To post a comment you must log in.
Revision history for this message
Blake Rouse (blake-rouse) wrote :

Self-approving backport.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/maasserver/migrations/0182_initial_networking_layout.py'
2--- src/maasserver/migrations/0182_initial_networking_layout.py 2015-09-30 07:29:27 +0000
3+++ src/maasserver/migrations/0182_initial_networking_layout.py 2015-11-18 19:55:23 +0000
4@@ -1,27 +1,81 @@
5 from django.db import models
6-from maasserver.enum import NODE_STATUS
7+from maasserver.enum import (
8+ IPADDRESS_TYPE,
9+ NODE_STATUS,
10+ NODEGROUP_STATUS,
11+ NODEGROUPINTERFACE_MANAGEMENT,
12+)
13 from maasserver.models.node import Node
14 from south.db import db
15 from south.utils import datetime_utils as datetime
16 from south.v2 import DataMigration
17
18
19+def get_managed_ngi_for_subnet(subnet):
20+ """Return the cluster interface that manages this subnet."""
21+ interfaces = subnet.nodegroupinterface_set.filter(
22+ nodegroup__status=NODEGROUP_STATUS.ENABLED)
23+ interfaces = interfaces.exclude(
24+ management=NODEGROUPINTERFACE_MANAGEMENT.UNMANAGED)
25+ return interfaces.first()
26+
27+
28+def get_managed_subnet_for_interface(interface):
29+ """Return the `Subnet` that manages the `VLAN` for this `interface`."""
30+ for subnet in interface.vlan.subnet_set.all():
31+ ngi = get_managed_ngi_for_subnet(subnet)
32+ if ngi is not None:
33+ return subnet
34+ return None
35+
36+
37 class Migration(DataMigration):
38
39 def forwards(self, orm):
40- # We don't use orm here because we want the current Node model to
41- # include all of the networking methods we require. This will break
42- # if the node these methods get removed from the Node, so we assert
43- # that they at least exists.
44- assert hasattr(Node, "_clear_networking_configuration")
45- assert hasattr(Node, "set_initial_networking_configuration")
46- for node in Node.objects.filter(
47+ now = datetime.datetime.now()
48+
49+ # Loop through all ready nodes and fix their interface configuration.
50+ # Deploying and deployed nodes will already have an AUTO or STICKY IP
51+ # address and on release MAAS will do the correct thing.
52+ for node in orm['maasserver.Node'].objects.filter(
53 installable=True, status=NODE_STATUS.READY):
54- # The node has to be ready and we set the default networking
55- # configuration. Deploying or deployed nodes will already have a
56- # configuration with an AUTO IP or STATIC IP.
57- node._clear_networking_configuration()
58- node.set_initial_networking_configuration()
59+
60+ # Check if any IP addresses already exists for this node.
61+ has_ip = False
62+ for interface in node.interface_set.all():
63+ has_ip = interface.ip_addresses.exclude(
64+ alloc_type=IPADDRESS_TYPE.DISCOVERED).exists()
65+ if has_ip:
66+ break
67+
68+ # If already has an IP address then nothing needs to be done. If
69+ # not then an AUTO IP address needs to be created on the boot
70+ # interface or fallback to DHCP if the boot interface is not on
71+ # a managed subnet.
72+ if not has_ip:
73+ # Get the boot interface for the node.
74+ boot_interface = node.boot_interface
75+ if boot_interface is None:
76+ boot_interface = node.interface_set.order_by('id').first()
77+ if boot_interface is None:
78+ continue
79+
80+ # Get the subnet that manages the VLAN this interface is
81+ # connected to.
82+ subnet = get_managed_subnet_for_interface(boot_interface)
83+ if subnet is not None:
84+ # Create an AUTO IP address for this subnet.
85+ sip = orm['maasserver.StaticIPAddress'].objects.create(
86+ alloc_type=IPADDRESS_TYPE.AUTO, user=None,
87+ ip=None, subnet=subnet, created=now, updated=now)
88+ else:
89+ # No subnet managing the VLAN this interface is connected
90+ # to, so set the boot interface to just DHCP.
91+ sip = orm['maasserver.StaticIPAddress'].objects.create(
92+ alloc_type=IPADDRESS_TYPE.DHCP, user=None,
93+ ip=None, subnet=None, created=now, updated=now)
94+ boot_interface.ip_addresses.add(sip)
95+ boot_interface.save()
96
97 def backwards(self, orm):
98 # No need to go backward.

Subscribers

People subscribed via source and target branches