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