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 | 25467ff | 2016-08-04 09:50:22 -0700 | [diff] [blame] | 17 | from django.http import HttpResponse, HttpResponseServerError |
| 18 | from core.models import * |
Scott Baker | c2a4d32 | 2016-08-09 09:16:57 -0700 | [diff] [blame] | 19 | from rest_framework.views import APIView |
Scott Baker | 25467ff | 2016-08-04 09:50:22 -0700 | [diff] [blame] | 20 | from services.hpc.models import * |
Scott Baker | 6ecad3b | 2016-08-09 09:01:55 -0700 | [diff] [blame] | 21 | #from services.requestrouter.models import * |
Scott Baker | 25467ff | 2016-08-04 09:50:22 -0700 | [diff] [blame] | 22 | import xos.settings |
| 23 | import json |
| 24 | import os |
| 25 | import time |
| 26 | |
| 27 | def 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 Baker | c2a4d32 | 2016-08-09 09:16:57 -0700 | [diff] [blame] | 34 | class HpcConfig(APIView): |
| 35 | method_kind = "list" |
| 36 | method_name = "hpcconfig" |
Scott Baker | 25467ff | 2016-08-04 09:50:22 -0700 | [diff] [blame] | 37 | |
Scott Baker | c2a4d32 | 2016-08-09 09:16:57 -0700 | [diff] [blame] | 38 | def get(self, request, format=None): |
| 39 | hpcSlice=None |
| 40 | cmiSlice=None |
| 41 | redirSlice=None |
| 42 | demuxSlice=None |
Scott Baker | 25467ff | 2016-08-04 09:50:22 -0700 | [diff] [blame] | 43 | |
Scott Baker | c2a4d32 | 2016-08-09 09:16:57 -0700 | [diff] [blame] | 44 | node_slicename = request.GET.get("slicename", None) |
| 45 | if not node_slicename: |
| 46 | return HttpResponseServerError("Error: no slicename passed in request") |
Scott Baker | 25467ff | 2016-08-04 09:50:22 -0700 | [diff] [blame] | 47 | |
Scott Baker | c2a4d32 | 2016-08-09 09:16:57 -0700 | [diff] [blame] | 48 | # 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 Baker | 25467ff | 2016-08-04 09:50:22 -0700 | [diff] [blame] | 56 | hpc = candidate |
| 57 | |
Scott Baker | c2a4d32 | 2016-08-09 09:16:57 -0700 | [diff] [blame] | 58 | for slice in get_service_slices(candidate): |
| 59 | if slice.name == node_slicename: |
| 60 | hpc = candidate |
Scott Baker | 25467ff | 2016-08-04 09:50:22 -0700 | [diff] [blame] | 61 | |
Scott Baker | c2a4d32 | 2016-08-09 09:16:57 -0700 | [diff] [blame] | 62 | if (not hpc): |
| 63 | return HttpResponseServerError("Error: no HPC service") |
Scott Baker | 25467ff | 2016-08-04 09:50:22 -0700 | [diff] [blame] | 64 | |
Scott Baker | c2a4d32 | 2016-08-09 09:16:57 -0700 | [diff] [blame] | 65 | 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 Baker | 25467ff | 2016-08-04 09:50:22 -0700 | [diff] [blame] | 74 | |
Scott Baker | c2a4d32 | 2016-08-09 09:16:57 -0700 | [diff] [blame] | 75 | if (hpc.cmi_hostname): |
| 76 | cmi_hostname = hpc.cmi_hostname |
| 77 | else: |
| 78 | if not cmiSlice: |
| 79 | return HttpResponseServerError("Error: no CMI slice") |
Scott Baker | 25467ff | 2016-08-04 09:50:22 -0700 | [diff] [blame] | 80 | |
Scott Baker | c2a4d32 | 2016-08-09 09:16:57 -0700 | [diff] [blame] | 81 | if len(cmiSlice.instances.all())==0: |
| 82 | return HttpResponseServerError("Error: CMI slice has no instances") |
Scott Baker | 25467ff | 2016-08-04 09:50:22 -0700 | [diff] [blame] | 83 | |
Scott Baker | c2a4d32 | 2016-08-09 09:16:57 -0700 | [diff] [blame] | 84 | # for now, assuming using NAT |
| 85 | cmi_hostname = cmiSlice.instances.all()[0].node.name |
Scott Baker | 25467ff | 2016-08-04 09:50:22 -0700 | [diff] [blame] | 86 | |
Scott Baker | c2a4d32 | 2016-08-09 09:16:57 -0700 | [diff] [blame] | 87 | if not hpcSlice: |
| 88 | return HttpResponseServerError("Error: no HPC slice") |
Scott Baker | 25467ff | 2016-08-04 09:50:22 -0700 | [diff] [blame] | 89 | |
Scott Baker | c2a4d32 | 2016-08-09 09:16:57 -0700 | [diff] [blame] | 90 | # 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 Baker | 25467ff | 2016-08-04 09:50:22 -0700 | [diff] [blame] | 109 | |
Scott Baker | c2a4d32 | 2016-08-09 09:16:57 -0700 | [diff] [blame] | 110 | if not redirSlice: |
| 111 | return HttpResponseServerError("Error: no dnsredir slice") |
Scott Baker | 25467ff | 2016-08-04 09:50:22 -0700 | [diff] [blame] | 112 | |
Scott Baker | c2a4d32 | 2016-08-09 09:16:57 -0700 | [diff] [blame] | 113 | if not demuxSlice: |
| 114 | return HttpResponseServerError("Error: no dnsdemux slice") |
Scott Baker | 25467ff | 2016-08-04 09:50:22 -0700 | [diff] [blame] | 115 | |
Scott Baker | c2a4d32 | 2016-08-09 09:16:57 -0700 | [diff] [blame] | 116 | 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 Baker | 25467ff | 2016-08-04 09:50:22 -0700 | [diff] [blame] | 123 | |
Scott Baker | c2a4d32 | 2016-08-09 09:16:57 -0700 | [diff] [blame] | 124 | 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 Baker | 25467ff | 2016-08-04 09:50:22 -0700 | [diff] [blame] | 152 | |