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