blob: 6426d00ef11896d8cd1e37e839ad1426425d7bdd [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:
Scott Baker3bbf1e92017-03-07 12:06:06 -080044 model_name = getattr(target, "model_name", target.__class__.__name__)
Scott Baker5db44a92017-03-06 17:27:52 -080045 # CordSubscriberRoot is a Proxy object, and o.target will point to
46 # the base class... so fix it up.
Scott Baker3bbf1e92017-03-07 12:06:06 -080047 if model_name == "TenantRoot":
Scott Baker5db44a92017-03-06 17:27:52 -080048 target = CordSubscriberRoot.objects.get(id=target.id)
49 return target
50 return None
51
Scott Baker171d35e2016-06-20 17:36:29 -070052 def get_vcpe_service(self, o):
Scott Baker5db44a92017-03-06 17:27:52 -080053 target = self.get_target(o)
54 if target and target.volt and target.volt.vcpe:
Scott Baker3bbf1e92017-03-07 12:06:06 -080055 vcpes = VSGService.objects.filter(id=target.volt.vcpe.provider_service.id)
Scott Baker5db44a92017-03-06 17:27:52 -080056 if not vcpes:
57 return None
58 return vcpes[0]
Scott Baker171d35e2016-06-20 17:36:29 -070059 return None
60
61 def get_instance(self, o):
Scott Baker5db44a92017-03-06 17:27:52 -080062 target = self.get_target(o)
63 if target and target.volt and target.volt.vcpe:
64 return target.volt.vcpe.instance
Scott Baker171d35e2016-06-20 17:36:29 -070065 else:
66 return None
67
68 def get_key_name(self, instance):
Scott Baker5809ed42017-03-07 10:45:00 -080069# if instance.slice.service and (instance.slice.service.kind==VCPE_KIND):
70# # We need to use the vsg service's private key. Onboarding won't
71# # by default give us another service's private key, so let's assume
72# # onboarding has been configured to add vsg_rsa to the vtr service.
73# return "/opt/xos/services/vtr/keys/vsg_rsa"
74
75 if instance.slice and instance.slice.service and instance.slice.service.private_key_fn:
76 # Assume the service has shared its key with VTR.
77 # Look for the instance's service key name in VTR's key directory.
78 service_keyfn = instance.slice.service.private_key_fn
79 return os.path.join("/opt/xos/services/vtr/keys", os.path.basename(service_keyfn))
Scott Baker171d35e2016-06-20 17:36:29 -070080 else:
81 raise Exception("VTR doesn't know how to get the private key for this instance")
82
83 def get_extra_attributes(self, o):
84 vtr_service = self.get_vtr_service(o)
85 vcpe_service = self.get_vcpe_service(o)
86
87 if not vcpe_service:
88 raise Exception("No vcpeservice")
89
90 instance = self.get_instance(o)
91
92 if not instance:
93 raise Exception("No instance")
94
Scott Baker5db44a92017-03-06 17:27:52 -080095 target = self.get_target(o)
96
Scott Baker171d35e2016-06-20 17:36:29 -070097 s_tags = []
98 c_tags = []
Scott Baker5db44a92017-03-06 17:27:52 -080099 if target and target.volt:
100 s_tags.append(target.volt.s_tag)
101 c_tags.append(target.volt.c_tag)
Scott Baker171d35e2016-06-20 17:36:29 -0700102
103 fields = {"s_tags": s_tags,
104 "c_tags": c_tags,
105 "isolation": instance.isolation,
106 "container_name": "vcpe-%s-%s" % (s_tags[0], c_tags[0]),
Scott Baker3bbf1e92017-03-07 12:06:06 -0800107# "dns_servers": [x.strip() for x in vcpe_service.dns_servers.split(",")],
Scott Baker171d35e2016-06-20 17:36:29 -0700108 "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 Baker3a5b8e12017-03-31 14:22:20 -0700113 if target and target.volt and target.volt.vcpe:
114 for attribute_name in ["wan_vm_ip", "wan_container_ip"]:
115 if hasattr(target.volt.vcpe, attribute_name):
116 fields[attribute_name] = getattr(target.volt.vcpe, attribute_name)
Scott Baker171d35e2016-06-20 17:36:29 -0700117
118 # add in the sync_attributes that come from the SubscriberRoot object
Scott Baker3bbf1e92017-03-07 12:06:06 -0800119# if target and hasattr(target, "sync_attributes"):
120# for attribute_name in target.sync_attributes:
121# fields[attribute_name] = getattr(target, attribute_name)
Scott Baker171d35e2016-06-20 17:36:29 -0700122
Scott Baker3bbf1e92017-03-07 12:06:06 -0800123 for attribute_name in ["scope", "test", "argument"]: # o.sync_attributes:
Scott Baker171d35e2016-06-20 17:36:29 -0700124 fields[attribute_name] = getattr(o,attribute_name)
125
126 return fields
127
128 def sync_fields(self, o, fields):
129 # the super causes the playbook to be run
130
131 super(SyncVTRTenant, self).sync_fields(o, fields)
132
133 def run_playbook(self, o, fields):
134 o.result = ""
135
136 result_fn = os.path.join("/opt/xos/synchronizers/vtr/result", fields["result_fn"])
137 if os.path.exists(result_fn):
138 os.remove(result_fn)
139
140 resultcode_fn = os.path.join("/opt/xos/synchronizers/vtr/result", fields["resultcode_fn"])
141 if os.path.exists(resultcode_fn):
142 os.remove(resultcode_fn)
143
144 super(SyncVTRTenant, self).run_playbook(o, fields)
145
146 if os.path.exists(result_fn):
147 o.result = open(result_fn).read()
148
149 if os.path.exists(resultcode_fn):
150 o.result_code = open(resultcode_fn).read()
151
152
153 def delete_record(self, m):
154 pass