Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 1 | from xos.config import Config |
| 2 | |
| 3 | def handle_delete(slice): |
| 4 | from core.models import Controller, ControllerSlice, SiteDeployment, Network, NetworkSlice,NetworkTemplate, Slice |
| 5 | from collections import defaultdict |
| 6 | |
| 7 | public_nets = [] |
| 8 | private_net = None |
| 9 | networks = Network.objects.filter(owner=slice) |
| 10 | |
| 11 | for n in networks: |
| 12 | n.delete() |
| 13 | |
| 14 | # Note that sliceprivileges and slicecontrollers are autodeleted, through the dependency graph |
| 15 | |
| 16 | def handle(slice): |
| 17 | from core.models import Controller, ControllerSlice, SiteDeployment, Network, NetworkSlice,NetworkTemplate, Slice |
| 18 | from collections import defaultdict |
| 19 | |
| 20 | # only create nat_net if not using VTN |
| 21 | support_nat_net = not getattr(Config(), "networking_use_vtn", False) |
| 22 | |
| 23 | print "MODEL POLICY: slice", slice |
| 24 | |
| 25 | # slice = Slice.get(slice_id) |
| 26 | |
| 27 | controller_slices = ControllerSlice.objects.filter(slice=slice) |
| 28 | existing_controllers = [cs.controller for cs in controller_slices] |
| 29 | |
| 30 | print "MODEL POLICY: slice existing_controllers=", existing_controllers |
| 31 | |
| 32 | all_controllers = Controller.objects.all() |
| 33 | for controller in all_controllers: |
| 34 | if controller not in existing_controllers: |
| 35 | print "MODEL POLICY: slice adding controller", controller |
| 36 | sd = ControllerSlice(slice=slice, controller=controller) |
| 37 | sd.save() |
| 38 | |
| 39 | if slice.network in ["host", "bridged"]: |
| 40 | # Host and Bridged docker containers need no networks and they will |
| 41 | # only get in the way. |
| 42 | print "MODEL POLICY: Skipping network creation" |
| 43 | elif slice.network in ["noauto"]: |
| 44 | # do nothing |
| 45 | pass |
| 46 | else: |
| 47 | # make sure slice has at least 1 public and 1 private networkd |
| 48 | public_nets = [] |
| 49 | private_nets = [] |
| 50 | networks = Network.objects.filter(owner=slice) |
| 51 | for network in networks: |
| 52 | if not network.autoconnect: |
| 53 | continue |
| 54 | if network.template.name == 'Public dedicated IPv4': |
| 55 | public_nets.append(network) |
| 56 | elif network.template.name == 'Public shared IPv4': |
| 57 | public_nets.append(network) |
| 58 | elif network.template.name == 'Private': |
| 59 | private_nets.append(network) |
| 60 | if support_nat_net and (not public_nets): |
| 61 | # ensure there is at least one public network, and default it to dedicated |
| 62 | nat_net = Network( |
| 63 | name = slice.name+'-nat', |
| 64 | template = NetworkTemplate.objects.get(name='Public shared IPv4'), |
| 65 | owner = slice |
| 66 | ) |
| 67 | if slice.exposed_ports: |
| 68 | nat_net.ports = slice.exposed_ports |
| 69 | nat_net.save() |
| 70 | public_nets.append(nat_net) |
| 71 | print "MODEL POLICY: slice", slice, "made nat-net" |
| 72 | |
| 73 | if not private_nets: |
| 74 | private_net = Network( |
| 75 | name = slice.name+'-private', |
| 76 | template = NetworkTemplate.objects.get(name='Private'), |
| 77 | owner = slice |
| 78 | ) |
| 79 | private_net.save() |
| 80 | print "MODEL POLICY: slice", slice, "made private net" |
| 81 | private_nets = [private_net] |
| 82 | # create slice networks |
| 83 | public_net_slice = None |
| 84 | private_net_slice = None |
| 85 | net_slices = NetworkSlice.objects.filter(slice=slice, network__in=private_nets+public_nets) |
| 86 | for net_slice in net_slices: |
| 87 | if net_slice.network in public_nets: |
| 88 | public_net_slice = net_slice |
| 89 | elif net_slice.network in private_nets: |
| 90 | private_net_slice = net_slice |
| 91 | if support_nat_net and (not public_net_slice): |
| 92 | public_net_slice = NetworkSlice(slice=slice, network=public_nets[0]) |
| 93 | public_net_slice.save() |
| 94 | print "MODEL POLICY: slice", slice, "made public_net_slice" |
| 95 | if not private_net_slice: |
| 96 | private_net_slice = NetworkSlice(slice=slice, network=private_nets[0]) |
| 97 | private_net_slice.save() |
| 98 | print "MODEL POLICY: slice", slice, "made private_net_slice" |
| 99 | |
| 100 | print "MODEL POLICY: slice", slice, "DONE" |
| 101 | |
| 102 | |