blob: 8cabfb19d88166198a80bad938cfc30c88addac7 [file] [log] [blame]
Scott Baker171d35e2016-06-20 17:36:29 -07001import os
2import socket
3import sys
4import base64
5import time
6from django.db.models import F, Q
7from xos.config import Config
8from synchronizers.base.syncstep import SyncStep
Sapan Bhatiaf09136a2017-01-24 19:32:59 +01009from synchronizers.base.ansible_helper import run_template_ssh
Scott Baker171d35e2016-06-20 17:36:29 -070010from synchronizers.base.SyncInstanceUsingAnsible import SyncInstanceUsingAnsible
11from core.models import Service, Slice, Tag
12from services.vsg.models import VSGService, VCPE_KIND
13from services.vtr.models import VTRService, VTRTenant
Scott Baker5db44a92017-03-06 17:27:52 -080014from services.volt.models import CordSubscriberRoot
Scott Baker171d35e2016-06-20 17:36:29 -070015from xos.logger import Logger, logging
16
17# hpclibrary will be in steps/..
18parentdir = os.path.join(os.path.dirname(__file__),"..")
19sys.path.insert(0,parentdir)
20
21logger = Logger(level=logging.INFO)
22
23CORD_USE_VTN = getattr(Config(), "networking_use_vtn", False)
24
25class SyncVTRTenant(SyncInstanceUsingAnsible):
26 provides=[VTRTenant]
27 observes=VTRTenant
28 requested_interval=0
29 template_name = "sync_vtrtenant.yaml"
30 #service_key_name = "/opt/xos/services/vtr/vcpe_private_key"
31
32 def __init__(self, *args, **kwargs):
33 super(SyncVTRTenant, self).__init__(*args, **kwargs)
34
Scott Baker171d35e2016-06-20 17:36:29 -070035 def get_vtr_service(self, o):
36 if not o.provider_service:
37 return None
38
Scott Baker5db44a92017-03-06 17:27:52 -080039 vtrs = VTRService.objects.filter(id=o.provider_service.id)
Scott Baker171d35e2016-06-20 17:36:29 -070040 if not vtrs:
41 return None
42
43 return vtrs[0]
44
Scott Baker5db44a92017-03-06 17:27:52 -080045 def get_target(self, o):
46 target = o.target
47 if target:
48 # CordSubscriberRoot is a Proxy object, and o.target will point to
49 # the base class... so fix it up.
50 if target.__class__.__name__ == "TenantRoot":
51 target = CordSubscriberRoot.objects.get(id=target.id)
52 return target
53 return None
54
Scott Baker171d35e2016-06-20 17:36:29 -070055 def get_vcpe_service(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 vcpes = VSGService.get_service_objects().filter(id=target.volt.vcpe.provider_service.id)
59 if not vcpes:
60 return None
61 return vcpes[0]
Scott Baker171d35e2016-06-20 17:36:29 -070062 return None
63
64 def get_instance(self, o):
Scott Baker5db44a92017-03-06 17:27:52 -080065 target = self.get_target(o)
66 if target and target.volt and target.volt.vcpe:
67 return target.volt.vcpe.instance
Scott Baker171d35e2016-06-20 17:36:29 -070068 else:
69 return None
70
71 def get_key_name(self, instance):
72 if instance.slice.service and (instance.slice.service.kind==VCPE_KIND):
73 # We need to use the vsg service's private key. Onboarding won't
74 # by default give us another service's private key, so let's assume
75 # onboarding has been configured to add vsg_rsa to the vtr service.
76 return "/opt/xos/services/vtr/keys/vsg_rsa"
77 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]),
104 "dns_servers": [x.strip() for x in vcpe_service.dns_servers.split(",")],
105
106 "result_fn": "%s-vcpe-%s-%s" % (o.test, s_tags[0], c_tags[0]),
107 "resultcode_fn": "code-%s-vcpe-%s-%s" % (o.test, s_tags[0], c_tags[0]) }
108
109 # add in the sync_attributes that come from the vSG object
110 # this will be wan_ip, wan_mac, wan_container_ip, wan_container_mac, ...
Scott Baker5db44a92017-03-06 17:27:52 -0800111 if target and target.volt and target.volt.vcpe:
112 for attribute_name in target.volt.vcpe.sync_attributes:
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 Baker5db44a92017-03-06 17:27:52 -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
120 for attribute_name in o.sync_attributes:
121 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