rdudyala | 996d70b | 2016-10-13 17:40:55 +0000 | [diff] [blame] | 1 | import hashlib |
| 2 | import os |
| 3 | import socket |
| 4 | import sys |
| 5 | import base64 |
| 6 | import time |
| 7 | import json |
| 8 | #import threading |
| 9 | import subprocess |
| 10 | import random |
| 11 | import tempfile |
| 12 | #from sshtunnel import SSHTunnelForwarder |
| 13 | from django.db.models import F, Q |
| 14 | from xos.config import Config |
| 15 | from synchronizers.base.syncstep import SyncStep |
| 16 | from synchronizers.base.ansible import run_template |
| 17 | from synchronizers.base.ansible import run_template_ssh |
| 18 | from synchronizers.base.SyncInstanceUsingAnsible import SyncInstanceUsingAnsible |
| 19 | from core.models import Service, Slice |
| 20 | from services.monitoring.models import CeilometerService, OpenStackServiceMonitoringPublisher |
| 21 | from xos.logger import Logger, logging |
| 22 | |
| 23 | parentdir = os.path.join(os.path.dirname(__file__),"..") |
| 24 | sys.path.insert(0,parentdir) |
| 25 | |
| 26 | logger = Logger(level=logging.INFO) |
| 27 | |
| 28 | class SyncOpenStackMonitoringPublisher(SyncInstanceUsingAnsible): |
| 29 | provides=[OpenStackServiceMonitoringPublisher] |
| 30 | observes=OpenStackServiceMonitoringPublisher |
| 31 | requested_interval=0 |
| 32 | template_name = "sync_openstackmonitoringpublisher.yaml" |
| 33 | |
| 34 | def __init__(self, *args, **kwargs): |
| 35 | super(SyncOpenStackMonitoringPublisher, self).__init__(*args, **kwargs) |
| 36 | |
| 37 | def fetch_pending(self, deleted): |
| 38 | if (not deleted): |
| 39 | objs = OpenStackServiceMonitoringPublisher.get_tenant_objects().filter(Q(enacted__lt=F('updated')) | Q(enacted=None),Q(lazy_blocked=False)) |
| 40 | else: |
| 41 | objs = OpenStackServiceMonitoringPublisher.get_deleted_tenant_objects() |
| 42 | |
| 43 | return objs |
| 44 | |
| 45 | def sync_record(self, o): |
| 46 | logger.info("sync'ing object %s" % str(o),extra=o.tologdict()) |
| 47 | |
| 48 | self.prepare_record(o) |
| 49 | |
| 50 | ceilometer_services = CeilometerService.get_service_objects().filter(id=o.provider_service.id) |
| 51 | if not ceilometer_services: |
| 52 | raise "No associated Ceilometer service" |
| 53 | ceilometer_service = ceilometer_services[0] |
| 54 | service_instance = ceilometer_service.get_instance() |
| 55 | # sync only when the corresponding service instance is fully synced |
| 56 | if not service_instance: |
| 57 | self.defer_sync(o, "waiting on associated service instance") |
| 58 | return |
| 59 | if not service_instance.instance_name: |
| 60 | self.defer_sync(o, "waiting on associated service instance.instance_name") |
| 61 | return |
| 62 | |
| 63 | # Step1: Orchestrate UDP proxy agent on the compute node where monitoring service VM is spawned |
| 64 | |
| 65 | fields = { "hostname": ceilometer_service.ceilometer_rabbit_compute_node, |
| 66 | "baremetal_ssh": True, |
| 67 | "instance_name": "rootcontext", |
| 68 | "username": "root", |
| 69 | "container_name": None, |
| 70 | "rabbit_host": ceilometer_service.ceilometer_rabbit_host, |
| 71 | "rabbit_user": ceilometer_service.ceilometer_rabbit_user, |
| 72 | "rabbit_password": ceilometer_service.ceilometer_rabbit_password, |
| 73 | "listen_ip_addr": socket.gethostbyname(ceilometer_service.ceilometer_rabbit_compute_node) |
| 74 | } |
| 75 | |
| 76 | # If 'o' defines a 'sync_attributes' list, then we'll copy those |
| 77 | # attributes into the Ansible recipe's field list automatically. |
| 78 | if hasattr(o, "sync_attributes"): |
| 79 | for attribute_name in o.sync_attributes: |
| 80 | fields[attribute_name] = getattr(o, attribute_name) |
| 81 | |
| 82 | key_name = self.get_node_key(service_instance.node) |
| 83 | if not os.path.exists(key_name): |
| 84 | raise Exception("Node key %s does not exist" % key_name) |
| 85 | key = file(key_name).read() |
| 86 | fields["private_key"] = key |
| 87 | |
| 88 | template_name = "sync_openstackmonitoringpublisher.yaml" |
| 89 | fields["ansible_tag"] = o.__class__.__name__ + "_" + str(o.id) + "_step1" |
| 90 | |
| 91 | self.run_playbook(o, fields, template_name) |
| 92 | |
| 93 | # Step2: Orchestrate OpenStack publish agent |
| 94 | target_uri = "udp://" + ceilometer_service.ceilometer_rabbit_compute_node + ":4455" |
| 95 | fields = {} |
| 96 | agent_info = [] |
| 97 | if o.monitoring_agents: |
| 98 | for agent in o.monitoring_agents.all(): |
| 99 | body = {'target': target_uri} |
| 100 | if agent.start_url_json_data: |
| 101 | start_url_dict = json.loads(agent.start_url_json_data) |
Srikanth Vavilapalli | f291900 | 2016-12-19 18:32:20 +0000 | [diff] [blame] | 102 | body.update(start_url_dict) |
rdudyala | 996d70b | 2016-10-13 17:40:55 +0000 | [diff] [blame] | 103 | a = {'url': agent.start_url, 'body': json.dumps(body)} |
| 104 | agent_info.append(a) |
| 105 | |
| 106 | fields["agents"] = agent_info |
| 107 | #fields["private_key"] = "" |
| 108 | |
| 109 | template_name = "enable_monitoring_service.yaml" |
| 110 | fields["ansible_tag"] = o.__class__.__name__ + "_" + str(o.id) + "_step2" |
| 111 | |
| 112 | run_template(template_name, fields) |
| 113 | |
rdudyala | 996d70b | 2016-10-13 17:40:55 +0000 | [diff] [blame] | 114 | def map_delete_inputs(self, o): |
| 115 | fields = {"unique_id": o.id, |
| 116 | "delete": True} |
| 117 | return fields |
| 118 | |
| 119 | def delete_record(self, o): |
| 120 | pass |
| 121 | |