blob: 5182f01d0f8d7f88c4bba3e1482c9966c133e9c4 [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):
Scott Bakerc7958a92017-07-18 11:07:00 -070026 if not o.owner:
Scott Baker171d35e2016-06-20 17:36:29 -070027 return None
28
Scott Bakerc7958a92017-07-18 11:07:00 -070029 # cast from Service to VTRService
30 vtrs = VTRService.objects.filter(id=o.owner.id)
Scott Baker171d35e2016-06-20 17:36:29 -070031 if not vtrs:
32 return None
33
34 return vtrs[0]
35
Scott Baker5db44a92017-03-06 17:27:52 -080036 def get_target(self, o):
37 target = o.target
38 if target:
Scott Baker3bbf1e92017-03-07 12:06:06 -080039 model_name = getattr(target, "model_name", target.__class__.__name__)
Scott Bakerc7958a92017-07-18 11:07:00 -070040 if model_name in ["ServiceInstance", "CordSubscriberRoot"]:
41 # cast from ServiceInstance to CordSubscriberRoot
42 csrs = CordSubscriberRoot.objects.filter(id=target.id)
43 if csrs:
44 return csrs[0]
Scott Baker5db44a92017-03-06 17:27:52 -080045 return None
46
Scott Baker171d35e2016-06-20 17:36:29 -070047 def get_vcpe_service(self, o):
Scott Baker5db44a92017-03-06 17:27:52 -080048 target = self.get_target(o)
49 if target and target.volt and target.volt.vcpe:
Scott Bakerc7958a92017-07-18 11:07:00 -070050 return target.volt.vcpe
Scott Baker171d35e2016-06-20 17:36:29 -070051 return None
52
53 def get_instance(self, o):
Scott Baker5db44a92017-03-06 17:27:52 -080054 target = self.get_target(o)
55 if target and target.volt and target.volt.vcpe:
56 return target.volt.vcpe.instance
Scott Baker171d35e2016-06-20 17:36:29 -070057 else:
58 return None
59
60 def get_key_name(self, instance):
Scott Baker5809ed42017-03-07 10:45:00 -080061# if instance.slice.service and (instance.slice.service.kind==VCPE_KIND):
62# # We need to use the vsg service's private key. Onboarding won't
63# # by default give us another service's private key, so let's assume
64# # onboarding has been configured to add vsg_rsa to the vtr service.
65# return "/opt/xos/services/vtr/keys/vsg_rsa"
66
67 if instance.slice and instance.slice.service and instance.slice.service.private_key_fn:
68 # Assume the service has shared its key with VTR.
69 # Look for the instance's service key name in VTR's key directory.
70 service_keyfn = instance.slice.service.private_key_fn
71 return os.path.join("/opt/xos/services/vtr/keys", os.path.basename(service_keyfn))
Scott Baker171d35e2016-06-20 17:36:29 -070072 else:
73 raise Exception("VTR doesn't know how to get the private key for this instance")
74
75 def get_extra_attributes(self, o):
76 vtr_service = self.get_vtr_service(o)
77 vcpe_service = self.get_vcpe_service(o)
78
79 if not vcpe_service:
80 raise Exception("No vcpeservice")
81
82 instance = self.get_instance(o)
83
84 if not instance:
85 raise Exception("No instance")
86
Scott Baker5db44a92017-03-06 17:27:52 -080087 target = self.get_target(o)
88
Scott Baker171d35e2016-06-20 17:36:29 -070089 s_tags = []
90 c_tags = []
Scott Baker5db44a92017-03-06 17:27:52 -080091 if target and target.volt:
92 s_tags.append(target.volt.s_tag)
93 c_tags.append(target.volt.c_tag)
Scott Baker171d35e2016-06-20 17:36:29 -070094
95 fields = {"s_tags": s_tags,
96 "c_tags": c_tags,
97 "isolation": instance.isolation,
98 "container_name": "vcpe-%s-%s" % (s_tags[0], c_tags[0]),
Scott Baker3bbf1e92017-03-07 12:06:06 -080099# "dns_servers": [x.strip() for x in vcpe_service.dns_servers.split(",")],
Scott Baker171d35e2016-06-20 17:36:29 -0700100 "result_fn": "%s-vcpe-%s-%s" % (o.test, s_tags[0], c_tags[0]),
101 "resultcode_fn": "code-%s-vcpe-%s-%s" % (o.test, s_tags[0], c_tags[0]) }
102
103 # add in the sync_attributes that come from the vSG object
104 # this will be wan_ip, wan_mac, wan_container_ip, wan_container_mac, ...
Scott Baker3a5b8e12017-03-31 14:22:20 -0700105 if target and target.volt and target.volt.vcpe:
106 for attribute_name in ["wan_vm_ip", "wan_container_ip"]:
107 if hasattr(target.volt.vcpe, attribute_name):
108 fields[attribute_name] = getattr(target.volt.vcpe, attribute_name)
Scott Baker171d35e2016-06-20 17:36:29 -0700109
110 # add in the sync_attributes that come from the SubscriberRoot object
Scott Baker3bbf1e92017-03-07 12:06:06 -0800111# if target and hasattr(target, "sync_attributes"):
112# for attribute_name in target.sync_attributes:
113# fields[attribute_name] = getattr(target, attribute_name)
Scott Baker171d35e2016-06-20 17:36:29 -0700114
Scott Baker3bbf1e92017-03-07 12:06:06 -0800115 for attribute_name in ["scope", "test", "argument"]: # o.sync_attributes:
Scott Baker171d35e2016-06-20 17:36:29 -0700116 fields[attribute_name] = getattr(o,attribute_name)
117
118 return fields
119
120 def sync_fields(self, o, fields):
121 # the super causes the playbook to be run
122
123 super(SyncVTRTenant, self).sync_fields(o, fields)
124
125 def run_playbook(self, o, fields):
126 o.result = ""
127
128 result_fn = os.path.join("/opt/xos/synchronizers/vtr/result", fields["result_fn"])
129 if os.path.exists(result_fn):
130 os.remove(result_fn)
131
132 resultcode_fn = os.path.join("/opt/xos/synchronizers/vtr/result", fields["resultcode_fn"])
133 if os.path.exists(resultcode_fn):
134 os.remove(resultcode_fn)
135
136 super(SyncVTRTenant, self).run_playbook(o, fields)
137
138 if os.path.exists(result_fn):
139 o.result = open(result_fn).read()
140
141 if os.path.exists(resultcode_fn):
142 o.result_code = open(resultcode_fn).read()
143
144
145 def delete_record(self, m):
146 pass