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 |
| 6 | from django.db.models import F, Q |
| 7 | from xos.config import Config |
| 8 | from synchronizers.base.syncstep import SyncStep |
Sapan Bhatia | f09136a | 2017-01-24 19:32:59 +0100 | [diff] [blame] | 9 | from synchronizers.base.ansible_helper import run_template_ssh |
Scott Baker | 171d35e | 2016-06-20 17:36:29 -0700 | [diff] [blame] | 10 | from synchronizers.base.SyncInstanceUsingAnsible import SyncInstanceUsingAnsible |
| 11 | from core.models import Service, Slice, Tag |
| 12 | from services.vsg.models import VSGService, VCPE_KIND |
| 13 | from services.vtr.models import VTRService, VTRTenant |
Scott Baker | 5db44a9 | 2017-03-06 17:27:52 -0800 | [diff] [blame^] | 14 | from services.volt.models import CordSubscriberRoot |
Scott Baker | 171d35e | 2016-06-20 17:36:29 -0700 | [diff] [blame] | 15 | from xos.logger import Logger, logging |
| 16 | |
| 17 | # hpclibrary will be in steps/.. |
| 18 | parentdir = os.path.join(os.path.dirname(__file__),"..") |
| 19 | sys.path.insert(0,parentdir) |
| 20 | |
| 21 | logger = Logger(level=logging.INFO) |
| 22 | |
| 23 | CORD_USE_VTN = getattr(Config(), "networking_use_vtn", False) |
| 24 | |
| 25 | class SyncVTRTenant(SyncInstanceUsingAnsible): |
| 26 | provides=[VTRTenant] |
| 27 | observes=VTRTenant |
| 28 | requested_interval=0 |
| 29 | template_name = "sync_vtrtenant.yaml" |
| 30 | #service_key_name = "/opt/xos/services/vtr/vcpe_private_key" |
| 31 | |
| 32 | def __init__(self, *args, **kwargs): |
| 33 | super(SyncVTRTenant, self).__init__(*args, **kwargs) |
| 34 | |
Scott Baker | 171d35e | 2016-06-20 17:36:29 -0700 | [diff] [blame] | 35 | def get_vtr_service(self, o): |
| 36 | if not o.provider_service: |
| 37 | return None |
| 38 | |
Scott Baker | 5db44a9 | 2017-03-06 17:27:52 -0800 | [diff] [blame^] | 39 | vtrs = VTRService.objects.filter(id=o.provider_service.id) |
Scott Baker | 171d35e | 2016-06-20 17:36:29 -0700 | [diff] [blame] | 40 | if not vtrs: |
| 41 | return None |
| 42 | |
| 43 | return vtrs[0] |
| 44 | |
Scott Baker | 5db44a9 | 2017-03-06 17:27:52 -0800 | [diff] [blame^] | 45 | def get_target(self, o): |
| 46 | target = o.target |
| 47 | if target: |
| 48 | # CordSubscriberRoot is a Proxy object, and o.target will point to |
| 49 | # the base class... so fix it up. |
| 50 | if target.__class__.__name__ == "TenantRoot": |
| 51 | target = CordSubscriberRoot.objects.get(id=target.id) |
| 52 | return target |
| 53 | return None |
| 54 | |
Scott Baker | 171d35e | 2016-06-20 17:36:29 -0700 | [diff] [blame] | 55 | def get_vcpe_service(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 | vcpes = VSGService.get_service_objects().filter(id=target.volt.vcpe.provider_service.id) |
| 59 | if not vcpes: |
| 60 | return None |
| 61 | return vcpes[0] |
Scott Baker | 171d35e | 2016-06-20 17:36:29 -0700 | [diff] [blame] | 62 | return None |
| 63 | |
| 64 | def get_instance(self, o): |
Scott Baker | 5db44a9 | 2017-03-06 17:27:52 -0800 | [diff] [blame^] | 65 | target = self.get_target(o) |
| 66 | if target and target.volt and target.volt.vcpe: |
| 67 | return target.volt.vcpe.instance |
Scott Baker | 171d35e | 2016-06-20 17:36:29 -0700 | [diff] [blame] | 68 | else: |
| 69 | return None |
| 70 | |
| 71 | def get_key_name(self, instance): |
| 72 | if instance.slice.service and (instance.slice.service.kind==VCPE_KIND): |
| 73 | # We need to use the vsg service's private key. Onboarding won't |
| 74 | # by default give us another service's private key, so let's assume |
| 75 | # onboarding has been configured to add vsg_rsa to the vtr service. |
| 76 | return "/opt/xos/services/vtr/keys/vsg_rsa" |
| 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]), |
| 104 | "dns_servers": [x.strip() for x in vcpe_service.dns_servers.split(",")], |
| 105 | |
| 106 | "result_fn": "%s-vcpe-%s-%s" % (o.test, s_tags[0], c_tags[0]), |
| 107 | "resultcode_fn": "code-%s-vcpe-%s-%s" % (o.test, s_tags[0], c_tags[0]) } |
| 108 | |
| 109 | # add in the sync_attributes that come from the vSG object |
| 110 | # 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^] | 111 | if target and target.volt and target.volt.vcpe: |
| 112 | for attribute_name in target.volt.vcpe.sync_attributes: |
| 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 | 5db44a9 | 2017-03-06 17:27:52 -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 | |
| 120 | for attribute_name in o.sync_attributes: |
| 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 |