Scott Baker | c7f363b | 2015-01-14 00:34:45 -0800 | [diff] [blame] | 1 | from core.models import Slice, SlicePrivilege, SliceRole, Sliver, Site, Node, User |
Scott Baker | e791dc6 | 2014-08-28 14:02:54 -0700 | [diff] [blame] | 2 | from plus import PlusObjectMixin |
Scott Baker | 2fbf7dc | 2015-01-13 16:22:57 -0800 | [diff] [blame] | 3 | from operator import itemgetter, attrgetter |
Scott Baker | 5f4770d | 2014-07-13 11:17:58 -0700 | [diff] [blame] | 4 | |
Scott Baker | e791dc6 | 2014-08-28 14:02:54 -0700 | [diff] [blame] | 5 | class SlicePlus(Slice, PlusObjectMixin): |
Scott Baker | 88e3437 | 2014-07-13 11:46:36 -0700 | [diff] [blame] | 6 | class Meta: |
| 7 | proxy = True |
| 8 | |
Scott Baker | 2fbf7dc | 2015-01-13 16:22:57 -0800 | [diff] [blame] | 9 | def __init__(self, *args, **kwargs): |
| 10 | super(SlicePlus, self).__init__(*args, **kwargs) |
Scott Baker | 2fbf7dc | 2015-01-13 16:22:57 -0800 | [diff] [blame] | 11 | self._update_users = None |
Scott Baker | a31bb24 | 2015-01-16 19:07:36 -0800 | [diff] [blame] | 12 | self._sliceInfo = None |
Scott Baker | cd369ea | 2015-01-18 16:07:58 -0800 | [diff] [blame] | 13 | self.getSliceInfo() |
| 14 | self._site_allocation = self._sliceInfo["sitesUsed"] |
| 15 | self._initial_site_allocation = self._site_allocation |
Scott Baker | 977a465 | 2015-01-20 13:30:40 -0800 | [diff] [blame] | 16 | self._network_ports = self._sliceInfo["networkPorts"] |
| 17 | self._initial_network_ports = self._network_ports |
Scott Baker | 2fbf7dc | 2015-01-13 16:22:57 -0800 | [diff] [blame] | 18 | |
Scott Baker | 5f4770d | 2014-07-13 11:17:58 -0700 | [diff] [blame] | 19 | def getSliceInfo(self, user=None): |
Scott Baker | a31bb24 | 2015-01-16 19:07:36 -0800 | [diff] [blame] | 20 | if not self._sliceInfo: |
| 21 | used_sites = {} |
| 22 | used_deployments = {} |
| 23 | sliverCount = 0 |
Scott Baker | 334919e | 2015-01-20 01:02:08 -0800 | [diff] [blame] | 24 | sshCommands = [] |
Scott Baker | a31bb24 | 2015-01-16 19:07:36 -0800 | [diff] [blame] | 25 | for sliver in self.slivers.all(): |
| 26 | site = sliver.node.site_deployment.site |
| 27 | deployment = sliver.node.site_deployment.deployment |
| 28 | used_sites[site.name] = used_sites.get(site.name, 0) + 1 |
| 29 | used_deployments[deployment.name] = used_deployments.get(deployment.name, 0) + 1 |
| 30 | sliverCount = sliverCount + 1 |
Scott Baker | 5f4770d | 2014-07-13 11:17:58 -0700 | [diff] [blame] | 31 | |
Scott Baker | 334919e | 2015-01-20 01:02:08 -0800 | [diff] [blame] | 32 | if (sliver.instance_id and sliver.instance_name): |
| 33 | sshCommand = 'ssh -o "ProxyCommand ssh -q %s@%s" ubuntu@%s' % (sliver.instance_id, sliver.node.name, sliver.instance_name) |
| 34 | sshCommands.append(sshCommand); |
| 35 | |
Scott Baker | a31bb24 | 2015-01-16 19:07:36 -0800 | [diff] [blame] | 36 | users = {} |
| 37 | for priv in SlicePrivilege.objects.filter(slice=self): |
| 38 | if not (priv.user.id in users.keys()): |
| 39 | users[priv.user.id] = {"name": priv.user.email, "id": priv.user.id, "roles": []} |
| 40 | users[priv.user.id]["roles"].append(priv.role.role) |
Scott Baker | 5f4770d | 2014-07-13 11:17:58 -0700 | [diff] [blame] | 41 | |
Scott Baker | 977a465 | 2015-01-20 13:30:40 -0800 | [diff] [blame] | 42 | # XXX this assumes there is only one network that can have ports bound |
| 43 | # to it for a given slice. This is intended for the tenant view, which |
| 44 | # will obey this field. |
| 45 | networkPorts = "" |
| 46 | for networkSlice in self.networkslices.all(): |
| 47 | network = networkSlice.network |
| 48 | if (network.owner.id != self.id): |
| 49 | continue |
| 50 | if network.ports: |
| 51 | networkPorts = network.ports |
| 52 | |
Scott Baker | a31bb24 | 2015-01-16 19:07:36 -0800 | [diff] [blame] | 53 | self._sliceInfo= {"sitesUsed": used_sites, |
| 54 | "deploymentsUsed": used_deployments, |
| 55 | "sliverCount": sliverCount, |
| 56 | "siteCount": len(used_sites.keys()), |
| 57 | "users": users, |
Scott Baker | 334919e | 2015-01-20 01:02:08 -0800 | [diff] [blame] | 58 | "roles": [], |
Scott Baker | 977a465 | 2015-01-20 13:30:40 -0800 | [diff] [blame] | 59 | "sshCommands": sshCommands, |
| 60 | "networkPorts": networkPorts} |
Scott Baker | a31bb24 | 2015-01-16 19:07:36 -0800 | [diff] [blame] | 61 | |
| 62 | if user: |
| 63 | auser = self._sliceInfo["users"].get(user.id, None) |
| 64 | if (auser): |
| 65 | self._sliceInfo["roles"] = auser["roles"] |
| 66 | |
| 67 | return self._sliceInfo |
Scott Baker | 88e3437 | 2014-07-13 11:46:36 -0700 | [diff] [blame] | 68 | |
Scott Baker | 9062b0c | 2015-01-08 22:34:51 -0800 | [diff] [blame] | 69 | @property |
Scott Baker | 03696ee | 2015-01-11 13:45:19 -0800 | [diff] [blame] | 70 | def site_allocation(self): |
Scott Baker | cd369ea | 2015-01-18 16:07:58 -0800 | [diff] [blame] | 71 | return self._site_allocation |
Scott Baker | 03696ee | 2015-01-11 13:45:19 -0800 | [diff] [blame] | 72 | |
| 73 | @site_allocation.setter |
| 74 | def site_allocation(self, value): |
Scott Baker | cd369ea | 2015-01-18 16:07:58 -0800 | [diff] [blame] | 75 | self._site_allocation = value |
Scott Baker | 03696ee | 2015-01-11 13:45:19 -0800 | [diff] [blame] | 76 | |
| 77 | @property |
Scott Baker | a31bb24 | 2015-01-16 19:07:36 -0800 | [diff] [blame] | 78 | def user_names(self): |
| 79 | return [user["name"] for user in self.getSliceInfo()["users"].values()] |
| 80 | |
Scott Baker | a410100 | 2015-01-16 19:26:54 -0800 | [diff] [blame] | 81 | @user_names.setter |
| 82 | def user_names(self, value): |
| 83 | pass # it's read-only |
| 84 | |
Scott Baker | a31bb24 | 2015-01-16 19:07:36 -0800 | [diff] [blame] | 85 | @property |
Scott Baker | fcf655e | 2015-01-12 19:45:40 -0800 | [diff] [blame] | 86 | def users(self): |
Scott Baker | a31bb24 | 2015-01-16 19:07:36 -0800 | [diff] [blame] | 87 | return [user["id"] for user in self.getSliceInfo()["users"].values()] |
Scott Baker | fcf655e | 2015-01-12 19:45:40 -0800 | [diff] [blame] | 88 | |
| 89 | @users.setter |
| 90 | def users(self, value): |
Scott Baker | 2fbf7dc | 2015-01-13 16:22:57 -0800 | [diff] [blame] | 91 | self._update_users = value |
| 92 | #print "XXX set users to", value |
Scott Baker | fcf655e | 2015-01-12 19:45:40 -0800 | [diff] [blame] | 93 | |
| 94 | @property |
Scott Baker | 697512d | 2015-01-08 22:37:34 -0800 | [diff] [blame] | 95 | def network_ports(self): |
Scott Baker | 977a465 | 2015-01-20 13:30:40 -0800 | [diff] [blame] | 96 | return self._network_ports |
Scott Baker | 9062b0c | 2015-01-08 22:34:51 -0800 | [diff] [blame] | 97 | |
Scott Baker | 697512d | 2015-01-08 22:37:34 -0800 | [diff] [blame] | 98 | @network_ports.setter |
| 99 | def network_ports(self, value): |
Scott Baker | 977a465 | 2015-01-20 13:30:40 -0800 | [diff] [blame] | 100 | self._network_ports = value |
| 101 | #print "XXX set networkPorts to", value |
Scott Baker | 9062b0c | 2015-01-08 22:34:51 -0800 | [diff] [blame] | 102 | |
Scott Baker | 88e3437 | 2014-07-13 11:46:36 -0700 | [diff] [blame] | 103 | @staticmethod |
| 104 | def select_by_user(user): |
Scott Baker | 88e3437 | 2014-07-13 11:46:36 -0700 | [diff] [blame] | 105 | if user.is_admin: |
| 106 | qs = SlicePlus.objects.all() |
| 107 | else: |
| 108 | slice_ids = [sp.slice.id for sp in SlicePrivilege.objects.filter(user=user)] |
| 109 | qs = SlicePlus.objects.filter(id__in=slice_ids) |
Scott Baker | 88e3437 | 2014-07-13 11:46:36 -0700 | [diff] [blame] | 110 | return qs |
Scott Baker | 2fbf7dc | 2015-01-13 16:22:57 -0800 | [diff] [blame] | 111 | |
Scott Baker | c7f363b | 2015-01-14 00:34:45 -0800 | [diff] [blame] | 112 | def get_node_allocation(self, siteList): |
Scott Baker | 2fbf7dc | 2015-01-13 16:22:57 -0800 | [diff] [blame] | 113 | siteIDList = [site.id for site in siteList] |
| 114 | nodeList = [] |
| 115 | for node in Node.objects.all(): |
| 116 | if (node.site_deployment.site.id in siteIDList): |
| 117 | node.sliverCount = 0 |
| 118 | for sliver in node.slivers.all(): |
| 119 | if sliver.slice.id == self.id: |
| 120 | node.sliverCount = node.sliverCount + 1 |
| 121 | nodeList.append(node) |
| 122 | return nodeList |
| 123 | |
| 124 | def save(self, *args, **kwargs): |
Scott Baker | cd369ea | 2015-01-18 16:07:58 -0800 | [diff] [blame] | 125 | updated_image = self.has_field_changed("default_image") |
| 126 | updated_flavor = self.has_field_changed("default_flavor") |
| 127 | |
Scott Baker | 2fbf7dc | 2015-01-13 16:22:57 -0800 | [diff] [blame] | 128 | super(SlicePlus, self).save(*args, **kwargs) |
| 129 | |
Scott Baker | 977a465 | 2015-01-20 13:30:40 -0800 | [diff] [blame] | 130 | # try things out first |
| 131 | |
Scott Baker | cd369ea | 2015-01-18 16:07:58 -0800 | [diff] [blame] | 132 | updated_sites = (self._site_allocation != self._initial_site_allocation) or updated_image or updated_flavor |
| 133 | if updated_sites: |
| 134 | self.save_site_allocation(noAct=True, reset=(updated_image or updated_flavor)) |
Scott Baker | c7f363b | 2015-01-14 00:34:45 -0800 | [diff] [blame] | 135 | |
| 136 | if self._update_users: |
| 137 | self.save_users(noAct=True) |
| 138 | |
Scott Baker | 977a465 | 2015-01-20 13:30:40 -0800 | [diff] [blame] | 139 | if (self._network_ports != self._initial_network_ports): |
| 140 | self.save_network_ports(noAct=True) |
| 141 | |
| 142 | # now actually save them |
| 143 | |
Scott Baker | cd369ea | 2015-01-18 16:07:58 -0800 | [diff] [blame] | 144 | if updated_sites: |
| 145 | self.save_site_allocation(reset=(updated_image or updated_flavor)) |
Scott Baker | 2fbf7dc | 2015-01-13 16:22:57 -0800 | [diff] [blame] | 146 | |
Scott Baker | c7f363b | 2015-01-14 00:34:45 -0800 | [diff] [blame] | 147 | if self._update_users: |
| 148 | self.save_users() |
| 149 | |
Scott Baker | 977a465 | 2015-01-20 13:30:40 -0800 | [diff] [blame] | 150 | if (self._network_ports != self._initial_network_ports): |
| 151 | self.save_network_ports() |
| 152 | |
Scott Baker | cd369ea | 2015-01-18 16:07:58 -0800 | [diff] [blame] | 153 | def save_site_allocation(self, noAct = False, reset=False): |
| 154 | print "save_site_allocation, reset=",reset |
Scott Baker | 2fbf7dc | 2015-01-13 16:22:57 -0800 | [diff] [blame] | 155 | |
Scott Baker | 34b3224 | 2015-01-19 08:43:50 -0800 | [diff] [blame] | 156 | if (not self._site_allocation): |
| 157 | # Must be a sliver that was just created, and has not site_allocation |
| 158 | # field. |
| 159 | return |
| 160 | |
Scott Baker | c7f363b | 2015-01-14 00:34:45 -0800 | [diff] [blame] | 161 | all_slice_slivers = self.slivers.all() |
Scott Baker | cd369ea | 2015-01-18 16:07:58 -0800 | [diff] [blame] | 162 | for site_name in self._site_allocation.keys(): |
| 163 | desired_allocation = self._site_allocation[site_name] |
Scott Baker | 2fbf7dc | 2015-01-13 16:22:57 -0800 | [diff] [blame] | 164 | |
| 165 | # make a list of the slivers for this site |
| 166 | slivers = [] |
| 167 | for sliver in all_slice_slivers: |
| 168 | if sliver.node.site_deployment.site.name == site_name: |
| 169 | slivers.append(sliver) |
| 170 | |
| 171 | # delete extra slivers |
Scott Baker | cd369ea | 2015-01-18 16:07:58 -0800 | [diff] [blame] | 172 | while (reset and len(slivers)>0) or (len(slivers) > desired_allocation): |
Scott Baker | 2fbf7dc | 2015-01-13 16:22:57 -0800 | [diff] [blame] | 173 | sliver = slivers.pop() |
Scott Baker | 2fbf7dc | 2015-01-13 16:22:57 -0800 | [diff] [blame] | 174 | if (not noAct): |
Scott Baker | cd369ea | 2015-01-18 16:07:58 -0800 | [diff] [blame] | 175 | print "deleting sliver", sliver |
Scott Baker | 2fbf7dc | 2015-01-13 16:22:57 -0800 | [diff] [blame] | 176 | sliver.delete() |
Scott Baker | cd369ea | 2015-01-18 16:07:58 -0800 | [diff] [blame] | 177 | else: |
| 178 | print "would delete sliver", sliver |
Scott Baker | 2fbf7dc | 2015-01-13 16:22:57 -0800 | [diff] [blame] | 179 | |
| 180 | # add more slivers |
| 181 | if (len(slivers) < desired_allocation): |
| 182 | site = Site.objects.get(name = site_name) |
Scott Baker | c7f363b | 2015-01-14 00:34:45 -0800 | [diff] [blame] | 183 | nodes = self.get_node_allocation([site]) |
Scott Baker | 2fbf7dc | 2015-01-13 16:22:57 -0800 | [diff] [blame] | 184 | |
| 185 | if (not nodes): |
Scott Baker | 79be5c0 | 2015-01-13 16:28:05 -0800 | [diff] [blame] | 186 | raise ValueError("no nodes in site %s" % site_name) |
Scott Baker | 2fbf7dc | 2015-01-13 16:22:57 -0800 | [diff] [blame] | 187 | |
| 188 | while (len(slivers) < desired_allocation): |
| 189 | # pick the least allocated node |
| 190 | nodes = sorted(nodes, key=attrgetter("sliverCount")) |
| 191 | node = nodes[0] |
| 192 | |
| 193 | sliver = Sliver(name=node.name, |
| 194 | slice=self, |
| 195 | node=node, |
| 196 | image = self.default_image, |
| 197 | flavor = self.default_flavor, |
| 198 | creator = self.creator, |
| 199 | deployment = node.site_deployment.deployment) |
| 200 | slivers.append(sliver) |
| 201 | if (not noAct): |
Scott Baker | cd369ea | 2015-01-18 16:07:58 -0800 | [diff] [blame] | 202 | print "added sliver", sliver |
Scott Baker | 2fbf7dc | 2015-01-13 16:22:57 -0800 | [diff] [blame] | 203 | sliver.save() |
Scott Baker | cd369ea | 2015-01-18 16:07:58 -0800 | [diff] [blame] | 204 | else: |
| 205 | print "would add sliver", sliver |
Scott Baker | 2fbf7dc | 2015-01-13 16:22:57 -0800 | [diff] [blame] | 206 | |
| 207 | node.sliverCount = node.sliverCount + 1 |
| 208 | |
Scott Baker | c7f363b | 2015-01-14 00:34:45 -0800 | [diff] [blame] | 209 | def save_users(self, noAct = False): |
| 210 | new_users = self._update_users |
| 211 | |
| 212 | default_role = SliceRole.objects.get(role="default") |
| 213 | |
| 214 | slice_privs = self.sliceprivileges.all() |
| 215 | slice_user_ids = [priv.user.id for priv in slice_privs] |
| 216 | |
| 217 | for user_id in new_users: |
| 218 | if (user_id not in slice_user_ids): |
Scott Baker | c7f363b | 2015-01-14 00:34:45 -0800 | [diff] [blame] | 219 | priv = SlicePrivilege(slice=self, user=User.objects.get(id=user_id), role=default_role) |
| 220 | if (not noAct): |
| 221 | priv.save() |
| 222 | |
| 223 | print "added user id", user_id |
| 224 | |
| 225 | for priv in slice_privs: |
| 226 | if (priv.role.id != default_role.id): |
| 227 | # only mess with 'default' users; don't kill an admin |
| 228 | continue |
| 229 | |
| 230 | if (priv.user.id not in new_users): |
| 231 | if (not noAct): |
| 232 | priv.delete() |
| 233 | |
| 234 | print "deleted user id", user_id |
| 235 | |
Scott Baker | 977a465 | 2015-01-20 13:30:40 -0800 | [diff] [blame] | 236 | def save_network_ports(self, noAct=False): |
| 237 | # First search for any network that already has a filled in 'ports' |
| 238 | # field. We'll assume there can only be one, so it must be the right |
| 239 | # one. |
| 240 | for networkSlice in self.networkslices.all(): |
| 241 | network = networkSlice.network |
| 242 | if (network.owner.id != self.id): |
| 243 | continue |
| 244 | if network.ports: |
| 245 | network.ports = self._network_ports |
| 246 | if (not noAct): |
| 247 | network.save() |
| 248 | return |
| 249 | |
| 250 | # Now try a network that is a "NAT", since setting ports on a non-NAT |
| 251 | # network doesn't make much sense. |
| 252 | for networkSlice in self.networkslices.all(): |
| 253 | network = networkSlice.network |
| 254 | if (network.owner.id != self.id): |
| 255 | continue |
| 256 | if network.template.translation=="NAT": |
| 257 | network.ports = self._network_ports |
| 258 | if (not noAct): |
| 259 | network.save() |
| 260 | return |
| 261 | |
| 262 | # uh oh, we didn't find a network |
| 263 | |
| 264 | raise ValueError("No network was found that ports could be set on") |
| 265 | |
Scott Baker | c7f363b | 2015-01-14 00:34:45 -0800 | [diff] [blame] | 266 | |
| 267 | |
| 268 | |
Scott Baker | 2fbf7dc | 2015-01-13 16:22:57 -0800 | [diff] [blame] | 269 | |