blob: 056543dbc059ab3574e2522793ac0ca814ec2614 [file] [log] [blame]
Matteo Scandolo6288d5a2017-08-08 13:05:26 -07001
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 Campanellaedfdbca2017-02-01 17:33:47 -080017import hashlib
18import os
19import socket
20import sys
21import base64
22import time
23from urlparse import urlparse
Andrea Campanellaedfdbca2017-02-01 17:33:47 -080024from xos.config import Config
Andrea Campanella08c14ca2017-03-31 16:13:09 +020025from synchronizers.new_base.SyncInstanceUsingAnsible import SyncInstanceUsingAnsible
26from synchronizers.new_base.modelaccessor import *
27from synchronizers.new_base.ansible_helper import run_template_ssh
Andrea Campanellaedfdbca2017-02-01 17:33:47 -080028from xos.logger import Logger, logging
29
Andrea Campanellaedfdbca2017-02-01 17:33:47 -080030# hpclibrary will be in steps/..
31parentdir = os.path.join(os.path.dirname(__file__),"..")
32sys.path.insert(0,parentdir)
33
Andrea Campanellaedfdbca2017-02-01 17:33:47 -080034
35logger = Logger(level=logging.INFO)
36
37ENABLE_QUICK_UPDATE=False
38
Andrea Campanellaedfdbca2017-02-01 17:33:47 -080039class SyncVEGTenant(SyncInstanceUsingAnsible):
40 provides=[VEGTenant]
41 observes=VEGTenant
42 requested_interval=0
43 template_name = "sync_vegtenant.yaml"
Andrea Campanella08c14ca2017-03-31 16:13:09 +020044 watches = [ModelLink(ServiceDependency, via='servicedependency'), ModelLink(ServiceMonitoringAgentInfo, via='monitoringagentinfo')]
Andrea Campanellaedfdbca2017-02-01 17:33:47 -080045
46 def __init__(self, *args, **kwargs):
47 super(SyncVEGTenant, self).__init__(*args, **kwargs)
48
Andrea Campanellaedfdbca2017-02-01 17:33:47 -080049 def get_veg_service(self, o):
50 if not o.provider_service:
51 return None
52
Andrea Campanella08c14ca2017-03-31 16:13:09 +020053 vegs = VEGService.objects.filter(id=o.provider_service.id)
Andrea Campanellaedfdbca2017-02-01 17:33:47 -080054 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 Campanellaedfdbca2017-02-01 17:33:47 -080064 # 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 Campanellaedfdbca2017-02-01 17:33:47 -080080
81 dnsdemux_ip = dnsdemux_ip or "none"
82
Andrea Campanellaedfdbca2017-02-01 17:33:47 -080083 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 Campanella08c14ca2017-03-31 16:13:09 +0200113 "docker_local_image_name": veg_service.docker_image_name,
Andrea Campanellaedfdbca2017-02-01 17:33:47 -0800114 "docker_opts": " ".join(docker_opts),
115 "dnsdemux_ip": dnsdemux_ip,
116 "cdn_prefixes": cdn_prefixes,
Andrea Campanellaedfdbca2017-02-01 17:33:47 -0800117 "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 Campanellaedfdbca2017-02-01 17:33:47 -0800137 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 Campanella08c14ca2017-03-31 16:13:09 +0200145 raise Exception("probably not implemented")
Andrea Campanellaedfdbca2017-02-01 17:33:47 -0800146 super(SyncVEGTenant, self).run_playbook(o, fields, "sync_vegtenant_new.yaml")
147 else:
Andrea Campanella08c14ca2017-03-31 16:13:09 +0200148 super(SyncVEGTenant, self).run_playbook(o, fields, template_name="sync_vegtenant_vtn.yaml")
Andrea Campanellaedfdbca2017-02-01 17:33:47 -0800149
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)