blob: 9187bff235ab2664201aee011f8f6e6dd8cba75d [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 Baker5f4770d2014-07-13 11:17:58 -07004
Scott Bakere791dc62014-08-28 14:02:54 -07005class SlicePlus(Slice, PlusObjectMixin):
Scott Baker88e34372014-07-13 11:46:36 -07006 class Meta:
7 proxy = True
8
Scott Bakera76f65d2015-01-13 16:22:57 -08009 def __init__(self, *args, **kwargs):
10 super(SlicePlus, self).__init__(*args, **kwargs)
Scott Bakera76f65d2015-01-13 16:22:57 -080011 self._update_users = None
Scott Baker7a76f322015-01-16 19:07:36 -080012 self._sliceInfo = None
Scott Baker55f6de62015-01-18 16:07:58 -080013 self.getSliceInfo()
14 self._site_allocation = self._sliceInfo["sitesUsed"]
15 self._initial_site_allocation = self._site_allocation
Scott Bakera76f65d2015-01-13 16:22:57 -080016
Scott Baker5f4770d2014-07-13 11:17:58 -070017 def getSliceInfo(self, user=None):
Scott Baker7a76f322015-01-16 19:07:36 -080018 if not self._sliceInfo:
19 used_sites = {}
20 used_deployments = {}
21 sliverCount = 0
Scott Bakerec930102015-01-20 01:02:08 -080022 sshCommands = []
Scott Baker7a76f322015-01-16 19:07:36 -080023 for sliver in self.slivers.all():
24 site = sliver.node.site_deployment.site
25 deployment = sliver.node.site_deployment.deployment
26 used_sites[site.name] = used_sites.get(site.name, 0) + 1
27 used_deployments[deployment.name] = used_deployments.get(deployment.name, 0) + 1
28 sliverCount = sliverCount + 1
Scott Baker5f4770d2014-07-13 11:17:58 -070029
Scott Bakerec930102015-01-20 01:02:08 -080030 if (sliver.instance_id and sliver.instance_name):
31 sshCommand = 'ssh -o "ProxyCommand ssh -q %s@%s" ubuntu@%s' % (sliver.instance_id, sliver.node.name, sliver.instance_name)
32 sshCommands.append(sshCommand);
33
Scott Baker7a76f322015-01-16 19:07:36 -080034 users = {}
35 for priv in SlicePrivilege.objects.filter(slice=self):
36 if not (priv.user.id in users.keys()):
37 users[priv.user.id] = {"name": priv.user.email, "id": priv.user.id, "roles": []}
38 users[priv.user.id]["roles"].append(priv.role.role)
Scott Baker5f4770d2014-07-13 11:17:58 -070039
Scott Baker7a76f322015-01-16 19:07:36 -080040 self._sliceInfo= {"sitesUsed": used_sites,
41 "deploymentsUsed": used_deployments,
42 "sliverCount": sliverCount,
43 "siteCount": len(used_sites.keys()),
44 "users": users,
Scott Bakerec930102015-01-20 01:02:08 -080045 "roles": [],
46 "sshCommands": sshCommands}
Scott Baker7a76f322015-01-16 19:07:36 -080047
48 if user:
49 auser = self._sliceInfo["users"].get(user.id, None)
50 if (auser):
51 self._sliceInfo["roles"] = auser["roles"]
52
53 return self._sliceInfo
Scott Baker88e34372014-07-13 11:46:36 -070054
Scott Baker8b89d302015-01-08 22:34:51 -080055 @property
Scott Bakerdcf6fbf2015-01-11 13:45:19 -080056 def site_allocation(self):
Scott Baker55f6de62015-01-18 16:07:58 -080057 return self._site_allocation
Scott Bakerdcf6fbf2015-01-11 13:45:19 -080058
59 @site_allocation.setter
60 def site_allocation(self, value):
Scott Baker55f6de62015-01-18 16:07:58 -080061 self._site_allocation = value
Scott Bakerdcf6fbf2015-01-11 13:45:19 -080062
63 @property
Scott Baker7a76f322015-01-16 19:07:36 -080064 def user_names(self):
65 return [user["name"] for user in self.getSliceInfo()["users"].values()]
66
Scott Baker12154242015-01-16 19:26:54 -080067 @user_names.setter
68 def user_names(self, value):
69 pass # it's read-only
70
Scott Baker7a76f322015-01-16 19:07:36 -080071 @property
Scott Baker97acad92015-01-12 19:45:40 -080072 def users(self):
Scott Baker7a76f322015-01-16 19:07:36 -080073 return [user["id"] for user in self.getSliceInfo()["users"].values()]
Scott Baker97acad92015-01-12 19:45:40 -080074
75 @users.setter
76 def users(self, value):
Scott Bakera76f65d2015-01-13 16:22:57 -080077 self._update_users = value
78 #print "XXX set users to", value
Scott Baker97acad92015-01-12 19:45:40 -080079
80 @property
Scott Bakerd3a6b2c2015-01-08 22:37:34 -080081 def network_ports(self):
Scott Baker8b89d302015-01-08 22:34:51 -080082 # XXX this assumes there is only one network that can have ports bound
83 # to it for a given slice. This is intended for the tenant view, which
84 # will obey this field.
85 networkPorts = ""
86 for networkSlice in self.networkslices.all():
87 network = networkSlice.network
88 if network.ports:
89 networkPorts = network.ports
90
91 return networkPorts
92
Scott Bakerd3a6b2c2015-01-08 22:37:34 -080093 @network_ports.setter
94 def network_ports(self, value):
Scott Baker8b89d302015-01-08 22:34:51 -080095 print "XXX set networkPorts to", value
96
Scott Baker88e34372014-07-13 11:46:36 -070097 @staticmethod
98 def select_by_user(user):
Scott Baker88e34372014-07-13 11:46:36 -070099 if user.is_admin:
100 qs = SlicePlus.objects.all()
101 else:
102 slice_ids = [sp.slice.id for sp in SlicePrivilege.objects.filter(user=user)]
103 qs = SlicePlus.objects.filter(id__in=slice_ids)
Scott Baker88e34372014-07-13 11:46:36 -0700104 return qs
Scott Bakera76f65d2015-01-13 16:22:57 -0800105
Scott Baker435c2c92015-01-14 00:34:45 -0800106 def get_node_allocation(self, siteList):
Scott Bakera76f65d2015-01-13 16:22:57 -0800107 siteIDList = [site.id for site in siteList]
108 nodeList = []
109 for node in Node.objects.all():
110 if (node.site_deployment.site.id in siteIDList):
111 node.sliverCount = 0
112 for sliver in node.slivers.all():
113 if sliver.slice.id == self.id:
114 node.sliverCount = node.sliverCount + 1
115 nodeList.append(node)
116 return nodeList
117
118 def save(self, *args, **kwargs):
Scott Baker55f6de62015-01-18 16:07:58 -0800119 updated_image = self.has_field_changed("default_image")
120 updated_flavor = self.has_field_changed("default_flavor")
121
Scott Bakera76f65d2015-01-13 16:22:57 -0800122 super(SlicePlus, self).save(*args, **kwargs)
123
Scott Baker55f6de62015-01-18 16:07:58 -0800124 updated_sites = (self._site_allocation != self._initial_site_allocation) or updated_image or updated_flavor
125 if updated_sites:
126 self.save_site_allocation(noAct=True, reset=(updated_image or updated_flavor))
Scott Baker435c2c92015-01-14 00:34:45 -0800127
128 if self._update_users:
129 self.save_users(noAct=True)
130
Scott Baker55f6de62015-01-18 16:07:58 -0800131 if updated_sites:
132 self.save_site_allocation(reset=(updated_image or updated_flavor))
Scott Bakera76f65d2015-01-13 16:22:57 -0800133
Scott Baker435c2c92015-01-14 00:34:45 -0800134 if self._update_users:
135 self.save_users()
136
Scott Baker55f6de62015-01-18 16:07:58 -0800137 def save_site_allocation(self, noAct = False, reset=False):
138 print "save_site_allocation, reset=",reset
Scott Bakera76f65d2015-01-13 16:22:57 -0800139
Scott Baker75081422015-01-19 08:43:50 -0800140 if (not self._site_allocation):
141 # Must be a sliver that was just created, and has not site_allocation
142 # field.
143 return
144
Scott Baker435c2c92015-01-14 00:34:45 -0800145 all_slice_slivers = self.slivers.all()
Scott Baker55f6de62015-01-18 16:07:58 -0800146 for site_name in self._site_allocation.keys():
147 desired_allocation = self._site_allocation[site_name]
Scott Bakera76f65d2015-01-13 16:22:57 -0800148
149 # make a list of the slivers for this site
150 slivers = []
151 for sliver in all_slice_slivers:
152 if sliver.node.site_deployment.site.name == site_name:
153 slivers.append(sliver)
154
155 # delete extra slivers
Scott Baker55f6de62015-01-18 16:07:58 -0800156 while (reset and len(slivers)>0) or (len(slivers) > desired_allocation):
Scott Bakera76f65d2015-01-13 16:22:57 -0800157 sliver = slivers.pop()
Scott Bakera76f65d2015-01-13 16:22:57 -0800158 if (not noAct):
Scott Baker55f6de62015-01-18 16:07:58 -0800159 print "deleting sliver", sliver
Scott Bakera76f65d2015-01-13 16:22:57 -0800160 sliver.delete()
Scott Baker55f6de62015-01-18 16:07:58 -0800161 else:
162 print "would delete sliver", sliver
Scott Bakera76f65d2015-01-13 16:22:57 -0800163
164 # add more slivers
165 if (len(slivers) < desired_allocation):
166 site = Site.objects.get(name = site_name)
Scott Baker435c2c92015-01-14 00:34:45 -0800167 nodes = self.get_node_allocation([site])
Scott Bakera76f65d2015-01-13 16:22:57 -0800168
169 if (not nodes):
Scott Baker1f7901b2015-01-13 16:28:05 -0800170 raise ValueError("no nodes in site %s" % site_name)
Scott Bakera76f65d2015-01-13 16:22:57 -0800171
172 while (len(slivers) < desired_allocation):
173 # pick the least allocated node
174 nodes = sorted(nodes, key=attrgetter("sliverCount"))
175 node = nodes[0]
176
177 sliver = Sliver(name=node.name,
178 slice=self,
179 node=node,
180 image = self.default_image,
181 flavor = self.default_flavor,
182 creator = self.creator,
183 deployment = node.site_deployment.deployment)
184 slivers.append(sliver)
185 if (not noAct):
Scott Baker55f6de62015-01-18 16:07:58 -0800186 print "added sliver", sliver
Scott Bakera76f65d2015-01-13 16:22:57 -0800187 sliver.save()
Scott Baker55f6de62015-01-18 16:07:58 -0800188 else:
189 print "would add sliver", sliver
Scott Bakera76f65d2015-01-13 16:22:57 -0800190
191 node.sliverCount = node.sliverCount + 1
192
Scott Baker435c2c92015-01-14 00:34:45 -0800193 def save_users(self, noAct = False):
194 new_users = self._update_users
195
196 default_role = SliceRole.objects.get(role="default")
197
198 slice_privs = self.sliceprivileges.all()
199 slice_user_ids = [priv.user.id for priv in slice_privs]
200
201 for user_id in new_users:
202 if (user_id not in slice_user_ids):
Scott Baker435c2c92015-01-14 00:34:45 -0800203 priv = SlicePrivilege(slice=self, user=User.objects.get(id=user_id), role=default_role)
204 if (not noAct):
205 priv.save()
206
207 print "added user id", user_id
208
209 for priv in slice_privs:
210 if (priv.role.id != default_role.id):
211 # only mess with 'default' users; don't kill an admin
212 continue
213
214 if (priv.user.id not in new_users):
215 if (not noAct):
216 priv.delete()
217
218 print "deleted user id", user_id
219
220
221
222
Scott Bakera76f65d2015-01-13 16:22:57 -0800223