blob: 79aca2ca22232bb1a15e11a413506d135f53e54e [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):
Scott Baker80238f82017-07-18 16:01:10 -070032 if not o.owner:
Scott Baker761e1062016-06-20 17:18:17 -070033 return None
34
Scott Baker80238f82017-07-18 16:01:10 -070035 vcpes = VSGService.objects.filter(id=o.owner.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
Scott Baker96746882017-06-09 14:12:15 -0700129 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 Vavilapallicd9d9bd2016-12-03 22:53:42 +0000138
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 Baker32f65122017-03-08 17:04:16 -0800148 objs = VSGTenant.objects.all()
Srikanth Vavilapallicd9d9bd2016-12-03 22:53:42 +0000149 for obj in objs:
Scott Baker80238f82017-07-18 16:01:10 -0700150 if obj.owner.id != monitoring_agent_info.service.id:
Srikanth Vavilapallicd9d9bd2016-12-03 22:53:42 +0000151 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 Baker32f65122017-03-08 17:04:16 -0800175