blob: fb60667218c221f3c792f802aa3d3dfe6348aee0 [file] [log] [blame]
Gabe Black90623222017-01-18 19:52:28 +00001import os
2import sys
3from django.db.models import Q, F
4from services.passivetest.models import PassiveTestService, PassiveTestTenant
5from synchronizers.base.SyncInstanceUsingAnsible import SyncInstanceUsingAnsible
6from core.models import ModelLink, CoarseTenant, ServiceMonitoringAgentInfo
7from xos.logger import Logger, logging
8from urlparse import urlparse
9
10parentdir = os.path.join(os.path.dirname(__file__), "..")
11sys.path.insert(0, parentdir)
12
13logger = Logger(level=logging.INFO)
14
15class SyncPassiveTestTenant(SyncInstanceUsingAnsible):
16 provides = [PassiveTestTenant]
17 observes = PassiveTestTenant
18 requested_interval = 0
19 template_name = "passivetesttenant_playbook.yaml"
20 service_key_name = "/opt/xos/synchronizers/passivetest/passivetest_private_key"
21 watches = [ModelLink(CoarseTenant,via='coarsetenant'),
22 ModelLink(ServiceMonitoringAgentInfo,via='monitoringagentinfo')]
23
24 def __init__(self, *args, **kwargs):
25 super(SyncPassiveTestTenant, self).__init__(*args, **kwargs)
26
27 def fetch_pending(self, deleted):
28
29 if (not deleted):
30 objs = PassiveTestTenant.get_tenant_objects().filter(
31 Q(enacted__lt=F('updated')) | Q(enacted=None), Q(lazy_blocked=False))
32 else:
33 # If this is a deletion we get all of the deleted tenants..
34 objs = PassiveTestTenant.get_deleted_tenant_objects()
35
36 return objs
37
38 def get_passivetestservice(self, o):
39 if not o.provider_service:
40 return None
41
42 passivetestservice = PassiveTestService.get_service_objects().filter(id=o.provider_service.id)
43
44 if not passivetestservice:
45 return None
46
47 return passivetestservice[0]
48
49 # Gets the attributes that are used by the Ansible template but are not
50 # part of the set of default attributes.
51 def get_extra_attributes(self, o):
52 passivetestservice = self.get_passivetestservice(o)
53 return { "public_ip": o.public_ip,
54 "synchronizer_ip": o.synchronizer_ip,
55 "tap_ports": passivetestservice.tap_ports,
56 "reset_viv": passivetestservice.reset_viv}
57
58 def handle_service_monitoringagentinfo_watch_notification(self, monitoring_agent_info):
59 if not monitoring_agent_info.service:
60 logger.info("handle watch notifications for service monitoring agent info...ignoring because service attribute in monitoring agent info:%s is null" % (monitoring_agent_info))
61 return
62
63 if not monitoring_agent_info.target_uri:
64 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))
65 return
66
67 objs = PassiveTestTenant.get_tenant_objects().all()
68 for obj in objs:
69 if obj.provider_service.id != monitoring_agent_info.service.id:
70 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))
71 return
72
73 instance = self.get_instance(obj)
74 if not instance:
75 logger.warn("handle watch notifications for service monitoring agent info...: No valid instance found for object %s" % (str(obj)))
76 return
77
78 logger.info("handling watch notification for monitoring agent info:%s for PassiveTestTenant object:%s" % (monitoring_agent_info, obj))
79
80 #Run ansible playbook to update the routing table entries in the instance
81 fields = self.get_ansible_fields(instance)
82 fields["ansible_tag"] = obj.__class__.__name__ + "_" + str(obj.id) + "_service_monitoring"
83
84 #Parse the monitoring agent target_uri
85 url = urlparse(monitoring_agent_info.target_uri)
86
87 #Assuming target_uri is rabbitmq URI
88 fields["rabbit_user"] = url.username
89 fields["rabbit_password"] = url.password
90 fields["rabbit_host"] = url.hostname
91
92 template_name = "sync_monitoring_agent.yaml"
93 super(SyncPassiveTestTenant, self).run_playbook(obj, fields, template_name)
94 pass
95