blob: 8d856012f183913369a27fdcbce98063b33d219c [file] [log] [blame]
Scott Baker64b889b2015-05-05 17:53:12 -07001import os
2import socket
3import sys
4import base64
5from django.db.models import F, Q
6from xos.config import Config
7from observer.syncstep import SyncStep
8from observer.ansible import run_template_ssh
9from core.models import Service
10from cord.models import VCPEService, VCPETenant, VOLTTenant
11from hpc.models import HpcService, CDNPrefix
12from util.logger import Logger, logging
13
14# hpclibrary will be in steps/..
15parentdir = os.path.join(os.path.dirname(__file__),"..")
16sys.path.insert(0,parentdir)
17
Scott Bakercdf47142015-06-01 16:15:42 -070018from broadbandshield import BBS
19
Scott Baker64b889b2015-05-05 17:53:12 -070020logger = Logger(level=logging.INFO)
21
22class 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 Baker59e88d52015-06-02 09:48:47 -070033 raise Exception("defer object %s due to %s" % (str(o), reason))
Scott Baker64b889b2015-05-05 17:53:12 -070034
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 Baker2e64a3a2015-05-06 20:06:21 -070059 cdn_prefixes = []
60 for prefix in CDNPrefix.objects.all():
61 cdn_prefixes.append(prefix.prefix)
62
Scott Bakerf674e912015-05-08 10:56:13 -070063 volts = [x for x in VOLTTenant.get_tenant_objects() if (x.vcpe is not None) and (x.vcpe.id==o.id)]
Scott Baker64b889b2015-05-05 17:53:12 -070064 vlan_ids = [x.vlan_id for x in volts]
65 return {"vlan_ids": vlan_ids,
Scott Baker2e64a3a2015-05-06 20:06:21 -070066 "dnsdemux_ip": dnsdemux_ip,
67 "cdn_prefixes": cdn_prefixes}
Scott Baker64b889b2015-05-05 17:53:12 -070068
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 Baker83734052015-05-12 16:13:29 -070099 "ansible_tag": "vcpe_tenant_" + str(o.id)
Scott Baker64b889b2015-05-05 17:53:12 -0700100 }
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 Baker64b889b2015-05-05 17:53:12 -0700107 run_template_ssh(self.template_name, fields)
108
Scott Bakercdf47142015-06-01 16:15:42 -0700109 if o.url_filter_enable:
110 bbs = BBS(o.bbs_account, "123")
111 bbs.sync(o.url_filter_level, o.users)
112
Scott Baker64b889b2015-05-05 17:53:12 -0700113 o.save()
114
115 def delete_record(self, m):
116 pass
117