Scott Baker | af599eb | 2017-03-21 12:43:26 -0700 | [diff] [blame] | 1 | from synchronizers.new_base.modelaccessor import * |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 2 | |
Scott Baker | af599eb | 2017-03-21 12:43:26 -0700 | [diff] [blame] | 3 | def handle_container_on_metal(instance): |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 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. |
Scott Baker | af599eb | 2017-03-21 12:43:26 -0700 | [diff] [blame] | 13 | if not Instance.objects.filter(slice_id=instance.slice.id, node_id=instance.node.id, isolation="vm").exists(): |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 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 | |
Scott Baker | af599eb | 2017-03-21 12:43:26 -0700 | [diff] [blame] | 38 | if not Port.objects.filter(network_id=network.id, instance_id=instance.id).exists(): |
Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 39 | port = Port(network = network, instance=instance) |
| 40 | port.save() |
| 41 | print "MODEL POLICY: instance", instance, "created port", port |
| 42 | |
| 43 | def handle(instance): |
Scott Baker | a0a688a | 2017-03-28 11:59:56 -0700 | [diff] [blame] | 44 | 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 Baker | af599eb | 2017-03-21 12:43:26 -0700 | [diff] [blame] | 46 | |
| 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 Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 50 | |
| 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) |