Matteo Scandolo | aca8665 | 2017-08-08 13:05:27 -0700 | [diff] [blame] | 1 | |
| 2 | # Copyright 2017-present Open Networking Foundation |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
| 16 | |
Scott Baker | 761e106 | 2016-06-20 17:18:17 -0700 | [diff] [blame] | 17 | import hashlib |
| 18 | import os |
| 19 | import socket |
| 20 | import sys |
| 21 | import base64 |
| 22 | import time |
Srikanth Vavilapalli | cd9d9bd | 2016-12-03 22:53:42 +0000 | [diff] [blame] | 23 | from urlparse import urlparse |
Scott Baker | 32f6512 | 2017-03-08 17:04:16 -0800 | [diff] [blame] | 24 | from synchronizers.new_base.SyncInstanceUsingAnsible import SyncInstanceUsingAnsible |
| 25 | from synchronizers.new_base.modelaccessor import * |
| 26 | from synchronizers.new_base.ansible_helper import run_template_ssh |
Scott Baker | 761e106 | 2016-06-20 17:18:17 -0700 | [diff] [blame] | 27 | from xos.logger import Logger, logging |
| 28 | |
| 29 | # hpclibrary will be in steps/.. |
| 30 | parentdir = os.path.join(os.path.dirname(__file__),"..") |
| 31 | sys.path.insert(0,parentdir) |
| 32 | |
Scott Baker | 761e106 | 2016-06-20 17:18:17 -0700 | [diff] [blame] | 33 | logger = Logger(level=logging.INFO) |
| 34 | |
| 35 | ENABLE_QUICK_UPDATE=False |
| 36 | |
Scott Baker | 761e106 | 2016-06-20 17:18:17 -0700 | [diff] [blame] | 37 | class SyncVSGTenant(SyncInstanceUsingAnsible): |
| 38 | provides=[VSGTenant] |
| 39 | observes=VSGTenant |
| 40 | requested_interval=0 |
| 41 | template_name = "sync_vcpetenant.yaml" |
Scott Baker | 3f8a390 | 2017-03-20 11:04:32 -0700 | [diff] [blame] | 42 | watches = [ModelLink(ServiceDependency,via='servicedependency'), ModelLink(ServiceMonitoringAgentInfo,via='monitoringagentinfo')] |
Scott Baker | 761e106 | 2016-06-20 17:18:17 -0700 | [diff] [blame] | 43 | |
| 44 | def __init__(self, *args, **kwargs): |
| 45 | super(SyncVSGTenant, self).__init__(*args, **kwargs) |
| 46 | |
Scott Baker | 761e106 | 2016-06-20 17:18:17 -0700 | [diff] [blame] | 47 | def get_vcpe_service(self, o): |
Scott Baker | 80238f8 | 2017-07-18 16:01:10 -0700 | [diff] [blame] | 48 | if not o.owner: |
Scott Baker | 761e106 | 2016-06-20 17:18:17 -0700 | [diff] [blame] | 49 | return None |
| 50 | |
Scott Baker | 80238f8 | 2017-07-18 16:01:10 -0700 | [diff] [blame] | 51 | vcpes = VSGService.objects.filter(id=o.owner.id) |
Scott Baker | 761e106 | 2016-06-20 17:18:17 -0700 | [diff] [blame] | 52 | if not vcpes: |
| 53 | return None |
| 54 | |
| 55 | return vcpes[0] |
| 56 | |
| 57 | def get_extra_attributes(self, o): |
| 58 | # This is a place to include extra attributes that aren't part of the |
| 59 | # object itself. In the case of vCPE, we need to know: |
| 60 | # 1) the addresses of dnsdemux, to setup dnsmasq in the vCPE |
| 61 | # 2) CDN prefixes, so we know what URLs to send to dnsdemux |
Scott Baker | 761e106 | 2016-06-20 17:18:17 -0700 | [diff] [blame] | 62 | # 4) vlan_ids, for setting up networking in the vCPE VM |
| 63 | |
| 64 | vcpe_service = self.get_vcpe_service(o) |
| 65 | |
| 66 | dnsdemux_ip = None |
| 67 | cdn_prefixes = [] |
| 68 | |
| 69 | cdn_config_fn = "/opt/xos/synchronizers/vsg/cdn_config" |
| 70 | if os.path.exists(cdn_config_fn): |
| 71 | # manual CDN configuration |
| 72 | # the first line is the address of dnsredir |
| 73 | # the remaining lines are domain names, one per line |
| 74 | lines = file(cdn_config_fn).readlines() |
| 75 | if len(lines)>=2: |
| 76 | dnsdemux_ip = lines[0].strip() |
| 77 | cdn_prefixes = [x.strip() for x in lines[1:] if x.strip()] |
Scott Baker | 761e106 | 2016-06-20 17:18:17 -0700 | [diff] [blame] | 78 | |
| 79 | dnsdemux_ip = dnsdemux_ip or "none" |
| 80 | |
Scott Baker | 761e106 | 2016-06-20 17:18:17 -0700 | [diff] [blame] | 81 | s_tags = [] |
| 82 | c_tags = [] |
| 83 | if o.volt: |
| 84 | s_tags.append(o.volt.s_tag) |
| 85 | c_tags.append(o.volt.c_tag) |
| 86 | |
Matteo Scandolo | 79f298e | 2017-06-05 11:18:01 -0700 | [diff] [blame] | 87 | full_setup = True |
Scott Baker | 761e106 | 2016-06-20 17:18:17 -0700 | [diff] [blame] | 88 | |
| 89 | safe_macs=[] |
| 90 | if vcpe_service.url_filter_kind == "safebrowsing": |
| 91 | if o.volt and o.volt.subscriber: |
| 92 | for user in o.volt.subscriber.devices: |
| 93 | level = user.get("level",None) |
| 94 | mac = user.get("mac",None) |
| 95 | if level in ["G", "PG"]: |
| 96 | if mac: |
| 97 | safe_macs.append(mac) |
| 98 | |
Scott Baker | 8e66d66 | 2016-10-13 13:22:49 -0700 | [diff] [blame] | 99 | docker_opts = [] |
| 100 | if vcpe_service.docker_insecure_registry: |
| 101 | reg_name = vcpe_service.docker_image_name.split("/",1)[0] |
| 102 | docker_opts.append("--insecure-registry " + reg_name) |
| 103 | |
Scott Baker | 761e106 | 2016-06-20 17:18:17 -0700 | [diff] [blame] | 104 | fields = {"s_tags": s_tags, |
| 105 | "c_tags": c_tags, |
Scott Baker | 8e66d66 | 2016-10-13 13:22:49 -0700 | [diff] [blame] | 106 | "docker_remote_image_name": vcpe_service.docker_image_name, |
Scott Baker | 32f6512 | 2017-03-08 17:04:16 -0800 | [diff] [blame] | 107 | "docker_local_image_name": vcpe_service.docker_image_name, |
Scott Baker | 8e66d66 | 2016-10-13 13:22:49 -0700 | [diff] [blame] | 108 | "docker_opts": " ".join(docker_opts), |
Scott Baker | 761e106 | 2016-06-20 17:18:17 -0700 | [diff] [blame] | 109 | "dnsdemux_ip": dnsdemux_ip, |
| 110 | "cdn_prefixes": cdn_prefixes, |
Scott Baker | 761e106 | 2016-06-20 17:18:17 -0700 | [diff] [blame] | 111 | "full_setup": full_setup, |
| 112 | "isolation": o.instance.isolation, |
| 113 | "safe_browsing_macs": safe_macs, |
| 114 | "container_name": "vcpe-%s-%s" % (s_tags[0], c_tags[0]), |
| 115 | "dns_servers": [x.strip() for x in vcpe_service.dns_servers.split(",")], |
| 116 | "url_filter_kind": vcpe_service.url_filter_kind } |
| 117 | |
| 118 | # add in the sync_attributes that come from the SubscriberRoot object |
| 119 | |
| 120 | if o.volt and o.volt.subscriber and hasattr(o.volt.subscriber, "sync_attributes"): |
| 121 | for attribute_name in o.volt.subscriber.sync_attributes: |
| 122 | fields[attribute_name] = getattr(o.volt.subscriber, attribute_name) |
| 123 | |
| 124 | return fields |
| 125 | |
| 126 | def sync_fields(self, o, fields): |
| 127 | # the super causes the playbook to be run |
Scott Baker | 761e106 | 2016-06-20 17:18:17 -0700 | [diff] [blame] | 128 | super(SyncVSGTenant, self).sync_fields(o, fields) |
| 129 | |
Scott Baker | 761e106 | 2016-06-20 17:18:17 -0700 | [diff] [blame] | 130 | def run_playbook(self, o, fields): |
| 131 | ansible_hash = hashlib.md5(repr(sorted(fields.items()))).hexdigest() |
| 132 | quick_update = (o.last_ansible_hash == ansible_hash) |
| 133 | |
| 134 | if ENABLE_QUICK_UPDATE and quick_update: |
| 135 | logger.info("quick_update triggered; skipping ansible recipe",extra=o.tologdict()) |
| 136 | else: |
| 137 | if o.instance.isolation in ["container", "container_vm"]: |
Scott Baker | ecee9b1 | 2017-03-08 09:56:20 -0800 | [diff] [blame] | 138 | raise Exception("probably not implemented") |
Scott Baker | 761e106 | 2016-06-20 17:18:17 -0700 | [diff] [blame] | 139 | super(SyncVSGTenant, self).run_playbook(o, fields, "sync_vcpetenant_new.yaml") |
| 140 | else: |
Scott Baker | ecee9b1 | 2017-03-08 09:56:20 -0800 | [diff] [blame] | 141 | super(SyncVSGTenant, self).run_playbook(o, fields, template_name="sync_vcpetenant_vtn.yaml") |
Scott Baker | 761e106 | 2016-06-20 17:18:17 -0700 | [diff] [blame] | 142 | |
| 143 | o.last_ansible_hash = ansible_hash |
| 144 | |
Scott Baker | 9674688 | 2017-06-09 14:12:15 -0700 | [diff] [blame] | 145 | def sync_record(self, o): |
| 146 | if (not o.policed) or (o.policed<o.updated): |
| 147 | defer_sync("waiting on model policy") |
| 148 | super(SyncVSGTenant, self).sync_record(o) |
| 149 | |
| 150 | def delete_record(self, o): |
| 151 | if (not o.policed) or (o.policed<o.updated): |
| 152 | defer_sync("waiting on model policy") |
| 153 | # do not call super, as we don't want to re-run the playbook |
Srikanth Vavilapalli | cd9d9bd | 2016-12-03 22:53:42 +0000 | [diff] [blame] | 154 | |
| 155 | def handle_service_monitoringagentinfo_watch_notification(self, monitoring_agent_info): |
| 156 | if not monitoring_agent_info.service: |
| 157 | logger.info("handle watch notifications for service monitoring agent info...ignoring because service attribute in monitoring agent info:%s is null" % (monitoring_agent_info)) |
| 158 | return |
| 159 | |
| 160 | if not monitoring_agent_info.target_uri: |
| 161 | logger.info("handle watch notifications for service monitoring agent info...ignoring because target_uri attribute in monitoring agent info:%s is null" % (monitoring_agent_info)) |
| 162 | return |
| 163 | |
Scott Baker | 32f6512 | 2017-03-08 17:04:16 -0800 | [diff] [blame] | 164 | objs = VSGTenant.objects.all() |
Srikanth Vavilapalli | cd9d9bd | 2016-12-03 22:53:42 +0000 | [diff] [blame] | 165 | for obj in objs: |
Scott Baker | 80238f8 | 2017-07-18 16:01:10 -0700 | [diff] [blame] | 166 | if obj.owner.id != monitoring_agent_info.service.id: |
Srikanth Vavilapalli | cd9d9bd | 2016-12-03 22:53:42 +0000 | [diff] [blame] | 167 | logger.info("handle watch notifications for service monitoring agent info...ignoring because service attribute in monitoring agent info:%s is not matching" % (monitoring_agent_info)) |
| 168 | return |
| 169 | |
| 170 | instance = self.get_instance(obj) |
| 171 | if not instance: |
| 172 | logger.warn("handle watch notifications for service monitoring agent info...: No valid instance found for object %s" % (str(obj))) |
| 173 | return |
| 174 | |
| 175 | logger.info("handling watch notification for monitoring agent info:%s for VSGTenant object:%s" % (monitoring_agent_info, obj)) |
| 176 | |
| 177 | #Run ansible playbook to update the routing table entries in the instance |
| 178 | fields = self.get_ansible_fields(instance) |
| 179 | fields["ansible_tag"] = obj.__class__.__name__ + "_" + str(obj.id) + "_service_monitoring" |
| 180 | |
| 181 | #Parse the monitoring agent target_uri |
| 182 | url = urlparse(monitoring_agent_info.target_uri) |
| 183 | |
| 184 | #Assuming target_uri is rabbitmq URI |
| 185 | fields["rabbit_user"] = url.username |
| 186 | fields["rabbit_password"] = url.password |
| 187 | fields["rabbit_host"] = url.hostname |
| 188 | |
| 189 | template_name = "sync_monitoring_agent.yaml" |
| 190 | super(SyncVSGTenant, self).run_playbook(obj, fields, template_name) |
Scott Baker | 32f6512 | 2017-03-08 17:04:16 -0800 | [diff] [blame] | 191 | |