Scott Baker | 25467ff | 2016-08-04 09:50:22 -0700 | [diff] [blame] | 1 | from django.http import HttpResponse, HttpResponseServerError |
| 2 | from core.models import * |
| 3 | from services.hpc.models import * |
Scott Baker | 6ecad3b | 2016-08-09 09:01:55 -0700 | [diff] [blame^] | 4 | #from services.requestrouter.models import * |
Scott Baker | 25467ff | 2016-08-04 09:50:22 -0700 | [diff] [blame] | 5 | import xos.settings |
| 6 | import json |
| 7 | import os |
| 8 | import time |
| 9 | |
| 10 | def get_service_slices(service): |
| 11 | try: |
| 12 | return service.slices.all() |
| 13 | except: |
| 14 | # this field used to be improperly named, and makemigrations won't fix it |
| 15 | return service.service.all() |
| 16 | |
| 17 | def HpcConfig(request): |
| 18 | hpcSlice=None |
| 19 | cmiSlice=None |
| 20 | redirSlice=None |
| 21 | demuxSlice=None |
| 22 | |
| 23 | node_slicename = request.GET.get("slicename", None) |
| 24 | if not node_slicename: |
| 25 | return HttpResponseServerError("Error: no slicename passed in request") |
| 26 | |
| 27 | # search for an HPC Service that owns the slicename that was passed |
| 28 | # to us. |
| 29 | hpc=None |
| 30 | for candidate in HpcService.objects.all(): |
| 31 | if candidate.cmi_hostname == node_slicename: |
| 32 | # A hack for standalone CMIs that aren't managed by XOS. Set |
| 33 | # /etc/slicename to cmi_hostname that's configured in the |
| 34 | # HPCService object. |
| 35 | hpc = candidate |
| 36 | |
| 37 | for slice in get_service_slices(candidate): |
| 38 | if slice.name == node_slicename: |
| 39 | hpc = candidate |
| 40 | |
| 41 | if (not hpc): |
| 42 | return HttpResponseServerError("Error: no HPC service") |
| 43 | |
| 44 | for slice in get_service_slices(hpc): |
| 45 | if "cmi" in slice.name: |
| 46 | cmiSlice = slice |
| 47 | elif ("hpc" in slice.name) or ("vcoblitz" in slice.name): |
| 48 | hpcSlice = slice |
| 49 | elif "redir" in slice.name: |
| 50 | redirSlice = slice |
| 51 | elif "demux" in slice.name: |
| 52 | demuxSlice = slice |
| 53 | |
| 54 | if (hpc.cmi_hostname): |
| 55 | cmi_hostname = hpc.cmi_hostname |
| 56 | else: |
| 57 | if not cmiSlice: |
| 58 | return HttpResponseServerError("Error: no CMI slice") |
| 59 | |
| 60 | if len(cmiSlice.instances.all())==0: |
| 61 | return HttpResponseServerError("Error: CMI slice has no instances") |
| 62 | |
| 63 | # for now, assuming using NAT |
| 64 | cmi_hostname = cmiSlice.instances.all()[0].node.name |
| 65 | |
| 66 | if not hpcSlice: |
| 67 | return HttpResponseServerError("Error: no HPC slice") |
| 68 | |
Scott Baker | 6ecad3b | 2016-08-09 09:01:55 -0700 | [diff] [blame^] | 69 | # if (redirSlice==None) or (demuxSlice==None): |
| 70 | # # The HPC Service didn't have a dnsredir or a dnsdemux, so try looking |
| 71 | # # in the RequestRouterService for one. |
| 72 | # |
| 73 | # rr = RequestRouterService.objects.all() |
| 74 | # if not (rr): |
| 75 | # return HttpResponseServerError("Error: no RR service") |
| 76 | # |
| 77 | # rr = rr[0] |
| 78 | # try: |
| 79 | # slices = rr.slices.all() |
| 80 | # except: |
| 81 | # # this field used to be improperly named, and makemigrations won't fix it |
| 82 | # slices = rr.service.all() |
| 83 | # for slice in slices: |
| 84 | # if "redir" in slice.name: |
| 85 | # redirSlice = slice |
| 86 | # elif "demux" in slice.name: |
| 87 | # demuxSlice = slice |
Scott Baker | 25467ff | 2016-08-04 09:50:22 -0700 | [diff] [blame] | 88 | |
| 89 | if not redirSlice: |
| 90 | return HttpResponseServerError("Error: no dnsredir slice") |
| 91 | |
| 92 | if not demuxSlice: |
| 93 | return HttpResponseServerError("Error: no dnsdemux slice") |
| 94 | |
| 95 | d = {} |
| 96 | d["hpc_slicename"] = hpcSlice.name |
| 97 | d["redir_slicename"] = redirSlice.name |
| 98 | d["demux_slicename"] = demuxSlice.name |
| 99 | d["cmi_hostname"] = cmi_hostname |
| 100 | d["xos_hostname"] = xos.settings.RESTAPI_HOSTNAME |
| 101 | d["xos_port"] = str(xos.settings.RESTAPI_PORT) |
| 102 | |
| 103 | if hpc.hpc_port80: |
| 104 | d["hpc_port80"] = "True" |
| 105 | else: |
| 106 | d["hpc_port80"] = "False" |
| 107 | |
| 108 | return HttpResponse("""# auto-generated by HpcConfig |
| 109 | ENABLE_PLC=False |
| 110 | ENABLE_PS=True |
| 111 | BASE_HRN="princeton" |
| 112 | RELEVANT_SERVICE_NAMES=['vcoblitz', 'coredirect', 'codnsdemux', "syndicate_comon_server"] |
| 113 | COBLITZ_SLICE_NAME=BASE_HRN+"_vcoblitz" |
| 114 | COBLITZ_SLICE_ID=70 |
| 115 | COBLITZ_PS_SLICE_NAME="{hpc_slicename}" |
| 116 | DNSREDIR_SLICE_NAME=BASE_HRN+"_coredirect" |
| 117 | DNSREDIR_SLICE_ID=71 |
| 118 | DNSREDIR_PS_SLICE_NAME="{redir_slicename}" |
| 119 | DNSDEMUX_SLICE_NAME=BASE_HRN+"_codnsdemux" |
| 120 | DNSDEMUX_SLICE_ID=69 |
| 121 | DNSDEMUX_PS_SLICE_NAME="{demux_slicename}" |
| 122 | CMI_URL="http://{cmi_hostname}/" |
| 123 | CMI_HTTP_PORT="8004" |
| 124 | CMI_HTTPS_PORT="8003" |
| 125 | PUPPET_MASTER_HOSTNAME="{cmi_hostname}" |
| 126 | PUPPET_MASTER_PORT="8140" |
| 127 | PS_HOSTNAME="{xos_hostname}" |
| 128 | PS_PORT="{xos_port}" |
| 129 | COBLITZ_PORT_80={hpc_port80} |
| 130 | """.format(**d)) |
| 131 | |