blob: b0faf07987ab6f818753bb9d8f50742472ce2f2b [file] [log] [blame]
Matteo Scandoloaca86652017-08-08 13:05:27 -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
Scott Baker761e1062016-06-20 17:18:17 -070017import hashlib
18import os
19import socket
20import sys
21import base64
22import time
Srikanth Vavilapallicd9d9bd2016-12-03 22:53:42 +000023from urlparse import urlparse
Scott Baker32f65122017-03-08 17:04:16 -080024from synchronizers.new_base.SyncInstanceUsingAnsible import SyncInstanceUsingAnsible
25from synchronizers.new_base.modelaccessor import *
26from synchronizers.new_base.ansible_helper import run_template_ssh
Scott Baker761e1062016-06-20 17:18:17 -070027from xos.logger import Logger, logging
28
29# hpclibrary will be in steps/..
30parentdir = os.path.join(os.path.dirname(__file__),"..")
31sys.path.insert(0,parentdir)
32
Scott Baker761e1062016-06-20 17:18:17 -070033logger = Logger(level=logging.INFO)
34
35ENABLE_QUICK_UPDATE=False
36
Scott Baker645c0c52017-09-15 10:38:32 -070037class SyncVSGServiceInstance(SyncInstanceUsingAnsible):
38 provides=[VSGServiceInstance]
39 observes=VSGServiceInstance
Scott Baker761e1062016-06-20 17:18:17 -070040 requested_interval=0
Scott Baker645c0c52017-09-15 10:38:32 -070041 template_name = "sync_vsgserviceinstance.yaml"
Scott Baker3f8a3902017-03-20 11:04:32 -070042 watches = [ModelLink(ServiceDependency,via='servicedependency'), ModelLink(ServiceMonitoringAgentInfo,via='monitoringagentinfo')]
Scott Baker761e1062016-06-20 17:18:17 -070043
44 def __init__(self, *args, **kwargs):
Scott Baker645c0c52017-09-15 10:38:32 -070045 super(SyncVSGServiceInstance, self).__init__(*args, **kwargs)
Scott Baker761e1062016-06-20 17:18:17 -070046
Scott Baker645c0c52017-09-15 10:38:32 -070047 def get_vsg_service(self, o):
Scott Bakere27de6a2017-11-28 17:10:08 -080048 return o.owner.leaf_model
Scott Baker761e1062016-06-20 17:18:17 -070049
50 def get_extra_attributes(self, o):
51 # This is a place to include extra attributes that aren't part of the
Scott Baker645c0c52017-09-15 10:38:32 -070052 # object itself. In the case of vSG, we need to know:
53 # 1) the addresses of dnsdemux, to setup dnsmasq in the vSG
Scott Baker761e1062016-06-20 17:18:17 -070054 # 2) CDN prefixes, so we know what URLs to send to dnsdemux
Scott Baker645c0c52017-09-15 10:38:32 -070055 # 4) vlan_ids, for setting up networking in the vSG VM
Scott Baker761e1062016-06-20 17:18:17 -070056
Scott Baker645c0c52017-09-15 10:38:32 -070057 vsg_service = self.get_vsg_service(o)
Scott Baker761e1062016-06-20 17:18:17 -070058
59 dnsdemux_ip = None
60 cdn_prefixes = []
61
62 cdn_config_fn = "/opt/xos/synchronizers/vsg/cdn_config"
63 if os.path.exists(cdn_config_fn):
64 # manual CDN configuration
65 # the first line is the address of dnsredir
66 # the remaining lines are domain names, one per line
67 lines = file(cdn_config_fn).readlines()
68 if len(lines)>=2:
69 dnsdemux_ip = lines[0].strip()
70 cdn_prefixes = [x.strip() for x in lines[1:] if x.strip()]
Scott Baker761e1062016-06-20 17:18:17 -070071
72 dnsdemux_ip = dnsdemux_ip or "none"
73
Scott Baker761e1062016-06-20 17:18:17 -070074 s_tags = []
75 c_tags = []
76 if o.volt:
77 s_tags.append(o.volt.s_tag)
78 c_tags.append(o.volt.c_tag)
79
Matteo Scandolo79f298e2017-06-05 11:18:01 -070080 full_setup = True
Scott Baker761e1062016-06-20 17:18:17 -070081
Scott Bakera45a6452018-05-31 08:38:37 -070082 # NOTE(smbaker): User devices removed as part of CORD-3116
Scott Baker761e1062016-06-20 17:18:17 -070083 safe_macs=[]
Scott Baker761e1062016-06-20 17:18:17 -070084
Scott Baker8e66d662016-10-13 13:22:49 -070085 docker_opts = []
Scott Baker645c0c52017-09-15 10:38:32 -070086 if vsg_service.docker_insecure_registry:
87 reg_name = vsg_service.docker_image_name.split("/",1)[0]
Scott Baker8e66d662016-10-13 13:22:49 -070088 docker_opts.append("--insecure-registry " + reg_name)
89
Scott Baker761e1062016-06-20 17:18:17 -070090 fields = {"s_tags": s_tags,
91 "c_tags": c_tags,
Scott Baker645c0c52017-09-15 10:38:32 -070092 "docker_remote_image_name": vsg_service.docker_image_name,
93 "docker_local_image_name": vsg_service.docker_image_name,
Scott Baker8e66d662016-10-13 13:22:49 -070094 "docker_opts": " ".join(docker_opts),
Scott Baker761e1062016-06-20 17:18:17 -070095 "dnsdemux_ip": dnsdemux_ip,
96 "cdn_prefixes": cdn_prefixes,
Scott Baker761e1062016-06-20 17:18:17 -070097 "full_setup": full_setup,
98 "isolation": o.instance.isolation,
99 "safe_browsing_macs": safe_macs,
Scott Baker645c0c52017-09-15 10:38:32 -0700100 "container_name": "vsg-%s-%s" % (s_tags[0], c_tags[0]),
101 "dns_servers": [x.strip() for x in vsg_service.dns_servers.split(",")],
102 "url_filter_kind": vsg_service.url_filter_kind }
Scott Baker761e1062016-06-20 17:18:17 -0700103
Scott Baker62829b02017-10-23 11:16:49 -0700104 # Some subscriber models may not implement all fields that we look for, so specify some defaults.
105 fields["firewall_rules"] = ""
106 fields["firewall_enable"] = False
107 fields["url_filter_enable"] = False
108 fields["url_filter_level"] = "PG"
109 fields["cdn_enable"] = False
110 fields["uplink_speed"] = 1000000000
111 fields["downlink_speed"] = 1000000000
112 fields["enable_uverse"] = True
113 fields["status"] = "enabled"
Scott Baker761e1062016-06-20 17:18:17 -0700114
Scott Baker62829b02017-10-23 11:16:49 -0700115 # add in the sync_attributes that come from the subscriber object
Scott Baker761e1062016-06-20 17:18:17 -0700116 if o.volt and o.volt.subscriber and hasattr(o.volt.subscriber, "sync_attributes"):
117 for attribute_name in o.volt.subscriber.sync_attributes:
118 fields[attribute_name] = getattr(o.volt.subscriber, attribute_name)
119
120 return fields
121
122 def sync_fields(self, o, fields):
123 # the super causes the playbook to be run
Scott Baker645c0c52017-09-15 10:38:32 -0700124 super(SyncVSGServiceInstance, self).sync_fields(o, fields)
Scott Baker761e1062016-06-20 17:18:17 -0700125
Scott Baker761e1062016-06-20 17:18:17 -0700126 def run_playbook(self, o, fields):
127 ansible_hash = hashlib.md5(repr(sorted(fields.items()))).hexdigest()
128 quick_update = (o.last_ansible_hash == ansible_hash)
129
130 if ENABLE_QUICK_UPDATE and quick_update:
131 logger.info("quick_update triggered; skipping ansible recipe",extra=o.tologdict())
132 else:
133 if o.instance.isolation in ["container", "container_vm"]:
Scott Baker645c0c52017-09-15 10:38:32 -0700134 raise Exception("Not implemented")
Scott Baker761e1062016-06-20 17:18:17 -0700135 else:
Scott Baker645c0c52017-09-15 10:38:32 -0700136 super(SyncVSGServiceInstance, self).run_playbook(o, fields)
Scott Baker761e1062016-06-20 17:18:17 -0700137
138 o.last_ansible_hash = ansible_hash
139
Scott Baker96746882017-06-09 14:12:15 -0700140 def sync_record(self, o):
141 if (not o.policed) or (o.policed<o.updated):
Scott Bakera3e7b582017-08-30 08:36:52 -0700142 self.defer_sync(o, "waiting on model policy")
Scott Baker645c0c52017-09-15 10:38:32 -0700143 super(SyncVSGServiceInstance, self).sync_record(o)
Scott Baker96746882017-06-09 14:12:15 -0700144
145 def delete_record(self, o):
146 if (not o.policed) or (o.policed<o.updated):
Scott Bakera3e7b582017-08-30 08:36:52 -0700147 self.defer_sync(o, "waiting on model policy")
Scott Baker96746882017-06-09 14:12:15 -0700148 # do not call super, as we don't want to re-run the playbook
Srikanth Vavilapallicd9d9bd2016-12-03 22:53:42 +0000149
150 def handle_service_monitoringagentinfo_watch_notification(self, monitoring_agent_info):
151 if not monitoring_agent_info.service:
152 logger.info("handle watch notifications for service monitoring agent info...ignoring because service attribute in monitoring agent info:%s is null" % (monitoring_agent_info))
153 return
154
155 if not monitoring_agent_info.target_uri:
156 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))
157 return
158
Scott Baker645c0c52017-09-15 10:38:32 -0700159 objs = VSGServiceInstance.objects.all()
Srikanth Vavilapallicd9d9bd2016-12-03 22:53:42 +0000160 for obj in objs:
Scott Baker80238f82017-07-18 16:01:10 -0700161 if obj.owner.id != monitoring_agent_info.service.id:
Srikanth Vavilapallicd9d9bd2016-12-03 22:53:42 +0000162 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))
163 return
164
165 instance = self.get_instance(obj)
166 if not instance:
167 logger.warn("handle watch notifications for service monitoring agent info...: No valid instance found for object %s" % (str(obj)))
168 return
169
Scott Baker645c0c52017-09-15 10:38:32 -0700170 logger.info("handling watch notification for monitoring agent info:%s for VSGServiceInstance object:%s" % (monitoring_agent_info, obj))
Srikanth Vavilapallicd9d9bd2016-12-03 22:53:42 +0000171
172 #Run ansible playbook to update the routing table entries in the instance
173 fields = self.get_ansible_fields(instance)
174 fields["ansible_tag"] = obj.__class__.__name__ + "_" + str(obj.id) + "_service_monitoring"
175
176 #Parse the monitoring agent target_uri
177 url = urlparse(monitoring_agent_info.target_uri)
178
179 #Assuming target_uri is rabbitmq URI
180 fields["rabbit_user"] = url.username
181 fields["rabbit_password"] = url.password
182 fields["rabbit_host"] = url.hostname
183
184 template_name = "sync_monitoring_agent.yaml"
Scott Baker645c0c52017-09-15 10:38:32 -0700185 super(SyncVSGServiceInstance, self).run_playbook(obj, fields, template_name)
Scott Baker32f65122017-03-08 17:04:16 -0800186