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