blob: 9e82aec6dbf196d6ac08656918eff869b994f314 [file] [log] [blame]
Scott Baker8df81662016-08-05 09:29:35 -07001from rest_framework.decorators import api_view
2from rest_framework.response import Response
3from rest_framework.reverse import reverse
4from rest_framework import serializers
5from rest_framework import generics
6from rest_framework.views import APIView
7from core.models import *
8from services.hpc.models import *
Scott Baker6ecad3b2016-08-09 09:01:55 -07009#from services.requestrouter.models import *
Scott Baker8df81662016-08-05 09:29:35 -070010from django.forms import widgets
11from django.core.exceptions import PermissionDenied
12from django.contrib.contenttypes.models import ContentType
13import json
14import socket
15import time
16
Scott Baker8df81662016-08-05 09:29:35 -070017def lookup_tag(service, instance, name, default=None):
18 instance_type = ContentType.objects.get_for_model(instance)
19 t = Tag.objects.filter(service=service, name=name, content_type__pk=instance_type.id, object_id=instance.id)
20 if t:
21 return t[0].value
22 else:
23 return default
24
25def lookup_time(service, instance, name):
26 v = lookup_tag(service, instance, name)
27 if v:
28 return str(time.time() - float(v))
29 else:
30 return None
31
32def json_default(d, default):
33 if not d:
34 return default
35 return json.loads(d)
36
37def compute_config_run(d):
38 if not d:
39 return "null"
40
41 try:
42 d = json.loads(d)
43 except:
44 return "error decoding json '%s'" % str(d)
45
46 status = d.get("status", "null")
47 if status!="success":
48 return status
49
50 config_run = d.get("config.run")
51 if not config_run:
52 return "null"
53
54 try:
55 config_run = max(0, int(time.time()) - int(float(config_run)))
56 except:
57 pass
58
59 return config_run
60
61# from hpc_watcher.py
62def get_public_ip(service, instance):
63 network_name = None
64 if "hpc" in instance.slice.name:
65 network_name = getattr(service, "watcher_hpc_network", None)
66 elif "demux" in instance.slice.name:
67 network_name = getattr(service, "watcher_dnsdemux_network", None)
68 elif "redir" in instance.slice.name:
69 network_name = getattr(service, "watcher_dnsredir_network", None)
70
71 if network_name and network_name.lower()=="nat":
72 return None
73
74 if (network_name is None) or (network_name=="") or (network_name.lower()=="public"):
75 return instance.get_public_ip()
76
77 for ns in instance.ports.all():
78 if (ns.ip) and (ns.network.name==network_name):
79 return ns.ip
80
81 raise ValueError("Couldn't find network %s" % str(network_name))
82
83def getHpcDict(user, pk):
84 hpc = HpcService.objects.get(pk=pk)
Scott Baker97ace872016-08-08 12:11:45 -070085 slices = hpc.slices.all()
Scott Baker8df81662016-08-05 09:29:35 -070086
87 dnsdemux_slice = None
88 dnsredir_slice = None
89 hpc_slice = None
90 for slice in slices:
91 if "dnsdemux" in slice.name:
92 dnsdemux_service = hpc
93 dnsdemux_slice = slice
94 if "dnsredir" in slice.name:
95 dnsredir_service = hpc
96 dnsredir_slice = slice
97 if "hpc" in slice.name:
98 hpc_service = hpc
99 hpc_slice = slice
100
Scott Baker6ecad3b2016-08-09 09:01:55 -0700101# if not dnsdemux_slice:
102# rr = RequestRouterService.objects.all()
103# if rr:
104# rr=rr[0]
105# slices = rr.slices.all()
106# for slice in slices:
107# if "dnsdemux" in slice.name:
108# dnsdemux_service = rr
109# dnsdemux_slice = slice
110# if "dnsredir" in slice.name:
111# dnsredir_service = rr
112# dnsredir_slice = slice
Scott Baker8df81662016-08-05 09:29:35 -0700113
114 if not dnsredir_slice:
115 print "no dnsredir slice"
116 return
117
118 if not dnsdemux_slice:
119 print "no dnsdemux slice"
120 return
121
122 #dnsdemux_has_public_network = False
123 #for network in dnsdemux_slice.networks.all():
124 # if (network.template) and (network.template.visibility=="public") and (network.template.translation=="none"):
125 # dnsdemux_has_public_network = True
126
127 nameservers = {}
128 for nshc in hpc.hpchealthcheck_set.filter(kind="nameserver"):
129 nameserver = nshc.resource_name
130 try:
131 nameservers[nameserver] = {"name": nameserver, "ip": socket.gethostbyname(nameserver), "hit": False}
132 except:
133 nameservers[nameserver] = {"name": nameserver, "ip": "exception", "hit": False}
134
135 dnsdemux=[]
136 for instance in dnsdemux_slice.instances.all():
137 ip=None
138 try:
139 ip = get_public_ip(dnsdemux_service, instance)
140 except Exception, e:
141 ip = "Exception: " + str(e)
142 if not ip:
143 try:
144 ip = socket.gethostbyname(instance.node.name)
145 except:
146 ip = "??? " + instance.node.name
147
148 instance_nameservers = []
149 for ns in nameservers.values():
150 if ns["ip"]==ip:
151 instance_nameservers.append(ns["name"])
152 ns["hit"]=True
153
154 # now find the dnsredir instance that is also on this node
155 watcherd_dnsredir = "no-redir-instance"
156 for dnsredir_instance in dnsredir_slice.instances.all():
157 if dnsredir_instance.node == instance.node:
158 watcherd_dnsredir = lookup_tag(dnsredir_service, dnsredir_instance, "watcher.watcher.msg")
159
160 watcherd_dnsdemux = lookup_tag(dnsdemux_service, instance, "watcher.watcher.msg")
161
162 dnsdemux.append( {"name": instance.node.name,
163 "watcher.DNS.msg": lookup_tag(dnsdemux_service, instance, "watcher.DNS.msg"),
164 "watcher.DNS.time": lookup_time(dnsdemux_service, instance, "watcher.DNS.time"),
165 "ip": ip,
166 "nameservers": instance_nameservers,
167 "dnsdemux_config_age": compute_config_run(watcherd_dnsdemux),
168 "dnsredir_config_age": compute_config_run(watcherd_dnsredir) })
169
170 hpc=[]
171 for instance in hpc_slice.instances.all():
172 watcherd_hpc = lookup_tag(hpc_service, instance, "watcher.watcher.msg")
173
174 hpc.append( {"name": instance.node.name,
175 "watcher.HPC-hb.msg": lookup_tag(hpc_service, instance, "watcher.HPC-hb.msg"),
176 "watcher.HPC-hb.time": lookup_time(hpc_service, instance, "watcher.HPC-hb.time"),
177 "watcher.HPC-fetch.msg": lookup_tag(hpc_service, instance, "watcher.HPC-fetch.msg"),
178 "watcher.HPC-fetch.time": lookup_time(hpc_service, instance, "watcher.HPC-fetch.time"),
179 "watcher.HPC-fetch.urls": json_default(lookup_tag(hpc_service, instance, "watcher.HPC-fetch-urls.msg"), []),
180 "config_age": compute_config_run(watcherd_hpc),
181
182 })
183
184 return { "id": pk,
185 "dnsdemux": dnsdemux,
186 "hpc": hpc,
187 "nameservers": nameservers,}
188
189
190class HpcList(APIView):
191 method_kind = "list"
192 method_name = "hpcview"
193
194 def get(self, request, format=None):
195 if (not request.user.is_authenticated()):
196 raise PermissionDenied("You must be authenticated in order to use this API")
197 results = []
198 for hpc in HpcService.objects.all():
199 results.append(getHpcDict(request.user, hpc.pk))
200 return Response( results )
201
202class HpcDetail(APIView):
203 method_kind = "detail"
204 method_name = "hpcview"
205
206 def get(self, request, format=None, pk=0):
207 if (not request.user.is_authenticated()):
208 raise PermissionDenied("You must be authenticated in order to use this API")
209 return Response( [getHpcDict(request.user, pk)] )
210