blob: dd1a8d5be0aac9fcca6f10cb0ddc675c4ff8ce67 [file] [log] [blame]
Scott Bakerb63ea792016-08-11 10:24:48 -07001def handle_container_on_metal(instance):
2 from core.models import Instance, Flavor, Port, Image
3
4 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.
13 if not Instance.objects.filter(slice=instance.slice, node=instance.node, isolation="vm").exists():
14 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
38 if not Port.objects.filter(network=network, instance=instance).exists():
39 port = Port(network = network, instance=instance)
40 port.save()
41 print "MODEL POLICY: instance", instance, "created port", port
42
43def handle(instance):
44 from core.models import Controller, ControllerSlice, ControllerNetwork, NetworkSlice
45
46 networks = [ns.network for ns in NetworkSlice.objects.filter(slice=instance.slice)]
47 controller_networks = ControllerNetwork.objects.filter(network__in=networks,
48 controller=instance.node.site_deployment.controller)
49
50 for cn in controller_networks:
51 if (cn.lazy_blocked):
52 print "MODEL POLICY: instance", instance, "unblocking network", cn.network
53 cn.lazy_blocked=False
54 cn.backend_register = '{}'
55 cn.save()
56
57 if (instance.isolation in ["container", "container_vm"]):
58 handle_container_on_metal(instance)