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