blob: 7cc480f9950cb681fbb3802918520ce5cee986a3 [file] [log] [blame]
Scott Baker171d35e2016-06-20 17:36:29 -07001import os
2import socket
3import sys
4import base64
5import time
Scott Baker5809ed42017-03-07 10:45:00 -08006from synchronizers.new_base.SyncInstanceUsingAnsible import SyncInstanceUsingAnsible
7from synchronizers.new_base.modelaccessor import *
Scott Baker171d35e2016-06-20 17:36:29 -07008from xos.logger import Logger, logging
9
10# hpclibrary will be in steps/..
11parentdir = os.path.join(os.path.dirname(__file__),"..")
12sys.path.insert(0,parentdir)
13
14logger = Logger(level=logging.INFO)
15
Scott Baker171d35e2016-06-20 17:36:29 -070016class SyncVTRTenant(SyncInstanceUsingAnsible):
17 provides=[VTRTenant]
18 observes=VTRTenant
19 requested_interval=0
20 template_name = "sync_vtrtenant.yaml"
Scott Baker171d35e2016-06-20 17:36:29 -070021
22 def __init__(self, *args, **kwargs):
23 super(SyncVTRTenant, self).__init__(*args, **kwargs)
24
Scott Baker171d35e2016-06-20 17:36:29 -070025 def get_vtr_service(self, o):
26 if not o.provider_service:
27 return None
28
Scott Baker5db44a92017-03-06 17:27:52 -080029 vtrs = VTRService.objects.filter(id=o.provider_service.id)
Scott Baker171d35e2016-06-20 17:36:29 -070030 if not vtrs:
31 return None
32
33 return vtrs[0]
34
Scott Baker5db44a92017-03-06 17:27:52 -080035 def get_target(self, o):
36 target = o.target
37 if target:
Scott Baker3bbf1e92017-03-07 12:06:06 -080038 model_name = getattr(target, "model_name", target.__class__.__name__)
Scott Baker5db44a92017-03-06 17:27:52 -080039 # CordSubscriberRoot is a Proxy object, and o.target will point to
40 # the base class... so fix it up.
Scott Baker3bbf1e92017-03-07 12:06:06 -080041 if model_name == "TenantRoot":
Scott Baker5db44a92017-03-06 17:27:52 -080042 target = CordSubscriberRoot.objects.get(id=target.id)
43 return target
44 return None
45
Scott Baker171d35e2016-06-20 17:36:29 -070046 def get_vcpe_service(self, o):
Scott Baker5db44a92017-03-06 17:27:52 -080047 target = self.get_target(o)
48 if target and target.volt and target.volt.vcpe:
Scott Baker3bbf1e92017-03-07 12:06:06 -080049 vcpes = VSGService.objects.filter(id=target.volt.vcpe.provider_service.id)
Scott Baker5db44a92017-03-06 17:27:52 -080050 if not vcpes:
51 return None
52 return vcpes[0]
Scott Baker171d35e2016-06-20 17:36:29 -070053 return None
54
55 def get_instance(self, o):
Scott Baker5db44a92017-03-06 17:27:52 -080056 target = self.get_target(o)
57 if target and target.volt and target.volt.vcpe:
58 return target.volt.vcpe.instance
Scott Baker171d35e2016-06-20 17:36:29 -070059 else:
60 return None
61
62 def get_key_name(self, instance):
Scott Baker5809ed42017-03-07 10:45:00 -080063# if instance.slice.service and (instance.slice.service.kind==VCPE_KIND):
64# # We need to use the vsg service's private key. Onboarding won't
65# # by default give us another service's private key, so let's assume
66# # onboarding has been configured to add vsg_rsa to the vtr service.
67# return "/opt/xos/services/vtr/keys/vsg_rsa"
68
69 if instance.slice and instance.slice.service and instance.slice.service.private_key_fn:
70 # Assume the service has shared its key with VTR.
71 # Look for the instance's service key name in VTR's key directory.
72 service_keyfn = instance.slice.service.private_key_fn
73 return os.path.join("/opt/xos/services/vtr/keys", os.path.basename(service_keyfn))
Scott Baker171d35e2016-06-20 17:36:29 -070074 else:
75 raise Exception("VTR doesn't know how to get the private key for this instance")
76
77 def get_extra_attributes(self, o):
78 vtr_service = self.get_vtr_service(o)
79 vcpe_service = self.get_vcpe_service(o)
80
81 if not vcpe_service:
82 raise Exception("No vcpeservice")
83
84 instance = self.get_instance(o)
85
86 if not instance:
87 raise Exception("No instance")
88
Scott Baker5db44a92017-03-06 17:27:52 -080089 target = self.get_target(o)
90
Scott Baker171d35e2016-06-20 17:36:29 -070091 s_tags = []
92 c_tags = []
Scott Baker5db44a92017-03-06 17:27:52 -080093 if target and target.volt:
94 s_tags.append(target.volt.s_tag)
95 c_tags.append(target.volt.c_tag)
Scott Baker171d35e2016-06-20 17:36:29 -070096
97 fields = {"s_tags": s_tags,
98 "c_tags": c_tags,
99 "isolation": instance.isolation,
100 "container_name": "vcpe-%s-%s" % (s_tags[0], c_tags[0]),
Scott Baker3bbf1e92017-03-07 12:06:06 -0800101# "dns_servers": [x.strip() for x in vcpe_service.dns_servers.split(",")],
Scott Baker171d35e2016-06-20 17:36:29 -0700102 "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, ...
Scott Baker3a5b8e12017-03-31 14:22:20 -0700107 if target and target.volt and target.volt.vcpe:
108 for attribute_name in ["wan_vm_ip", "wan_container_ip"]:
109 if hasattr(target.volt.vcpe, attribute_name):
110 fields[attribute_name] = getattr(target.volt.vcpe, attribute_name)
Scott Baker171d35e2016-06-20 17:36:29 -0700111
112 # add in the sync_attributes that come from the SubscriberRoot object
Scott Baker3bbf1e92017-03-07 12:06:06 -0800113# if target and hasattr(target, "sync_attributes"):
114# for attribute_name in target.sync_attributes:
115# fields[attribute_name] = getattr(target, attribute_name)
Scott Baker171d35e2016-06-20 17:36:29 -0700116
Scott Baker3bbf1e92017-03-07 12:06:06 -0800117 for attribute_name in ["scope", "test", "argument"]: # o.sync_attributes:
Scott Baker171d35e2016-06-20 17:36:29 -0700118 fields[attribute_name] = getattr(o,attribute_name)
119
120 return fields
121
122 def sync_fields(self, o, fields):
123 # the super causes the playbook to be run
124
125 super(SyncVTRTenant, self).sync_fields(o, fields)
126
127 def run_playbook(self, o, fields):
128 o.result = ""
129
130 result_fn = os.path.join("/opt/xos/synchronizers/vtr/result", fields["result_fn"])
131 if os.path.exists(result_fn):
132 os.remove(result_fn)
133
134 resultcode_fn = os.path.join("/opt/xos/synchronizers/vtr/result", fields["resultcode_fn"])
135 if os.path.exists(resultcode_fn):
136 os.remove(resultcode_fn)
137
138 super(SyncVTRTenant, self).run_playbook(o, fields)
139
140 if os.path.exists(result_fn):
141 o.result = open(result_fn).read()
142
143 if os.path.exists(resultcode_fn):
144 o.result_code = open(resultcode_fn).read()
145
146
147 def delete_record(self, m):
148 pass