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 | edfdbca | 2017-02-01 17:33:47 -0800 | [diff] [blame] | 24 | from xos.config import Config |
Andrea Campanella | 08c14ca | 2017-03-31 16:13:09 +0200 | [diff] [blame] | 25 | from synchronizers.new_base.SyncInstanceUsingAnsible import SyncInstanceUsingAnsible |
| 26 | from synchronizers.new_base.modelaccessor import * |
| 27 | from synchronizers.new_base.ansible_helper import run_template_ssh |
Andrea Campanella | edfdbca | 2017-02-01 17:33:47 -0800 | [diff] [blame] | 28 | from xos.logger import Logger, logging |
| 29 | |
Andrea Campanella | edfdbca | 2017-02-01 17:33:47 -0800 | [diff] [blame] | 30 | # hpclibrary will be in steps/.. |
| 31 | parentdir = os.path.join(os.path.dirname(__file__),"..") |
| 32 | sys.path.insert(0,parentdir) |
| 33 | |
Andrea Campanella | edfdbca | 2017-02-01 17:33:47 -0800 | [diff] [blame] | 34 | |
| 35 | logger = Logger(level=logging.INFO) |
| 36 | |
| 37 | ENABLE_QUICK_UPDATE=False |
| 38 | |
Andrea Campanella | edfdbca | 2017-02-01 17:33:47 -0800 | [diff] [blame] | 39 | class SyncVEGTenant(SyncInstanceUsingAnsible): |
| 40 | provides=[VEGTenant] |
| 41 | observes=VEGTenant |
| 42 | requested_interval=0 |
| 43 | template_name = "sync_vegtenant.yaml" |
Andrea Campanella | 08c14ca | 2017-03-31 16:13:09 +0200 | [diff] [blame] | 44 | watches = [ModelLink(ServiceDependency, via='servicedependency'), ModelLink(ServiceMonitoringAgentInfo, via='monitoringagentinfo')] |
Andrea Campanella | edfdbca | 2017-02-01 17:33:47 -0800 | [diff] [blame] | 45 | |
| 46 | def __init__(self, *args, **kwargs): |
| 47 | super(SyncVEGTenant, self).__init__(*args, **kwargs) |
| 48 | |
Andrea Campanella | edfdbca | 2017-02-01 17:33:47 -0800 | [diff] [blame] | 49 | def get_veg_service(self, o): |
| 50 | if not o.provider_service: |
| 51 | return None |
| 52 | |
Andrea Campanella | 08c14ca | 2017-03-31 16:13:09 +0200 | [diff] [blame] | 53 | vegs = VEGService.objects.filter(id=o.provider_service.id) |
Andrea Campanella | edfdbca | 2017-02-01 17:33:47 -0800 | [diff] [blame] | 54 | if not vegs: |
| 55 | return None |
| 56 | |
| 57 | return vegs[0] |
| 58 | |
| 59 | def get_extra_attributes(self, o): |
| 60 | # This is a place to include extra attributes that aren't part of the |
| 61 | # object itself. In the case of vEG, we need to know: |
| 62 | # 1) the addresses of dnsdemux, to setup dnsmasq in the vEG |
| 63 | # 2) CDN prefixes, so we know what URLs to send to dnsdemux |
Andrea Campanella | edfdbca | 2017-02-01 17:33:47 -0800 | [diff] [blame] | 64 | # 4) vlan_ids, for setting up networking in the vEG VM |
| 65 | |
| 66 | veg_service = self.get_veg_service(o) |
| 67 | |
| 68 | dnsdemux_ip = None |
| 69 | cdn_prefixes = [] |
| 70 | #FIXME this will probably break since no folder is under syncronizers |
| 71 | cdn_config_fn = "/opt/xos/synchronizers/veg/cdn_config" |
| 72 | if os.path.exists(cdn_config_fn): |
| 73 | # manual CDN configuration |
| 74 | # the first line is the address of dnsredir |
| 75 | # the remaining lines are domain names, one per line |
| 76 | lines = file(cdn_config_fn).readlines() |
| 77 | if len(lines)>=2: |
| 78 | dnsdemux_ip = lines[0].strip() |
| 79 | cdn_prefixes = [x.strip() for x in lines[1:] if x.strip()] |
Andrea Campanella | edfdbca | 2017-02-01 17:33:47 -0800 | [diff] [blame] | 80 | |
| 81 | dnsdemux_ip = dnsdemux_ip or "none" |
| 82 | |
Andrea Campanella | edfdbca | 2017-02-01 17:33:47 -0800 | [diff] [blame] | 83 | s_tags = [] |
| 84 | c_tags = [] |
| 85 | if o.volt: |
| 86 | s_tags.append(o.volt.s_tag) |
| 87 | c_tags.append(o.volt.c_tag) |
| 88 | |
| 89 | try: |
| 90 | full_setup = Config().observer_full_setup |
| 91 | except: |
| 92 | full_setup = True |
| 93 | |
| 94 | safe_macs=[] |
| 95 | if veg_service.url_filter_kind == "safebrowsing": |
| 96 | if o.volt and o.volt.subscriber: |
| 97 | for user in o.volt.subscriber.devices: |
| 98 | level = user.get("level",None) |
| 99 | mac = user.get("mac",None) |
| 100 | if level in ["G", "PG"]: |
| 101 | if mac: |
| 102 | safe_macs.append(mac) |
| 103 | |
| 104 | |
| 105 | docker_opts = [] |
| 106 | if veg_service.docker_insecure_registry: |
| 107 | reg_name = veg_service.docker_image_name.split("/",1)[0] |
| 108 | docker_opts.append("--insecure-registry " + reg_name) |
| 109 | |
| 110 | fields = {"s_tags": s_tags, |
| 111 | "c_tags": c_tags, |
| 112 | "docker_remote_image_name": veg_service.docker_image_name, |
Andrea Campanella | 08c14ca | 2017-03-31 16:13:09 +0200 | [diff] [blame] | 113 | "docker_local_image_name": veg_service.docker_image_name, |
Andrea Campanella | edfdbca | 2017-02-01 17:33:47 -0800 | [diff] [blame] | 114 | "docker_opts": " ".join(docker_opts), |
| 115 | "dnsdemux_ip": dnsdemux_ip, |
| 116 | "cdn_prefixes": cdn_prefixes, |
Andrea Campanella | edfdbca | 2017-02-01 17:33:47 -0800 | [diff] [blame] | 117 | "full_setup": full_setup, |
| 118 | "isolation": o.instance.isolation, |
| 119 | "safe_browsing_macs": safe_macs, |
| 120 | "container_name": "veg-%s-%s" % (s_tags[0], c_tags[0]), |
| 121 | "dns_servers": [x.strip() for x in veg_service.dns_servers.split(",")], |
| 122 | "url_filter_kind": veg_service.url_filter_kind } |
| 123 | |
| 124 | # add in the sync_attributes that come from the SubscriberRoot object |
| 125 | |
| 126 | if o.volt and o.volt.subscriber and hasattr(o.volt.subscriber, "sync_attributes"): |
| 127 | for attribute_name in o.volt.subscriber.sync_attributes: |
| 128 | fields[attribute_name] = getattr(o.volt.subscriber, attribute_name) |
| 129 | |
| 130 | return fields |
| 131 | |
| 132 | def sync_fields(self, o, fields): |
| 133 | # the super causes the playbook to be run |
| 134 | |
| 135 | super(SyncVEGTenant, self).sync_fields(o, fields) |
| 136 | |
Andrea Campanella | edfdbca | 2017-02-01 17:33:47 -0800 | [diff] [blame] | 137 | def run_playbook(self, o, fields): |
| 138 | ansible_hash = hashlib.md5(repr(sorted(fields.items()))).hexdigest() |
| 139 | quick_update = (o.last_ansible_hash == ansible_hash) |
| 140 | |
| 141 | if ENABLE_QUICK_UPDATE and quick_update: |
| 142 | logger.info("quick_update triggered; skipping ansible recipe",extra=o.tologdict()) |
| 143 | else: |
| 144 | if o.instance.isolation in ["container", "container_vm"]: |
Andrea Campanella | 08c14ca | 2017-03-31 16:13:09 +0200 | [diff] [blame] | 145 | raise Exception("probably not implemented") |
Andrea Campanella | edfdbca | 2017-02-01 17:33:47 -0800 | [diff] [blame] | 146 | super(SyncVEGTenant, self).run_playbook(o, fields, "sync_vegtenant_new.yaml") |
| 147 | else: |
Andrea Campanella | 08c14ca | 2017-03-31 16:13:09 +0200 | [diff] [blame] | 148 | 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] | 149 | |
| 150 | o.last_ansible_hash = ansible_hash |
| 151 | |
| 152 | def delete_record(self, m): |
| 153 | pass |
| 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 | |
| 164 | objs = VEGTenant.get_tenant_objects().all() |
| 165 | for obj in objs: |
| 166 | if obj.provider_service.id != monitoring_agent_info.service.id: |
| 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 VEGTenant 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(SyncVEGTenant, self).run_playbook(obj, fields, template_name) |