blob: 5c45cf3efe9869992d5aed0defd110946c04143c [file] [log] [blame]
Scott Baker761e1062016-06-20 17:18:17 -07001import hashlib
2import os
3import socket
4import sys
5import base64
6import time
Srikanth Vavilapallicd9d9bd2016-12-03 22:53:42 +00007from urlparse import urlparse
Scott Baker32f65122017-03-08 17:04:16 -08008from synchronizers.new_base.SyncInstanceUsingAnsible import SyncInstanceUsingAnsible
9from synchronizers.new_base.modelaccessor import *
10from synchronizers.new_base.ansible_helper import run_template_ssh
Scott Baker761e1062016-06-20 17:18:17 -070011from xos.logger import Logger, logging
12
13# hpclibrary will be in steps/..
14parentdir = os.path.join(os.path.dirname(__file__),"..")
15sys.path.insert(0,parentdir)
16
Scott Baker761e1062016-06-20 17:18:17 -070017logger = Logger(level=logging.INFO)
18
19ENABLE_QUICK_UPDATE=False
20
Scott Baker761e1062016-06-20 17:18:17 -070021class SyncVSGTenant(SyncInstanceUsingAnsible):
22 provides=[VSGTenant]
23 observes=VSGTenant
24 requested_interval=0
25 template_name = "sync_vcpetenant.yaml"
Scott Baker3f8a3902017-03-20 11:04:32 -070026 watches = [ModelLink(ServiceDependency,via='servicedependency'), ModelLink(ServiceMonitoringAgentInfo,via='monitoringagentinfo')]
Scott Baker761e1062016-06-20 17:18:17 -070027
28 def __init__(self, *args, **kwargs):
29 super(SyncVSGTenant, self).__init__(*args, **kwargs)
30
Scott Baker761e1062016-06-20 17:18:17 -070031 def get_vcpe_service(self, o):
32 if not o.provider_service:
33 return None
34
Scott Baker32f65122017-03-08 17:04:16 -080035 vcpes = VSGService.objects.filter(id=o.provider_service.id)
Scott Baker761e1062016-06-20 17:18:17 -070036 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 Baker761e1062016-06-20 17:18:17 -070046 # 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 Baker761e1062016-06-20 17:18:17 -070062
63 dnsdemux_ip = dnsdemux_ip or "none"
64
Scott Baker761e1062016-06-20 17:18:17 -070065 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 Scandolo79f298e2017-06-05 11:18:01 -070071 full_setup = True
Scott Baker761e1062016-06-20 17:18:17 -070072
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 Baker8e66d662016-10-13 13:22:49 -070083 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 Baker761e1062016-06-20 17:18:17 -070088 fields = {"s_tags": s_tags,
89 "c_tags": c_tags,
Scott Baker8e66d662016-10-13 13:22:49 -070090 "docker_remote_image_name": vcpe_service.docker_image_name,
Scott Baker32f65122017-03-08 17:04:16 -080091 "docker_local_image_name": vcpe_service.docker_image_name,
Scott Baker8e66d662016-10-13 13:22:49 -070092 "docker_opts": " ".join(docker_opts),
Scott Baker761e1062016-06-20 17:18:17 -070093 "dnsdemux_ip": dnsdemux_ip,
94 "cdn_prefixes": cdn_prefixes,
Scott Baker761e1062016-06-20 17:18:17 -070095 "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 Baker761e1062016-06-20 17:18:17 -0700112 super(SyncVSGTenant, self).sync_fields(o, fields)
113
Scott Baker761e1062016-06-20 17:18:17 -0700114 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 Bakerecee9b12017-03-08 09:56:20 -0800122 raise Exception("probably not implemented")
Scott Baker761e1062016-06-20 17:18:17 -0700123 super(SyncVSGTenant, self).run_playbook(o, fields, "sync_vcpetenant_new.yaml")
124 else:
Scott Bakerecee9b12017-03-08 09:56:20 -0800125 super(SyncVSGTenant, self).run_playbook(o, fields, template_name="sync_vcpetenant_vtn.yaml")
Scott Baker761e1062016-06-20 17:18:17 -0700126
127 o.last_ansible_hash = ansible_hash
128
129 def delete_record(self, m):
130 pass
Srikanth Vavilapallicd9d9bd2016-12-03 22:53:42 +0000131
132 def handle_service_monitoringagentinfo_watch_notification(self, monitoring_agent_info):
133 if not monitoring_agent_info.service:
134 logger.info("handle watch notifications for service monitoring agent info...ignoring because service attribute in monitoring agent info:%s is null" % (monitoring_agent_info))
135 return
136
137 if not monitoring_agent_info.target_uri:
138 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))
139 return
140
Scott Baker32f65122017-03-08 17:04:16 -0800141 objs = VSGTenant.objects.all()
Srikanth Vavilapallicd9d9bd2016-12-03 22:53:42 +0000142 for obj in objs:
143 if obj.provider_service.id != monitoring_agent_info.service.id:
144 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))
145 return
146
147 instance = self.get_instance(obj)
148 if not instance:
149 logger.warn("handle watch notifications for service monitoring agent info...: No valid instance found for object %s" % (str(obj)))
150 return
151
152 logger.info("handling watch notification for monitoring agent info:%s for VSGTenant object:%s" % (monitoring_agent_info, obj))
153
154 #Run ansible playbook to update the routing table entries in the instance
155 fields = self.get_ansible_fields(instance)
156 fields["ansible_tag"] = obj.__class__.__name__ + "_" + str(obj.id) + "_service_monitoring"
157
158 #Parse the monitoring agent target_uri
159 url = urlparse(monitoring_agent_info.target_uri)
160
161 #Assuming target_uri is rabbitmq URI
162 fields["rabbit_user"] = url.username
163 fields["rabbit_password"] = url.password
164 fields["rabbit_host"] = url.hostname
165
166 template_name = "sync_monitoring_agent.yaml"
167 super(SyncVSGTenant, self).run_playbook(obj, fields, template_name)
Scott Baker32f65122017-03-08 17:04:16 -0800168