Scott Baker | 64b889b | 2015-05-05 17:53:12 -0700 | [diff] [blame] | 1 | import os |
| 2 | import socket |
| 3 | import sys |
| 4 | import base64 |
Scott Baker | 3145da1 | 2015-06-09 12:03:07 -0700 | [diff] [blame^] | 5 | import time |
Scott Baker | 64b889b | 2015-05-05 17:53:12 -0700 | [diff] [blame] | 6 | from django.db.models import F, Q |
| 7 | from xos.config import Config |
| 8 | from observer.syncstep import SyncStep |
| 9 | from observer.ansible import run_template_ssh |
| 10 | from core.models import Service |
| 11 | from cord.models import VCPEService, VCPETenant, VOLTTenant |
| 12 | from hpc.models import HpcService, CDNPrefix |
| 13 | from util.logger import Logger, logging |
| 14 | |
| 15 | # hpclibrary will be in steps/.. |
| 16 | parentdir = os.path.join(os.path.dirname(__file__),"..") |
| 17 | sys.path.insert(0,parentdir) |
| 18 | |
Scott Baker | cdf4714 | 2015-06-01 16:15:42 -0700 | [diff] [blame] | 19 | from broadbandshield import BBS |
| 20 | |
Scott Baker | 64b889b | 2015-05-05 17:53:12 -0700 | [diff] [blame] | 21 | logger = Logger(level=logging.INFO) |
| 22 | |
| 23 | class SyncVCPETenant(SyncStep): |
| 24 | provides=[VCPETenant] |
| 25 | observes=VCPETenant |
| 26 | requested_interval=0 |
| 27 | template_name = "sync_vcpetenant.yaml" |
| 28 | service_key_name = "/opt/xos/observers/vcpe/vcpe_private_key" |
| 29 | |
| 30 | def __init__(self, **args): |
| 31 | SyncStep.__init__(self, **args) |
| 32 | |
| 33 | def defer_sync(self, o, reason): |
Scott Baker | 61c8e8d | 2015-06-02 14:34:04 -0700 | [diff] [blame] | 34 | logger.info("defer object %s due to %s" % (str(o), reason)) |
Scott Baker | 59e88d5 | 2015-06-02 09:48:47 -0700 | [diff] [blame] | 35 | raise Exception("defer object %s due to %s" % (str(o), reason)) |
Scott Baker | 64b889b | 2015-05-05 17:53:12 -0700 | [diff] [blame] | 36 | |
| 37 | def fetch_pending(self, deleted): |
| 38 | if (not deleted): |
| 39 | objs = VCPETenant.get_tenant_objects().filter(Q(enacted__lt=F('updated')) | Q(enacted=None),Q(lazy_blocked=False)) |
| 40 | else: |
| 41 | objs = VCPETenant.get_deleted_tenant_objects() |
| 42 | |
| 43 | return objs |
| 44 | |
| 45 | def get_extra_attributes(self, o): |
| 46 | # This is a place to include extra attributes that aren't part of the |
| 47 | # object itself. In our case, it's handy to know the VLAN IDs when |
| 48 | # configuring the VCPE. |
| 49 | |
| 50 | dnsdemux_ip = "none" |
| 51 | for service in HpcService.objects.all(): |
| 52 | for slice in service.slices.all(): |
| 53 | if "dnsdemux" in slice.name: |
| 54 | for sliver in slice.slivers.all(): |
Scott Baker | 4d5d5d7 | 2015-06-05 12:09:36 -0700 | [diff] [blame] | 55 | # Connect to a dnsdemux that's on the hpc_client network |
| 56 | # if one is available. |
| 57 | for ns in sliver.networkslivers.all(): |
| 58 | if ns.ip and ns.network.labels and ("hpc_client" in ns.network.labels): |
| 59 | dnsdemux_ip = ns.ip |
Scott Baker | 64b889b | 2015-05-05 17:53:12 -0700 | [diff] [blame] | 60 | if dnsdemux_ip=="none": |
| 61 | try: |
| 62 | dnsdemux_ip = socket.gethostbyname(sliver.node.name) |
| 63 | except: |
| 64 | pass |
| 65 | |
Scott Baker | 2e64a3a | 2015-05-06 20:06:21 -0700 | [diff] [blame] | 66 | cdn_prefixes = [] |
| 67 | for prefix in CDNPrefix.objects.all(): |
| 68 | cdn_prefixes.append(prefix.prefix) |
| 69 | |
Scott Baker | f674e91 | 2015-05-08 10:56:13 -0700 | [diff] [blame] | 70 | volts = [x for x in VOLTTenant.get_tenant_objects() if (x.vcpe is not None) and (x.vcpe.id==o.id)] |
Scott Baker | 64b889b | 2015-05-05 17:53:12 -0700 | [diff] [blame] | 71 | vlan_ids = [x.vlan_id for x in volts] |
| 72 | return {"vlan_ids": vlan_ids, |
Scott Baker | 2e64a3a | 2015-05-06 20:06:21 -0700 | [diff] [blame] | 73 | "dnsdemux_ip": dnsdemux_ip, |
| 74 | "cdn_prefixes": cdn_prefixes} |
Scott Baker | 64b889b | 2015-05-05 17:53:12 -0700 | [diff] [blame] | 75 | |
| 76 | def get_sliver(self, o): |
| 77 | # We need to know what slivers is associated with the object. |
| 78 | # For vCPE this is easy, as VCPETenant has a sliver field. |
| 79 | |
| 80 | return o.sliver |
| 81 | |
| 82 | def sync_record(self, o): |
| 83 | logger.info("sync'ing VCPETenant %s" % str(o)) |
| 84 | |
| 85 | sliver = self.get_sliver(o) |
| 86 | if not sliver: |
| 87 | self.defer_sync(o, "waiting on sliver") |
| 88 | return |
| 89 | |
| 90 | service = o.sliver.slice.service |
| 91 | if not service: |
| 92 | # Ansible uses the service's keypair in order to SSH into the |
| 93 | # instance. It would be bad if the slice had no service. |
| 94 | |
| 95 | raise Exception("Slice %s is not associated with a service" % sliver.slice.name) |
| 96 | |
| 97 | if not os.path.exists(self.service_key_name): |
| 98 | raise Exception("Service key %s does not exist" % self.service_key_name) |
| 99 | |
| 100 | service_key = file(self.service_key_name).read() |
| 101 | |
| 102 | fields = { "sliver_name": sliver.name, |
| 103 | "hostname": sliver.node.name, |
| 104 | "instance_id": sliver.instance_id, |
| 105 | "private_key": service_key, |
Scott Baker | 8373405 | 2015-05-12 16:13:29 -0700 | [diff] [blame] | 106 | "ansible_tag": "vcpe_tenant_" + str(o.id) |
Scott Baker | 64b889b | 2015-05-05 17:53:12 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | if hasattr(o, "sync_attributes"): |
| 110 | for attribute_name in o.sync_attributes: |
| 111 | fields[attribute_name] = getattr(o, attribute_name) |
| 112 | |
| 113 | fields.update(self.get_extra_attributes(o)) |
Scott Baker | 3145da1 | 2015-06-09 12:03:07 -0700 | [diff] [blame^] | 114 | tStart = time.time() |
Scott Baker | 64b889b | 2015-05-05 17:53:12 -0700 | [diff] [blame] | 115 | run_template_ssh(self.template_name, fields) |
Scott Baker | 3145da1 | 2015-06-09 12:03:07 -0700 | [diff] [blame^] | 116 | logger.info("playbook execution time %d" % int(time.time()-tStart)) |
Scott Baker | 64b889b | 2015-05-05 17:53:12 -0700 | [diff] [blame] | 117 | |
Scott Baker | cdf4714 | 2015-06-01 16:15:42 -0700 | [diff] [blame] | 118 | if o.url_filter_enable: |
Scott Baker | cf155f4 | 2015-06-08 19:09:53 -0700 | [diff] [blame] | 119 | if (str(o.service_specific_id) != "SYNCME"): |
Scott Baker | df877da | 2015-06-08 18:58:39 -0700 | [diff] [blame] | 120 | # XXX FIXME |
| 121 | # Also fix the spot in cord/models.py |
| 122 | logger.info("skipping sync of URL filter for SSID %s" % str(o.service_specific_id)) |
| 123 | else: |
Scott Baker | 3145da1 | 2015-06-09 12:03:07 -0700 | [diff] [blame^] | 124 | tStart = time.time() |
Scott Baker | df877da | 2015-06-08 18:58:39 -0700 | [diff] [blame] | 125 | bbs = BBS(o.bbs_account, "123") |
| 126 | bbs.sync(o.url_filter_level, o.users) |
Scott Baker | 3145da1 | 2015-06-09 12:03:07 -0700 | [diff] [blame^] | 127 | logger.info("bbs update tiem %d" % int(time.time()-tStart)) |
Scott Baker | cdf4714 | 2015-06-01 16:15:42 -0700 | [diff] [blame] | 128 | |
Scott Baker | 64b889b | 2015-05-05 17:53:12 -0700 | [diff] [blame] | 129 | o.save() |
| 130 | |
| 131 | def delete_record(self, m): |
| 132 | pass |
| 133 | |