Matteo Scandolo | ede125b | 2017-08-08 13:05:25 -0700 | [diff] [blame] | 1 | |
| 2 | # Copyright 2017-present Open Networking Foundation |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
| 16 | |
Scott Baker | 8df8166 | 2016-08-05 09:29:35 -0700 | [diff] [blame] | 17 | from rest_framework.decorators import api_view |
| 18 | from rest_framework.response import Response |
| 19 | from rest_framework.reverse import reverse |
| 20 | from rest_framework import serializers |
| 21 | from rest_framework import generics |
| 22 | from rest_framework.views import APIView |
| 23 | from core.models import * |
| 24 | from services.hpc.models import * |
Scott Baker | 6ecad3b | 2016-08-09 09:01:55 -0700 | [diff] [blame] | 25 | #from services.requestrouter.models import * |
Scott Baker | 8df8166 | 2016-08-05 09:29:35 -0700 | [diff] [blame] | 26 | from django.forms import widgets |
| 27 | from django.core.exceptions import PermissionDenied |
| 28 | from django.contrib.contenttypes.models import ContentType |
| 29 | import json |
| 30 | import socket |
| 31 | import time |
| 32 | |
Scott Baker | 8df8166 | 2016-08-05 09:29:35 -0700 | [diff] [blame] | 33 | def lookup_tag(service, instance, name, default=None): |
| 34 | instance_type = ContentType.objects.get_for_model(instance) |
| 35 | t = Tag.objects.filter(service=service, name=name, content_type__pk=instance_type.id, object_id=instance.id) |
| 36 | if t: |
| 37 | return t[0].value |
| 38 | else: |
| 39 | return default |
| 40 | |
| 41 | def lookup_time(service, instance, name): |
| 42 | v = lookup_tag(service, instance, name) |
| 43 | if v: |
| 44 | return str(time.time() - float(v)) |
| 45 | else: |
| 46 | return None |
| 47 | |
| 48 | def json_default(d, default): |
| 49 | if not d: |
| 50 | return default |
| 51 | return json.loads(d) |
| 52 | |
| 53 | def compute_config_run(d): |
| 54 | if not d: |
| 55 | return "null" |
| 56 | |
| 57 | try: |
| 58 | d = json.loads(d) |
| 59 | except: |
| 60 | return "error decoding json '%s'" % str(d) |
| 61 | |
| 62 | status = d.get("status", "null") |
| 63 | if status!="success": |
| 64 | return status |
| 65 | |
| 66 | config_run = d.get("config.run") |
| 67 | if not config_run: |
| 68 | return "null" |
| 69 | |
| 70 | try: |
| 71 | config_run = max(0, int(time.time()) - int(float(config_run))) |
| 72 | except: |
| 73 | pass |
| 74 | |
| 75 | return config_run |
| 76 | |
| 77 | # from hpc_watcher.py |
| 78 | def get_public_ip(service, instance): |
| 79 | network_name = None |
| 80 | if "hpc" in instance.slice.name: |
| 81 | network_name = getattr(service, "watcher_hpc_network", None) |
| 82 | elif "demux" in instance.slice.name: |
| 83 | network_name = getattr(service, "watcher_dnsdemux_network", None) |
| 84 | elif "redir" in instance.slice.name: |
| 85 | network_name = getattr(service, "watcher_dnsredir_network", None) |
| 86 | |
| 87 | if network_name and network_name.lower()=="nat": |
| 88 | return None |
| 89 | |
| 90 | if (network_name is None) or (network_name=="") or (network_name.lower()=="public"): |
| 91 | return instance.get_public_ip() |
| 92 | |
| 93 | for ns in instance.ports.all(): |
| 94 | if (ns.ip) and (ns.network.name==network_name): |
| 95 | return ns.ip |
| 96 | |
| 97 | raise ValueError("Couldn't find network %s" % str(network_name)) |
| 98 | |
| 99 | def getHpcDict(user, pk): |
| 100 | hpc = HpcService.objects.get(pk=pk) |
Scott Baker | 97ace87 | 2016-08-08 12:11:45 -0700 | [diff] [blame] | 101 | slices = hpc.slices.all() |
Scott Baker | 8df8166 | 2016-08-05 09:29:35 -0700 | [diff] [blame] | 102 | |
| 103 | dnsdemux_slice = None |
| 104 | dnsredir_slice = None |
| 105 | hpc_slice = None |
| 106 | for slice in slices: |
| 107 | if "dnsdemux" in slice.name: |
| 108 | dnsdemux_service = hpc |
| 109 | dnsdemux_slice = slice |
| 110 | if "dnsredir" in slice.name: |
| 111 | dnsredir_service = hpc |
| 112 | dnsredir_slice = slice |
| 113 | if "hpc" in slice.name: |
| 114 | hpc_service = hpc |
| 115 | hpc_slice = slice |
| 116 | |
Scott Baker | 6ecad3b | 2016-08-09 09:01:55 -0700 | [diff] [blame] | 117 | # if not dnsdemux_slice: |
| 118 | # rr = RequestRouterService.objects.all() |
| 119 | # if rr: |
| 120 | # rr=rr[0] |
| 121 | # slices = rr.slices.all() |
| 122 | # for slice in slices: |
| 123 | # if "dnsdemux" in slice.name: |
| 124 | # dnsdemux_service = rr |
| 125 | # dnsdemux_slice = slice |
| 126 | # if "dnsredir" in slice.name: |
| 127 | # dnsredir_service = rr |
| 128 | # dnsredir_slice = slice |
Scott Baker | 8df8166 | 2016-08-05 09:29:35 -0700 | [diff] [blame] | 129 | |
| 130 | if not dnsredir_slice: |
| 131 | print "no dnsredir slice" |
| 132 | return |
| 133 | |
| 134 | if not dnsdemux_slice: |
| 135 | print "no dnsdemux slice" |
| 136 | return |
| 137 | |
| 138 | #dnsdemux_has_public_network = False |
| 139 | #for network in dnsdemux_slice.networks.all(): |
| 140 | # if (network.template) and (network.template.visibility=="public") and (network.template.translation=="none"): |
| 141 | # dnsdemux_has_public_network = True |
| 142 | |
| 143 | nameservers = {} |
| 144 | for nshc in hpc.hpchealthcheck_set.filter(kind="nameserver"): |
| 145 | nameserver = nshc.resource_name |
| 146 | try: |
| 147 | nameservers[nameserver] = {"name": nameserver, "ip": socket.gethostbyname(nameserver), "hit": False} |
| 148 | except: |
| 149 | nameservers[nameserver] = {"name": nameserver, "ip": "exception", "hit": False} |
| 150 | |
| 151 | dnsdemux=[] |
| 152 | for instance in dnsdemux_slice.instances.all(): |
| 153 | ip=None |
| 154 | try: |
| 155 | ip = get_public_ip(dnsdemux_service, instance) |
| 156 | except Exception, e: |
| 157 | ip = "Exception: " + str(e) |
| 158 | if not ip: |
| 159 | try: |
| 160 | ip = socket.gethostbyname(instance.node.name) |
| 161 | except: |
| 162 | ip = "??? " + instance.node.name |
| 163 | |
| 164 | instance_nameservers = [] |
| 165 | for ns in nameservers.values(): |
| 166 | if ns["ip"]==ip: |
| 167 | instance_nameservers.append(ns["name"]) |
| 168 | ns["hit"]=True |
| 169 | |
| 170 | # now find the dnsredir instance that is also on this node |
| 171 | watcherd_dnsredir = "no-redir-instance" |
| 172 | for dnsredir_instance in dnsredir_slice.instances.all(): |
| 173 | if dnsredir_instance.node == instance.node: |
| 174 | watcherd_dnsredir = lookup_tag(dnsredir_service, dnsredir_instance, "watcher.watcher.msg") |
| 175 | |
| 176 | watcherd_dnsdemux = lookup_tag(dnsdemux_service, instance, "watcher.watcher.msg") |
| 177 | |
| 178 | dnsdemux.append( {"name": instance.node.name, |
| 179 | "watcher.DNS.msg": lookup_tag(dnsdemux_service, instance, "watcher.DNS.msg"), |
| 180 | "watcher.DNS.time": lookup_time(dnsdemux_service, instance, "watcher.DNS.time"), |
| 181 | "ip": ip, |
| 182 | "nameservers": instance_nameservers, |
| 183 | "dnsdemux_config_age": compute_config_run(watcherd_dnsdemux), |
| 184 | "dnsredir_config_age": compute_config_run(watcherd_dnsredir) }) |
| 185 | |
| 186 | hpc=[] |
| 187 | for instance in hpc_slice.instances.all(): |
| 188 | watcherd_hpc = lookup_tag(hpc_service, instance, "watcher.watcher.msg") |
| 189 | |
| 190 | hpc.append( {"name": instance.node.name, |
| 191 | "watcher.HPC-hb.msg": lookup_tag(hpc_service, instance, "watcher.HPC-hb.msg"), |
| 192 | "watcher.HPC-hb.time": lookup_time(hpc_service, instance, "watcher.HPC-hb.time"), |
| 193 | "watcher.HPC-fetch.msg": lookup_tag(hpc_service, instance, "watcher.HPC-fetch.msg"), |
| 194 | "watcher.HPC-fetch.time": lookup_time(hpc_service, instance, "watcher.HPC-fetch.time"), |
| 195 | "watcher.HPC-fetch.urls": json_default(lookup_tag(hpc_service, instance, "watcher.HPC-fetch-urls.msg"), []), |
| 196 | "config_age": compute_config_run(watcherd_hpc), |
| 197 | |
| 198 | }) |
| 199 | |
| 200 | return { "id": pk, |
| 201 | "dnsdemux": dnsdemux, |
| 202 | "hpc": hpc, |
| 203 | "nameservers": nameservers,} |
| 204 | |
| 205 | |
| 206 | class HpcList(APIView): |
| 207 | method_kind = "list" |
| 208 | method_name = "hpcview" |
| 209 | |
| 210 | def get(self, request, format=None): |
| 211 | if (not request.user.is_authenticated()): |
| 212 | raise PermissionDenied("You must be authenticated in order to use this API") |
| 213 | results = [] |
| 214 | for hpc in HpcService.objects.all(): |
| 215 | results.append(getHpcDict(request.user, hpc.pk)) |
| 216 | return Response( results ) |
| 217 | |
| 218 | class HpcDetail(APIView): |
| 219 | method_kind = "detail" |
| 220 | method_name = "hpcview" |
| 221 | |
| 222 | def get(self, request, format=None, pk=0): |
| 223 | if (not request.user.is_authenticated()): |
| 224 | raise PermissionDenied("You must be authenticated in order to use this API") |
| 225 | return Response( [getHpcDict(request.user, pk)] ) |
| 226 | |