Matteo Scandolo | a86e0a1 | 2017-08-08 13:05:27 -0700 | [diff] [blame] | 1 | |
| 2 | # Copyright 2017-present Open Networking Foundation |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
| 16 | |
Scott Baker | 171d35e | 2016-06-20 17:36:29 -0700 | [diff] [blame] | 17 | import os |
| 18 | import socket |
| 19 | import sys |
| 20 | import base64 |
| 21 | import time |
Scott Baker | 5809ed4 | 2017-03-07 10:45:00 -0800 | [diff] [blame] | 22 | from synchronizers.new_base.SyncInstanceUsingAnsible import SyncInstanceUsingAnsible |
| 23 | from synchronizers.new_base.modelaccessor import * |
Scott Baker | 171d35e | 2016-06-20 17:36:29 -0700 | [diff] [blame] | 24 | from xos.logger import Logger, logging |
| 25 | |
| 26 | # hpclibrary will be in steps/.. |
| 27 | parentdir = os.path.join(os.path.dirname(__file__),"..") |
| 28 | sys.path.insert(0,parentdir) |
| 29 | |
| 30 | logger = Logger(level=logging.INFO) |
| 31 | |
Scott Baker | 171d35e | 2016-06-20 17:36:29 -0700 | [diff] [blame] | 32 | class SyncVTRTenant(SyncInstanceUsingAnsible): |
| 33 | provides=[VTRTenant] |
| 34 | observes=VTRTenant |
| 35 | requested_interval=0 |
| 36 | template_name = "sync_vtrtenant.yaml" |
Scott Baker | 171d35e | 2016-06-20 17:36:29 -0700 | [diff] [blame] | 37 | |
| 38 | def __init__(self, *args, **kwargs): |
| 39 | super(SyncVTRTenant, self).__init__(*args, **kwargs) |
| 40 | |
Scott Baker | 171d35e | 2016-06-20 17:36:29 -0700 | [diff] [blame] | 41 | def get_vtr_service(self, o): |
Scott Baker | c7958a9 | 2017-07-18 11:07:00 -0700 | [diff] [blame] | 42 | if not o.owner: |
Scott Baker | 171d35e | 2016-06-20 17:36:29 -0700 | [diff] [blame] | 43 | return None |
| 44 | |
Scott Baker | c7958a9 | 2017-07-18 11:07:00 -0700 | [diff] [blame] | 45 | # cast from Service to VTRService |
| 46 | vtrs = VTRService.objects.filter(id=o.owner.id) |
Scott Baker | 171d35e | 2016-06-20 17:36:29 -0700 | [diff] [blame] | 47 | if not vtrs: |
| 48 | return None |
| 49 | |
| 50 | return vtrs[0] |
| 51 | |
Scott Baker | 5db44a9 | 2017-03-06 17:27:52 -0800 | [diff] [blame] | 52 | def get_target(self, o): |
| 53 | target = o.target |
| 54 | if target: |
Scott Baker | 3bbf1e9 | 2017-03-07 12:06:06 -0800 | [diff] [blame] | 55 | model_name = getattr(target, "model_name", target.__class__.__name__) |
Scott Baker | c7958a9 | 2017-07-18 11:07:00 -0700 | [diff] [blame] | 56 | if model_name in ["ServiceInstance", "CordSubscriberRoot"]: |
| 57 | # cast from ServiceInstance to CordSubscriberRoot |
| 58 | csrs = CordSubscriberRoot.objects.filter(id=target.id) |
| 59 | if csrs: |
| 60 | return csrs[0] |
Scott Baker | 5db44a9 | 2017-03-06 17:27:52 -0800 | [diff] [blame] | 61 | return None |
| 62 | |
Scott Baker | 171d35e | 2016-06-20 17:36:29 -0700 | [diff] [blame] | 63 | def get_vcpe_service(self, o): |
Scott Baker | 5db44a9 | 2017-03-06 17:27:52 -0800 | [diff] [blame] | 64 | target = self.get_target(o) |
| 65 | if target and target.volt and target.volt.vcpe: |
Scott Baker | c7958a9 | 2017-07-18 11:07:00 -0700 | [diff] [blame] | 66 | return target.volt.vcpe |
Scott Baker | 171d35e | 2016-06-20 17:36:29 -0700 | [diff] [blame] | 67 | return None |
| 68 | |
| 69 | def get_instance(self, o): |
Scott Baker | 5db44a9 | 2017-03-06 17:27:52 -0800 | [diff] [blame] | 70 | target = self.get_target(o) |
| 71 | if target and target.volt and target.volt.vcpe: |
| 72 | return target.volt.vcpe.instance |
Scott Baker | 171d35e | 2016-06-20 17:36:29 -0700 | [diff] [blame] | 73 | else: |
| 74 | return None |
| 75 | |
| 76 | def get_key_name(self, instance): |
Scott Baker | 5809ed4 | 2017-03-07 10:45:00 -0800 | [diff] [blame] | 77 | # if instance.slice.service and (instance.slice.service.kind==VCPE_KIND): |
| 78 | # # We need to use the vsg service's private key. Onboarding won't |
| 79 | # # by default give us another service's private key, so let's assume |
| 80 | # # onboarding has been configured to add vsg_rsa to the vtr service. |
| 81 | # return "/opt/xos/services/vtr/keys/vsg_rsa" |
| 82 | |
| 83 | if instance.slice and instance.slice.service and instance.slice.service.private_key_fn: |
| 84 | # Assume the service has shared its key with VTR. |
| 85 | # Look for the instance's service key name in VTR's key directory. |
| 86 | service_keyfn = instance.slice.service.private_key_fn |
| 87 | return os.path.join("/opt/xos/services/vtr/keys", os.path.basename(service_keyfn)) |
Scott Baker | 171d35e | 2016-06-20 17:36:29 -0700 | [diff] [blame] | 88 | else: |
| 89 | raise Exception("VTR doesn't know how to get the private key for this instance") |
| 90 | |
| 91 | def get_extra_attributes(self, o): |
| 92 | vtr_service = self.get_vtr_service(o) |
| 93 | vcpe_service = self.get_vcpe_service(o) |
| 94 | |
| 95 | if not vcpe_service: |
| 96 | raise Exception("No vcpeservice") |
| 97 | |
| 98 | instance = self.get_instance(o) |
| 99 | |
| 100 | if not instance: |
| 101 | raise Exception("No instance") |
| 102 | |
Scott Baker | 5db44a9 | 2017-03-06 17:27:52 -0800 | [diff] [blame] | 103 | target = self.get_target(o) |
| 104 | |
Scott Baker | 171d35e | 2016-06-20 17:36:29 -0700 | [diff] [blame] | 105 | s_tags = [] |
| 106 | c_tags = [] |
Scott Baker | 5db44a9 | 2017-03-06 17:27:52 -0800 | [diff] [blame] | 107 | if target and target.volt: |
| 108 | s_tags.append(target.volt.s_tag) |
| 109 | c_tags.append(target.volt.c_tag) |
Scott Baker | 171d35e | 2016-06-20 17:36:29 -0700 | [diff] [blame] | 110 | |
| 111 | fields = {"s_tags": s_tags, |
| 112 | "c_tags": c_tags, |
| 113 | "isolation": instance.isolation, |
| 114 | "container_name": "vcpe-%s-%s" % (s_tags[0], c_tags[0]), |
Scott Baker | 3bbf1e9 | 2017-03-07 12:06:06 -0800 | [diff] [blame] | 115 | # "dns_servers": [x.strip() for x in vcpe_service.dns_servers.split(",")], |
Scott Baker | 171d35e | 2016-06-20 17:36:29 -0700 | [diff] [blame] | 116 | "result_fn": "%s-vcpe-%s-%s" % (o.test, s_tags[0], c_tags[0]), |
| 117 | "resultcode_fn": "code-%s-vcpe-%s-%s" % (o.test, s_tags[0], c_tags[0]) } |
| 118 | |
| 119 | # add in the sync_attributes that come from the vSG object |
| 120 | # this will be wan_ip, wan_mac, wan_container_ip, wan_container_mac, ... |
Scott Baker | 3a5b8e1 | 2017-03-31 14:22:20 -0700 | [diff] [blame] | 121 | if target and target.volt and target.volt.vcpe: |
| 122 | for attribute_name in ["wan_vm_ip", "wan_container_ip"]: |
| 123 | if hasattr(target.volt.vcpe, attribute_name): |
| 124 | fields[attribute_name] = getattr(target.volt.vcpe, attribute_name) |
Scott Baker | 171d35e | 2016-06-20 17:36:29 -0700 | [diff] [blame] | 125 | |
| 126 | # add in the sync_attributes that come from the SubscriberRoot object |
Scott Baker | 3bbf1e9 | 2017-03-07 12:06:06 -0800 | [diff] [blame] | 127 | # if target and hasattr(target, "sync_attributes"): |
| 128 | # for attribute_name in target.sync_attributes: |
| 129 | # fields[attribute_name] = getattr(target, attribute_name) |
Scott Baker | 171d35e | 2016-06-20 17:36:29 -0700 | [diff] [blame] | 130 | |
Scott Baker | 3bbf1e9 | 2017-03-07 12:06:06 -0800 | [diff] [blame] | 131 | for attribute_name in ["scope", "test", "argument"]: # o.sync_attributes: |
Scott Baker | 171d35e | 2016-06-20 17:36:29 -0700 | [diff] [blame] | 132 | fields[attribute_name] = getattr(o,attribute_name) |
| 133 | |
| 134 | return fields |
| 135 | |
| 136 | def sync_fields(self, o, fields): |
| 137 | # the super causes the playbook to be run |
| 138 | |
| 139 | super(SyncVTRTenant, self).sync_fields(o, fields) |
| 140 | |
| 141 | def run_playbook(self, o, fields): |
| 142 | o.result = "" |
| 143 | |
| 144 | result_fn = os.path.join("/opt/xos/synchronizers/vtr/result", fields["result_fn"]) |
| 145 | if os.path.exists(result_fn): |
| 146 | os.remove(result_fn) |
| 147 | |
| 148 | resultcode_fn = os.path.join("/opt/xos/synchronizers/vtr/result", fields["resultcode_fn"]) |
| 149 | if os.path.exists(resultcode_fn): |
| 150 | os.remove(resultcode_fn) |
| 151 | |
| 152 | super(SyncVTRTenant, self).run_playbook(o, fields) |
| 153 | |
| 154 | if os.path.exists(result_fn): |
| 155 | o.result = open(result_fn).read() |
| 156 | |
| 157 | if os.path.exists(resultcode_fn): |
| 158 | o.result_code = open(resultcode_fn).read() |
| 159 | |
| 160 | |
| 161 | def delete_record(self, m): |
| 162 | pass |