Scott Baker | 171d35e | 2016-06-20 17:36:29 -0700 | [diff] [blame] | 1 | import os |
| 2 | import socket |
| 3 | import sys |
| 4 | import base64 |
| 5 | import time |
Scott Baker | 171d35e | 2016-06-20 17:36:29 -0700 | [diff] [blame] | 6 | from xos.config import Config |
Scott Baker | 5809ed4 | 2017-03-07 10:45:00 -0800 | [diff] [blame^] | 7 | from synchronizers.new_base.SyncInstanceUsingAnsible import SyncInstanceUsingAnsible |
| 8 | from 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 Baker | 171d35e | 2016-06-20 17:36:29 -0700 | [diff] [blame] | 12 | from xos.logger import Logger, logging |
| 13 | |
| 14 | # hpclibrary will be in steps/.. |
| 15 | parentdir = os.path.join(os.path.dirname(__file__),"..") |
| 16 | sys.path.insert(0,parentdir) |
| 17 | |
| 18 | logger = Logger(level=logging.INFO) |
| 19 | |
| 20 | CORD_USE_VTN = getattr(Config(), "networking_use_vtn", False) |
| 21 | |
| 22 | class SyncVTRTenant(SyncInstanceUsingAnsible): |
| 23 | provides=[VTRTenant] |
| 24 | observes=VTRTenant |
| 25 | requested_interval=0 |
| 26 | template_name = "sync_vtrtenant.yaml" |
Scott Baker | 171d35e | 2016-06-20 17:36:29 -0700 | [diff] [blame] | 27 | |
| 28 | def __init__(self, *args, **kwargs): |
| 29 | super(SyncVTRTenant, self).__init__(*args, **kwargs) |
| 30 | |
Scott Baker | 171d35e | 2016-06-20 17:36:29 -0700 | [diff] [blame] | 31 | def get_vtr_service(self, o): |
| 32 | if not o.provider_service: |
| 33 | return None |
| 34 | |
Scott Baker | 5db44a9 | 2017-03-06 17:27:52 -0800 | [diff] [blame] | 35 | vtrs = VTRService.objects.filter(id=o.provider_service.id) |
Scott Baker | 171d35e | 2016-06-20 17:36:29 -0700 | [diff] [blame] | 36 | if not vtrs: |
| 37 | return None |
| 38 | |
| 39 | return vtrs[0] |
| 40 | |
Scott Baker | 5db44a9 | 2017-03-06 17:27:52 -0800 | [diff] [blame] | 41 | def get_target(self, o): |
| 42 | target = o.target |
| 43 | if target: |
| 44 | # CordSubscriberRoot is a Proxy object, and o.target will point to |
| 45 | # the base class... so fix it up. |
| 46 | if target.__class__.__name__ == "TenantRoot": |
| 47 | target = CordSubscriberRoot.objects.get(id=target.id) |
| 48 | return target |
| 49 | return None |
| 50 | |
Scott Baker | 171d35e | 2016-06-20 17:36:29 -0700 | [diff] [blame] | 51 | def get_vcpe_service(self, o): |
Scott Baker | 5db44a9 | 2017-03-06 17:27:52 -0800 | [diff] [blame] | 52 | target = self.get_target(o) |
| 53 | if target and target.volt and target.volt.vcpe: |
| 54 | vcpes = VSGService.get_service_objects().filter(id=target.volt.vcpe.provider_service.id) |
| 55 | if not vcpes: |
| 56 | return None |
| 57 | return vcpes[0] |
Scott Baker | 171d35e | 2016-06-20 17:36:29 -0700 | [diff] [blame] | 58 | return None |
| 59 | |
| 60 | def get_instance(self, o): |
Scott Baker | 5db44a9 | 2017-03-06 17:27:52 -0800 | [diff] [blame] | 61 | target = self.get_target(o) |
| 62 | if target and target.volt and target.volt.vcpe: |
| 63 | return target.volt.vcpe.instance |
Scott Baker | 171d35e | 2016-06-20 17:36:29 -0700 | [diff] [blame] | 64 | else: |
| 65 | return None |
| 66 | |
| 67 | def get_key_name(self, instance): |
Scott Baker | 5809ed4 | 2017-03-07 10:45:00 -0800 | [diff] [blame^] | 68 | # if instance.slice.service and (instance.slice.service.kind==VCPE_KIND): |
| 69 | # # We need to use the vsg service's private key. Onboarding won't |
| 70 | # # by default give us another service's private key, so let's assume |
| 71 | # # onboarding has been configured to add vsg_rsa to the vtr service. |
| 72 | # return "/opt/xos/services/vtr/keys/vsg_rsa" |
| 73 | |
| 74 | if instance.slice and instance.slice.service and instance.slice.service.private_key_fn: |
| 75 | # Assume the service has shared its key with VTR. |
| 76 | # Look for the instance's service key name in VTR's key directory. |
| 77 | service_keyfn = instance.slice.service.private_key_fn |
| 78 | 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] | 79 | else: |
| 80 | raise Exception("VTR doesn't know how to get the private key for this instance") |
| 81 | |
| 82 | def get_extra_attributes(self, o): |
| 83 | vtr_service = self.get_vtr_service(o) |
| 84 | vcpe_service = self.get_vcpe_service(o) |
| 85 | |
| 86 | if not vcpe_service: |
| 87 | raise Exception("No vcpeservice") |
| 88 | |
| 89 | instance = self.get_instance(o) |
| 90 | |
| 91 | if not instance: |
| 92 | raise Exception("No instance") |
| 93 | |
Scott Baker | 5db44a9 | 2017-03-06 17:27:52 -0800 | [diff] [blame] | 94 | target = self.get_target(o) |
| 95 | |
Scott Baker | 171d35e | 2016-06-20 17:36:29 -0700 | [diff] [blame] | 96 | s_tags = [] |
| 97 | c_tags = [] |
Scott Baker | 5db44a9 | 2017-03-06 17:27:52 -0800 | [diff] [blame] | 98 | if target and target.volt: |
| 99 | s_tags.append(target.volt.s_tag) |
| 100 | c_tags.append(target.volt.c_tag) |
Scott Baker | 171d35e | 2016-06-20 17:36:29 -0700 | [diff] [blame] | 101 | |
| 102 | fields = {"s_tags": s_tags, |
| 103 | "c_tags": c_tags, |
| 104 | "isolation": instance.isolation, |
| 105 | "container_name": "vcpe-%s-%s" % (s_tags[0], c_tags[0]), |
| 106 | "dns_servers": [x.strip() for x in vcpe_service.dns_servers.split(",")], |
| 107 | |
| 108 | "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 Baker | 5db44a9 | 2017-03-06 17:27:52 -0800 | [diff] [blame] | 113 | if target and target.volt and target.volt.vcpe: |
| 114 | for attribute_name in target.volt.vcpe.sync_attributes: |
| 115 | fields[attribute_name] = getattr(target.volt.vcpe, attribute_name) |
Scott Baker | 171d35e | 2016-06-20 17:36:29 -0700 | [diff] [blame] | 116 | |
| 117 | # add in the sync_attributes that come from the SubscriberRoot object |
Scott Baker | 5db44a9 | 2017-03-06 17:27:52 -0800 | [diff] [blame] | 118 | if target and hasattr(target, "sync_attributes"): |
| 119 | for attribute_name in target.sync_attributes: |
| 120 | fields[attribute_name] = getattr(target, attribute_name) |
Scott Baker | 171d35e | 2016-06-20 17:36:29 -0700 | [diff] [blame] | 121 | |
| 122 | for attribute_name in o.sync_attributes: |
| 123 | fields[attribute_name] = getattr(o,attribute_name) |
| 124 | |
| 125 | return fields |
| 126 | |
| 127 | def sync_fields(self, o, fields): |
| 128 | # the super causes the playbook to be run |
| 129 | |
| 130 | super(SyncVTRTenant, self).sync_fields(o, fields) |
| 131 | |
| 132 | def run_playbook(self, o, fields): |
| 133 | o.result = "" |
| 134 | |
| 135 | result_fn = os.path.join("/opt/xos/synchronizers/vtr/result", fields["result_fn"]) |
| 136 | if os.path.exists(result_fn): |
| 137 | os.remove(result_fn) |
| 138 | |
| 139 | resultcode_fn = os.path.join("/opt/xos/synchronizers/vtr/result", fields["resultcode_fn"]) |
| 140 | if os.path.exists(resultcode_fn): |
| 141 | os.remove(resultcode_fn) |
| 142 | |
| 143 | super(SyncVTRTenant, self).run_playbook(o, fields) |
| 144 | |
| 145 | if os.path.exists(result_fn): |
| 146 | o.result = open(result_fn).read() |
| 147 | |
| 148 | if os.path.exists(resultcode_fn): |
| 149 | o.result_code = open(resultcode_fn).read() |
| 150 | |
| 151 | |
| 152 | def delete_record(self, m): |
| 153 | pass |