Scott Baker | b30aa08 | 2014-01-03 08:36:00 -0800 | [diff] [blame] | 1 | import os |
| 2 | import base64 |
| 3 | import sys |
| 4 | |
| 5 | if __name__ == '__main__': |
| 6 | sys.path.append("/opt/planetstack") |
| 7 | os.environ.setdefault("DJANGO_SETTINGS_MODULE", "planetstack.settings") |
| 8 | |
| 9 | from planetstack.config import Config |
| 10 | from core.models import Service |
| 11 | from hpc.models import HpcService |
| 12 | from requestrouter.models import RequestRouterService |
| 13 | from util.logger import Logger, logging |
| 14 | |
| 15 | logger = Logger(level=logging.INFO) |
| 16 | |
| 17 | class HpcLibrary: |
| 18 | def extract_slice_info(self, service): |
| 19 | """ Produce a dict that describes the slices for the CMI |
| 20 | |
| 21 | slice_coblitz = <name of coblitz slice> |
| 22 | service_coblitz = <name of coblitz service> |
| 23 | hostname_coblitz = <name of first coblitz slice> |
| 24 | hostnames_coblitz = <name_of_first_cob_slice>,<name_of_second_cob_slice>,... |
| 25 | |
| 26 | slice_cmi = <name of cmi slice> |
| 27 | ... |
| 28 | """ |
| 29 | |
| 30 | slicenames = {} |
| 31 | slicehosts = {} |
| 32 | for slice in service.service.all(): |
| 33 | name = slice.name |
| 34 | if not ("_" in name): |
| 35 | continue |
| 36 | |
| 37 | if "coblitz" in name: |
| 38 | slicenames["coblitz"] = name |
| 39 | slicehosts["coblitz"] = [sliver.node.name for sliver in slice.slivers.all()] |
| 40 | elif "cmi" in name: |
| 41 | slicenames["cmi"] = name |
| 42 | slicehosts["cmi"] = [sliver.node.name for sliver in slice.slivers.all()] |
| 43 | elif "dnsredir" in name: |
| 44 | slicenames["dnsredir"] = name |
| 45 | slicehosts["dnsredir"] = [sliver.node.name for sliver in slice.slivers.all()] |
| 46 | elif "dnsdemux" in name: |
| 47 | slicenames["dnsdemux"] = name |
| 48 | slicehosts["dnsdemux"] = [sliver.node.name for sliver in slice.slivers.all()] |
| 49 | |
| 50 | base_hrn = None |
| 51 | if "coblitz" in slicenames: |
| 52 | base_hrn = slicenames["coblitz"].split("_")[0] |
| 53 | |
| 54 | mapping = {} |
| 55 | mapping["base_hrn"] = base_hrn |
| 56 | for (k,v) in slicenames.items(): |
| 57 | mapping["slice_" + k] = v |
| 58 | mapping["service_" + k] = v.split("_",1)[1] |
| 59 | for (k,v) in slicehosts.items(): |
| 60 | mapping["hostname_" + k] = v[0] |
| 61 | mapping["hostnames_" + k] = ",".join(v) |
| 62 | |
| 63 | return mapping |
| 64 | |
| 65 | def write_slices_file(self, hpc_service, rr_service): |
| 66 | mapping = self.extract_slice_info(hpc_service) |
| 67 | rr_mapping = self.extract_slice_info(rr_service) |
| 68 | |
| 69 | mapping.update(rr_mapping) |
| 70 | |
| 71 | print mapping |
| 72 | |
| 73 | fn = "/tmp/slices" |
| 74 | |
| 75 | f = open(fn, "w") |
| 76 | f.write(""" |
| 77 | ENABLE_PLC=True |
| 78 | ENABLE_PS=False |
| 79 | BASE_HRN="%(base_hrn)s" |
| 80 | RELEVANT_SERVICE_NAMES=['%(service_coblitz)s', '%(service_dnsredir)s', '%(service_dnsdemux)s'] |
| 81 | COBLITZ_SLICE_NAME="%(slice_coblitz)s" |
| 82 | COBLITZ_SLICE_ID=1 |
| 83 | COBLITZ_PS_SLICE_NAME="%(slice_coblitz)s" |
| 84 | DNSREDIR_SLICE_NAME="%(slice_dnsredir)s" |
| 85 | DNSREDIR_SLICE_ID=2 |
| 86 | DNSREDIR_PS_SLICE_NAME="%(slice_dnsredir)s" |
| 87 | DNSDEMUX_SLICE_NAME="%(slice_dnsdemux)s" |
| 88 | DNSDEMUX_SLICE_ID=3 |
| 89 | DNSDEMUX_PS_SLICE_NAME="%(slice_dnsdemux)s" |
| 90 | CMI_URL="http://%(hostname_cmi)s" |
| 91 | CMI_HTTP_PORT="8004" |
| 92 | CMI_HTTPS_PORT="8003" |
| 93 | PUPPET_MASTER_HOSTNAME="%(hostname_cmi)s" |
| 94 | PUPPET_MASTER_PORT="8140" |
| 95 | """ % mapping) |
| 96 | |
| 97 | if __name__ == '__main__': |
| 98 | hpc_service = HpcService.objects.get() |
| 99 | rr_service = RequestRouterService.objects.get() |
| 100 | lib = HpcLibrary() |
| 101 | lib.write_slices_file(hpc_service, rr_service) |
| 102 | |
| 103 | |