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 * |
Scott Baker | 171d35e | 2016-06-20 17:36:29 -0700 | [diff] [blame] | 9 | from xos.logger import Logger, logging |
| 10 | |
| 11 | # hpclibrary will be in steps/.. |
| 12 | parentdir = os.path.join(os.path.dirname(__file__),"..") |
| 13 | sys.path.insert(0,parentdir) |
| 14 | |
| 15 | logger = Logger(level=logging.INFO) |
| 16 | |
| 17 | CORD_USE_VTN = getattr(Config(), "networking_use_vtn", False) |
| 18 | |
| 19 | class SyncVTRTenant(SyncInstanceUsingAnsible): |
| 20 | provides=[VTRTenant] |
| 21 | observes=VTRTenant |
| 22 | requested_interval=0 |
| 23 | template_name = "sync_vtrtenant.yaml" |
Scott Baker | 171d35e | 2016-06-20 17:36:29 -0700 | [diff] [blame] | 24 | |
| 25 | def __init__(self, *args, **kwargs): |
| 26 | super(SyncVTRTenant, self).__init__(*args, **kwargs) |
| 27 | |
Scott Baker | 171d35e | 2016-06-20 17:36:29 -0700 | [diff] [blame] | 28 | def get_vtr_service(self, o): |
| 29 | if not o.provider_service: |
| 30 | return None |
| 31 | |
Scott Baker | 5db44a9 | 2017-03-06 17:27:52 -0800 | [diff] [blame] | 32 | vtrs = VTRService.objects.filter(id=o.provider_service.id) |
Scott Baker | 171d35e | 2016-06-20 17:36:29 -0700 | [diff] [blame] | 33 | if not vtrs: |
| 34 | return None |
| 35 | |
| 36 | return vtrs[0] |
| 37 | |
Scott Baker | 5db44a9 | 2017-03-06 17:27:52 -0800 | [diff] [blame] | 38 | def get_target(self, o): |
| 39 | target = o.target |
| 40 | if target: |
Scott Baker | 3bbf1e9 | 2017-03-07 12:06:06 -0800 | [diff] [blame] | 41 | model_name = getattr(target, "model_name", target.__class__.__name__) |
Scott Baker | 5db44a9 | 2017-03-06 17:27:52 -0800 | [diff] [blame] | 42 | # CordSubscriberRoot is a Proxy object, and o.target will point to |
| 43 | # the base class... so fix it up. |
Scott Baker | 3bbf1e9 | 2017-03-07 12:06:06 -0800 | [diff] [blame] | 44 | if model_name == "TenantRoot": |
Scott Baker | 5db44a9 | 2017-03-06 17:27:52 -0800 | [diff] [blame] | 45 | target = CordSubscriberRoot.objects.get(id=target.id) |
| 46 | return target |
| 47 | return None |
| 48 | |
Scott Baker | 171d35e | 2016-06-20 17:36:29 -0700 | [diff] [blame] | 49 | def get_vcpe_service(self, o): |
Scott Baker | 5db44a9 | 2017-03-06 17:27:52 -0800 | [diff] [blame] | 50 | target = self.get_target(o) |
| 51 | if target and target.volt and target.volt.vcpe: |
Scott Baker | 3bbf1e9 | 2017-03-07 12:06:06 -0800 | [diff] [blame] | 52 | vcpes = VSGService.objects.filter(id=target.volt.vcpe.provider_service.id) |
Scott Baker | 5db44a9 | 2017-03-06 17:27:52 -0800 | [diff] [blame] | 53 | if not vcpes: |
| 54 | return None |
| 55 | return vcpes[0] |
Scott Baker | 171d35e | 2016-06-20 17:36:29 -0700 | [diff] [blame] | 56 | return None |
| 57 | |
| 58 | def get_instance(self, o): |
Scott Baker | 5db44a9 | 2017-03-06 17:27:52 -0800 | [diff] [blame] | 59 | target = self.get_target(o) |
| 60 | if target and target.volt and target.volt.vcpe: |
| 61 | return target.volt.vcpe.instance |
Scott Baker | 171d35e | 2016-06-20 17:36:29 -0700 | [diff] [blame] | 62 | else: |
| 63 | return None |
| 64 | |
| 65 | def get_key_name(self, instance): |
Scott Baker | 5809ed4 | 2017-03-07 10:45:00 -0800 | [diff] [blame] | 66 | # if instance.slice.service and (instance.slice.service.kind==VCPE_KIND): |
| 67 | # # We need to use the vsg service's private key. Onboarding won't |
| 68 | # # by default give us another service's private key, so let's assume |
| 69 | # # onboarding has been configured to add vsg_rsa to the vtr service. |
| 70 | # return "/opt/xos/services/vtr/keys/vsg_rsa" |
| 71 | |
| 72 | if instance.slice and instance.slice.service and instance.slice.service.private_key_fn: |
| 73 | # Assume the service has shared its key with VTR. |
| 74 | # Look for the instance's service key name in VTR's key directory. |
| 75 | service_keyfn = instance.slice.service.private_key_fn |
| 76 | 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] | 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 Baker | 5db44a9 | 2017-03-06 17:27:52 -0800 | [diff] [blame] | 92 | target = self.get_target(o) |
| 93 | |
Scott Baker | 171d35e | 2016-06-20 17:36:29 -0700 | [diff] [blame] | 94 | s_tags = [] |
| 95 | c_tags = [] |
Scott Baker | 5db44a9 | 2017-03-06 17:27:52 -0800 | [diff] [blame] | 96 | if target and target.volt: |
| 97 | s_tags.append(target.volt.s_tag) |
| 98 | c_tags.append(target.volt.c_tag) |
Scott Baker | 171d35e | 2016-06-20 17:36:29 -0700 | [diff] [blame] | 99 | |
| 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]), |
Scott Baker | 3bbf1e9 | 2017-03-07 12:06:06 -0800 | [diff] [blame] | 104 | # "dns_servers": [x.strip() for x in vcpe_service.dns_servers.split(",")], |
Scott Baker | 171d35e | 2016-06-20 17:36:29 -0700 | [diff] [blame] | 105 | "result_fn": "%s-vcpe-%s-%s" % (o.test, s_tags[0], c_tags[0]), |
| 106 | "resultcode_fn": "code-%s-vcpe-%s-%s" % (o.test, s_tags[0], c_tags[0]) } |
| 107 | |
| 108 | # add in the sync_attributes that come from the vSG object |
| 109 | # 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] | 110 | if target and target.volt and target.volt.vcpe: |
| 111 | for attribute_name in ["wan_vm_ip", "wan_container_ip"]: |
| 112 | if hasattr(target.volt.vcpe, attribute_name): |
| 113 | fields[attribute_name] = getattr(target.volt.vcpe, attribute_name) |
Scott Baker | 171d35e | 2016-06-20 17:36:29 -0700 | [diff] [blame] | 114 | |
| 115 | # add in the sync_attributes that come from the SubscriberRoot object |
Scott Baker | 3bbf1e9 | 2017-03-07 12:06:06 -0800 | [diff] [blame] | 116 | # if target and hasattr(target, "sync_attributes"): |
| 117 | # for attribute_name in target.sync_attributes: |
| 118 | # fields[attribute_name] = getattr(target, attribute_name) |
Scott Baker | 171d35e | 2016-06-20 17:36:29 -0700 | [diff] [blame] | 119 | |
Scott Baker | 3bbf1e9 | 2017-03-07 12:06:06 -0800 | [diff] [blame] | 120 | for attribute_name in ["scope", "test", "argument"]: # o.sync_attributes: |
Scott Baker | 171d35e | 2016-06-20 17:36:29 -0700 | [diff] [blame] | 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 |