Scott Baker | 619de67 | 2016-06-20 12:49:38 -0700 | [diff] [blame] | 1 | import os |
| 2 | import sys |
Scott Baker | e95023b | 2017-02-23 09:50:04 -0800 | [diff] [blame] | 3 | from synchronizers.new_base.SyncInstanceUsingAnsible import SyncInstanceUsingAnsible |
| 4 | from synchronizers.new_base.modelaccessor import * |
Srikanth Vavilapalli | 974c9ce | 2017-01-25 01:50:27 +0000 | [diff] [blame] | 5 | from xos.logger import Logger, logging |
Scott Baker | 619de67 | 2016-06-20 12:49:38 -0700 | [diff] [blame] | 6 | |
| 7 | parentdir = os.path.join(os.path.dirname(__file__), "..") |
| 8 | sys.path.insert(0, parentdir) |
| 9 | |
Srikanth Vavilapalli | 974c9ce | 2017-01-25 01:50:27 +0000 | [diff] [blame] | 10 | logger = Logger(level=logging.INFO) |
| 11 | |
Scott Baker | 619de67 | 2016-06-20 12:49:38 -0700 | [diff] [blame] | 12 | class SyncExampleTenant(SyncInstanceUsingAnsible): |
| 13 | |
| 14 | provides = [ExampleTenant] |
| 15 | |
| 16 | observes = ExampleTenant |
| 17 | |
| 18 | requested_interval = 0 |
| 19 | |
| 20 | template_name = "exampletenant_playbook.yaml" |
| 21 | |
| 22 | service_key_name = "/opt/xos/synchronizers/exampleservice/exampleservice_private_key" |
| 23 | |
Scott Baker | 1bfb91b | 2017-03-20 11:00:23 -0700 | [diff] [blame] | 24 | watches = [ModelLink(ServiceDependency,via='servicedependency'), ModelLink(ServiceMonitoringAgentInfo,via='monitoringagentinfo')] |
Srikanth Vavilapalli | 2c41ab3 | 2016-11-17 03:14:30 +0000 | [diff] [blame] | 25 | |
Scott Baker | 619de67 | 2016-06-20 12:49:38 -0700 | [diff] [blame] | 26 | def __init__(self, *args, **kwargs): |
| 27 | super(SyncExampleTenant, self).__init__(*args, **kwargs) |
| 28 | |
Scott Baker | 619de67 | 2016-06-20 12:49:38 -0700 | [diff] [blame] | 29 | def get_exampleservice(self, o): |
Scott Baker | 0982002 | 2017-07-18 11:29:59 -0700 | [diff] [blame] | 30 | if not o.owner: |
Scott Baker | 619de67 | 2016-06-20 12:49:38 -0700 | [diff] [blame] | 31 | return None |
| 32 | |
Scott Baker | 0982002 | 2017-07-18 11:29:59 -0700 | [diff] [blame] | 33 | exampleservice = ExampleService.objects.filter(id=o.owner.id) |
Scott Baker | 619de67 | 2016-06-20 12:49:38 -0700 | [diff] [blame] | 34 | |
| 35 | if not exampleservice: |
| 36 | return None |
| 37 | |
| 38 | return exampleservice[0] |
| 39 | |
| 40 | # Gets the attributes that are used by the Ansible template but are not |
| 41 | # part of the set of default attributes. |
| 42 | def get_extra_attributes(self, o): |
| 43 | fields = {} |
| 44 | fields['tenant_message'] = o.tenant_message |
| 45 | exampleservice = self.get_exampleservice(o) |
| 46 | fields['service_message'] = exampleservice.service_message |
| 47 | return fields |
| 48 | |
Scott Baker | 9b1a3eb | 2017-01-23 14:46:27 -0800 | [diff] [blame] | 49 | def delete_record(self, port): |
| 50 | # Nothing needs to be done to delete an exampleservice; it goes away |
| 51 | # when the instance holding the exampleservice is deleted. |
| 52 | pass |
| 53 | |
Srikanth Vavilapalli | 974c9ce | 2017-01-25 01:50:27 +0000 | [diff] [blame] | 54 | def handle_service_monitoringagentinfo_watch_notification(self, monitoring_agent_info): |
| 55 | if not monitoring_agent_info.service: |
| 56 | logger.info("handle watch notifications for service monitoring agent info...ignoring because service attribute in monitoring agent info:%s is null" % (monitoring_agent_info)) |
| 57 | return |
| 58 | |
| 59 | if not monitoring_agent_info.target_uri: |
| 60 | 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)) |
| 61 | return |
| 62 | |
Scott Baker | e95023b | 2017-02-23 09:50:04 -0800 | [diff] [blame] | 63 | objs = ExampleTenant.objects.all() |
Srikanth Vavilapalli | 974c9ce | 2017-01-25 01:50:27 +0000 | [diff] [blame] | 64 | for obj in objs: |
Scott Baker | 0982002 | 2017-07-18 11:29:59 -0700 | [diff] [blame] | 65 | if obj.owner.id != monitoring_agent_info.service.id: |
Srikanth Vavilapalli | 974c9ce | 2017-01-25 01:50:27 +0000 | [diff] [blame] | 66 | 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)) |
| 67 | return |
| 68 | |
| 69 | instance = self.get_instance(obj) |
| 70 | if not instance: |
| 71 | logger.warn("handle watch notifications for service monitoring agent info...: No valid instance found for object %s" % (str(obj))) |
| 72 | return |
| 73 | |
| 74 | logger.info("handling watch notification for monitoring agent info:%s for ExampleTenant object:%s" % (monitoring_agent_info, obj)) |
| 75 | |
| 76 | #Run ansible playbook to update the routing table entries in the instance |
| 77 | fields = self.get_ansible_fields(instance) |
| 78 | fields["ansible_tag"] = obj.__class__.__name__ + "_" + str(obj.id) + "_monitoring" |
| 79 | fields["target_uri"] = monitoring_agent_info.target_uri |
| 80 | |
| 81 | template_name = "monitoring_agent.yaml" |
| 82 | super(SyncExampleTenant, self).run_playbook(obj, fields, template_name) |
| 83 | pass |