blob: db07cf69ae95cc51189231495ee4b53a7531fd3c [file] [log] [blame]
Scott Bakeraf599eb2017-03-21 12:43:26 -07001from synchronizers.new_base.modelaccessor import *
Scott Bakerb63ea792016-08-11 10:24:48 -07002
Scott Bakeraf599eb2017-03-21 12:43:26 -07003def handle_container_on_metal(instance):
Scott Bakerb63ea792016-08-11 10:24:48 -07004 print "MODEL POLICY: instance", instance, "handle container_on_metal"
5
6 if instance.deleted:
7 return
8
9 if (instance.isolation in ["container"]) and (instance.slice.network not in ["host", "bridged"]):
10 # Our current docker-on-metal network strategy requires that there be some
11 # VM on the server that connects to the networks, so that
12 # the containers can piggyback off of that configuration.
Scott Bakeraf599eb2017-03-21 12:43:26 -070013 if not Instance.objects.filter(slice_id=instance.slice.id, node_id=instance.node.id, isolation="vm").exists():
Scott Bakerb63ea792016-08-11 10:24:48 -070014 flavors = Flavor.objects.filter(name="m1.small")
15 if not flavors:
16 raise XOSConfigurationError("No m1.small flavor")
17
18 images = Image.objects.filter(kind="vm")
19
20 companion_instance = Instance(slice = instance.slice,
21 node = instance.node,
22 image = images[0],
23 creator = instance.creator,
24 deployment = instance.node.site_deployment.deployment,
25 flavor = flavors[0])
26 companion_instance.save()
27
28 print "MODEL POLICY: instance", instance, "created companion", companion_instance
29
30 # Add the ports for the container
31 for network in instance.slice.networks.all():
32 # hmmm... The NAT ports never become ready, because sync_ports never
33 # instantiates them. Need to think about this.
34 print "MODEL POLICY: instance", instance, "handling network", network
35 if (network.name.endswith("-nat")):
36 continue
37
Scott Bakeraf599eb2017-03-21 12:43:26 -070038 if not Port.objects.filter(network_id=network.id, instance_id=instance.id).exists():
Scott Bakerb63ea792016-08-11 10:24:48 -070039 port = Port(network = network, instance=instance)
40 port.save()
41 print "MODEL POLICY: instance", instance, "created port", port
42
43def handle(instance):
Scott Bakera0a688a2017-03-28 11:59:56 -070044 networks = [ns.network for ns in NetworkSlice.objects.filter(slice_id=instance.slice.id)]
45 controller_networks = ControllerNetwork.objects.filter(controller_id=instance.node.site_deployment.controller.id)
Scott Bakeraf599eb2017-03-21 12:43:26 -070046
47 # a little clumsy because the API ORM doesn't support __in queries
48 network_ids = [x.id for x in networks]
49 controller_networks = [x for x in controller_networks if x.network.id in network_ids]
Scott Bakerb63ea792016-08-11 10:24:48 -070050
51 for cn in controller_networks:
52 if (cn.lazy_blocked):
53 print "MODEL POLICY: instance", instance, "unblocking network", cn.network
54 cn.lazy_blocked=False
55 cn.backend_register = '{}'
56 cn.save()
57
58 if (instance.isolation in ["container", "container_vm"]):
59 handle_container_on_metal(instance)