Scott Baker | 31acc65 | 2016-06-23 15:47:56 -0700 | [diff] [blame] | 1 | import hashlib |
| 2 | import os |
| 3 | import socket |
| 4 | import sys |
| 5 | import base64 |
| 6 | import time |
| 7 | from django.db.models import F, Q |
| 8 | from xos.config import Config |
| 9 | from synchronizers.base.syncstep import SyncStep |
| 10 | from synchronizers.base.ansible import run_template_ssh |
| 11 | from synchronizers.base.SyncInstanceUsingAnsible import SyncInstanceUsingAnsible |
| 12 | from core.models import Service, Slice |
| 13 | from services.ceilometer.models import CeilometerService, MonitoringChannel |
| 14 | from xos.logger import Logger, logging |
| 15 | |
| 16 | parentdir = os.path.join(os.path.dirname(__file__),"..") |
| 17 | sys.path.insert(0,parentdir) |
| 18 | |
| 19 | logger = Logger(level=logging.INFO) |
| 20 | |
| 21 | class SyncMonitoringChannel(SyncInstanceUsingAnsible): |
| 22 | provides=[MonitoringChannel] |
| 23 | observes=MonitoringChannel |
| 24 | requested_interval=0 |
| 25 | template_name = "sync_monitoringchannel.yaml" |
| 26 | service_key_name = "/opt/xos/synchronizers/monitoring_channel/monitoring_channel_private_key" |
| 27 | |
| 28 | def __init__(self, *args, **kwargs): |
| 29 | super(SyncMonitoringChannel, self).__init__(*args, **kwargs) |
| 30 | |
| 31 | def fetch_pending(self, deleted): |
| 32 | if (not deleted): |
| 33 | objs = MonitoringChannel.get_tenant_objects().filter(Q(enacted__lt=F('updated')) | Q(enacted=None),Q(lazy_blocked=False)) |
| 34 | else: |
| 35 | objs = MonitoringChannel.get_deleted_tenant_objects() |
| 36 | |
| 37 | return objs |
| 38 | |
| 39 | def get_extra_attributes(self, o): |
| 40 | # This is a place to include extra attributes. In the case of Monitoring Channel, we need to know |
| 41 | # 1) Allowed tenant ids |
| 42 | # 2) Ceilometer API service endpoint URL if running externally |
| 43 | # 3) Credentials to access Ceilometer API service |
| 44 | |
| 45 | ceilometer_services = CeilometerService.get_service_objects().filter(id=o.provider_service.id) |
| 46 | if not ceilometer_services: |
| 47 | raise "No associated Ceilometer service" |
| 48 | ceilometer_service = ceilometer_services[0] |
| 49 | ceilometer_pub_sub_url = ceilometer_service.ceilometer_pub_sub_url |
| 50 | if not ceilometer_pub_sub_url: |
| 51 | ceilometer_pub_sub_url = '' |
| 52 | instance = self.get_instance(o) |
| 53 | |
| 54 | try: |
| 55 | full_setup = Config().observer_full_setup |
| 56 | except: |
| 57 | full_setup = True |
| 58 | |
| 59 | fields = {"unique_id": o.id, |
| 60 | "allowed_tenant_ids": o.tenant_list, |
| 61 | "auth_url":instance.controller.auth_url, |
| 62 | "admin_user":instance.controller.admin_user, |
| 63 | "admin_password":instance.controller.admin_password, |
| 64 | "admin_tenant":instance.controller.admin_tenant, |
| 65 | "ceilometer_pub_sub_url": ceilometer_pub_sub_url, |
| 66 | "full_setup": full_setup} |
| 67 | |
| 68 | return fields |
| 69 | |
| 70 | def run_playbook(self, o, fields): |
| 71 | #ansible_hash = hashlib.md5(repr(sorted(fields.items()))).hexdigest() |
| 72 | #quick_update = (o.last_ansible_hash == ansible_hash) |
| 73 | |
| 74 | #if quick_update: |
| 75 | # logger.info("quick_update triggered; skipping ansible recipe") |
| 76 | #else: |
| 77 | super(SyncMonitoringChannel, self).run_playbook(o, fields) |
| 78 | |
| 79 | #o.last_ansible_hash = ansible_hash |
| 80 | |
| 81 | def map_delete_inputs(self, o): |
| 82 | fields = {"unique_id": o.id, |
| 83 | "delete": True} |
| 84 | return fields |