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