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: |
Scott Baker | 3bbf1e9 | 2017-03-07 12:06:06 -0800 | [diff] [blame] | 44 | model_name = getattr(target, "model_name", target.__class__.__name__) |
Scott Baker | 5db44a9 | 2017-03-06 17:27:52 -0800 | [diff] [blame] | 45 | # CordSubscriberRoot is a Proxy object, and o.target will point to |
| 46 | # the base class... so fix it up. |
Scott Baker | 3bbf1e9 | 2017-03-07 12:06:06 -0800 | [diff] [blame] | 47 | if model_name == "TenantRoot": |
Scott Baker | 5db44a9 | 2017-03-06 17:27:52 -0800 | [diff] [blame] | 48 | target = CordSubscriberRoot.objects.get(id=target.id) |
| 49 | return target |
| 50 | return None |
| 51 | |
Scott Baker | 171d35e | 2016-06-20 17:36:29 -0700 | [diff] [blame] | 52 | def get_vcpe_service(self, o): |
Scott Baker | 5db44a9 | 2017-03-06 17:27:52 -0800 | [diff] [blame] | 53 | target = self.get_target(o) |
| 54 | if target and target.volt and target.volt.vcpe: |
Scott Baker | 3bbf1e9 | 2017-03-07 12:06:06 -0800 | [diff] [blame] | 55 | vcpes = VSGService.objects.filter(id=target.volt.vcpe.provider_service.id) |
Scott Baker | 5db44a9 | 2017-03-06 17:27:52 -0800 | [diff] [blame] | 56 | if not vcpes: |
| 57 | return None |
| 58 | return vcpes[0] |
Scott Baker | 171d35e | 2016-06-20 17:36:29 -0700 | [diff] [blame] | 59 | return None |
| 60 | |
| 61 | def get_instance(self, o): |
Scott Baker | 5db44a9 | 2017-03-06 17:27:52 -0800 | [diff] [blame] | 62 | target = self.get_target(o) |
| 63 | if target and target.volt and target.volt.vcpe: |
| 64 | return target.volt.vcpe.instance |
Scott Baker | 171d35e | 2016-06-20 17:36:29 -0700 | [diff] [blame] | 65 | else: |
| 66 | return None |
| 67 | |
| 68 | def get_key_name(self, instance): |
Scott Baker | 5809ed4 | 2017-03-07 10:45:00 -0800 | [diff] [blame] | 69 | # 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 Baker | 171d35e | 2016-06-20 17:36:29 -0700 | [diff] [blame] | 80 | 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 Baker | 5db44a9 | 2017-03-06 17:27:52 -0800 | [diff] [blame] | 95 | target = self.get_target(o) |
| 96 | |
Scott Baker | 171d35e | 2016-06-20 17:36:29 -0700 | [diff] [blame] | 97 | s_tags = [] |
| 98 | c_tags = [] |
Scott Baker | 5db44a9 | 2017-03-06 17:27:52 -0800 | [diff] [blame] | 99 | if target and target.volt: |
| 100 | s_tags.append(target.volt.s_tag) |
| 101 | c_tags.append(target.volt.c_tag) |
Scott Baker | 171d35e | 2016-06-20 17:36:29 -0700 | [diff] [blame] | 102 | |
| 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 Baker | 3bbf1e9 | 2017-03-07 12:06:06 -0800 | [diff] [blame] | 107 | # "dns_servers": [x.strip() for x in vcpe_service.dns_servers.split(",")], |
Scott Baker | 171d35e | 2016-06-20 17:36:29 -0700 | [diff] [blame] | 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 | 3bbf1e9 | 2017-03-07 12:06:06 -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 | 3bbf1e9 | 2017-03-07 12:06:06 -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 | |
Scott Baker | 3bbf1e9 | 2017-03-07 12:06:06 -0800 | [diff] [blame] | 122 | for attribute_name in ["scope", "test", "argument"]: # o.sync_attributes: |
Scott Baker | 171d35e | 2016-06-20 17:36:29 -0700 | [diff] [blame] | 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 |