blob: 0c662585fbda6b98617708927ca64e8bca098730 [file] [log] [blame]
Scott Baker435c2c92015-01-14 00:34:45 -08001from core.models import Slice, SlicePrivilege, SliceRole, Sliver, Site, Node, User
Scott Bakere791dc62014-08-28 14:02:54 -07002from plus import PlusObjectMixin
Scott Bakera76f65d2015-01-13 16:22:57 -08003from operator import itemgetter, attrgetter
Scott Baker3e035222015-01-24 00:01:29 -08004from rest_framework.exceptions import APIException
Scott Baker5f4770d2014-07-13 11:17:58 -07005
Scott Bakere791dc62014-08-28 14:02:54 -07006class SlicePlus(Slice, PlusObjectMixin):
Scott Baker88e34372014-07-13 11:46:36 -07007 class Meta:
8 proxy = True
9
Scott Bakera76f65d2015-01-13 16:22:57 -080010 def __init__(self, *args, **kwargs):
11 super(SlicePlus, self).__init__(*args, **kwargs)
Scott Bakera76f65d2015-01-13 16:22:57 -080012 self._update_users = None
Scott Baker7a76f322015-01-16 19:07:36 -080013 self._sliceInfo = None
Scott Baker55f6de62015-01-18 16:07:58 -080014 self.getSliceInfo()
15 self._site_allocation = self._sliceInfo["sitesUsed"]
16 self._initial_site_allocation = self._site_allocation
Scott Baker04ab7c82015-01-20 13:30:40 -080017 self._network_ports = self._sliceInfo["networkPorts"]
18 self._initial_network_ports = self._network_ports
Scott Bakera76f65d2015-01-13 16:22:57 -080019
Scott Baker5f4770d2014-07-13 11:17:58 -070020 def getSliceInfo(self, user=None):
Scott Baker7a76f322015-01-16 19:07:36 -080021 if not self._sliceInfo:
22 used_sites = {}
Scott Baker21909342015-01-22 15:21:24 -080023 ready_sites = {}
Scott Baker7a76f322015-01-16 19:07:36 -080024 used_deployments = {}
25 sliverCount = 0
Scott Bakerec930102015-01-20 01:02:08 -080026 sshCommands = []
Scott Baker7a76f322015-01-16 19:07:36 -080027 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 Baker5f4770d2014-07-13 11:17:58 -070033
Scott Bakerec930102015-01-20 01:02:08 -080034 if (sliver.instance_id and sliver.instance_name):
35 sshCommand = 'ssh -o "ProxyCommand ssh -q %s@%s" ubuntu@%s' % (sliver.instance_id, sliver.node.name, sliver.instance_name)
36 sshCommands.append(sshCommand);
37
Scott Baker21909342015-01-22 15:21:24 -080038 ready_sites[site.name] = ready_sites.get(site.name, 0) + 1
39
Scott Baker7a76f322015-01-16 19:07:36 -080040 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 Baker5f4770d2014-07-13 11:17:58 -070045
Scott Baker04ab7c82015-01-20 13:30:40 -080046 # 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 Baker7a76f322015-01-16 19:07:36 -080057 self._sliceInfo= {"sitesUsed": used_sites,
Scott Baker21909342015-01-22 15:21:24 -080058 "sitesReady": ready_sites,
Scott Baker7a76f322015-01-16 19:07:36 -080059 "deploymentsUsed": used_deployments,
60 "sliverCount": sliverCount,
61 "siteCount": len(used_sites.keys()),
62 "users": users,
Scott Bakerec930102015-01-20 01:02:08 -080063 "roles": [],
Scott Baker04ab7c82015-01-20 13:30:40 -080064 "sshCommands": sshCommands,
65 "networkPorts": networkPorts}
Scott Baker7a76f322015-01-16 19:07:36 -080066
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 Baker88e34372014-07-13 11:46:36 -070073
Scott Baker8b89d302015-01-08 22:34:51 -080074 @property
Scott Baker21909342015-01-22 15:21:24 -080075 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 Bakerdcf6fbf2015-01-11 13:45:19 -080083 def site_allocation(self):
Scott Baker55f6de62015-01-18 16:07:58 -080084 return self._site_allocation
Scott Bakerdcf6fbf2015-01-11 13:45:19 -080085
86 @site_allocation.setter
87 def site_allocation(self, value):
Scott Baker55f6de62015-01-18 16:07:58 -080088 self._site_allocation = value
Scott Bakerdcf6fbf2015-01-11 13:45:19 -080089
90 @property
Scott Baker7a76f322015-01-16 19:07:36 -080091 def user_names(self):
92 return [user["name"] for user in self.getSliceInfo()["users"].values()]
93
Scott Baker12154242015-01-16 19:26:54 -080094 @user_names.setter
95 def user_names(self, value):
96 pass # it's read-only
97
Scott Baker7a76f322015-01-16 19:07:36 -080098 @property
Scott Baker97acad92015-01-12 19:45:40 -080099 def users(self):
Scott Baker7a76f322015-01-16 19:07:36 -0800100 return [user["id"] for user in self.getSliceInfo()["users"].values()]
Scott Baker97acad92015-01-12 19:45:40 -0800101
102 @users.setter
103 def users(self, value):
Scott Bakera76f65d2015-01-13 16:22:57 -0800104 self._update_users = value
105 #print "XXX set users to", value
Scott Baker97acad92015-01-12 19:45:40 -0800106
107 @property
Scott Bakerd3a6b2c2015-01-08 22:37:34 -0800108 def network_ports(self):
Scott Baker04ab7c82015-01-20 13:30:40 -0800109 return self._network_ports
Scott Baker8b89d302015-01-08 22:34:51 -0800110
Scott Bakerd3a6b2c2015-01-08 22:37:34 -0800111 @network_ports.setter
112 def network_ports(self, value):
Scott Baker04ab7c82015-01-20 13:30:40 -0800113 self._network_ports = value
114 #print "XXX set networkPorts to", value
Scott Baker8b89d302015-01-08 22:34:51 -0800115
Scott Baker88e34372014-07-13 11:46:36 -0700116 @staticmethod
117 def select_by_user(user):
Scott Baker88e34372014-07-13 11:46:36 -0700118 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 Baker88e34372014-07-13 11:46:36 -0700123 return qs
Scott Bakera76f65d2015-01-13 16:22:57 -0800124
Scott Baker435c2c92015-01-14 00:34:45 -0800125 def get_node_allocation(self, siteList):
Scott Bakera76f65d2015-01-13 16:22:57 -0800126 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 Baker55f6de62015-01-18 16:07:58 -0800138 updated_image = self.has_field_changed("default_image")
139 updated_flavor = self.has_field_changed("default_flavor")
140
Scott Bakera76f65d2015-01-13 16:22:57 -0800141 super(SlicePlus, self).save(*args, **kwargs)
142
Scott Baker04ab7c82015-01-20 13:30:40 -0800143 # try things out first
144
Scott Baker55f6de62015-01-18 16:07:58 -0800145 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 Baker435c2c92015-01-14 00:34:45 -0800148
149 if self._update_users:
150 self.save_users(noAct=True)
151
Scott Baker04ab7c82015-01-20 13:30:40 -0800152 if (self._network_ports != self._initial_network_ports):
153 self.save_network_ports(noAct=True)
154
155 # now actually save them
156
Scott Baker55f6de62015-01-18 16:07:58 -0800157 if updated_sites:
158 self.save_site_allocation(reset=(updated_image or updated_flavor))
Scott Bakera76f65d2015-01-13 16:22:57 -0800159
Scott Baker435c2c92015-01-14 00:34:45 -0800160 if self._update_users:
161 self.save_users()
162
Scott Baker04ab7c82015-01-20 13:30:40 -0800163 if (self._network_ports != self._initial_network_ports):
164 self.save_network_ports()
165
Scott Baker55f6de62015-01-18 16:07:58 -0800166 def save_site_allocation(self, noAct = False, reset=False):
167 print "save_site_allocation, reset=",reset
Scott Bakera76f65d2015-01-13 16:22:57 -0800168
Scott Baker75081422015-01-19 08:43:50 -0800169 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 Baker435c2c92015-01-14 00:34:45 -0800174 all_slice_slivers = self.slivers.all()
Scott Baker55f6de62015-01-18 16:07:58 -0800175 for site_name in self._site_allocation.keys():
176 desired_allocation = self._site_allocation[site_name]
Scott Bakera76f65d2015-01-13 16:22:57 -0800177
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 Baker55f6de62015-01-18 16:07:58 -0800185 while (reset and len(slivers)>0) or (len(slivers) > desired_allocation):
Scott Bakera76f65d2015-01-13 16:22:57 -0800186 sliver = slivers.pop()
Scott Bakera76f65d2015-01-13 16:22:57 -0800187 if (not noAct):
Scott Baker55f6de62015-01-18 16:07:58 -0800188 print "deleting sliver", sliver
Scott Bakera76f65d2015-01-13 16:22:57 -0800189 sliver.delete()
Scott Baker55f6de62015-01-18 16:07:58 -0800190 else:
191 print "would delete sliver", sliver
Scott Bakera76f65d2015-01-13 16:22:57 -0800192
193 # add more slivers
194 if (len(slivers) < desired_allocation):
195 site = Site.objects.get(name = site_name)
Scott Baker435c2c92015-01-14 00:34:45 -0800196 nodes = self.get_node_allocation([site])
Scott Bakera76f65d2015-01-13 16:22:57 -0800197
198 if (not nodes):
Scott Baker3e035222015-01-24 00:01:29 -0800199 raise APIException(detail="no nodes in site %s" % site_name)
Scott Bakera76f65d2015-01-13 16:22:57 -0800200
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 Baker55f6de62015-01-18 16:07:58 -0800215 print "added sliver", sliver
Scott Bakera76f65d2015-01-13 16:22:57 -0800216 sliver.save()
Scott Baker55f6de62015-01-18 16:07:58 -0800217 else:
218 print "would add sliver", sliver
Scott Bakera76f65d2015-01-13 16:22:57 -0800219
220 node.sliverCount = node.sliverCount + 1
221
Scott Baker435c2c92015-01-14 00:34:45 -0800222 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 Baker435c2c92015-01-14 00:34:45 -0800232 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 Baker04ab7c82015-01-20 13:30:40 -0800249 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 Baker3e035222015-01-24 00:01:29 -0800277 raise APIException(detail="No network was found that ports could be set on")
Scott Baker04ab7c82015-01-20 13:30:40 -0800278
Scott Baker435c2c92015-01-14 00:34:45 -0800279
280
281
Scott Bakera76f65d2015-01-13 16:22:57 -0800282