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