blob: d22b7384554e4ec16ab724462639fd440629a6e4 [file] [log] [blame]
Andrea Campanellaedfdbca2017-02-01 17:33:47 -08001import hashlib
2import os
3import socket
4import sys
5import base64
6import time
7from urlparse import urlparse
Andrea Campanellaedfdbca2017-02-01 17:33:47 -08008from xos.config import Config
Andrea Campanella08c14ca2017-03-31 16:13:09 +02009from synchronizers.new_base.SyncInstanceUsingAnsible import SyncInstanceUsingAnsible
10from synchronizers.new_base.modelaccessor import *
11from synchronizers.new_base.ansible_helper import run_template_ssh
Andrea Campanellaedfdbca2017-02-01 17:33:47 -080012from xos.logger import Logger, logging
13
Andrea Campanellaedfdbca2017-02-01 17:33:47 -080014# hpclibrary will be in steps/..
15parentdir = os.path.join(os.path.dirname(__file__),"..")
16sys.path.insert(0,parentdir)
17
Andrea Campanellaedfdbca2017-02-01 17:33:47 -080018
19logger = Logger(level=logging.INFO)
20
21ENABLE_QUICK_UPDATE=False
22
Andrea Campanellaedfdbca2017-02-01 17:33:47 -080023class SyncVEGTenant(SyncInstanceUsingAnsible):
24 provides=[VEGTenant]
25 observes=VEGTenant
26 requested_interval=0
27 template_name = "sync_vegtenant.yaml"
Andrea Campanella08c14ca2017-03-31 16:13:09 +020028 watches = [ModelLink(ServiceDependency, via='servicedependency'), ModelLink(ServiceMonitoringAgentInfo, via='monitoringagentinfo')]
Andrea Campanellaedfdbca2017-02-01 17:33:47 -080029
30 def __init__(self, *args, **kwargs):
31 super(SyncVEGTenant, self).__init__(*args, **kwargs)
32
Andrea Campanellaedfdbca2017-02-01 17:33:47 -080033 def get_veg_service(self, o):
34 if not o.provider_service:
35 return None
36
Andrea Campanella08c14ca2017-03-31 16:13:09 +020037 vegs = VEGService.objects.filter(id=o.provider_service.id)
Andrea Campanellaedfdbca2017-02-01 17:33:47 -080038 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 Campanellaedfdbca2017-02-01 17:33:47 -080048 # 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 Campanellaedfdbca2017-02-01 17:33:47 -080064
65 dnsdemux_ip = dnsdemux_ip or "none"
66
Andrea Campanellaedfdbca2017-02-01 17:33:47 -080067 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 Campanella08c14ca2017-03-31 16:13:09 +020097 "docker_local_image_name": veg_service.docker_image_name,
Andrea Campanellaedfdbca2017-02-01 17:33:47 -080098 "docker_opts": " ".join(docker_opts),
99 "dnsdemux_ip": dnsdemux_ip,
100 "cdn_prefixes": cdn_prefixes,
Andrea Campanellaedfdbca2017-02-01 17:33:47 -0800101 "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 Campanellaedfdbca2017-02-01 17:33:47 -0800121 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 Campanella08c14ca2017-03-31 16:13:09 +0200129 raise Exception("probably not implemented")
Andrea Campanellaedfdbca2017-02-01 17:33:47 -0800130 super(SyncVEGTenant, self).run_playbook(o, fields, "sync_vegtenant_new.yaml")
131 else:
Andrea Campanella08c14ca2017-03-31 16:13:09 +0200132 super(SyncVEGTenant, self).run_playbook(o, fields, template_name="sync_vegtenant_vtn.yaml")
Andrea Campanellaedfdbca2017-02-01 17:33:47 -0800133
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)