blob: 6c84fdcb59768eb723cc306480c8b8e31d8e6650 [file] [log] [blame]
Matteo Scandoloede125b2017-08-08 13:05:25 -07001
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 Baker25467ff2016-08-04 09:50:22 -070017from django.http import HttpResponse, HttpResponseServerError
18from core.models import *
Scott Bakerc2a4d322016-08-09 09:16:57 -070019from rest_framework.views import APIView
Scott Baker25467ff2016-08-04 09:50:22 -070020from services.hpc.models import *
Scott Baker6ecad3b2016-08-09 09:01:55 -070021#from services.requestrouter.models import *
Scott Baker25467ff2016-08-04 09:50:22 -070022import xos.settings
23import json
24import os
25import time
26
27def get_service_slices(service):
28 try:
29 return service.slices.all()
30 except:
31 # this field used to be improperly named, and makemigrations won't fix it
32 return service.service.all()
33
Scott Bakerc2a4d322016-08-09 09:16:57 -070034class HpcConfig(APIView):
35 method_kind = "list"
36 method_name = "hpcconfig"
Scott Baker25467ff2016-08-04 09:50:22 -070037
Scott Bakerc2a4d322016-08-09 09:16:57 -070038 def get(self, request, format=None):
39 hpcSlice=None
40 cmiSlice=None
41 redirSlice=None
42 demuxSlice=None
Scott Baker25467ff2016-08-04 09:50:22 -070043
Scott Bakerc2a4d322016-08-09 09:16:57 -070044 node_slicename = request.GET.get("slicename", None)
45 if not node_slicename:
46 return HttpResponseServerError("Error: no slicename passed in request")
Scott Baker25467ff2016-08-04 09:50:22 -070047
Scott Bakerc2a4d322016-08-09 09:16:57 -070048 # search for an HPC Service that owns the slicename that was passed
49 # to us.
50 hpc=None
51 for candidate in HpcService.objects.all():
52 if candidate.cmi_hostname == node_slicename:
53 # A hack for standalone CMIs that aren't managed by XOS. Set
54 # /etc/slicename to cmi_hostname that's configured in the
55 # HPCService object.
Scott Baker25467ff2016-08-04 09:50:22 -070056 hpc = candidate
57
Scott Bakerc2a4d322016-08-09 09:16:57 -070058 for slice in get_service_slices(candidate):
59 if slice.name == node_slicename:
60 hpc = candidate
Scott Baker25467ff2016-08-04 09:50:22 -070061
Scott Bakerc2a4d322016-08-09 09:16:57 -070062 if (not hpc):
63 return HttpResponseServerError("Error: no HPC service")
Scott Baker25467ff2016-08-04 09:50:22 -070064
Scott Bakerc2a4d322016-08-09 09:16:57 -070065 for slice in get_service_slices(hpc):
66 if "cmi" in slice.name:
67 cmiSlice = slice
68 elif ("hpc" in slice.name) or ("vcoblitz" in slice.name):
69 hpcSlice = slice
70 elif "redir" in slice.name:
71 redirSlice = slice
72 elif "demux" in slice.name:
73 demuxSlice = slice
Scott Baker25467ff2016-08-04 09:50:22 -070074
Scott Bakerc2a4d322016-08-09 09:16:57 -070075 if (hpc.cmi_hostname):
76 cmi_hostname = hpc.cmi_hostname
77 else:
78 if not cmiSlice:
79 return HttpResponseServerError("Error: no CMI slice")
Scott Baker25467ff2016-08-04 09:50:22 -070080
Scott Bakerc2a4d322016-08-09 09:16:57 -070081 if len(cmiSlice.instances.all())==0:
82 return HttpResponseServerError("Error: CMI slice has no instances")
Scott Baker25467ff2016-08-04 09:50:22 -070083
Scott Bakerc2a4d322016-08-09 09:16:57 -070084 # for now, assuming using NAT
85 cmi_hostname = cmiSlice.instances.all()[0].node.name
Scott Baker25467ff2016-08-04 09:50:22 -070086
Scott Bakerc2a4d322016-08-09 09:16:57 -070087 if not hpcSlice:
88 return HttpResponseServerError("Error: no HPC slice")
Scott Baker25467ff2016-08-04 09:50:22 -070089
Scott Bakerc2a4d322016-08-09 09:16:57 -070090 # if (redirSlice==None) or (demuxSlice==None):
91 # # The HPC Service didn't have a dnsredir or a dnsdemux, so try looking
92 # # in the RequestRouterService for one.
93 #
94 # rr = RequestRouterService.objects.all()
95 # if not (rr):
96 # return HttpResponseServerError("Error: no RR service")
97 #
98 # rr = rr[0]
99 # try:
100 # slices = rr.slices.all()
101 # except:
102 # # this field used to be improperly named, and makemigrations won't fix it
103 # slices = rr.service.all()
104 # for slice in slices:
105 # if "redir" in slice.name:
106 # redirSlice = slice
107 # elif "demux" in slice.name:
108 # demuxSlice = slice
Scott Baker25467ff2016-08-04 09:50:22 -0700109
Scott Bakerc2a4d322016-08-09 09:16:57 -0700110 if not redirSlice:
111 return HttpResponseServerError("Error: no dnsredir slice")
Scott Baker25467ff2016-08-04 09:50:22 -0700112
Scott Bakerc2a4d322016-08-09 09:16:57 -0700113 if not demuxSlice:
114 return HttpResponseServerError("Error: no dnsdemux slice")
Scott Baker25467ff2016-08-04 09:50:22 -0700115
Scott Bakerc2a4d322016-08-09 09:16:57 -0700116 d = {}
117 d["hpc_slicename"] = hpcSlice.name
118 d["redir_slicename"] = redirSlice.name
119 d["demux_slicename"] = demuxSlice.name
120 d["cmi_hostname"] = cmi_hostname
121 d["xos_hostname"] = xos.settings.RESTAPI_HOSTNAME
122 d["xos_port"] = str(xos.settings.RESTAPI_PORT)
Scott Baker25467ff2016-08-04 09:50:22 -0700123
Scott Bakerc2a4d322016-08-09 09:16:57 -0700124 if hpc.hpc_port80:
125 d["hpc_port80"] = "True"
126 else:
127 d["hpc_port80"] = "False"
128
129 return HttpResponse("""# auto-generated by HpcConfig
130 ENABLE_PLC=False
131 ENABLE_PS=True
132 BASE_HRN="princeton"
133 RELEVANT_SERVICE_NAMES=['vcoblitz', 'coredirect', 'codnsdemux', "syndicate_comon_server"]
134 COBLITZ_SLICE_NAME=BASE_HRN+"_vcoblitz"
135 COBLITZ_SLICE_ID=70
136 COBLITZ_PS_SLICE_NAME="{hpc_slicename}"
137 DNSREDIR_SLICE_NAME=BASE_HRN+"_coredirect"
138 DNSREDIR_SLICE_ID=71
139 DNSREDIR_PS_SLICE_NAME="{redir_slicename}"
140 DNSDEMUX_SLICE_NAME=BASE_HRN+"_codnsdemux"
141 DNSDEMUX_SLICE_ID=69
142 DNSDEMUX_PS_SLICE_NAME="{demux_slicename}"
143 CMI_URL="http://{cmi_hostname}/"
144 CMI_HTTP_PORT="8004"
145 CMI_HTTPS_PORT="8003"
146 PUPPET_MASTER_HOSTNAME="{cmi_hostname}"
147 PUPPET_MASTER_PORT="8140"
148 PS_HOSTNAME="{xos_hostname}"
149 PS_PORT="{xos_port}"
150 COBLITZ_PORT_80={hpc_port80}
151 """.format(**d))
Scott Baker25467ff2016-08-04 09:50:22 -0700152