Scott Baker | 619de67 | 2016-06-20 12:49:38 -0700 | [diff] [blame] | 1 | import os |
| 2 | import sys |
| 3 | from django.db.models import Q, F |
Srikanth Vavilapalli | 974c9ce | 2017-01-25 01:50:27 +0000 | [diff] [blame] | 4 | from core.models import ModelLink, CoarseTenant, ServiceMonitoringAgentInfo |
Scott Baker | 619de67 | 2016-06-20 12:49:38 -0700 | [diff] [blame] | 5 | from services.exampleservice.models import ExampleService, ExampleTenant |
| 6 | from synchronizers.base.SyncInstanceUsingAnsible import SyncInstanceUsingAnsible |
Srikanth Vavilapalli | 974c9ce | 2017-01-25 01:50:27 +0000 | [diff] [blame] | 7 | from xos.logger import Logger, logging |
Scott Baker | 619de67 | 2016-06-20 12:49:38 -0700 | [diff] [blame] | 8 | |
| 9 | parentdir = os.path.join(os.path.dirname(__file__), "..") |
| 10 | sys.path.insert(0, parentdir) |
| 11 | |
Srikanth Vavilapalli | 974c9ce | 2017-01-25 01:50:27 +0000 | [diff] [blame] | 12 | logger = Logger(level=logging.INFO) |
| 13 | |
Scott Baker | 619de67 | 2016-06-20 12:49:38 -0700 | [diff] [blame] | 14 | class 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 Vavilapalli | 974c9ce | 2017-01-25 01:50:27 +0000 | [diff] [blame] | 26 | watches = [ModelLink(CoarseTenant,via='coarsetenant'), ModelLink(ServiceMonitoringAgentInfo,via='monitoringagentinfo')] |
Srikanth Vavilapalli | 2c41ab3 | 2016-11-17 03:14:30 +0000 | [diff] [blame] | 27 | |
Scott Baker | 619de67 | 2016-06-20 12:49:38 -0700 | [diff] [blame] | 28 | 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 Baker | 9b1a3eb | 2017-01-23 14:46:27 -0800 | [diff] [blame] | 62 | 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 Vavilapalli | 974c9ce | 2017-01-25 01:50:27 +0000 | [diff] [blame] | 67 | 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 |