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 | 171d35e | 2016-06-20 17:36:29 -0700 | [diff] [blame] | 14 | from xos.logger import Logger, logging |
| 15 | |
| 16 | # hpclibrary will be in steps/.. |
| 17 | parentdir = os.path.join(os.path.dirname(__file__),"..") |
| 18 | sys.path.insert(0,parentdir) |
| 19 | |
| 20 | logger = Logger(level=logging.INFO) |
| 21 | |
| 22 | CORD_USE_VTN = getattr(Config(), "networking_use_vtn", False) |
| 23 | |
| 24 | class SyncVTRTenant(SyncInstanceUsingAnsible): |
| 25 | provides=[VTRTenant] |
| 26 | observes=VTRTenant |
| 27 | requested_interval=0 |
| 28 | template_name = "sync_vtrtenant.yaml" |
| 29 | #service_key_name = "/opt/xos/services/vtr/vcpe_private_key" |
| 30 | |
| 31 | def __init__(self, *args, **kwargs): |
| 32 | super(SyncVTRTenant, self).__init__(*args, **kwargs) |
| 33 | |
| 34 | def fetch_pending(self, deleted): |
| 35 | if (not deleted): |
| 36 | objs = VTRTenant.get_tenant_objects().filter(Q(enacted__lt=F('updated')) | Q(enacted=None),Q(lazy_blocked=False)) |
| 37 | else: |
| 38 | objs = VTRTenant.get_deleted_tenant_objects() |
| 39 | |
| 40 | return objs |
| 41 | |
| 42 | def get_vtr_service(self, o): |
| 43 | if not o.provider_service: |
| 44 | return None |
| 45 | |
| 46 | vtrs = VTRService.get_service_objects().filter(id=o.provider_service.id) |
| 47 | if not vtrs: |
| 48 | return None |
| 49 | |
| 50 | return vtrs[0] |
| 51 | |
| 52 | def get_vcpe_service(self, o): |
| 53 | if o.target: |
| 54 | # o.target is a CordSubscriberRoot |
| 55 | if o.target.volt and o.target.volt.vcpe: |
| 56 | vcpes = VSGService.get_service_objects().filter(id=o.target.volt.vcpe.provider_service.id) |
| 57 | if not vcpes: |
| 58 | return None |
| 59 | return vcpes[0] |
| 60 | return None |
| 61 | |
| 62 | def get_instance(self, o): |
| 63 | if o.target and o.target.volt and o.target.volt.vcpe: |
| 64 | return o.target.volt.vcpe.instance |
| 65 | else: |
| 66 | return None |
| 67 | |
| 68 | def get_key_name(self, instance): |
| 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 | 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 | |
| 89 | s_tags = [] |
| 90 | c_tags = [] |
| 91 | if o.target and o.target.volt: |
| 92 | s_tags.append(o.target.volt.s_tag) |
| 93 | c_tags.append(o.target.volt.c_tag) |
| 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]), |
| 99 | "dns_servers": [x.strip() for x in vcpe_service.dns_servers.split(",")], |
| 100 | |
| 101 | "result_fn": "%s-vcpe-%s-%s" % (o.test, s_tags[0], c_tags[0]), |
| 102 | "resultcode_fn": "code-%s-vcpe-%s-%s" % (o.test, s_tags[0], c_tags[0]) } |
| 103 | |
| 104 | # add in the sync_attributes that come from the vSG object |
| 105 | # this will be wan_ip, wan_mac, wan_container_ip, wan_container_mac, ... |
| 106 | if o.target and o.target.volt and o.target.volt.vcpe: |
| 107 | for attribute_name in o.target.volt.vcpe.sync_attributes: |
| 108 | fields[attribute_name] = getattr(o.target.volt.vcpe, attribute_name) |
| 109 | |
| 110 | # add in the sync_attributes that come from the SubscriberRoot object |
| 111 | if o.target and hasattr(o.target, "sync_attributes"): |
| 112 | for attribute_name in o.target.sync_attributes: |
| 113 | fields[attribute_name] = getattr(o.target, attribute_name) |
| 114 | |
| 115 | for attribute_name in o.sync_attributes: |
| 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 |