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