blob: 7de64a86ba60f27e38eb80a971464b8f533af50b [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 *
Scott Baker171d35e2016-06-20 17:36:29 -07009from xos.logger import Logger, logging
10
11# hpclibrary will be in steps/..
12parentdir = os.path.join(os.path.dirname(__file__),"..")
13sys.path.insert(0,parentdir)
14
15logger = Logger(level=logging.INFO)
16
17CORD_USE_VTN = getattr(Config(), "networking_use_vtn", False)
18
19class SyncVTRTenant(SyncInstanceUsingAnsible):
20 provides=[VTRTenant]
21 observes=VTRTenant
22 requested_interval=0
23 template_name = "sync_vtrtenant.yaml"
Scott Baker171d35e2016-06-20 17:36:29 -070024
25 def __init__(self, *args, **kwargs):
26 super(SyncVTRTenant, self).__init__(*args, **kwargs)
27
Scott Baker171d35e2016-06-20 17:36:29 -070028 def get_vtr_service(self, o):
29 if not o.provider_service:
30 return None
31
Scott Baker5db44a92017-03-06 17:27:52 -080032 vtrs = VTRService.objects.filter(id=o.provider_service.id)
Scott Baker171d35e2016-06-20 17:36:29 -070033 if not vtrs:
34 return None
35
36 return vtrs[0]
37
Scott Baker5db44a92017-03-06 17:27:52 -080038 def get_target(self, o):
39 target = o.target
40 if target:
Scott Baker3bbf1e92017-03-07 12:06:06 -080041 model_name = getattr(target, "model_name", target.__class__.__name__)
Scott Baker5db44a92017-03-06 17:27:52 -080042 # CordSubscriberRoot is a Proxy object, and o.target will point to
43 # the base class... so fix it up.
Scott Baker3bbf1e92017-03-07 12:06:06 -080044 if model_name == "TenantRoot":
Scott Baker5db44a92017-03-06 17:27:52 -080045 target = CordSubscriberRoot.objects.get(id=target.id)
46 return target
47 return None
48
Scott Baker171d35e2016-06-20 17:36:29 -070049 def get_vcpe_service(self, o):
Scott Baker5db44a92017-03-06 17:27:52 -080050 target = self.get_target(o)
51 if target and target.volt and target.volt.vcpe:
Scott Baker3bbf1e92017-03-07 12:06:06 -080052 vcpes = VSGService.objects.filter(id=target.volt.vcpe.provider_service.id)
Scott Baker5db44a92017-03-06 17:27:52 -080053 if not vcpes:
54 return None
55 return vcpes[0]
Scott Baker171d35e2016-06-20 17:36:29 -070056 return None
57
58 def get_instance(self, o):
Scott Baker5db44a92017-03-06 17:27:52 -080059 target = self.get_target(o)
60 if target and target.volt and target.volt.vcpe:
61 return target.volt.vcpe.instance
Scott Baker171d35e2016-06-20 17:36:29 -070062 else:
63 return None
64
65 def get_key_name(self, instance):
Scott Baker5809ed42017-03-07 10:45:00 -080066# if instance.slice.service and (instance.slice.service.kind==VCPE_KIND):
67# # We need to use the vsg service's private key. Onboarding won't
68# # by default give us another service's private key, so let's assume
69# # onboarding has been configured to add vsg_rsa to the vtr service.
70# return "/opt/xos/services/vtr/keys/vsg_rsa"
71
72 if instance.slice and instance.slice.service and instance.slice.service.private_key_fn:
73 # Assume the service has shared its key with VTR.
74 # Look for the instance's service key name in VTR's key directory.
75 service_keyfn = instance.slice.service.private_key_fn
76 return os.path.join("/opt/xos/services/vtr/keys", os.path.basename(service_keyfn))
Scott Baker171d35e2016-06-20 17:36:29 -070077 else:
78 raise Exception("VTR doesn't know how to get the private key for this instance")
79
80 def get_extra_attributes(self, o):
81 vtr_service = self.get_vtr_service(o)
82 vcpe_service = self.get_vcpe_service(o)
83
84 if not vcpe_service:
85 raise Exception("No vcpeservice")
86
87 instance = self.get_instance(o)
88
89 if not instance:
90 raise Exception("No instance")
91
Scott Baker5db44a92017-03-06 17:27:52 -080092 target = self.get_target(o)
93
Scott Baker171d35e2016-06-20 17:36:29 -070094 s_tags = []
95 c_tags = []
Scott Baker5db44a92017-03-06 17:27:52 -080096 if target and target.volt:
97 s_tags.append(target.volt.s_tag)
98 c_tags.append(target.volt.c_tag)
Scott Baker171d35e2016-06-20 17:36:29 -070099
100 fields = {"s_tags": s_tags,
101 "c_tags": c_tags,
102 "isolation": instance.isolation,
103 "container_name": "vcpe-%s-%s" % (s_tags[0], c_tags[0]),
Scott Baker3bbf1e92017-03-07 12:06:06 -0800104# "dns_servers": [x.strip() for x in vcpe_service.dns_servers.split(",")],
Scott Baker171d35e2016-06-20 17:36:29 -0700105 "result_fn": "%s-vcpe-%s-%s" % (o.test, s_tags[0], c_tags[0]),
106 "resultcode_fn": "code-%s-vcpe-%s-%s" % (o.test, s_tags[0], c_tags[0]) }
107
108 # add in the sync_attributes that come from the vSG object
109 # this will be wan_ip, wan_mac, wan_container_ip, wan_container_mac, ...
Scott Baker3a5b8e12017-03-31 14:22:20 -0700110 if target and target.volt and target.volt.vcpe:
111 for attribute_name in ["wan_vm_ip", "wan_container_ip"]:
112 if hasattr(target.volt.vcpe, attribute_name):
113 fields[attribute_name] = getattr(target.volt.vcpe, attribute_name)
Scott Baker171d35e2016-06-20 17:36:29 -0700114
115 # add in the sync_attributes that come from the SubscriberRoot object
Scott Baker3bbf1e92017-03-07 12:06:06 -0800116# if target and hasattr(target, "sync_attributes"):
117# for attribute_name in target.sync_attributes:
118# fields[attribute_name] = getattr(target, attribute_name)
Scott Baker171d35e2016-06-20 17:36:29 -0700119
Scott Baker3bbf1e92017-03-07 12:06:06 -0800120 for attribute_name in ["scope", "test", "argument"]: # o.sync_attributes:
Scott Baker171d35e2016-06-20 17:36:29 -0700121 fields[attribute_name] = getattr(o,attribute_name)
122
123 return fields
124
125 def sync_fields(self, o, fields):
126 # the super causes the playbook to be run
127
128 super(SyncVTRTenant, self).sync_fields(o, fields)
129
130 def run_playbook(self, o, fields):
131 o.result = ""
132
133 result_fn = os.path.join("/opt/xos/synchronizers/vtr/result", fields["result_fn"])
134 if os.path.exists(result_fn):
135 os.remove(result_fn)
136
137 resultcode_fn = os.path.join("/opt/xos/synchronizers/vtr/result", fields["resultcode_fn"])
138 if os.path.exists(resultcode_fn):
139 os.remove(resultcode_fn)
140
141 super(SyncVTRTenant, self).run_playbook(o, fields)
142
143 if os.path.exists(result_fn):
144 o.result = open(result_fn).read()
145
146 if os.path.exists(resultcode_fn):
147 o.result_code = open(resultcode_fn).read()
148
149
150 def delete_record(self, m):
151 pass