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