blob: f0f7ef3b3d1ef6878963d7944edc10fbaf5e77e8 [file] [log] [blame]
Scott Baker171d35e2016-06-20 17:36:29 -07001import os
2import socket
3import sys
4import base64
5import time
6from django.db.models import F, Q
7from xos.config import Config
8from synchronizers.base.syncstep import SyncStep
9from synchronizers.base.ansible import run_template_ssh
10from synchronizers.base.SyncInstanceUsingAnsible import SyncInstanceUsingAnsible
11from core.models import Service, Slice, Tag
12from services.vsg.models import VSGService, VCPE_KIND
13from services.vtr.models import VTRService, VTRTenant
14from services.hpc.models import HpcService, CDNPrefix
15from xos.logger import Logger, logging
16
17# hpclibrary will be in steps/..
18parentdir = os.path.join(os.path.dirname(__file__),"..")
19sys.path.insert(0,parentdir)
20
21logger = Logger(level=logging.INFO)
22
23CORD_USE_VTN = getattr(Config(), "networking_use_vtn", False)
24
25class SyncVTRTenant(SyncInstanceUsingAnsible):
26 provides=[VTRTenant]
27 observes=VTRTenant
28 requested_interval=0
29 template_name = "sync_vtrtenant.yaml"
30 #service_key_name = "/opt/xos/services/vtr/vcpe_private_key"
31
32 def __init__(self, *args, **kwargs):
33 super(SyncVTRTenant, self).__init__(*args, **kwargs)
34
35 def fetch_pending(self, deleted):
36 if (not deleted):
37 objs = VTRTenant.get_tenant_objects().filter(Q(enacted__lt=F('updated')) | Q(enacted=None),Q(lazy_blocked=False))
38 else:
39 objs = VTRTenant.get_deleted_tenant_objects()
40
41 return objs
42
43 def get_vtr_service(self, o):
44 if not o.provider_service:
45 return None
46
47 vtrs = VTRService.get_service_objects().filter(id=o.provider_service.id)
48 if not vtrs:
49 return None
50
51 return vtrs[0]
52
53 def get_vcpe_service(self, o):
54 if o.target:
55 # o.target is a CordSubscriberRoot
56 if o.target.volt and o.target.volt.vcpe:
57 vcpes = VSGService.get_service_objects().filter(id=o.target.volt.vcpe.provider_service.id)
58 if not vcpes:
59 return None
60 return vcpes[0]
61 return None
62
63 def get_instance(self, o):
64 if o.target and o.target.volt and o.target.volt.vcpe:
65 return o.target.volt.vcpe.instance
66 else:
67 return None
68
69 def get_key_name(self, instance):
70 if instance.slice.service and (instance.slice.service.kind==VCPE_KIND):
71 # We need to use the vsg service's private key. Onboarding won't
72 # by default give us another service's private key, so let's assume
73 # onboarding has been configured to add vsg_rsa to the vtr service.
74 return "/opt/xos/services/vtr/keys/vsg_rsa"
75 else:
76 raise Exception("VTR doesn't know how to get the private key for this instance")
77
78 def get_extra_attributes(self, o):
79 vtr_service = self.get_vtr_service(o)
80 vcpe_service = self.get_vcpe_service(o)
81
82 if not vcpe_service:
83 raise Exception("No vcpeservice")
84
85 instance = self.get_instance(o)
86
87 if not instance:
88 raise Exception("No instance")
89
90 s_tags = []
91 c_tags = []
92 if o.target and o.target.volt:
93 s_tags.append(o.target.volt.s_tag)
94 c_tags.append(o.target.volt.c_tag)
95
96 fields = {"s_tags": s_tags,
97 "c_tags": c_tags,
98 "isolation": instance.isolation,
99 "container_name": "vcpe-%s-%s" % (s_tags[0], c_tags[0]),
100 "dns_servers": [x.strip() for x in vcpe_service.dns_servers.split(",")],
101
102 "result_fn": "%s-vcpe-%s-%s" % (o.test, s_tags[0], c_tags[0]),
103 "resultcode_fn": "code-%s-vcpe-%s-%s" % (o.test, s_tags[0], c_tags[0]) }
104
105 # add in the sync_attributes that come from the vSG object
106 # this will be wan_ip, wan_mac, wan_container_ip, wan_container_mac, ...
107 if o.target and o.target.volt and o.target.volt.vcpe:
108 for attribute_name in o.target.volt.vcpe.sync_attributes:
109 fields[attribute_name] = getattr(o.target.volt.vcpe, attribute_name)
110
111 # add in the sync_attributes that come from the SubscriberRoot object
112 if o.target and hasattr(o.target, "sync_attributes"):
113 for attribute_name in o.target.sync_attributes:
114 fields[attribute_name] = getattr(o.target, attribute_name)
115
116 for attribute_name in o.sync_attributes:
117 fields[attribute_name] = getattr(o,attribute_name)
118
119 return fields
120
121 def sync_fields(self, o, fields):
122 # the super causes the playbook to be run
123
124 super(SyncVTRTenant, self).sync_fields(o, fields)
125
126 def run_playbook(self, o, fields):
127 o.result = ""
128
129 result_fn = os.path.join("/opt/xos/synchronizers/vtr/result", fields["result_fn"])
130 if os.path.exists(result_fn):
131 os.remove(result_fn)
132
133 resultcode_fn = os.path.join("/opt/xos/synchronizers/vtr/result", fields["resultcode_fn"])
134 if os.path.exists(resultcode_fn):
135 os.remove(resultcode_fn)
136
137 super(SyncVTRTenant, self).run_playbook(o, fields)
138
139 if os.path.exists(result_fn):
140 o.result = open(result_fn).read()
141
142 if os.path.exists(resultcode_fn):
143 o.result_code = open(resultcode_fn).read()
144
145
146 def delete_record(self, m):
147 pass