Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 1 | #views.py |
Scott Baker | bd57a43 | 2014-04-14 16:12:15 -0700 | [diff] [blame] | 2 | import functools |
| 3 | import math |
Scott Baker | 771819b | 2014-03-19 22:10:17 -0700 | [diff] [blame] | 4 | import os |
| 5 | import sys |
Siobhan Tully | 06d2c03 | 2014-02-23 23:48:52 -0500 | [diff] [blame] | 6 | from django.views.generic import TemplateView, View |
| 7 | import datetime |
Scott Baker | 771819b | 2014-03-19 22:10:17 -0700 | [diff] [blame] | 8 | from pprint import pprint |
| 9 | import json |
Scott Baker | 168b741 | 2014-05-16 14:25:41 -0700 | [diff] [blame] | 10 | from syndicate.models import * |
Scott Baker | 537d823 | 2014-04-08 23:40:56 -0700 | [diff] [blame] | 11 | from core.models import * |
Scott Baker | e9f4ec9 | 2014-04-29 11:29:45 -0700 | [diff] [blame] | 12 | from hpc.models import ContentProvider |
Scott Baker | bd57a43 | 2014-04-14 16:12:15 -0700 | [diff] [blame] | 13 | from operator import attrgetter |
Scott Baker | 186ae61 | 2014-05-19 10:42:51 -0700 | [diff] [blame] | 14 | from django import template |
Scott Baker | bd57a43 | 2014-04-14 16:12:15 -0700 | [diff] [blame] | 15 | from django.views.decorators.csrf import csrf_exempt |
Scott Baker | 60fd8cd | 2014-05-21 18:02:44 -0700 | [diff] [blame] | 16 | from django.http import HttpResponse, HttpResponseServerError, HttpResponseForbidden |
Scott Baker | 537d823 | 2014-04-08 23:40:56 -0700 | [diff] [blame] | 17 | from django.core import urlresolvers |
Scott Baker | bd57a43 | 2014-04-14 16:12:15 -0700 | [diff] [blame] | 18 | from django.contrib.gis.geoip import GeoIP |
Scott Baker | 6b65420 | 2014-05-27 16:55:00 -0700 | [diff] [blame] | 19 | from django.db.models import Q |
Scott Baker | bd57a43 | 2014-04-14 16:12:15 -0700 | [diff] [blame] | 20 | from ipware.ip import get_ip |
Scott Baker | 2c3cb64 | 2014-05-19 17:55:56 -0700 | [diff] [blame] | 21 | from operator import itemgetter, attrgetter |
Scott Baker | 771819b | 2014-03-19 22:10:17 -0700 | [diff] [blame] | 22 | import traceback |
Scott Baker | 6253dfc | 2014-04-14 09:29:30 -0700 | [diff] [blame] | 23 | import socket |
Siobhan Tully | 06d2c03 | 2014-02-23 23:48:52 -0500 | [diff] [blame] | 24 | |
Scott Baker | a880bb5 | 2014-04-14 23:52:48 -0700 | [diff] [blame] | 25 | BLESSED_SITES = ["Stanford", "Washington", "Princeton", "GeorgiaTech", "MaxPlanck"] |
| 26 | |
Scott Baker | 771819b | 2014-03-19 22:10:17 -0700 | [diff] [blame] | 27 | if os.path.exists("/home/smbaker/projects/vicci/cdn/bigquery"): |
| 28 | sys.path.append("/home/smbaker/projects/vicci/cdn/bigquery") |
| 29 | else: |
| 30 | sys.path.append("/opt/planetstack/hpc_wizard") |
| 31 | import hpc_wizard |
Scott Baker | 75095b6 | 2014-04-21 17:32:09 -0700 | [diff] [blame] | 32 | from planetstack_analytics import DoPlanetStackAnalytics, PlanetStackAnalytics, RED_LOAD, BLUE_LOAD |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 33 | |
| 34 | class DashboardWelcomeView(TemplateView): |
| 35 | template_name = 'admin/dashboard/welcome.html' |
| 36 | |
| 37 | def get(self, request, *args, **kwargs): |
| 38 | context = self.get_context_data(**kwargs) |
Scott Baker | 2c3cb64 | 2014-05-19 17:55:56 -0700 | [diff] [blame] | 39 | context = getDashboardContext(request.user, context) |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 40 | return self.render_to_response(context=context) |
Siobhan Tully | e18b344 | 2014-02-23 14:23:34 -0500 | [diff] [blame] | 41 | |
Scott Baker | 2c3cb64 | 2014-05-19 17:55:56 -0700 | [diff] [blame] | 42 | class DashboardDynamicView(TemplateView): |
Scott Baker | 186ae61 | 2014-05-19 10:42:51 -0700 | [diff] [blame] | 43 | head_template = r"""{% extends "admin/dashboard/dashboard_base.html" %} |
| 44 | {% load admin_static %} |
| 45 | {% block content %} |
| 46 | """ |
| 47 | |
| 48 | tail_template = r"{% endblock %}" |
| 49 | |
Scott Baker | 2c3cb64 | 2014-05-19 17:55:56 -0700 | [diff] [blame] | 50 | def get(self, request, name="root", *args, **kwargs): |
Scott Baker | 186ae61 | 2014-05-19 10:42:51 -0700 | [diff] [blame] | 51 | context = self.get_context_data(**kwargs) |
Scott Baker | 2c3cb64 | 2014-05-19 17:55:56 -0700 | [diff] [blame] | 52 | context = getDashboardContext(request.user, context) |
Scott Baker | 186ae61 | 2014-05-19 10:42:51 -0700 | [diff] [blame] | 53 | |
Scott Baker | 2c3cb64 | 2014-05-19 17:55:56 -0700 | [diff] [blame] | 54 | if name=="root": |
| 55 | return self.multiDashboardView(request, context) |
| 56 | else: |
| 57 | return self.singleDashboardView(request, name, context) |
| 58 | |
| 59 | def readDashboard(self, fn): |
| 60 | try: |
| 61 | template= open("/opt/planetstack/templates/admin/dashboard/%s.html" % fn, "r").read() |
| 62 | if (fn=="tenant"): |
Scott Baker | 7237cdc | 2014-05-21 16:55:18 -0700 | [diff] [blame] | 63 | # fix for tenant view - it writes html to a div called tabs-5 |
Scott Baker | 2c3cb64 | 2014-05-19 17:55:56 -0700 | [diff] [blame] | 64 | template = '<div id="tabs-5"></div>' + template |
| 65 | return template |
| 66 | except: |
| 67 | return "failed to open %s" % fn |
| 68 | |
| 69 | def multiDashboardView(self, request, context): |
Scott Baker | fc06e78 | 2014-05-19 10:52:10 -0700 | [diff] [blame] | 70 | head_template = self.head_template |
| 71 | tail_template = self.tail_template |
| 72 | |
Scott Baker | 2c3cb64 | 2014-05-19 17:55:56 -0700 | [diff] [blame] | 73 | body = """ |
| 74 | <div id="hometabs" > |
| 75 | <ul id="suit_form_tabs" class="nav nav-tabs nav-tabs-suit" data-tab-prefix="suit-tab"> |
| 76 | """ |
Scott Baker | fc06e78 | 2014-05-19 10:52:10 -0700 | [diff] [blame] | 77 | |
Scott Baker | 2c3cb64 | 2014-05-19 17:55:56 -0700 | [diff] [blame] | 78 | dashboards = request.user.get_dashboards() |
Scott Baker | fc06e78 | 2014-05-19 10:52:10 -0700 | [diff] [blame] | 79 | |
Scott Baker | 2c3cb64 | 2014-05-19 17:55:56 -0700 | [diff] [blame] | 80 | # customize is a special dashboard they always get |
| 81 | customize = DashboardView.objects.filter(name="Customize") |
| 82 | if customize: |
| 83 | dashboards.append(customize[0]) |
Scott Baker | 186ae61 | 2014-05-19 10:42:51 -0700 | [diff] [blame] | 84 | |
Scott Baker | 2c3cb64 | 2014-05-19 17:55:56 -0700 | [diff] [blame] | 85 | for i,view in enumerate(dashboards): |
| 86 | body = body + '<li><a href="#dashtab-%d">%s</a></li>\n' % (i, view.name) |
Scott Baker | 186ae61 | 2014-05-19 10:42:51 -0700 | [diff] [blame] | 87 | |
Scott Baker | 2c3cb64 | 2014-05-19 17:55:56 -0700 | [diff] [blame] | 88 | body = body + "</ul>\n" |
| 89 | |
| 90 | for i,view in enumerate(dashboards): |
| 91 | url = view.url |
| 92 | body = body + '<div id="dashtab-%d">\n' % i |
| 93 | if url.startswith("template:"): |
| 94 | fn = url[9:] |
| 95 | body = body + self.readDashboard(fn) |
| 96 | body = body + '</div>\n' |
| 97 | |
| 98 | body=body+"</div>\n" |
| 99 | |
| 100 | t = template.Template(head_template + body + self.tail_template) |
Scott Baker | 186ae61 | 2014-05-19 10:42:51 -0700 | [diff] [blame] | 101 | |
| 102 | response_kwargs = {} |
| 103 | response_kwargs.setdefault('content_type', self.content_type) |
| 104 | return self.response_class(
|
| 105 | request = request,
|
| 106 | template = t,
|
| 107 | context = context,
|
| 108 | **response_kwargs) |
| 109 | |
Scott Baker | 2c3cb64 | 2014-05-19 17:55:56 -0700 | [diff] [blame] | 110 | def singleDashboardView(self, request, name, context): |
| 111 | head_template = self.head_template |
| 112 | tail_template = self.tail_template |
| 113 | |
Scott Baker | 6b65420 | 2014-05-27 16:55:00 -0700 | [diff] [blame] | 114 | t = template.Template(head_template + self.readDashboard(name) + self.tail_template) |
Scott Baker | 2c3cb64 | 2014-05-19 17:55:56 -0700 | [diff] [blame] | 115 | |
| 116 | response_kwargs = {} |
| 117 | response_kwargs.setdefault('content_type', self.content_type) |
| 118 | return self.response_class(
|
| 119 | request = request,
|
| 120 | template = t,
|
| 121 | context = context,
|
| 122 | **response_kwargs) |
| 123 | |
| 124 | def getDashboardContext(user, context={}, tableFormat = False): |
| 125 | context = {} |
Scott Baker | 771819b | 2014-03-19 22:10:17 -0700 | [diff] [blame] | 126 | |
| 127 | userSliceData = getSliceInfo(user) |
| 128 | if (tableFormat): |
Scott Baker | 2c3cb64 | 2014-05-19 17:55:56 -0700 | [diff] [blame] | 129 | context['userSliceInfo'] = userSliceTableFormatter(userSliceData) |
Scott Baker | 771819b | 2014-03-19 22:10:17 -0700 | [diff] [blame] | 130 | else: |
Scott Baker | 2c3cb64 | 2014-05-19 17:55:56 -0700 | [diff] [blame] | 131 | context['userSliceInfo'] = userSliceData |
| 132 | context['cdnData'] = getCDNOperatorData(wait=False) |
| 133 | context['cdnContentProviders'] = getCDNContentProviderData() |
| 134 | |
| 135 | (dashboards, unusedDashboards)= getDashboards(user) |
| 136 | unusedDashboards=[x for x in unusedDashboards if x!="Customize"] |
| 137 | context['dashboards'] = dashboards |
| 138 | context['unusedDashboards'] = unusedDashboards |
| 139 | |
| 140 | return context |
| 141 | |
| 142 | def getDashboards(user): |
| 143 | #dashboards = sorted(list(user.dashboardViews.all()), key=attrgetter('order')) |
| 144 | dashboards = user.get_dashboards() |
| 145 | |
| 146 | dashboard_names = [d.name for d in dashboards] |
| 147 | |
| 148 | unused_dashboard_names = [] |
| 149 | for dashboardView in DashboardView.objects.all(): |
| 150 | if not dashboardView.name in dashboard_names: |
| 151 | unused_dashboard_names.append(dashboardView.name) |
| 152 | |
| 153 | return (dashboard_names, unused_dashboard_names) |
Scott Baker | 771819b | 2014-03-19 22:10:17 -0700 | [diff] [blame] | 154 | |
Scott Baker | 537d823 | 2014-04-08 23:40:56 -0700 | [diff] [blame] | 155 | class TenantCreateSlice(View): |
| 156 | def post(self, request, *args, **kwargs): |
Scott Baker | 94319c5 | 2014-05-26 11:45:29 -0700 | [diff] [blame] | 157 | if request.user.isReadOnlyUser(): |
| 158 | return HttpResponseForbidden("User is in read-only mode") |
| 159 | |
Scott Baker | 537d823 | 2014-04-08 23:40:56 -0700 | [diff] [blame] | 160 | sliceName = request.POST.get("sliceName", "0") |
| 161 | serviceClass = request.POST.get("serviceClass", "0") |
| 162 | imageName = request.POST.get("imageName", "0") |
| 163 | actionToDo = request.POST.get("actionToDo", "0") |
Scott Baker | 9d9650d | 2014-05-23 11:50:54 -0700 | [diff] [blame] | 164 | networkPorts = request.POST.get("network","0") |
Scott Baker | edf2f67 | 2014-04-22 10:17:37 -0700 | [diff] [blame] | 165 | mountDataSets = request.POST.get("mountDataSets","0") |
Scott Baker | 9d9650d | 2014-05-23 11:50:54 -0700 | [diff] [blame] | 166 | privateVolume = request.POST.get("privateVolume","0") |
Scott Baker | 537d823 | 2014-04-08 23:40:56 -0700 | [diff] [blame] | 167 | if (actionToDo == "add"): |
| 168 | serviceClass = ServiceClass.objects.get(name=serviceClass) |
| 169 | site = request.user.site |
Scott Baker | 4b0cedd | 2014-04-22 21:01:21 -0700 | [diff] [blame] | 170 | image = Image.objects.get(name=imageName) |
Scott Baker | 168b741 | 2014-05-16 14:25:41 -0700 | [diff] [blame] | 171 | newSlice = Slice(name=sliceName,serviceClass=serviceClass,site=site,imagePreference=image,mountDataSets=mountDataSets) |
Scott Baker | 537d823 | 2014-04-08 23:40:56 -0700 | [diff] [blame] | 172 | newSlice.save() |
Scott Baker | 9d9650d | 2014-05-23 11:50:54 -0700 | [diff] [blame] | 173 | privateTemplate="Private" |
| 174 | publicTemplate="Public shared IPv4"
|
| 175 | privateNetworkName = sliceName+"-"+privateTemplate
|
| 176 | publicNetworkName = sliceName+"-"+publicTemplate
|
| 177 | slice=Slice.objects.get(name=sliceName)
|
| 178 | addNetwork(privateNetworkName,privateTemplate,slice)
|
| 179 | addNetwork(publicNetworkName,publicTemplate,slice)
|
| 180 | addOrModifyPorts(networkPorts,sliceName)
|
| 181 | if privateVolume=="true":
|
| 182 | privateVolForSlice(request.user,sliceName) |
Scott Baker | 4b0cedd | 2014-04-22 21:01:21 -0700 | [diff] [blame] | 183 | return HttpResponse("Slice created") |
Scott Baker | 537d823 | 2014-04-08 23:40:56 -0700 | [diff] [blame] | 184 | |
Scott Baker | 9d9650d | 2014-05-23 11:50:54 -0700 | [diff] [blame] | 185 | def privateVolForSlice(user,sliceName): |
| 186 | if not hasPrivateVolume(sliceName):
|
| 187 | volumeName=createPrivateVolume(user,sliceName)
|
| 188 | readWrite="true"
|
| 189 | mountVolume(sliceName,volumeName,readWrite) |
| 190 | |
Scott Baker | edf2f67 | 2014-04-22 10:17:37 -0700 | [diff] [blame] | 191 | class TenantUpdateSlice(View): |
| 192 | def post(self, request, *args, **kwargs):
|
Scott Baker | 94319c5 | 2014-05-26 11:45:29 -0700 | [diff] [blame] | 193 | if request.user.isReadOnlyUser():
|
| 194 | return HttpResponseForbidden("User is in read-only mode")
|
| 195 |
|
Scott Baker | edf2f67 | 2014-04-22 10:17:37 -0700 | [diff] [blame] | 196 | sliceName = request.POST.get("sliceName", "0")
|
| 197 | serviceClass = request.POST.get("serviceClass", "0")
|
| 198 | imageName = request.POST.get("imageName", "0")
|
| 199 | actionToDo = request.POST.get("actionToDo", "0")
|
Scott Baker | 9d9650d | 2014-05-23 11:50:54 -0700 | [diff] [blame] | 200 | networkPorts = request.POST.get("networkPorts","0")
|
Scott Baker | 4b0cedd | 2014-04-22 21:01:21 -0700 | [diff] [blame] | 201 | dataSet = request.POST.get("dataSet","0")
|
Scott Baker | 9d9650d | 2014-05-23 11:50:54 -0700 | [diff] [blame] | 202 | privateVolume = request.POST.get("privateVolume","0")
|
Scott Baker | 4b0cedd | 2014-04-22 21:01:21 -0700 | [diff] [blame] | 203 | slice = Slice.objects.all()
|
| 204 | for entry in slice:
|
| 205 | serviceClass = ServiceClass.objects.get(name=serviceClass)
|
| 206 | if(entry.name==sliceName):
|
| 207 | if (actionToDo == "update"):
|
| 208 | setattr(entry,'serviceClass',serviceClass)
|
| 209 | setattr(entry,'imagePreference',imageName)
|
Scott Baker | 4b0cedd | 2014-04-22 21:01:21 -0700 | [diff] [blame] | 210 | setattr(entry,'mountDataSets',dataSet)
|
| 211 | entry.save()
|
| 212 | break
|
Scott Baker | 9d9650d | 2014-05-23 11:50:54 -0700 | [diff] [blame] | 213 | addOrModifyPorts(networkPorts,sliceName)
|
| 214 | if privateVolume=="true":
|
| 215 | privateVolForSlice(request.user,sliceName)
|
Scott Baker | 4b0cedd | 2014-04-22 21:01:21 -0700 | [diff] [blame] | 216 | return HttpResponse("Slice updated")
|
| 217 |
|
Scott Baker | 9d9650d | 2014-05-23 11:50:54 -0700 | [diff] [blame] | 218 | def addNetwork(name,template,sliceName):
|
| 219 | networkTemplate=NetworkTemplate.objects.get(name=template)
|
| 220 | newNetwork = Network(name = name,
|
| 221 | template = networkTemplate,
|
| 222 | owner = sliceName)
|
| 223 | newNetwork.save()
|
| 224 | addNetworkSlice(newNetwork,sliceName)
|
| 225 |
|
| 226 | def addNetworkSlice(networkSlice,sliceName):
|
| 227 | newNetworkSlice=NetworkSlice(network =networkSlice,
|
| 228 | slice=sliceName)
|
| 229 | newNetworkSlice.save()
|
| 230 |
|
| 231 | def addOrModifyPorts(networkPorts,sliceName):
|
| 232 | networkList = Network.objects.all()
|
| 233 | networkInfo = []
|
| 234 | if networkPorts:
|
| 235 | for networkEntry in networkList:
|
| 236 | networkSlices = networkEntry.slices.all()
|
| 237 | for slice in networkSlices:
|
| 238 | if slice.name==sliceName:
|
| 239 | if networkEntry.template.name=="Public shared IPv4":
|
| 240 | setattr(networkEntry,'ports',networkPorts)
|
| 241 | networkEntry.save()
|
| 242 |
|
Scott Baker | 537d823 | 2014-04-08 23:40:56 -0700 | [diff] [blame] | 243 | def getTenantSliceInfo(user, tableFormat = False): |
| 244 | tenantSliceDetails = {} |
| 245 | tenantSliceData = getTenantInfo(user) |
| 246 | tenantServiceClassData = getServiceClassInfo(user) |
| 247 | if (tableFormat): |
| 248 | tenantSliceDetails['userSliceInfo'] = userSliceTableFormatter(tenantSliceData) |
| 249 | tenantSliceDetails['sliceServiceClass']=userSliceTableFormatter(tenantServiceClassData) |
| 250 | else: |
| 251 | tenantSliceDetails['userSliceInfo'] = tenantSliceData |
| 252 | tenantSliceDetails['sliceServiceClass']=userSliceTableFormatter(tenantServiceClassData) |
| 253 | tenantSliceDetails['image']=userSliceTableFormatter(getImageInfo(user)) |
Scott Baker | 537d823 | 2014-04-08 23:40:56 -0700 | [diff] [blame] | 254 | tenantSliceDetails['deploymentSites']=userSliceTableFormatter(getDeploymentSites()) |
Scott Baker | b85c26b | 2014-04-18 11:25:32 -0700 | [diff] [blame] | 255 | tenantSliceDetails['sites'] = userSliceTableFormatter(getTenantSitesInfo()) |
| 256 | tenantSliceDetails['mountDataSets'] = userSliceTableFormatter(getMountDataSets()) |
Scott Baker | 168b741 | 2014-05-16 14:25:41 -0700 | [diff] [blame] | 257 | tenantSliceDetails['publicKey'] = getPublicKey(user) |
Scott Baker | 537d823 | 2014-04-08 23:40:56 -0700 | [diff] [blame] | 258 | return tenantSliceDetails |
| 259 | |
Scott Baker | 537d823 | 2014-04-08 23:40:56 -0700 | [diff] [blame] | 260 | def getTenantInfo(user): |
| 261 | slices =Slice.objects.all() |
| 262 | userSliceInfo = [] |
| 263 | for entry in slices: |
| 264 | sliceName = Slice.objects.get(id=entry.id).name |
| 265 | slice = Slice.objects.get(name=Slice.objects.get(id=entry.id).name) |
| 266 | sliceServiceClass = entry.serviceClass.name |
| 267 | preferredImage = entry.imagePreference |
Scott Baker | 9d9650d | 2014-05-23 11:50:54 -0700 | [diff] [blame] | 268 | #sliceDataSet = entry.mountDataSets |
| 269 | sliceNetwork = {} |
Scott Baker | 537d823 | 2014-04-08 23:40:56 -0700 | [diff] [blame] | 270 | numSliver = 0 |
| 271 | sliceImage="" |
| 272 | sliceSite = {} |
Scott Baker | edf2f67 | 2014-04-22 10:17:37 -0700 | [diff] [blame] | 273 | sliceNode = {} |
| 274 | sliceInstance= {} |
Scott Baker | 9d9650d | 2014-05-23 11:50:54 -0700 | [diff] [blame] | 275 | #createPrivateVolume(user,sliceName) |
Scott Baker | 537d823 | 2014-04-08 23:40:56 -0700 | [diff] [blame] | 276 | for sliver in slice.slivers.all(): |
Scott Baker | a880bb5 | 2014-04-14 23:52:48 -0700 | [diff] [blame] | 277 | if sliver.node.site.name in BLESSED_SITES: |
Scott Baker | a880bb5 | 2014-04-14 23:52:48 -0700 | [diff] [blame] | 278 | sliceSite[sliver.node.site.name] = sliceSite.get(sliver.node.site.name,0) + 1 |
| 279 | sliceImage = sliver.image.name |
Scott Baker | b37ce33 | 2014-04-22 22:57:40 -0700 | [diff] [blame] | 280 | sliceNode[str(sliver)] = sliver.node.name |
Scott Baker | edf2f67 | 2014-04-22 10:17:37 -0700 | [diff] [blame] | 281 | numSliver = sum(sliceSite.values()) |
Scott Baker | b85c26b | 2014-04-18 11:25:32 -0700 | [diff] [blame] | 282 | numSites = len(sliceSite) |
Scott Baker | 9d9650d | 2014-05-23 11:50:54 -0700 | [diff] [blame] | 283 | userSliceInfo.append({'sliceName': sliceName,'sliceServiceClass': sliceServiceClass,'preferredImage':preferredImage,'numOfSites':numSites, 'sliceSite':sliceSite,'sliceImage':sliceImage,'numOfSlivers':numSliver,'instanceNodePair':sliceNode}) |
Scott Baker | 537d823 | 2014-04-08 23:40:56 -0700 | [diff] [blame] | 284 | return userSliceInfo |
| 285 | |
| 286 | def getTenantSitesInfo(): |
| 287 | tenantSiteInfo=[] |
| 288 | for entry in Site.objects.all(): |
Scott Baker | a880bb5 | 2014-04-14 23:52:48 -0700 | [diff] [blame] | 289 | if entry.name in BLESSED_SITES: |
| 290 | tenantSiteInfo.append({'siteName':entry.name}) |
Scott Baker | 537d823 | 2014-04-08 23:40:56 -0700 | [diff] [blame] | 291 | return tenantSiteInfo |
| 292 | |
Scott Baker | 771819b | 2014-03-19 22:10:17 -0700 | [diff] [blame] | 293 | def userSliceTableFormatter(data): |
| 294 | # pprint(data) |
| 295 | formattedData = { |
| 296 | 'rows' : data |
| 297 | } |
| 298 | return formattedData |
| 299 | |
Scott Baker | 168b741 | 2014-05-16 14:25:41 -0700 | [diff] [blame] | 300 | def getPublicKey(user): |
| 301 | users=User.objects.all()
|
| 302 | for key in users:
|
| 303 | if (str(key.email)==str(user)):
|
| 304 | sshKey = key.public_key
|
| 305 | return sshKey |
| 306 | |
Scott Baker | 537d823 | 2014-04-08 23:40:56 -0700 | [diff] [blame] | 307 | def getServiceClassInfo(user): |
| 308 | serviceClassList = ServiceClass.objects.all() |
| 309 | sliceInfo = [] |
| 310 | for entry in serviceClassList: |
| 311 | sliceInfo.append({'serviceClass':entry.name}) |
| 312 | return sliceInfo |
| 313 | |
| 314 | def getImageInfo(user): |
Scott Baker | 4b0cedd | 2014-04-22 21:01:21 -0700 | [diff] [blame] | 315 | imageList = Image.objects.all() |
| 316 | #imageList = ['Fedora 16 LXC rev 1.3','Hadoop','MPI'] |
Scott Baker | 537d823 | 2014-04-08 23:40:56 -0700 | [diff] [blame] | 317 | imageInfo = [] |
| 318 | for imageEntry in imageList: |
Scott Baker | 4b0cedd | 2014-04-22 21:01:21 -0700 | [diff] [blame] | 319 | imageInfo.append({'Image':imageEntry.name}) |
| 320 | #imageInfo.append({'Image':imageEntry}) |
Scott Baker | 537d823 | 2014-04-08 23:40:56 -0700 | [diff] [blame] | 321 | return imageInfo |
| 322 | |
Scott Baker | 168b741 | 2014-05-16 14:25:41 -0700 | [diff] [blame] | 323 | def createPrivateVolume(user, sliceName): |
| 324 | caps = Volume.CAP_READ_DATA | Volume.CAP_WRITE_DATA | Volume.CAP_HOST_DATA |
| 325 | getattr(Volume.default_gateway_caps,"read data") | \ |
| 326 | getattr(Volume.default_gateway_caps,"write data") | \ |
| 327 | getattr(Volume.default_gateway_caps,"host files") |
| 328 | v = Volume(name="private_" + sliceName, owner_id=user, description="private volume for %s" % sliceName, blocksize=61440, private=True, archive=False, default_gateway_caps = caps) |
| 329 | v.save() |
Scott Baker | 9d9650d | 2014-05-23 11:50:54 -0700 | [diff] [blame] | 330 | return v |
Scott Baker | 168b741 | 2014-05-16 14:25:41 -0700 | [diff] [blame] | 331 | |
| 332 | SYNDICATE_REPLICATE_PORTNUM = 1025 |
| 333 | |
| 334 | def get_free_port(): |
| 335 | inuse={} |
| 336 | inuse[SYNDICATE_REPLICATE_PORTNUM] = True |
| 337 | for vs in VolumeSlice.objects.all(): |
| 338 | inuse[vs.peer_portnum]=True |
| 339 | inuse[vs.replicate_portnum]=True |
| 340 | for network in Network.objects.all(): |
Scott Baker | 6b65420 | 2014-05-27 16:55:00 -0700 | [diff] [blame] | 341 | if not network.ports: |
Scott Baker | 9d9650d | 2014-05-23 11:50:54 -0700 | [diff] [blame] | 342 | continue |
Scott Baker | 168b741 | 2014-05-16 14:25:41 -0700 | [diff] [blame] | 343 | network_ports = [x.strip() for x in network.ports.split(",")] |
| 344 | for network_port in network_ports: |
| 345 | try: |
| 346 | inuse[int(network_port)] = True |
| 347 | except: |
| 348 | # in case someone has put a malformed port number in the list |
| 349 | pass |
| 350 | for i in range(1025, 65535): |
| 351 | if not inuse.get(i,False): |
| 352 | return i |
| 353 | return False |
| 354 | |
| 355 | def mountVolume(sliceName, volumeName, readWrite): |
| 356 | slice = Slice.objects.get(name=sliceName) |
| 357 | volume = Volume.objects.get(name=volumeName) |
| 358 | # choose some unused port numbers |
| 359 | flags = Volume.CAP_READ_DATA |
| 360 | if readWrite: |
| 361 | flags = flags | Volume.CAP_WRITE_DATA |
| 362 | vs = VolumeSlice(volume_id = volume, slice_id = slice, gateway_caps=flags, peer_portnum = get_free_port(), replicate_portnum = SYNDICATE_REPLICATE_PORTNUM) |
| 363 | vs.save() |
| 364 | |
| 365 | def hasPrivateVolume(sliceName): |
| 366 | slice = Slice.objects.get(name=sliceName) |
| 367 | for vs in VolumeSlice.objects.filter(slice_id=slice): |
| 368 | if vs.volume_id.private: |
| 369 | return True |
| 370 | return False |
| 371 | |
Scott Baker | b85c26b | 2014-04-18 11:25:32 -0700 | [diff] [blame] | 372 | def getMountDataSets(): |
Scott Baker | 168b741 | 2014-05-16 14:25:41 -0700 | [diff] [blame] | 373 | dataSetInfo=[]
|
| 374 | for volume in Volume.objects.all():
|
| 375 | if not volume.private:
|
| 376 | dataSetInfo.append({'DataSet': volume.name})
|
| 377 |
|
Scott Baker | b85c26b | 2014-04-18 11:25:32 -0700 | [diff] [blame] | 378 | return dataSetInfo |
| 379 | |
Scott Baker | 537d823 | 2014-04-08 23:40:56 -0700 | [diff] [blame] | 380 | def getDeploymentSites(): |
| 381 | deploymentList = Deployment.objects.all() |
| 382 | deploymentInfo = [] |
| 383 | for entry in deploymentList: |
| 384 | deploymentInfo.append({'DeploymentSite':entry.name}) |
| 385 | return deploymentInfo |
| 386 | |
Scott Baker | 771819b | 2014-03-19 22:10:17 -0700 | [diff] [blame] | 387 | def getSliceInfo(user): |
Siobhan Tully | 06d2c03 | 2014-02-23 23:48:52 -0500 | [diff] [blame] | 388 | sliceList = Slice.objects.all() |
Scott Baker | 771819b | 2014-03-19 22:10:17 -0700 | [diff] [blame] | 389 | slicePrivs = SlicePrivilege.objects.filter(user=user) |
Siobhan Tully | 06d2c03 | 2014-02-23 23:48:52 -0500 | [diff] [blame] | 390 | userSliceInfo = [] |
| 391 | for entry in slicePrivs: |
Siobhan Tully | e18b344 | 2014-02-23 14:23:34 -0500 | [diff] [blame] | 392 | |
Scott Baker | 771819b | 2014-03-19 22:10:17 -0700 | [diff] [blame] | 393 | slicename = Slice.objects.get(id=entry.slice.id).name |
Scott Baker | 537d823 | 2014-04-08 23:40:56 -0700 | [diff] [blame] | 394 | slice = Slice.objects.get(name=Slice.objects.get(id=entry.slice.id).name) |
| 395 | sliverList=Sliver.objects.all() |
| 396 | sites_used = {} |
| 397 | for sliver in slice.slivers.all(): |
| 398 | #sites_used['deploymentSites'] = sliver.node.deployment.name |
| 399 | # sites_used[sliver.image.name] = sliver.image.name |
| 400 | sites_used[sliver.node.site.name] = sliver.numberCores |
Scott Baker | 771819b | 2014-03-19 22:10:17 -0700 | [diff] [blame] | 401 | sliceid = Slice.objects.get(id=entry.slice.id).id |
Siobhan Tully | 06d2c03 | 2014-02-23 23:48:52 -0500 | [diff] [blame] | 402 | try: |
Scott Baker | 771819b | 2014-03-19 22:10:17 -0700 | [diff] [blame] | 403 | sliverList = Sliver.objects.filter(slice=entry.slice.id) |
| 404 | siteList = {} |
| 405 | for x in sliverList: |
| 406 | if x.node.site not in siteList: |
| 407 | siteList[x.node.site] = 1 |
| 408 | slivercount = len(sliverList) |
| 409 | sitecount = len(siteList) |
Siobhan Tully | 06d2c03 | 2014-02-23 23:48:52 -0500 | [diff] [blame] | 410 | except: |
Scott Baker | 771819b | 2014-03-19 22:10:17 -0700 | [diff] [blame] | 411 | traceback.print_exc() |
| 412 | slivercount = 0 |
| 413 | sitecount = 0 |
Siobhan Tully | 06d2c03 | 2014-02-23 23:48:52 -0500 | [diff] [blame] | 414 | |
Scott Baker | 771819b | 2014-03-19 22:10:17 -0700 | [diff] [blame] | 415 | userSliceInfo.append({'slicename': slicename, 'sliceid':sliceid, |
Scott Baker | 537d823 | 2014-04-08 23:40:56 -0700 | [diff] [blame] | 416 | 'sitesUsed':sites_used, |
| 417 | 'role': SliceRole.objects.get(id=entry.role.id).role, |
| 418 | 'slivercount': slivercount, |
| 419 | 'sitecount':sitecount}) |
Scott Baker | 771819b | 2014-03-19 22:10:17 -0700 | [diff] [blame] | 420 | |
Siobhan Tully | 06d2c03 | 2014-02-23 23:48:52 -0500 | [diff] [blame] | 421 | return userSliceInfo |
| 422 | |
Scott Baker | e9f4ec9 | 2014-04-29 11:29:45 -0700 | [diff] [blame] | 423 | def getCDNContentProviderData(): |
| 424 | cps = [] |
| 425 | for dm_cp in ContentProvider.objects.all(): |
| 426 | cp = {"name": dm_cp.name, |
| 427 | "account": dm_cp.account} |
| 428 | cps.append(cp) |
| 429 | |
| 430 | return cps |
| 431 | |
Scott Baker | 75095b6 | 2014-04-21 17:32:09 -0700 | [diff] [blame] | 432 | def getCDNOperatorData(randomizeData = False, wait=True): |
| 433 | HPC_SLICE_NAME = "HyperCache" |
| 434 | |
Scott Baker | 0017c10 | 2014-04-18 10:48:31 -0700 | [diff] [blame] | 435 | bq = PlanetStackAnalytics() |
Siobhan Tully | 06d2c03 | 2014-02-23 23:48:52 -0500 | [diff] [blame] | 436 | |
Scott Baker | 4a4472d | 2014-05-13 17:07:46 -0700 | [diff] [blame] | 437 | rows = bq.get_cached_query_results(bq.compose_cached_query(), wait) |
Siobhan Tully | 06d2c03 | 2014-02-23 23:48:52 -0500 | [diff] [blame] | 438 | |
Scott Baker | edf2f67 | 2014-04-22 10:17:37 -0700 | [diff] [blame] | 439 | # wait=False on the first time the Dashboard is opened. This means we might |
| 440 | # not have any rows yet. The dashboard code polls every 30 seconds, so it |
| 441 | # will eventually pick them up. |
Scott Baker | 0017c10 | 2014-04-18 10:48:31 -0700 | [diff] [blame] | 442 | |
Scott Baker | 75095b6 | 2014-04-21 17:32:09 -0700 | [diff] [blame] | 443 | if rows: |
Scott Baker | 696e266 | 2014-04-24 17:03:29 -0700 | [diff] [blame] | 444 | rows = bq.postprocess_results(rows, filter={"event": "hpc_heartbeat"}, maxi=["cpu"], count=["hostname"], computed=["bytes_sent/elapsed"], groupBy=["Time","site"], maxDeltaTime=80) |
Scott Baker | 0017c10 | 2014-04-18 10:48:31 -0700 | [diff] [blame] | 445 | |
Scott Baker | 75095b6 | 2014-04-21 17:32:09 -0700 | [diff] [blame] | 446 | # dictionaryize the statistics rows by site name |
| 447 | stats_rows = {} |
| 448 | for row in rows: |
| 449 | stats_rows[row["site"]] = row |
Scott Baker | edf2f67 | 2014-04-22 10:17:37 -0700 | [diff] [blame] | 450 | else: |
| 451 | stats_rows = {} |
Scott Baker | 0017c10 | 2014-04-18 10:48:31 -0700 | [diff] [blame] | 452 | |
Scott Baker | 09fad02 | 2014-04-30 09:50:01 -0700 | [diff] [blame] | 453 | slice = Slice.objects.filter(name=HPC_SLICE_NAME) |
| 454 | if slice: |
| 455 | slice_slivers = list(slice[0].slivers.all()) |
| 456 | else: |
| 457 | slice_slivers = [] |
Scott Baker | 0017c10 | 2014-04-18 10:48:31 -0700 | [diff] [blame] | 458 | |
| 459 | new_rows = {} |
Scott Baker | 75095b6 | 2014-04-21 17:32:09 -0700 | [diff] [blame] | 460 | for site in Site.objects.all(): |
| 461 | # compute number of slivers allocated in the data model |
| 462 | allocated_slivers = 0 |
| 463 | for sliver in slice_slivers: |
| 464 | if sliver.node.site == site: |
| 465 | allocated_slivers = allocated_slivers + 1 |
| 466 | |
| 467 | stats_row = stats_rows.get(site.name,{}) |
| 468 | |
| 469 | max_cpu = stats_row.get("max_avg_cpu", stats_row.get("max_cpu",0)) |
| 470 | cpu=float(max_cpu)/100.0 |
| 471 | hotness = max(0.0, ((cpu*RED_LOAD) - BLUE_LOAD)/(RED_LOAD-BLUE_LOAD)) |
| 472 | |
| 473 | # format it to what that CDN Operations View is expecting |
| 474 | new_row = {"lat": float(site.location.longitude), |
| 475 | "long": float(site.location.longitude), |
| 476 | "lat": float(site.location.latitude), |
Scott Baker | 0017c10 | 2014-04-18 10:48:31 -0700 | [diff] [blame] | 477 | "health": 0, |
Scott Baker | 75095b6 | 2014-04-21 17:32:09 -0700 | [diff] [blame] | 478 | "numNodes": int(site.nodes.count()), |
Scott Baker | e9f4ec9 | 2014-04-29 11:29:45 -0700 | [diff] [blame] | 479 | "activeHPCSlivers": int(stats_row.get("count_hostname", 0)), # measured number of slivers, from bigquery statistics |
Scott Baker | 75095b6 | 2014-04-21 17:32:09 -0700 | [diff] [blame] | 480 | "numHPCSlivers": allocated_slivers, # allocated number of slivers, from data model |
| 481 | "siteUrl": str(site.site_url), |
| 482 | "bandwidth": stats_row.get("sum_computed_bytes_sent_div_elapsed",0), |
| 483 | "load": max_cpu, |
| 484 | "hot": float(hotness)} |
| 485 | new_rows[str(site.name)] = new_row |
| 486 | |
| 487 | # get rid of sites with 0 slivers that overlap other sites with >0 slivers |
| 488 | for (k,v) in new_rows.items(): |
| 489 | bad=False |
| 490 | if v["numHPCSlivers"]==0: |
| 491 | for v2 in new_rows.values(): |
| 492 | if (v!=v2) and (v2["numHPCSlivers"]>=0): |
| 493 | d = haversine(v["lat"],v["long"],v2["lat"],v2["long"]) |
| 494 | if d<100: |
| 495 | bad=True |
| 496 | if bad: |
| 497 | del new_rows[k] |
Scott Baker | 0017c10 | 2014-04-18 10:48:31 -0700 | [diff] [blame] | 498 | |
| 499 | return new_rows |
Siobhan Tully | 06d2c03 | 2014-02-23 23:48:52 -0500 | [diff] [blame] | 500 | |
Scott Baker | 168b741 | 2014-05-16 14:25:41 -0700 | [diff] [blame] | 501 | def getPageSummary(request): |
| 502 | slice = request.GET.get('slice', None) |
| 503 | site = request.GET.get('site', None) |
| 504 | node = request.GET.get('node', None) |
| 505 | |
Scott Baker | 771819b | 2014-03-19 22:10:17 -0700 | [diff] [blame] | 506 | class SimulatorView(View): |
| 507 | def get(self, request, **kwargs): |
| 508 | sim = json.loads(file("/tmp/simulator.json","r").read()) |
| 509 | text = "<html><head></head><body>" |
| 510 | text += "Iteration: %d<br>" % sim["iteration"] |
| 511 | text += "Elapsed since report %d<br><br>" % sim["elapsed_since_report"] |
| 512 | text += "<table border=1>" |
| 513 | text += "<tr><th>site</th><th>trend</th><th>weight</th><th>bytes_sent</th><th>hot</th></tr>" |
| 514 | for site in sim["site_load"].values(): |
| 515 | text += "<tr>" |
| 516 | text += "<td>%s</td><td>%0.2f</td><td>%0.2f</td><td>%d</td><td>%0.2f</td>" % \ |
| 517 | (site["name"], site["trend"], site["weight"], site["bytes_sent"], site["load_frac"]) |
| 518 | text += "</tr>" |
| 519 | text += "</table>" |
| 520 | text += "</body></html>" |
| 521 | return HttpResponse(text) |
| 522 | |
| 523 | class DashboardUserSiteView(View): |
| 524 | def get(self, request, **kwargs): |
Scott Baker | 2c3cb64 | 2014-05-19 17:55:56 -0700 | [diff] [blame] | 525 | return HttpResponse(json.dumps(getDashboardContext(request.user, tableFormat=True)), mimetype='application/javascript') |
Scott Baker | 771819b | 2014-03-19 22:10:17 -0700 | [diff] [blame] | 526 | |
Scott Baker | 537d823 | 2014-04-08 23:40:56 -0700 | [diff] [blame] | 527 | class TenantViewData(View): |
| 528 | def get(self, request, **kwargs): |
| 529 | return HttpResponse(json.dumps(getTenantSliceInfo(request.user, True)), mimetype='application/javascript') |
| 530 | |
Scott Baker | 75095b6 | 2014-04-21 17:32:09 -0700 | [diff] [blame] | 531 | def haversine(site_lat, site_lon, lat, lon): |
Scott Baker | 75095b6 | 2014-04-21 17:32:09 -0700 | [diff] [blame] | 532 | d=0 |
| 533 | if lat and lon and site_lat and site_lon: |
Scott Baker | 4b0cedd | 2014-04-22 21:01:21 -0700 | [diff] [blame] | 534 | site_lat = float(site_lat) |
| 535 | site_lon = float(site_lon) |
| 536 | lat = float(lat) |
| 537 | lon = float(lon) |
Scott Baker | 75095b6 | 2014-04-21 17:32:09 -0700 | [diff] [blame] | 538 | R = 6378.1 |
| 539 | a = math.sin( math.radians((lat - site_lat)/2.0) )**2 + math.cos( math.radians(lat) )*math.cos( math.radians(site_lat) )*(math.sin( math.radians((lon - site_lon)/2.0 ) )**2) |
| 540 | c = 2 * math.atan2( math.sqrt(a), math.sqrt(1 - a) ) |
| 541 | d = R * c |
| 542 | |
| 543 | return d |
| 544 | |
Scott Baker | bd57a43 | 2014-04-14 16:12:15 -0700 | [diff] [blame] | 545 | def siteSortKey(site, slice=None, count=None, lat=None, lon=None): |
| 546 | # try to pick a site we're already using |
| 547 | has_slivers_here=False |
| 548 | if slice: |
| 549 | for sliver in slice.slivers.all(): |
| 550 | if sliver.node.site.name == site.name: |
| 551 | has_slivers_here=True |
| 552 | |
| 553 | # Haversine method |
Scott Baker | 75095b6 | 2014-04-21 17:32:09 -0700 | [diff] [blame] | 554 | d = haversine(site.location.latitude, site.location.longitude, lat, lon) |
Scott Baker | bd57a43 | 2014-04-14 16:12:15 -0700 | [diff] [blame] | 555 | |
| 556 | return (-has_slivers_here, d) |
| 557 | |
| 558 | def tenant_pick_sites(user, user_ip=None, slice=None, count=None): |
| 559 | """ Returns list of sites, sorted from most favorable to least favorable """ |
| 560 | lat=None |
| 561 | lon=None |
| 562 | try: |
| 563 | client_geo = GeoIP().city(user_ip) |
| 564 | if client_geo: |
| 565 | lat=float(client_geo["latitude"]) |
| 566 | lon=float(client_geo["longitude"]) |
| 567 | except: |
| 568 | print "exception in geo code" |
| 569 | traceback.print_exc() |
| 570 | |
| 571 | sites = Site.objects.all() |
Scott Baker | a880bb5 | 2014-04-14 23:52:48 -0700 | [diff] [blame] | 572 | sites = [x for x in sites if x.name in BLESSED_SITES] |
Scott Baker | bd57a43 | 2014-04-14 16:12:15 -0700 | [diff] [blame] | 573 | sites = sorted(sites, key=functools.partial(siteSortKey, slice=slice, count=count, lat=lat, lon=lon)) |
| 574 | |
| 575 | return sites |
| 576 | |
Scott Baker | fe6a7e1 | 2014-04-15 10:58:31 -0700 | [diff] [blame] | 577 | def slice_increase_slivers(user, user_ip, siteList, slice, count, noAct=False): |
Scott Baker | e4deddd | 2014-04-14 16:48:05 -0700 | [diff] [blame] | 578 | sitesChanged = {} |
| 579 | |
Scott Baker | bd57a43 | 2014-04-14 16:12:15 -0700 | [diff] [blame] | 580 | # let's compute how many slivers are in use in each node of each site |
| 581 | for site in siteList: |
| 582 | site.nodeList = list(site.nodes.all()) |
| 583 | for node in site.nodeList: |
| 584 | node.sliverCount = 0 |
Scott Baker | 6253dfc | 2014-04-14 09:29:30 -0700 | [diff] [blame] | 585 | for sliver in node.slivers.all(): |
Scott Baker | 2151194 | 2014-04-15 17:44:27 -0700 | [diff] [blame] | 586 | if sliver.slice.id == slice.id: |
Scott Baker | 0017c10 | 2014-04-18 10:48:31 -0700 | [diff] [blame] | 587 | node.sliverCount = node.sliverCount + 1 |
Scott Baker | 6253dfc | 2014-04-14 09:29:30 -0700 | [diff] [blame] | 588 | |
Scott Baker | bd57a43 | 2014-04-14 16:12:15 -0700 | [diff] [blame] | 589 | # Allocate slivers to nodes |
| 590 | # for now, assume we want to allocate all slivers from the same site |
| 591 | nodes = siteList[0].nodeList |
| 592 | while (count>0): |
| 593 | # Sort the node list by number of slivers per node, then pick the |
| 594 | # node with the least number of slivers. |
| 595 | nodes = sorted(nodes, key=attrgetter("sliverCount")) |
| 596 | node = nodes[0] |
Scott Baker | 6253dfc | 2014-04-14 09:29:30 -0700 | [diff] [blame] | 597 | |
Scott Baker | bd57a43 | 2014-04-14 16:12:15 -0700 | [diff] [blame] | 598 | print "adding sliver at node", node.name, "of site", node.site.name |
Scott Baker | 6253dfc | 2014-04-14 09:29:30 -0700 | [diff] [blame] | 599 | |
Scott Baker | e4deddd | 2014-04-14 16:48:05 -0700 | [diff] [blame] | 600 | if not noAct: |
| 601 | sliver = Sliver(name=node.name, |
| 602 | slice=slice, |
| 603 | node=node, |
| 604 | image = Image.objects.all()[0], |
| 605 | creator = User.objects.get(email=user), |
| 606 | deploymentNetwork=node.deployment, |
| 607 | numberCores =1 ) |
| 608 | sliver.save() |
Scott Baker | bd57a43 | 2014-04-14 16:12:15 -0700 | [diff] [blame] | 609 | |
| 610 | node.sliverCount = node.sliverCount + 1 |
| 611 | |
| 612 | count = count - 1 |
| 613 | |
Scott Baker | e4deddd | 2014-04-14 16:48:05 -0700 | [diff] [blame] | 614 | sitesChanged[node.site.name] = sitesChanged.get(node.site.name,0) + 1 |
| 615 | |
| 616 | return sitesChanged |
| 617 | |
Scott Baker | fe6a7e1 | 2014-04-15 10:58:31 -0700 | [diff] [blame] | 618 | def slice_decrease_slivers(user, siteList, slice, count, noAct=False): |
Scott Baker | e4deddd | 2014-04-14 16:48:05 -0700 | [diff] [blame] | 619 | sitesChanged = {} |
Scott Baker | b85c26b | 2014-04-18 11:25:32 -0700 | [diff] [blame] | 620 | sliverList ={} |
Scott Baker | bd57a43 | 2014-04-14 16:12:15 -0700 | [diff] [blame] | 621 | if siteList: |
| 622 | siteNames = [site.name for site in siteList] |
| 623 | else: |
| 624 | siteNames = None |
| 625 | |
| 626 | for sliver in slice.slivers.all(): |
Scott Baker | b85c26b | 2014-04-18 11:25:32 -0700 | [diff] [blame] | 627 | if(not siteNames) or (sliver.node.site.name in siteNames):
|
| 628 | node = sliver.node
|
| 629 | sliverList[sliver.name]=node.name |
Scott Baker | bd57a43 | 2014-04-14 16:12:15 -0700 | [diff] [blame] | 630 | |
Scott Baker | b85c26b | 2014-04-18 11:25:32 -0700 | [diff] [blame] | 631 | for key in sliverList: |
| 632 | if count>0: |
Scott Baker | f1b65c0 | 2014-04-21 01:03:17 -0700 | [diff] [blame] | 633 | sliver = Sliver.objects.filter(name=key)[0]
|
Scott Baker | b85c26b | 2014-04-18 11:25:32 -0700 | [diff] [blame] | 634 | sliver.delete()
|
| 635 | print "deleting sliver",sliverList[key],"at node",sliver.node.name
|
| 636 | count=count-1
|
Scott Baker | f1b65c0 | 2014-04-21 01:03:17 -0700 | [diff] [blame] | 637 | sitesChanged[sliver.node.site.name] = sitesChanged.get(sliver.node.site.name,0) - 1
|
Scott Baker | b85c26b | 2014-04-18 11:25:32 -0700 | [diff] [blame] | 638 |
|
Scott Baker | e4deddd | 2014-04-14 16:48:05 -0700 | [diff] [blame] | 639 | return sitesChanged |
| 640 | |
Scott Baker | b85c26b | 2014-04-18 11:25:32 -0700 | [diff] [blame] | 641 | class TenantDeleteSliceView(View): |
| 642 | def post(self,request):
|
Scott Baker | 94319c5 | 2014-05-26 11:45:29 -0700 | [diff] [blame] | 643 | if request.user.isReadOnlyUser():
|
| 644 | return HttpResponseForbidden("User is in read-only mode")
|
Scott Baker | b85c26b | 2014-04-18 11:25:32 -0700 | [diff] [blame] | 645 | sliceName = request.POST.get("sliceName",None)
|
| 646 | slice = Slice.objects.get(name=sliceName)
|
Scott Baker | 168b741 | 2014-05-16 14:25:41 -0700 | [diff] [blame] | 647 | #print slice, slice.id
|
Scott Baker | b85c26b | 2014-04-18 11:25:32 -0700 | [diff] [blame] | 648 | sliceToDel=Slice(name=sliceName, id=slice.id)
|
| 649 | sliceToDel.delete() |
Scott Baker | 4b0cedd | 2014-04-22 21:01:21 -0700 | [diff] [blame] | 650 | return HttpResponse("Slice deleted") |
Scott Baker | b85c26b | 2014-04-18 11:25:32 -0700 | [diff] [blame] | 651 | |
Scott Baker | 6253dfc | 2014-04-14 09:29:30 -0700 | [diff] [blame] | 652 | class TenantAddOrRemoveSliverView(View): |
Scott Baker | e4deddd | 2014-04-14 16:48:05 -0700 | [diff] [blame] | 653 | """ Add or remove slivers from a Slice |
| 654 | |
| 655 | Arguments: |
| 656 | siteName - name of site. If not specified, PlanetStack will pick the |
| 657 | best site., |
| 658 | actionToDo - [add | rem] |
| 659 | count - number of slivers to add or remove |
| 660 | sliceName - name of slice |
Scott Baker | a880bb5 | 2014-04-14 23:52:48 -0700 | [diff] [blame] | 661 | noAct - if set, no changes will be made to db, but result will still |
| 662 | show which sites would have been modified. |
Scott Baker | e4deddd | 2014-04-14 16:48:05 -0700 | [diff] [blame] | 663 | |
| 664 | Returns: |
| 665 | Dictionary of sites that were modified, and the count of nodes |
| 666 | that were added or removed at each site. |
| 667 | """ |
Scott Baker | 6253dfc | 2014-04-14 09:29:30 -0700 | [diff] [blame] | 668 | def post(self, request, *args, **kwargs): |
Scott Baker | bd57a43 | 2014-04-14 16:12:15 -0700 | [diff] [blame] | 669 | siteName = request.POST.get("siteName", None) |
| 670 | actionToDo = request.POST.get("actionToDo", None) |
| 671 | count = int(request.POST.get("count","0")) |
| 672 | sliceName = request.POST.get("slice", None) |
Scott Baker | e4deddd | 2014-04-14 16:48:05 -0700 | [diff] [blame] | 673 | noAct = request.POST.get("noAct", False) |
Scott Baker | bd57a43 | 2014-04-14 16:12:15 -0700 | [diff] [blame] | 674 | |
| 675 | if not sliceName: |
| 676 | return HttpResponseServerError("No slice name given") |
| 677 | |
| 678 | slice = Slice.objects.get(name=sliceName) |
| 679 | |
| 680 | if siteName: |
| 681 | siteList = [Site.objects.get(name=siteName)] |
| 682 | else: |
| 683 | siteList = None |
Scott Baker | 6253dfc | 2014-04-14 09:29:30 -0700 | [diff] [blame] | 684 | |
| 685 | if (actionToDo == "add"): |
Scott Baker | bd57a43 | 2014-04-14 16:12:15 -0700 | [diff] [blame] | 686 | user_ip = request.GET.get("ip", get_ip(request)) |
| 687 | if (siteList is None): |
| 688 | siteList = tenant_pick_sites(user, user_ip, slice, count) |
| 689 | |
Scott Baker | fe6a7e1 | 2014-04-15 10:58:31 -0700 | [diff] [blame] | 690 | sitesChanged = slice_increase_slivers(request.user, user_ip, siteList, slice, count, noAct) |
Scott Baker | 6253dfc | 2014-04-14 09:29:30 -0700 | [diff] [blame] | 691 | elif (actionToDo == "rem"): |
Scott Baker | fe6a7e1 | 2014-04-15 10:58:31 -0700 | [diff] [blame] | 692 | sitesChanged = slice_decrease_slivers(request.user, siteList, slice, count, noAct) |
Scott Baker | bd57a43 | 2014-04-14 16:12:15 -0700 | [diff] [blame] | 693 | else: |
| 694 | return HttpResponseServerError("Unknown actionToDo %s" % actionToDo) |
| 695 | |
Scott Baker | e4deddd | 2014-04-14 16:48:05 -0700 | [diff] [blame] | 696 | return HttpResponse(json.dumps(sitesChanged), mimetype='application/javascript') |
Scott Baker | bd57a43 | 2014-04-14 16:12:15 -0700 | [diff] [blame] | 697 | |
| 698 | def get(self, request, *args, **kwargs): |
| 699 | request.POST = request.GET |
| 700 | return self.post(request, *args, **kwargs) # for testing REST in browser |
| 701 | #return HttpResponseServerError("GET is not supported") |
| 702 | |
| 703 | class TenantPickSitesView(View): |
| 704 | """ primarily just for testing purposes """ |
| 705 | def get(self, request, *args, **kwargs): |
| 706 | count = request.GET.get("count","0") |
| 707 | slice = request.GET.get("slice",None) |
| 708 | if slice: |
| 709 | slice = Slice.objects.get(name=slice) |
| 710 | ip = request.GET.get("ip", get_ip(request)) |
| 711 | sites = tenant_pick_sites(request.user, user_ip=ip, count=0, slice=slice) |
| 712 | sites = [x.name for x in sites] |
| 713 | return HttpResponse(json.dumps(sites), mimetype='application/javascript') |
Scott Baker | 6253dfc | 2014-04-14 09:29:30 -0700 | [diff] [blame] | 714 | |
Scott Baker | 771819b | 2014-03-19 22:10:17 -0700 | [diff] [blame] | 715 | class DashboardSummaryAjaxView(View): |
| 716 | def get(self, request, **kwargs): |
Scott Baker | 0017c10 | 2014-04-18 10:48:31 -0700 | [diff] [blame] | 717 | def avg(x): |
Scott Baker | 111702b | 2014-05-15 16:17:16 -0700 | [diff] [blame] | 718 | if len(x)==0: |
| 719 | return 0 |
Scott Baker | 0017c10 | 2014-04-18 10:48:31 -0700 | [diff] [blame] | 720 | return float(sum(x))/len(x) |
| 721 | |
| 722 | sites = getCDNOperatorData().values() |
| 723 | |
| 724 | sites = [site for site in sites if site["numHPCSlivers"]>0] |
| 725 | |
| 726 | total_slivers = sum( [site["numHPCSlivers"] for site in sites] ) |
| 727 | total_bandwidth = sum( [site["bandwidth"] for site in sites] ) |
| 728 | average_cpu = int(avg( [site["load"] for site in sites] )) |
| 729 | |
| 730 | result= {"total_slivers": total_slivers, |
| 731 | "total_bandwidth": total_bandwidth, |
| 732 | "average_cpu": average_cpu} |
| 733 | |
| 734 | return HttpResponse(json.dumps(result), mimetype='application/javascript') |
Scott Baker | 771819b | 2014-03-19 22:10:17 -0700 | [diff] [blame] | 735 | |
| 736 | class DashboardAddOrRemoveSliverView(View): |
Scott Baker | fe6a7e1 | 2014-04-15 10:58:31 -0700 | [diff] [blame] | 737 | # TODO: deprecate this view in favor of using TenantAddOrRemoveSliverView |
| 738 | |
Scott Baker | 771819b | 2014-03-19 22:10:17 -0700 | [diff] [blame] | 739 | def post(self, request, *args, **kwargs): |
Scott Baker | fe6a7e1 | 2014-04-15 10:58:31 -0700 | [diff] [blame] | 740 | siteName = request.POST.get("site", None) |
Scott Baker | 771819b | 2014-03-19 22:10:17 -0700 | [diff] [blame] | 741 | actionToDo = request.POST.get("actionToDo", "0") |
| 742 | |
Scott Baker | fe6a7e1 | 2014-04-15 10:58:31 -0700 | [diff] [blame] | 743 | siteList = [Site.objects.get(name=siteName)] |
| 744 | slice = Slice.objects.get(name="HyperCache") |
| 745 | |
Scott Baker | d927d66 | 2014-05-22 09:29:06 -0700 | [diff] [blame] | 746 | if request.user.isReadOnlyUser(): |
| 747 | return HttpResponseForbidden("User is in read-only mode") |
| 748 | |
Scott Baker | 771819b | 2014-03-19 22:10:17 -0700 | [diff] [blame] | 749 | if (actionToDo == "add"): |
Scott Baker | fe6a7e1 | 2014-04-15 10:58:31 -0700 | [diff] [blame] | 750 | user_ip = request.GET.get("ip", get_ip(request)) |
| 751 | slice_increase_slivers(request.user, user_ip, siteList, slice, 1) |
Scott Baker | 771819b | 2014-03-19 22:10:17 -0700 | [diff] [blame] | 752 | elif (actionToDo == "rem"): |
Scott Baker | fe6a7e1 | 2014-04-15 10:58:31 -0700 | [diff] [blame] | 753 | slice_decrease_slivers(request.user, siteList, slice, 1) |
Scott Baker | 771819b | 2014-03-19 22:10:17 -0700 | [diff] [blame] | 754 | |
| 755 | print '*' * 50 |
| 756 | print 'Ask for site: ' + siteName + ' to ' + actionToDo + ' another HPC Sliver' |
Scott Baker | d927d66 | 2014-05-22 09:29:06 -0700 | [diff] [blame] | 757 | return HttpResponse(json.dumps("Success"), mimetype='application/javascript') |
Siobhan Tully | 06d2c03 | 2014-02-23 23:48:52 -0500 | [diff] [blame] | 758 | |
| 759 | class DashboardAjaxView(View): |
| 760 | def get(self, request, **kwargs): |
| 761 | return HttpResponse(json.dumps(getCDNOperatorData(True)), mimetype='application/javascript') |
Scott Baker | 771819b | 2014-03-19 22:10:17 -0700 | [diff] [blame] | 762 | |
| 763 | class DashboardAnalyticsAjaxView(View): |
| 764 | def get(self, request, name="hello_world", **kwargs): |
| 765 | if (name == "hpcSummary"): |
| 766 | return HttpResponse(json.dumps(hpc_wizard.get_hpc_wizard().get_summary_for_view()), mimetype='application/javascript') |
| 767 | elif (name == "hpcUserSite"): |
Scott Baker | 2c3cb64 | 2014-05-19 17:55:56 -0700 | [diff] [blame] | 768 | return HttpResponse(json.dumps(getDashboardContext(request.user, tableFormat=True)), mimetype='application/javascript') |
Scott Baker | 771819b | 2014-03-19 22:10:17 -0700 | [diff] [blame] | 769 | elif (name == "hpcMap"): |
| 770 | return HttpResponse(json.dumps(getCDNOperatorData(True)), mimetype='application/javascript') |
| 771 | elif (name == "bigquery"): |
| 772 | (mimetype, data) = DoPlanetStackAnalytics(request) |
| 773 | return HttpResponse(data, mimetype=mimetype) |
| 774 | else: |
| 775 | return HttpResponse(json.dumps("Unknown"), mimetype='application/javascript') |
| 776 | |
Scott Baker | 2c3cb64 | 2014-05-19 17:55:56 -0700 | [diff] [blame] | 777 | class DashboardCustomize(View): |
| 778 | def post(self, request, *args, **kwargs):
|
Scott Baker | 60fd8cd | 2014-05-21 18:02:44 -0700 | [diff] [blame] | 779 | if request.user.isReadOnlyUser():
|
| 780 | return HttpResponseForbidden("User is in read-only mode")
|
| 781 |
|
Scott Baker | 2c3cb64 | 2014-05-19 17:55:56 -0700 | [diff] [blame] | 782 | dashboards = request.POST.get("dashboards", None)
|
| 783 | if not dashboards:
|
Scott Baker | 2870e27 | 2014-05-19 18:14:54 -0700 | [diff] [blame] | 784 | dashboards=[]
|
| 785 | else:
|
| 786 | dashboards = [x.strip() for x in dashboards.split(",")]
|
| 787 | dashboards = [DashboardView.objects.get(name=x) for x in dashboards]
|
Scott Baker | 2c3cb64 | 2014-05-19 17:55:56 -0700 | [diff] [blame] | 788 |
|
| 789 | request.user.dashboardViews.all().delete()
|
| 790 |
|
| 791 | for i,dashboard in enumerate(dashboards):
|
| 792 | udbv = UserDashboardView(user=request.user, dashboardView=dashboard, order=i)
|
| 793 | udbv.save()
|
| 794 |
|
Scott Baker | 60fd8cd | 2014-05-21 18:02:44 -0700 | [diff] [blame] | 795 | return HttpResponse(json.dumps("Success"), mimetype='application/javascript')
|
Scott Baker | 2c3cb64 | 2014-05-19 17:55:56 -0700 | [diff] [blame] | 796 | |
Scott Baker | 6b65420 | 2014-05-27 16:55:00 -0700 | [diff] [blame] | 797 | class DashboardSliceInteractions(View): |
| 798 | def get(self, request, name="users", **kwargs): |
| 799 | colors = ["#005586", "#6ebe49", "orange", "#707170", "#00c4b3", "#077767", "dodgerblue", "#a79b94", "#c4e76a", "red"] |
| 800 | |
| 801 | groups = [] |
| 802 | matrix = [] |
| 803 | slices = list(Slice.objects.all()) |
| 804 | |
| 805 | slices = [x for x in slices if not self.isEmpty(x,name)] |
| 806 | |
| 807 | for i,slice in enumerate(slices): |
| 808 | groups.append({"name": slice.name, "color": colors[i%len(colors)]}) |
| 809 | matrix.append(self.buildMatrix(slice, slices, name)) |
| 810 | |
| 811 | result = {"groups": groups, "matrix": matrix} |
| 812 | |
| 813 | if name=="users": |
| 814 | result["title"] = "Slice interactions by user privilege" |
| 815 | result["objectName"] = "users" |
| 816 | elif name=="networks": |
| 817 | result["title"] = "Slice interactions by network membership" |
| 818 | result["objectName"] = "networks" |
| 819 | elif name=="sites": |
| 820 | result["title"] = "Slice interactions by site ownership" |
| 821 | result["objectName"] = "sites" |
| 822 | elif name=="sliver_sites": |
| 823 | result["title"] = "Slice interactions by sliver sites" |
| 824 | result["objectName"] = "sites" |
| 825 | elif name=="sliver_nodes": |
| 826 | result["title"] = "Slice interactions by sliver nodes" |
| 827 | result["objectName"] = "nodes" |
| 828 | |
| 829 | return HttpResponse(json.dumps(result), mimetype='application/javascript') |
| 830 | |
| 831 | def buildMatrix(self, slice, slices, name): |
| 832 | row = [] |
| 833 | for otherSlice in slices: |
| 834 | if (otherSlice == slice): |
| 835 | row.append(self.getCount(slice, name)) |
| 836 | else: |
| 837 | row.append(self.getInCommon(slice, otherSlice, name)) |
| 838 | return row |
| 839 | |
| 840 | def other_slice_sites(self, slice): |
| 841 | ids=[] |
| 842 | for sliver in Sliver.objects.all(): |
| 843 | if sliver.slice!=slice: |
| 844 | if sliver.node.site.id not in ids: |
| 845 | ids.append(sliver.node.site.id) |
| 846 | return ids |
| 847 | |
| 848 | def other_slice_nodes(self, slice): |
| 849 | ids=[] |
| 850 | for sliver in Sliver.objects.all(): |
| 851 | if sliver.slice!=slice: |
| 852 | if sliver.node.id not in ids: |
| 853 | ids.append(sliver.node.id) |
| 854 | return ids |
| 855 | |
| 856 | def getIds(self, slice, name, onlySelf=False): |
| 857 | ids=[] |
| 858 | if name=="users": |
| 859 | for sp in slice.slice_privileges.all(): |
| 860 | if (not onlySelf) or (len(sp.user.slice_privileges.all())==1): |
| 861 | if sp.user.id not in ids: |
| 862 | ids.append(sp.user.id) |
| 863 | elif name=="networks": |
| 864 | for sp in slice.networkslice_set.all(): |
| 865 | if (not onlySelf) or (len(sp.network.networkslice_set.all())==1): |
| 866 | if sp.network.id not in ids: |
| 867 | ids.append(sp.network.id) |
| 868 | elif name=="sites": |
| 869 | ids = [slice.site.id] |
| 870 | elif name=="sliver_sites": |
| 871 | for sp in slice.slivers.all(): |
| 872 | if sp.node.site.id not in ids: |
| 873 | ids.append(sp.node.site.id) |
| 874 | if onlySelf: |
| 875 | other_slice_sites = self.other_slice_sites(slice) |
| 876 | ids = [x for x in ids if x not in other_slice_sites] |
| 877 | elif name=="sliver_nodes": |
| 878 | for sp in slice.slivers.all(): |
| 879 | if sp.node.id not in ids: |
| 880 | ids.append(sp.node.id) |
| 881 | if onlySelf: |
| 882 | other_slice_nodes = self.other_slice_nodes(slice) |
| 883 | ids = [x for x in ids if x not in other_slice_nodes] |
| 884 | |
| 885 | return ids |
| 886 | |
| 887 | def inCommonIds(self, ids1, ids2): |
| 888 | count = 0 |
| 889 | for id in ids1: |
| 890 | if id in ids2: |
| 891 | count+=1 |
| 892 | return count |
| 893 | |
| 894 | def getCount(self, slice, name): |
| 895 | if (name in ["users", "networks", "sites", "sliver_nodes", "sliver_sites"]): |
| 896 | return len(self.getIds(slice,name,onlySelf=True)) |
| 897 | |
| 898 | def getInCommon(self, slice, otherSlice, name): |
| 899 | if (name in ["users", "networks", "sites", "sliver_nodes", "sliver_sites"]): |
| 900 | slice_ids = self.getIds(slice,name) |
| 901 | otherSlice_ids = self.getIds(otherSlice,name) |
| 902 | return self.inCommonIds(slice_ids, otherSlice_ids) |
| 903 | |
| 904 | def isEmpty(self, slice, name): |
| 905 | if (name in ["users", "networks", "sites", "sliver_nodes", "sliver_sites"]): |
| 906 | return (len(self.getIds(slice,name)) == 0) |
| 907 | |