Matteo Scandolo | 6288d5a | 2017-08-08 13:05:26 -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 | |
Andrea Campanella | edfdbca | 2017-02-01 17:33:47 -0800 | [diff] [blame] | 17 | import hashlib |
| 18 | import os |
| 19 | import socket |
| 20 | import sys |
| 21 | import base64 |
| 22 | import time |
| 23 | from urlparse import urlparse |
Andrea Campanella | 08c14ca | 2017-03-31 16:13:09 +0200 | [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 |
Andrea Campanella | edfdbca | 2017-02-01 17:33:47 -0800 | [diff] [blame] | 27 | from xos.logger import Logger, logging |
| 28 | |
Andrea Campanella | edfdbca | 2017-02-01 17:33:47 -0800 | [diff] [blame] | 29 | # hpclibrary will be in steps/.. |
| 30 | parentdir = os.path.join(os.path.dirname(__file__),"..") |
| 31 | sys.path.insert(0,parentdir) |
| 32 | |
Andrea Campanella | edfdbca | 2017-02-01 17:33:47 -0800 | [diff] [blame] | 33 | |
| 34 | logger = Logger(level=logging.INFO) |
| 35 | |
| 36 | ENABLE_QUICK_UPDATE=False |
| 37 | |
Andrea Campanella | edfdbca | 2017-02-01 17:33:47 -0800 | [diff] [blame] | 38 | class SyncVEGTenant(SyncInstanceUsingAnsible): |
| 39 | provides=[VEGTenant] |
| 40 | observes=VEGTenant |
| 41 | requested_interval=0 |
| 42 | template_name = "sync_vegtenant.yaml" |
Andrea Campanella | 08c14ca | 2017-03-31 16:13:09 +0200 | [diff] [blame] | 43 | watches = [ModelLink(ServiceDependency, via='servicedependency'), ModelLink(ServiceMonitoringAgentInfo, via='monitoringagentinfo')] |
Andrea Campanella | edfdbca | 2017-02-01 17:33:47 -0800 | [diff] [blame] | 44 | |
| 45 | def __init__(self, *args, **kwargs): |
| 46 | super(SyncVEGTenant, self).__init__(*args, **kwargs) |
| 47 | |
Andrea Campanella | edfdbca | 2017-02-01 17:33:47 -0800 | [diff] [blame] | 48 | def get_veg_service(self, o): |
Andrea Campanella | 2a2df42 | 2017-08-30 16:59:17 +0200 | [diff] [blame] | 49 | if not o.owner: |
Andrea Campanella | edfdbca | 2017-02-01 17:33:47 -0800 | [diff] [blame] | 50 | return None |
| 51 | |
Scott Baker | 3b8ceca | 2017-10-12 11:38:50 -0700 | [diff] [blame] | 52 | vegs = VEGService.objects.filter(id=o.owner.id) |
Andrea Campanella | edfdbca | 2017-02-01 17:33:47 -0800 | [diff] [blame] | 53 | if not vegs: |
| 54 | return None |
| 55 | |
| 56 | return vegs[0] |
| 57 | |
| 58 | def get_extra_attributes(self, o): |
| 59 | # This is a place to include extra attributes that aren't part of the |
| 60 | # object itself. In the case of vEG, we need to know: |
| 61 | # 1) the addresses of dnsdemux, to setup dnsmasq in the vEG |
| 62 | # 2) CDN prefixes, so we know what URLs to send to dnsdemux |
Andrea Campanella | edfdbca | 2017-02-01 17:33:47 -0800 | [diff] [blame] | 63 | # 4) vlan_ids, for setting up networking in the vEG VM |
| 64 | |
| 65 | veg_service = self.get_veg_service(o) |
| 66 | |
| 67 | dnsdemux_ip = None |
| 68 | cdn_prefixes = [] |
| 69 | #FIXME this will probably break since no folder is under syncronizers |
| 70 | cdn_config_fn = "/opt/xos/synchronizers/veg/cdn_config" |
| 71 | if os.path.exists(cdn_config_fn): |
| 72 | # manual CDN configuration |
| 73 | # the first line is the address of dnsredir |
| 74 | # the remaining lines are domain names, one per line |
| 75 | lines = file(cdn_config_fn).readlines() |
| 76 | if len(lines)>=2: |
| 77 | dnsdemux_ip = lines[0].strip() |
| 78 | cdn_prefixes = [x.strip() for x in lines[1:] if x.strip()] |
Andrea Campanella | edfdbca | 2017-02-01 17:33:47 -0800 | [diff] [blame] | 79 | |
| 80 | dnsdemux_ip = dnsdemux_ip or "none" |
| 81 | |
Andrea Campanella | edfdbca | 2017-02-01 17:33:47 -0800 | [diff] [blame] | 82 | s_tags = [] |
| 83 | c_tags = [] |
| 84 | if o.volt: |
| 85 | s_tags.append(o.volt.s_tag) |
| 86 | c_tags.append(o.volt.c_tag) |
| 87 | |
Andrea Campanella | 2a2df42 | 2017-08-30 16:59:17 +0200 | [diff] [blame] | 88 | full_setup = True |
Andrea Campanella | edfdbca | 2017-02-01 17:33:47 -0800 | [diff] [blame] | 89 | |
| 90 | safe_macs=[] |
| 91 | if veg_service.url_filter_kind == "safebrowsing": |
Scott Baker | 3b8ceca | 2017-10-12 11:38:50 -0700 | [diff] [blame] | 92 | if o.volt and o.volt.subscriber and hasattr(o.volt.subscriber, "devices"): |
Andrea Campanella | edfdbca | 2017-02-01 17:33:47 -0800 | [diff] [blame] | 93 | for user in o.volt.subscriber.devices: |
| 94 | level = user.get("level",None) |
| 95 | mac = user.get("mac",None) |
| 96 | if level in ["G", "PG"]: |
| 97 | if mac: |
| 98 | safe_macs.append(mac) |
| 99 | |
| 100 | |
| 101 | docker_opts = [] |
| 102 | if veg_service.docker_insecure_registry: |
| 103 | reg_name = veg_service.docker_image_name.split("/",1)[0] |
| 104 | docker_opts.append("--insecure-registry " + reg_name) |
| 105 | |
| 106 | fields = {"s_tags": s_tags, |
| 107 | "c_tags": c_tags, |
| 108 | "docker_remote_image_name": veg_service.docker_image_name, |
Andrea Campanella | 08c14ca | 2017-03-31 16:13:09 +0200 | [diff] [blame] | 109 | "docker_local_image_name": veg_service.docker_image_name, |
Andrea Campanella | edfdbca | 2017-02-01 17:33:47 -0800 | [diff] [blame] | 110 | "docker_opts": " ".join(docker_opts), |
| 111 | "dnsdemux_ip": dnsdemux_ip, |
| 112 | "cdn_prefixes": cdn_prefixes, |
Andrea Campanella | edfdbca | 2017-02-01 17:33:47 -0800 | [diff] [blame] | 113 | "full_setup": full_setup, |
| 114 | "isolation": o.instance.isolation, |
| 115 | "safe_browsing_macs": safe_macs, |
| 116 | "container_name": "veg-%s-%s" % (s_tags[0], c_tags[0]), |
| 117 | "dns_servers": [x.strip() for x in veg_service.dns_servers.split(",")], |
| 118 | "url_filter_kind": veg_service.url_filter_kind } |
| 119 | |
Scott Baker | 3b8ceca | 2017-10-12 11:38:50 -0700 | [diff] [blame] | 120 | # Some subscriber models may not implement all fields that we look for, so specify some defaults. |
| 121 | fields["firewall_rules"] = "" |
| 122 | fields["firewall_enable"] = False |
| 123 | fields["url_filter_enable"] = False |
| 124 | fields["url_filter_level"] = "PG" |
| 125 | fields["cdn_enable"] = False |
| 126 | fields["uplink_speed"] = 1000000000 |
| 127 | fields["downlink_speed"] = 1000000000 |
| 128 | fields["enable_uverse"] = True |
| 129 | fields["status"] = "enabled" |
Andrea Campanella | edfdbca | 2017-02-01 17:33:47 -0800 | [diff] [blame] | 130 | |
Scott Baker | 3b8ceca | 2017-10-12 11:38:50 -0700 | [diff] [blame] | 131 | # add in the sync_attributes that come from the SubscriberRoot object |
Andrea Campanella | edfdbca | 2017-02-01 17:33:47 -0800 | [diff] [blame] | 132 | if o.volt and o.volt.subscriber and hasattr(o.volt.subscriber, "sync_attributes"): |
| 133 | for attribute_name in o.volt.subscriber.sync_attributes: |
| 134 | fields[attribute_name] = getattr(o.volt.subscriber, attribute_name) |
| 135 | |
| 136 | return fields |
| 137 | |
| 138 | def sync_fields(self, o, fields): |
| 139 | # the super causes the playbook to be run |
| 140 | |
| 141 | super(SyncVEGTenant, self).sync_fields(o, fields) |
| 142 | |
Andrea Campanella | edfdbca | 2017-02-01 17:33:47 -0800 | [diff] [blame] | 143 | def run_playbook(self, o, fields): |
| 144 | ansible_hash = hashlib.md5(repr(sorted(fields.items()))).hexdigest() |
| 145 | quick_update = (o.last_ansible_hash == ansible_hash) |
| 146 | |
| 147 | if ENABLE_QUICK_UPDATE and quick_update: |
| 148 | logger.info("quick_update triggered; skipping ansible recipe",extra=o.tologdict()) |
| 149 | else: |
| 150 | if o.instance.isolation in ["container", "container_vm"]: |
Andrea Campanella | 08c14ca | 2017-03-31 16:13:09 +0200 | [diff] [blame] | 151 | raise Exception("probably not implemented") |
Andrea Campanella | edfdbca | 2017-02-01 17:33:47 -0800 | [diff] [blame] | 152 | super(SyncVEGTenant, self).run_playbook(o, fields, "sync_vegtenant_new.yaml") |
| 153 | else: |
Andrea Campanella | 08c14ca | 2017-03-31 16:13:09 +0200 | [diff] [blame] | 154 | super(SyncVEGTenant, self).run_playbook(o, fields, template_name="sync_vegtenant_vtn.yaml") |
Andrea Campanella | edfdbca | 2017-02-01 17:33:47 -0800 | [diff] [blame] | 155 | |
| 156 | o.last_ansible_hash = ansible_hash |
| 157 | |
Andrea Campanella | 2a2df42 | 2017-08-30 16:59:17 +0200 | [diff] [blame] | 158 | def sync_record(self, o): |
| 159 | if (not o.policed) or (o.policed<o.updated): |
| 160 | defer_sync("waiting on model policy") |
| 161 | super(SyncVEGTenant, self).sync_record(o) |
| 162 | |
| 163 | def delete_record(self, o): |
| 164 | if (not o.policed) or (o.policed<o.updated): |
| 165 | defer_sync("waiting on model policy") |
| 166 | # do not call super, as we don't want to re-run the playbook |
Andrea Campanella | edfdbca | 2017-02-01 17:33:47 -0800 | [diff] [blame] | 167 | |
| 168 | def handle_service_monitoringagentinfo_watch_notification(self, monitoring_agent_info): |
| 169 | if not monitoring_agent_info.service: |
| 170 | logger.info("handle watch notifications for service monitoring agent info...ignoring because service attribute in monitoring agent info:%s is null" % (monitoring_agent_info)) |
| 171 | return |
| 172 | |
| 173 | if not monitoring_agent_info.target_uri: |
| 174 | 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)) |
| 175 | return |
| 176 | |
| 177 | objs = VEGTenant.get_tenant_objects().all() |
| 178 | for obj in objs: |
Andrea Campanella | 2a2df42 | 2017-08-30 16:59:17 +0200 | [diff] [blame] | 179 | if obj.owner.id != monitoring_agent_info.service.id: |
Andrea Campanella | edfdbca | 2017-02-01 17:33:47 -0800 | [diff] [blame] | 180 | 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)) |
| 181 | return |
| 182 | |
| 183 | instance = self.get_instance(obj) |
| 184 | if not instance: |
| 185 | logger.warn("handle watch notifications for service monitoring agent info...: No valid instance found for object %s" % (str(obj))) |
| 186 | return |
| 187 | |
| 188 | logger.info("handling watch notification for monitoring agent info:%s for VEGTenant object:%s" % (monitoring_agent_info, obj)) |
| 189 | |
| 190 | #Run ansible playbook to update the routing table entries in the instance |
| 191 | fields = self.get_ansible_fields(instance) |
| 192 | fields["ansible_tag"] = obj.__class__.__name__ + "_" + str(obj.id) + "_service_monitoring" |
| 193 | |
| 194 | #Parse the monitoring agent target_uri |
| 195 | url = urlparse(monitoring_agent_info.target_uri) |
| 196 | |
| 197 | #Assuming target_uri is rabbitmq URI |
| 198 | fields["rabbit_user"] = url.username |
| 199 | fields["rabbit_password"] = url.password |
| 200 | fields["rabbit_host"] = url.hostname |
| 201 | |
| 202 | template_name = "sync_monitoring_agent.yaml" |
| 203 | super(SyncVEGTenant, self).run_playbook(obj, fields, template_name) |