blob: 1b2f619971503fe03e5a835887fad4dc2f037885 [file] [log] [blame]
Scott Baker619de672016-06-20 12:49:38 -07001import os
2import sys
3from django.db.models import Q, F
Srikanth Vavilapalli974c9ce2017-01-25 01:50:27 +00004from core.models import ModelLink, CoarseTenant, ServiceMonitoringAgentInfo
Scott Baker619de672016-06-20 12:49:38 -07005from services.exampleservice.models import ExampleService, ExampleTenant
6from synchronizers.base.SyncInstanceUsingAnsible import SyncInstanceUsingAnsible
Srikanth Vavilapalli974c9ce2017-01-25 01:50:27 +00007from xos.logger import Logger, logging
Scott Baker619de672016-06-20 12:49:38 -07008
9parentdir = os.path.join(os.path.dirname(__file__), "..")
10sys.path.insert(0, parentdir)
11
Srikanth Vavilapalli974c9ce2017-01-25 01:50:27 +000012logger = Logger(level=logging.INFO)
13
Scott Baker619de672016-06-20 12:49:38 -070014class SyncExampleTenant(SyncInstanceUsingAnsible):
15
16 provides = [ExampleTenant]
17
18 observes = ExampleTenant
19
20 requested_interval = 0
21
22 template_name = "exampletenant_playbook.yaml"
23
24 service_key_name = "/opt/xos/synchronizers/exampleservice/exampleservice_private_key"
25
Srikanth Vavilapalli974c9ce2017-01-25 01:50:27 +000026 watches = [ModelLink(CoarseTenant,via='coarsetenant'), ModelLink(ServiceMonitoringAgentInfo,via='monitoringagentinfo')]
Srikanth Vavilapalli2c41ab32016-11-17 03:14:30 +000027
Scott Baker619de672016-06-20 12:49:38 -070028 def __init__(self, *args, **kwargs):
29 super(SyncExampleTenant, self).__init__(*args, **kwargs)
30
31 def fetch_pending(self, deleted):
32
33 if (not deleted):
34 objs = ExampleTenant.get_tenant_objects().filter(
35 Q(enacted__lt=F('updated')) | Q(enacted=None), Q(lazy_blocked=False))
36 else:
37 # If this is a deletion we get all of the deleted tenants..
38 objs = ExampleTenant.get_deleted_tenant_objects()
39
40 return objs
41
42 def get_exampleservice(self, o):
43 if not o.provider_service:
44 return None
45
46 exampleservice = ExampleService.get_service_objects().filter(id=o.provider_service.id)
47
48 if not exampleservice:
49 return None
50
51 return exampleservice[0]
52
53 # Gets the attributes that are used by the Ansible template but are not
54 # part of the set of default attributes.
55 def get_extra_attributes(self, o):
56 fields = {}
57 fields['tenant_message'] = o.tenant_message
58 exampleservice = self.get_exampleservice(o)
59 fields['service_message'] = exampleservice.service_message
60 return fields
61
Scott Baker9b1a3eb2017-01-23 14:46:27 -080062 def delete_record(self, port):
63 # Nothing needs to be done to delete an exampleservice; it goes away
64 # when the instance holding the exampleservice is deleted.
65 pass
66
Srikanth Vavilapalli974c9ce2017-01-25 01:50:27 +000067 def handle_service_monitoringagentinfo_watch_notification(self, monitoring_agent_info):
68 if not monitoring_agent_info.service:
69 logger.info("handle watch notifications for service monitoring agent info...ignoring because service attribute in monitoring agent info:%s is null" % (monitoring_agent_info))
70 return
71
72 if not monitoring_agent_info.target_uri:
73 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))
74 return
75
76 objs = ExampleTenant.get_tenant_objects().all()
77 for obj in objs:
78 if obj.provider_service.id != monitoring_agent_info.service.id:
79 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))
80 return
81
82 instance = self.get_instance(obj)
83 if not instance:
84 logger.warn("handle watch notifications for service monitoring agent info...: No valid instance found for object %s" % (str(obj)))
85 return
86
87 logger.info("handling watch notification for monitoring agent info:%s for ExampleTenant object:%s" % (monitoring_agent_info, obj))
88
89 #Run ansible playbook to update the routing table entries in the instance
90 fields = self.get_ansible_fields(instance)
91 fields["ansible_tag"] = obj.__class__.__name__ + "_" + str(obj.id) + "_monitoring"
92 fields["target_uri"] = monitoring_agent_info.target_uri
93
94 template_name = "monitoring_agent.yaml"
95 super(SyncExampleTenant, self).run_playbook(obj, fields, template_name)
96 pass