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 | 645c0c5 | 2017-09-15 10:38:32 -0700 | [diff] [blame] | 37 | class SyncVSGServiceInstance(SyncInstanceUsingAnsible): |
| 38 | provides=[VSGServiceInstance] |
| 39 | observes=VSGServiceInstance |
Scott Baker | 761e106 | 2016-06-20 17:18:17 -0700 | [diff] [blame] | 40 | requested_interval=0 |
Scott Baker | 645c0c5 | 2017-09-15 10:38:32 -0700 | [diff] [blame] | 41 | template_name = "sync_vsgserviceinstance.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): |
Scott Baker | 645c0c5 | 2017-09-15 10:38:32 -0700 | [diff] [blame] | 45 | super(SyncVSGServiceInstance, self).__init__(*args, **kwargs) |
Scott Baker | 761e106 | 2016-06-20 17:18:17 -0700 | [diff] [blame] | 46 | |
Scott Baker | 645c0c5 | 2017-09-15 10:38:32 -0700 | [diff] [blame] | 47 | def get_vsg_service(self, o): |
Scott Baker | e27de6a | 2017-11-28 17:10:08 -0800 | [diff] [blame] | 48 | return o.owner.leaf_model |
Scott Baker | 761e106 | 2016-06-20 17:18:17 -0700 | [diff] [blame] | 49 | |
| 50 | def get_extra_attributes(self, o): |
| 51 | # This is a place to include extra attributes that aren't part of the |
Scott Baker | 645c0c5 | 2017-09-15 10:38:32 -0700 | [diff] [blame] | 52 | # object itself. In the case of vSG, we need to know: |
| 53 | # 1) the addresses of dnsdemux, to setup dnsmasq in the vSG |
Scott Baker | 761e106 | 2016-06-20 17:18:17 -0700 | [diff] [blame] | 54 | # 2) CDN prefixes, so we know what URLs to send to dnsdemux |
Scott Baker | 645c0c5 | 2017-09-15 10:38:32 -0700 | [diff] [blame] | 55 | # 4) vlan_ids, for setting up networking in the vSG VM |
Scott Baker | 761e106 | 2016-06-20 17:18:17 -0700 | [diff] [blame] | 56 | |
Scott Baker | 645c0c5 | 2017-09-15 10:38:32 -0700 | [diff] [blame] | 57 | vsg_service = self.get_vsg_service(o) |
Scott Baker | 761e106 | 2016-06-20 17:18:17 -0700 | [diff] [blame] | 58 | |
| 59 | dnsdemux_ip = None |
| 60 | cdn_prefixes = [] |
| 61 | |
| 62 | cdn_config_fn = "/opt/xos/synchronizers/vsg/cdn_config" |
| 63 | if os.path.exists(cdn_config_fn): |
| 64 | # manual CDN configuration |
| 65 | # the first line is the address of dnsredir |
| 66 | # the remaining lines are domain names, one per line |
| 67 | lines = file(cdn_config_fn).readlines() |
| 68 | if len(lines)>=2: |
| 69 | dnsdemux_ip = lines[0].strip() |
| 70 | cdn_prefixes = [x.strip() for x in lines[1:] if x.strip()] |
Scott Baker | 761e106 | 2016-06-20 17:18:17 -0700 | [diff] [blame] | 71 | |
| 72 | dnsdemux_ip = dnsdemux_ip or "none" |
| 73 | |
Scott Baker | 761e106 | 2016-06-20 17:18:17 -0700 | [diff] [blame] | 74 | s_tags = [] |
| 75 | c_tags = [] |
| 76 | if o.volt: |
| 77 | s_tags.append(o.volt.s_tag) |
| 78 | c_tags.append(o.volt.c_tag) |
| 79 | |
Matteo Scandolo | 79f298e | 2017-06-05 11:18:01 -0700 | [diff] [blame] | 80 | full_setup = True |
Scott Baker | 761e106 | 2016-06-20 17:18:17 -0700 | [diff] [blame] | 81 | |
| 82 | safe_macs=[] |
Scott Baker | 645c0c5 | 2017-09-15 10:38:32 -0700 | [diff] [blame] | 83 | if vsg_service.url_filter_kind == "safebrowsing": |
Scott Baker | 761e106 | 2016-06-20 17:18:17 -0700 | [diff] [blame] | 84 | if o.volt and o.volt.subscriber: |
Scott Baker | 62829b0 | 2017-10-23 11:16:49 -0700 | [diff] [blame] | 85 | for user in o.volt.subscriber.devices and hasattr(o.volt.subscriber, "devices"): |
Scott Baker | 761e106 | 2016-06-20 17:18:17 -0700 | [diff] [blame] | 86 | level = user.get("level",None) |
| 87 | mac = user.get("mac",None) |
| 88 | if level in ["G", "PG"]: |
| 89 | if mac: |
| 90 | safe_macs.append(mac) |
| 91 | |
Scott Baker | 8e66d66 | 2016-10-13 13:22:49 -0700 | [diff] [blame] | 92 | docker_opts = [] |
Scott Baker | 645c0c5 | 2017-09-15 10:38:32 -0700 | [diff] [blame] | 93 | if vsg_service.docker_insecure_registry: |
| 94 | reg_name = vsg_service.docker_image_name.split("/",1)[0] |
Scott Baker | 8e66d66 | 2016-10-13 13:22:49 -0700 | [diff] [blame] | 95 | docker_opts.append("--insecure-registry " + reg_name) |
| 96 | |
Scott Baker | 761e106 | 2016-06-20 17:18:17 -0700 | [diff] [blame] | 97 | fields = {"s_tags": s_tags, |
| 98 | "c_tags": c_tags, |
Scott Baker | 645c0c5 | 2017-09-15 10:38:32 -0700 | [diff] [blame] | 99 | "docker_remote_image_name": vsg_service.docker_image_name, |
| 100 | "docker_local_image_name": vsg_service.docker_image_name, |
Scott Baker | 8e66d66 | 2016-10-13 13:22:49 -0700 | [diff] [blame] | 101 | "docker_opts": " ".join(docker_opts), |
Scott Baker | 761e106 | 2016-06-20 17:18:17 -0700 | [diff] [blame] | 102 | "dnsdemux_ip": dnsdemux_ip, |
| 103 | "cdn_prefixes": cdn_prefixes, |
Scott Baker | 761e106 | 2016-06-20 17:18:17 -0700 | [diff] [blame] | 104 | "full_setup": full_setup, |
| 105 | "isolation": o.instance.isolation, |
| 106 | "safe_browsing_macs": safe_macs, |
Scott Baker | 645c0c5 | 2017-09-15 10:38:32 -0700 | [diff] [blame] | 107 | "container_name": "vsg-%s-%s" % (s_tags[0], c_tags[0]), |
| 108 | "dns_servers": [x.strip() for x in vsg_service.dns_servers.split(",")], |
| 109 | "url_filter_kind": vsg_service.url_filter_kind } |
Scott Baker | 761e106 | 2016-06-20 17:18:17 -0700 | [diff] [blame] | 110 | |
Scott Baker | 62829b0 | 2017-10-23 11:16:49 -0700 | [diff] [blame] | 111 | # Some subscriber models may not implement all fields that we look for, so specify some defaults. |
| 112 | fields["firewall_rules"] = "" |
| 113 | fields["firewall_enable"] = False |
| 114 | fields["url_filter_enable"] = False |
| 115 | fields["url_filter_level"] = "PG" |
| 116 | fields["cdn_enable"] = False |
| 117 | fields["uplink_speed"] = 1000000000 |
| 118 | fields["downlink_speed"] = 1000000000 |
| 119 | fields["enable_uverse"] = True |
| 120 | fields["status"] = "enabled" |
Scott Baker | 761e106 | 2016-06-20 17:18:17 -0700 | [diff] [blame] | 121 | |
Scott Baker | 62829b0 | 2017-10-23 11:16:49 -0700 | [diff] [blame] | 122 | # add in the sync_attributes that come from the subscriber object |
Scott Baker | 761e106 | 2016-06-20 17:18:17 -0700 | [diff] [blame] | 123 | if o.volt and o.volt.subscriber and hasattr(o.volt.subscriber, "sync_attributes"): |
| 124 | for attribute_name in o.volt.subscriber.sync_attributes: |
| 125 | fields[attribute_name] = getattr(o.volt.subscriber, attribute_name) |
| 126 | |
| 127 | return fields |
| 128 | |
| 129 | def sync_fields(self, o, fields): |
| 130 | # the super causes the playbook to be run |
Scott Baker | 645c0c5 | 2017-09-15 10:38:32 -0700 | [diff] [blame] | 131 | super(SyncVSGServiceInstance, self).sync_fields(o, fields) |
Scott Baker | 761e106 | 2016-06-20 17:18:17 -0700 | [diff] [blame] | 132 | |
Scott Baker | 761e106 | 2016-06-20 17:18:17 -0700 | [diff] [blame] | 133 | def run_playbook(self, o, fields): |
| 134 | ansible_hash = hashlib.md5(repr(sorted(fields.items()))).hexdigest() |
| 135 | quick_update = (o.last_ansible_hash == ansible_hash) |
| 136 | |
| 137 | if ENABLE_QUICK_UPDATE and quick_update: |
| 138 | logger.info("quick_update triggered; skipping ansible recipe",extra=o.tologdict()) |
| 139 | else: |
| 140 | if o.instance.isolation in ["container", "container_vm"]: |
Scott Baker | 645c0c5 | 2017-09-15 10:38:32 -0700 | [diff] [blame] | 141 | raise Exception("Not implemented") |
Scott Baker | 761e106 | 2016-06-20 17:18:17 -0700 | [diff] [blame] | 142 | else: |
Scott Baker | 645c0c5 | 2017-09-15 10:38:32 -0700 | [diff] [blame] | 143 | super(SyncVSGServiceInstance, self).run_playbook(o, fields) |
Scott Baker | 761e106 | 2016-06-20 17:18:17 -0700 | [diff] [blame] | 144 | |
| 145 | o.last_ansible_hash = ansible_hash |
| 146 | |
Scott Baker | 9674688 | 2017-06-09 14:12:15 -0700 | [diff] [blame] | 147 | def sync_record(self, o): |
| 148 | if (not o.policed) or (o.policed<o.updated): |
Scott Baker | a3e7b58 | 2017-08-30 08:36:52 -0700 | [diff] [blame] | 149 | self.defer_sync(o, "waiting on model policy") |
Scott Baker | 645c0c5 | 2017-09-15 10:38:32 -0700 | [diff] [blame] | 150 | super(SyncVSGServiceInstance, self).sync_record(o) |
Scott Baker | 9674688 | 2017-06-09 14:12:15 -0700 | [diff] [blame] | 151 | |
| 152 | def delete_record(self, o): |
| 153 | if (not o.policed) or (o.policed<o.updated): |
Scott Baker | a3e7b58 | 2017-08-30 08:36:52 -0700 | [diff] [blame] | 154 | self.defer_sync(o, "waiting on model policy") |
Scott Baker | 9674688 | 2017-06-09 14:12:15 -0700 | [diff] [blame] | 155 | # 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] | 156 | |
| 157 | def handle_service_monitoringagentinfo_watch_notification(self, monitoring_agent_info): |
| 158 | if not monitoring_agent_info.service: |
| 159 | logger.info("handle watch notifications for service monitoring agent info...ignoring because service attribute in monitoring agent info:%s is null" % (monitoring_agent_info)) |
| 160 | return |
| 161 | |
| 162 | if not monitoring_agent_info.target_uri: |
| 163 | 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)) |
| 164 | return |
| 165 | |
Scott Baker | 645c0c5 | 2017-09-15 10:38:32 -0700 | [diff] [blame] | 166 | objs = VSGServiceInstance.objects.all() |
Srikanth Vavilapalli | cd9d9bd | 2016-12-03 22:53:42 +0000 | [diff] [blame] | 167 | for obj in objs: |
Scott Baker | 80238f8 | 2017-07-18 16:01:10 -0700 | [diff] [blame] | 168 | if obj.owner.id != monitoring_agent_info.service.id: |
Srikanth Vavilapalli | cd9d9bd | 2016-12-03 22:53:42 +0000 | [diff] [blame] | 169 | 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)) |
| 170 | return |
| 171 | |
| 172 | instance = self.get_instance(obj) |
| 173 | if not instance: |
| 174 | logger.warn("handle watch notifications for service monitoring agent info...: No valid instance found for object %s" % (str(obj))) |
| 175 | return |
| 176 | |
Scott Baker | 645c0c5 | 2017-09-15 10:38:32 -0700 | [diff] [blame] | 177 | logger.info("handling watch notification for monitoring agent info:%s for VSGServiceInstance object:%s" % (monitoring_agent_info, obj)) |
Srikanth Vavilapalli | cd9d9bd | 2016-12-03 22:53:42 +0000 | [diff] [blame] | 178 | |
| 179 | #Run ansible playbook to update the routing table entries in the instance |
| 180 | fields = self.get_ansible_fields(instance) |
| 181 | fields["ansible_tag"] = obj.__class__.__name__ + "_" + str(obj.id) + "_service_monitoring" |
| 182 | |
| 183 | #Parse the monitoring agent target_uri |
| 184 | url = urlparse(monitoring_agent_info.target_uri) |
| 185 | |
| 186 | #Assuming target_uri is rabbitmq URI |
| 187 | fields["rabbit_user"] = url.username |
| 188 | fields["rabbit_password"] = url.password |
| 189 | fields["rabbit_host"] = url.hostname |
| 190 | |
| 191 | template_name = "sync_monitoring_agent.yaml" |
Scott Baker | 645c0c5 | 2017-09-15 10:38:32 -0700 | [diff] [blame] | 192 | super(SyncVSGServiceInstance, self).run_playbook(obj, fields, template_name) |
Scott Baker | 32f6512 | 2017-03-08 17:04:16 -0800 | [diff] [blame] | 193 | |