Scott Baker | 435c2c9 | 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 | a76f65d | 2015-01-13 16:22:57 -0800 | [diff] [blame] | 3 | from operator import itemgetter, attrgetter |
Scott Baker | 3e03522 | 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 | a76f65d | 2015-01-13 16:22:57 -0800 | [diff] [blame] | 10 | def __init__(self, *args, **kwargs): |
| 11 | super(SlicePlus, self).__init__(*args, **kwargs) |
Scott Baker | a76f65d | 2015-01-13 16:22:57 -0800 | [diff] [blame] | 12 | self._update_users = None |
Scott Baker | 7a76f32 | 2015-01-16 19:07:36 -0800 | [diff] [blame] | 13 | self._sliceInfo = None |
Scott Baker | 55f6de6 | 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 | 04ab7c8 | 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 | a76f65d | 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 | 7a76f32 | 2015-01-16 19:07:36 -0800 | [diff] [blame] | 21 | if not self._sliceInfo: |
| 22 | used_sites = {} |
Scott Baker | 2190934 | 2015-01-22 15:21:24 -0800 | [diff] [blame] | 23 | ready_sites = {} |
Scott Baker | 7a76f32 | 2015-01-16 19:07:36 -0800 | [diff] [blame] | 24 | used_deployments = {} |
| 25 | sliverCount = 0 |
Scott Baker | ec93010 | 2015-01-20 01:02:08 -0800 | [diff] [blame] | 26 | sshCommands = [] |
Scott Baker | 7a76f32 | 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 | 970314b | 2015-01-25 22:16:13 -0800 | [diff] [blame] | 34 | sshCommand = sliver.get_ssh_command() |
| 35 | if sshCommand: |
| 36 | sshCommands.append(sshCommand) |
Scott Baker | ec93010 | 2015-01-20 01:02:08 -0800 | [diff] [blame] | 37 | |
Scott Baker | 2190934 | 2015-01-22 15:21:24 -0800 | [diff] [blame] | 38 | ready_sites[site.name] = ready_sites.get(site.name, 0) + 1 |
| 39 | |
Scott Baker | 7a76f32 | 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 | 04ab7c8 | 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 | 7a76f32 | 2015-01-16 19:07:36 -0800 | [diff] [blame] | 57 | self._sliceInfo= {"sitesUsed": used_sites, |
Scott Baker | 2190934 | 2015-01-22 15:21:24 -0800 | [diff] [blame] | 58 | "sitesReady": ready_sites, |
Scott Baker | 7a76f32 | 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 | ec93010 | 2015-01-20 01:02:08 -0800 | [diff] [blame] | 63 | "roles": [], |
Scott Baker | 04ab7c8 | 2015-01-20 13:30:40 -0800 | [diff] [blame] | 64 | "sshCommands": sshCommands, |
| 65 | "networkPorts": networkPorts} |
Scott Baker | 7a76f32 | 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 | 8b89d30 | 2015-01-08 22:34:51 -0800 | [diff] [blame] | 74 | @property |
Scott Baker | 2190934 | 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 | dcf6fbf | 2015-01-11 13:45:19 -0800 | [diff] [blame] | 83 | def site_allocation(self): |
Scott Baker | 55f6de6 | 2015-01-18 16:07:58 -0800 | [diff] [blame] | 84 | return self._site_allocation |
Scott Baker | dcf6fbf | 2015-01-11 13:45:19 -0800 | [diff] [blame] | 85 | |
| 86 | @site_allocation.setter |
| 87 | def site_allocation(self, value): |
Scott Baker | 55f6de6 | 2015-01-18 16:07:58 -0800 | [diff] [blame] | 88 | self._site_allocation = value |
Scott Baker | dcf6fbf | 2015-01-11 13:45:19 -0800 | [diff] [blame] | 89 | |
| 90 | @property |
Scott Baker | 7a76f32 | 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 | 1215424 | 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 | 7a76f32 | 2015-01-16 19:07:36 -0800 | [diff] [blame] | 98 | @property |
Scott Baker | 97acad9 | 2015-01-12 19:45:40 -0800 | [diff] [blame] | 99 | def users(self): |
Scott Baker | 7a76f32 | 2015-01-16 19:07:36 -0800 | [diff] [blame] | 100 | return [user["id"] for user in self.getSliceInfo()["users"].values()] |
Scott Baker | 97acad9 | 2015-01-12 19:45:40 -0800 | [diff] [blame] | 101 | |
| 102 | @users.setter |
| 103 | def users(self, value): |
Scott Baker | a76f65d | 2015-01-13 16:22:57 -0800 | [diff] [blame] | 104 | self._update_users = value |
| 105 | #print "XXX set users to", value |
Scott Baker | 97acad9 | 2015-01-12 19:45:40 -0800 | [diff] [blame] | 106 | |
| 107 | @property |
Scott Baker | d3a6b2c | 2015-01-08 22:37:34 -0800 | [diff] [blame] | 108 | def network_ports(self): |
Scott Baker | 04ab7c8 | 2015-01-20 13:30:40 -0800 | [diff] [blame] | 109 | return self._network_ports |
Scott Baker | 8b89d30 | 2015-01-08 22:34:51 -0800 | [diff] [blame] | 110 | |
Scott Baker | d3a6b2c | 2015-01-08 22:37:34 -0800 | [diff] [blame] | 111 | @network_ports.setter |
| 112 | def network_ports(self, value): |
Scott Baker | 04ab7c8 | 2015-01-20 13:30:40 -0800 | [diff] [blame] | 113 | self._network_ports = value |
| 114 | #print "XXX set networkPorts to", value |
Scott Baker | 8b89d30 | 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 | a76f65d | 2015-01-13 16:22:57 -0800 | [diff] [blame] | 124 | |
Scott Baker | 435c2c9 | 2015-01-14 00:34:45 -0800 | [diff] [blame] | 125 | def get_node_allocation(self, siteList): |
Scott Baker | a76f65d | 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 | 8974e55 | 2015-02-10 19:26:00 -0800 | [diff] [blame] | 138 | if (not hasattr(self,"caller")) or self.caller==None: |
| 139 | raise APIException("no self.caller in SlicePlus.save") |
| 140 | |
Scott Baker | 55f6de6 | 2015-01-18 16:07:58 -0800 | [diff] [blame] | 141 | updated_image = self.has_field_changed("default_image") |
| 142 | updated_flavor = self.has_field_changed("default_flavor") |
| 143 | |
Scott Baker | a76f65d | 2015-01-13 16:22:57 -0800 | [diff] [blame] | 144 | super(SlicePlus, self).save(*args, **kwargs) |
| 145 | |
Scott Baker | 04ab7c8 | 2015-01-20 13:30:40 -0800 | [diff] [blame] | 146 | # try things out first |
| 147 | |
Scott Baker | 55f6de6 | 2015-01-18 16:07:58 -0800 | [diff] [blame] | 148 | updated_sites = (self._site_allocation != self._initial_site_allocation) or updated_image or updated_flavor |
| 149 | if updated_sites: |
| 150 | self.save_site_allocation(noAct=True, reset=(updated_image or updated_flavor)) |
Scott Baker | 435c2c9 | 2015-01-14 00:34:45 -0800 | [diff] [blame] | 151 | |
| 152 | if self._update_users: |
| 153 | self.save_users(noAct=True) |
| 154 | |
Scott Baker | 04ab7c8 | 2015-01-20 13:30:40 -0800 | [diff] [blame] | 155 | if (self._network_ports != self._initial_network_ports): |
| 156 | self.save_network_ports(noAct=True) |
| 157 | |
| 158 | # now actually save them |
| 159 | |
Scott Baker | 55f6de6 | 2015-01-18 16:07:58 -0800 | [diff] [blame] | 160 | if updated_sites: |
| 161 | self.save_site_allocation(reset=(updated_image or updated_flavor)) |
Scott Baker | a76f65d | 2015-01-13 16:22:57 -0800 | [diff] [blame] | 162 | |
Scott Baker | 435c2c9 | 2015-01-14 00:34:45 -0800 | [diff] [blame] | 163 | if self._update_users: |
| 164 | self.save_users() |
| 165 | |
Scott Baker | 04ab7c8 | 2015-01-20 13:30:40 -0800 | [diff] [blame] | 166 | if (self._network_ports != self._initial_network_ports): |
| 167 | self.save_network_ports() |
| 168 | |
Scott Baker | 55f6de6 | 2015-01-18 16:07:58 -0800 | [diff] [blame] | 169 | def save_site_allocation(self, noAct = False, reset=False): |
| 170 | print "save_site_allocation, reset=",reset |
Scott Baker | a76f65d | 2015-01-13 16:22:57 -0800 | [diff] [blame] | 171 | |
Scott Baker | 7508142 | 2015-01-19 08:43:50 -0800 | [diff] [blame] | 172 | if (not self._site_allocation): |
| 173 | # Must be a sliver that was just created, and has not site_allocation |
| 174 | # field. |
| 175 | return |
| 176 | |
Scott Baker | 435c2c9 | 2015-01-14 00:34:45 -0800 | [diff] [blame] | 177 | all_slice_slivers = self.slivers.all() |
Scott Baker | 55f6de6 | 2015-01-18 16:07:58 -0800 | [diff] [blame] | 178 | for site_name in self._site_allocation.keys(): |
| 179 | desired_allocation = self._site_allocation[site_name] |
Scott Baker | a76f65d | 2015-01-13 16:22:57 -0800 | [diff] [blame] | 180 | |
| 181 | # make a list of the slivers for this site |
| 182 | slivers = [] |
| 183 | for sliver in all_slice_slivers: |
| 184 | if sliver.node.site_deployment.site.name == site_name: |
| 185 | slivers.append(sliver) |
| 186 | |
| 187 | # delete extra slivers |
Scott Baker | 55f6de6 | 2015-01-18 16:07:58 -0800 | [diff] [blame] | 188 | while (reset and len(slivers)>0) or (len(slivers) > desired_allocation): |
Scott Baker | a76f65d | 2015-01-13 16:22:57 -0800 | [diff] [blame] | 189 | sliver = slivers.pop() |
Scott Baker | a76f65d | 2015-01-13 16:22:57 -0800 | [diff] [blame] | 190 | if (not noAct): |
Scott Baker | 55f6de6 | 2015-01-18 16:07:58 -0800 | [diff] [blame] | 191 | print "deleting sliver", sliver |
Scott Baker | a76f65d | 2015-01-13 16:22:57 -0800 | [diff] [blame] | 192 | sliver.delete() |
Scott Baker | 55f6de6 | 2015-01-18 16:07:58 -0800 | [diff] [blame] | 193 | else: |
| 194 | print "would delete sliver", sliver |
Scott Baker | a76f65d | 2015-01-13 16:22:57 -0800 | [diff] [blame] | 195 | |
| 196 | # add more slivers |
| 197 | if (len(slivers) < desired_allocation): |
| 198 | site = Site.objects.get(name = site_name) |
Scott Baker | 435c2c9 | 2015-01-14 00:34:45 -0800 | [diff] [blame] | 199 | nodes = self.get_node_allocation([site]) |
Scott Baker | a76f65d | 2015-01-13 16:22:57 -0800 | [diff] [blame] | 200 | |
| 201 | if (not nodes): |
Scott Baker | 3e03522 | 2015-01-24 00:01:29 -0800 | [diff] [blame] | 202 | raise APIException(detail="no nodes in site %s" % site_name) |
Scott Baker | a76f65d | 2015-01-13 16:22:57 -0800 | [diff] [blame] | 203 | |
| 204 | while (len(slivers) < desired_allocation): |
| 205 | # pick the least allocated node |
| 206 | nodes = sorted(nodes, key=attrgetter("sliverCount")) |
| 207 | node = nodes[0] |
| 208 | |
| 209 | sliver = Sliver(name=node.name, |
| 210 | slice=self, |
| 211 | node=node, |
| 212 | image = self.default_image, |
| 213 | flavor = self.default_flavor, |
| 214 | creator = self.creator, |
| 215 | deployment = node.site_deployment.deployment) |
Scott Baker | 8974e55 | 2015-02-10 19:26:00 -0800 | [diff] [blame] | 216 | sliver.caller = self.caller |
Scott Baker | a76f65d | 2015-01-13 16:22:57 -0800 | [diff] [blame] | 217 | slivers.append(sliver) |
| 218 | if (not noAct): |
Scott Baker | 55f6de6 | 2015-01-18 16:07:58 -0800 | [diff] [blame] | 219 | print "added sliver", sliver |
Scott Baker | a76f65d | 2015-01-13 16:22:57 -0800 | [diff] [blame] | 220 | sliver.save() |
Scott Baker | 55f6de6 | 2015-01-18 16:07:58 -0800 | [diff] [blame] | 221 | else: |
| 222 | print "would add sliver", sliver |
Scott Baker | a76f65d | 2015-01-13 16:22:57 -0800 | [diff] [blame] | 223 | |
| 224 | node.sliverCount = node.sliverCount + 1 |
| 225 | |
Scott Baker | 435c2c9 | 2015-01-14 00:34:45 -0800 | [diff] [blame] | 226 | def save_users(self, noAct = False): |
| 227 | new_users = self._update_users |
| 228 | |
Scott Baker | 8974e55 | 2015-02-10 19:26:00 -0800 | [diff] [blame] | 229 | try: |
| 230 | default_role = SliceRole.objects.get(role="access") |
| 231 | except: |
| 232 | default_role = SliceRole.objects.get(role="default") |
Scott Baker | 435c2c9 | 2015-01-14 00:34:45 -0800 | [diff] [blame] | 233 | |
| 234 | slice_privs = self.sliceprivileges.all() |
| 235 | slice_user_ids = [priv.user.id for priv in slice_privs] |
| 236 | |
| 237 | for user_id in new_users: |
| 238 | if (user_id not in slice_user_ids): |
Scott Baker | 435c2c9 | 2015-01-14 00:34:45 -0800 | [diff] [blame] | 239 | priv = SlicePrivilege(slice=self, user=User.objects.get(id=user_id), role=default_role) |
Scott Baker | 8974e55 | 2015-02-10 19:26:00 -0800 | [diff] [blame] | 240 | priv.caller = self.caller |
Scott Baker | 435c2c9 | 2015-01-14 00:34:45 -0800 | [diff] [blame] | 241 | if (not noAct): |
| 242 | priv.save() |
| 243 | |
| 244 | print "added user id", user_id |
| 245 | |
| 246 | for priv in slice_privs: |
| 247 | if (priv.role.id != default_role.id): |
| 248 | # only mess with 'default' users; don't kill an admin |
| 249 | continue |
| 250 | |
| 251 | if (priv.user.id not in new_users): |
| 252 | if (not noAct): |
| 253 | priv.delete() |
| 254 | |
| 255 | print "deleted user id", user_id |
| 256 | |
Scott Baker | 04ab7c8 | 2015-01-20 13:30:40 -0800 | [diff] [blame] | 257 | def save_network_ports(self, noAct=False): |
| 258 | # First search for any network that already has a filled in 'ports' |
| 259 | # field. We'll assume there can only be one, so it must be the right |
| 260 | # one. |
| 261 | for networkSlice in self.networkslices.all(): |
| 262 | network = networkSlice.network |
| 263 | if (network.owner.id != self.id): |
| 264 | continue |
| 265 | if network.ports: |
| 266 | network.ports = self._network_ports |
Scott Baker | 8974e55 | 2015-02-10 19:26:00 -0800 | [diff] [blame] | 267 | network.caller = self.caller |
Scott Baker | 04ab7c8 | 2015-01-20 13:30:40 -0800 | [diff] [blame] | 268 | if (not noAct): |
| 269 | network.save() |
| 270 | return |
| 271 | |
| 272 | # Now try a network that is a "NAT", since setting ports on a non-NAT |
| 273 | # network doesn't make much sense. |
| 274 | for networkSlice in self.networkslices.all(): |
| 275 | network = networkSlice.network |
| 276 | if (network.owner.id != self.id): |
| 277 | continue |
| 278 | if network.template.translation=="NAT": |
| 279 | network.ports = self._network_ports |
Scott Baker | 8974e55 | 2015-02-10 19:26:00 -0800 | [diff] [blame] | 280 | network.caller = self.caller |
Scott Baker | 04ab7c8 | 2015-01-20 13:30:40 -0800 | [diff] [blame] | 281 | if (not noAct): |
| 282 | network.save() |
| 283 | return |
| 284 | |
| 285 | # uh oh, we didn't find a network |
| 286 | |
Scott Baker | 3e03522 | 2015-01-24 00:01:29 -0800 | [diff] [blame] | 287 | raise APIException(detail="No network was found that ports could be set on") |
Scott Baker | 04ab7c8 | 2015-01-20 13:30:40 -0800 | [diff] [blame] | 288 | |
Scott Baker | 435c2c9 | 2015-01-14 00:34:45 -0800 | [diff] [blame] | 289 | |
| 290 | |
| 291 | |
Scott Baker | a76f65d | 2015-01-13 16:22:57 -0800 | [diff] [blame] | 292 | |