blob: 0209d3f167a1a7cd4fdca20ef6dc0ef6939ec49c [file] [log] [blame]
Scott Baker171d35e2016-06-20 17:36:29 -07001import os
2import socket
3import sys
4import base64
5import time
Scott Baker171d35e2016-06-20 17:36:29 -07006from xos.config import Config
Scott Baker5809ed42017-03-07 10:45:00 -08007from synchronizers.new_base.SyncInstanceUsingAnsible import SyncInstanceUsingAnsible
8from synchronizers.new_base.modelaccessor import *
9#from services.vsg.models import VSGService, VCPE_KIND
10#from services.vtr.models import VTRService, VTRTenant
11#from services.volt.models import CordSubscriberRoot
Scott Baker171d35e2016-06-20 17:36:29 -070012from xos.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
18logger = Logger(level=logging.INFO)
19
20CORD_USE_VTN = getattr(Config(), "networking_use_vtn", False)
21
22class SyncVTRTenant(SyncInstanceUsingAnsible):
23 provides=[VTRTenant]
24 observes=VTRTenant
25 requested_interval=0
26 template_name = "sync_vtrtenant.yaml"
Scott Baker171d35e2016-06-20 17:36:29 -070027
28 def __init__(self, *args, **kwargs):
29 super(SyncVTRTenant, self).__init__(*args, **kwargs)
30
Scott Baker171d35e2016-06-20 17:36:29 -070031 def get_vtr_service(self, o):
32 if not o.provider_service:
33 return None
34
Scott Baker5db44a92017-03-06 17:27:52 -080035 vtrs = VTRService.objects.filter(id=o.provider_service.id)
Scott Baker171d35e2016-06-20 17:36:29 -070036 if not vtrs:
37 return None
38
39 return vtrs[0]
40
Scott Baker5db44a92017-03-06 17:27:52 -080041 def get_target(self, o):
42 target = o.target
43 if target:
44 # CordSubscriberRoot is a Proxy object, and o.target will point to
45 # the base class... so fix it up.
46 if target.__class__.__name__ == "TenantRoot":
47 target = CordSubscriberRoot.objects.get(id=target.id)
48 return target
49 return None
50
Scott Baker171d35e2016-06-20 17:36:29 -070051 def get_vcpe_service(self, o):
Scott Baker5db44a92017-03-06 17:27:52 -080052 target = self.get_target(o)
53 if target and target.volt and target.volt.vcpe:
54 vcpes = VSGService.get_service_objects().filter(id=target.volt.vcpe.provider_service.id)
55 if not vcpes:
56 return None
57 return vcpes[0]
Scott Baker171d35e2016-06-20 17:36:29 -070058 return None
59
60 def get_instance(self, o):
Scott Baker5db44a92017-03-06 17:27:52 -080061 target = self.get_target(o)
62 if target and target.volt and target.volt.vcpe:
63 return target.volt.vcpe.instance
Scott Baker171d35e2016-06-20 17:36:29 -070064 else:
65 return None
66
67 def get_key_name(self, instance):
Scott Baker5809ed42017-03-07 10:45:00 -080068# if instance.slice.service and (instance.slice.service.kind==VCPE_KIND):
69# # We need to use the vsg service's private key. Onboarding won't
70# # by default give us another service's private key, so let's assume
71# # onboarding has been configured to add vsg_rsa to the vtr service.
72# return "/opt/xos/services/vtr/keys/vsg_rsa"
73
74 if instance.slice and instance.slice.service and instance.slice.service.private_key_fn:
75 # Assume the service has shared its key with VTR.
76 # Look for the instance's service key name in VTR's key directory.
77 service_keyfn = instance.slice.service.private_key_fn
78 return os.path.join("/opt/xos/services/vtr/keys", os.path.basename(service_keyfn))
Scott Baker171d35e2016-06-20 17:36:29 -070079 else:
80 raise Exception("VTR doesn't know how to get the private key for this instance")
81
82 def get_extra_attributes(self, o):
83 vtr_service = self.get_vtr_service(o)
84 vcpe_service = self.get_vcpe_service(o)
85
86 if not vcpe_service:
87 raise Exception("No vcpeservice")
88
89 instance = self.get_instance(o)
90
91 if not instance:
92 raise Exception("No instance")
93
Scott Baker5db44a92017-03-06 17:27:52 -080094 target = self.get_target(o)
95
Scott Baker171d35e2016-06-20 17:36:29 -070096 s_tags = []
97 c_tags = []
Scott Baker5db44a92017-03-06 17:27:52 -080098 if target and target.volt:
99 s_tags.append(target.volt.s_tag)
100 c_tags.append(target.volt.c_tag)
Scott Baker171d35e2016-06-20 17:36:29 -0700101
102 fields = {"s_tags": s_tags,
103 "c_tags": c_tags,
104 "isolation": instance.isolation,
105 "container_name": "vcpe-%s-%s" % (s_tags[0], c_tags[0]),
106 "dns_servers": [x.strip() for x in vcpe_service.dns_servers.split(",")],
107
108 "result_fn": "%s-vcpe-%s-%s" % (o.test, s_tags[0], c_tags[0]),
109 "resultcode_fn": "code-%s-vcpe-%s-%s" % (o.test, s_tags[0], c_tags[0]) }
110
111 # add in the sync_attributes that come from the vSG object
112 # this will be wan_ip, wan_mac, wan_container_ip, wan_container_mac, ...
Scott Baker5db44a92017-03-06 17:27:52 -0800113 if target and target.volt and target.volt.vcpe:
114 for attribute_name in target.volt.vcpe.sync_attributes:
115 fields[attribute_name] = getattr(target.volt.vcpe, attribute_name)
Scott Baker171d35e2016-06-20 17:36:29 -0700116
117 # add in the sync_attributes that come from the SubscriberRoot object
Scott Baker5db44a92017-03-06 17:27:52 -0800118 if target and hasattr(target, "sync_attributes"):
119 for attribute_name in target.sync_attributes:
120 fields[attribute_name] = getattr(target, attribute_name)
Scott Baker171d35e2016-06-20 17:36:29 -0700121
122 for attribute_name in o.sync_attributes:
123 fields[attribute_name] = getattr(o,attribute_name)
124
125 return fields
126
127 def sync_fields(self, o, fields):
128 # the super causes the playbook to be run
129
130 super(SyncVTRTenant, self).sync_fields(o, fields)
131
132 def run_playbook(self, o, fields):
133 o.result = ""
134
135 result_fn = os.path.join("/opt/xos/synchronizers/vtr/result", fields["result_fn"])
136 if os.path.exists(result_fn):
137 os.remove(result_fn)
138
139 resultcode_fn = os.path.join("/opt/xos/synchronizers/vtr/result", fields["resultcode_fn"])
140 if os.path.exists(resultcode_fn):
141 os.remove(resultcode_fn)
142
143 super(SyncVTRTenant, self).run_playbook(o, fields)
144
145 if os.path.exists(result_fn):
146 o.result = open(result_fn).read()
147
148 if os.path.exists(resultcode_fn):
149 o.result_code = open(resultcode_fn).read()
150
151
152 def delete_record(self, m):
153 pass