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