Matteo Scandolo | eb0d11c | 2017-08-08 13:05:26 -0700 | [diff] [blame] | 1 | |
| 2 | # Copyright 2017-present Open Networking Foundation |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
| 16 | |
rdudyala | 996d70b | 2016-10-13 17:40:55 +0000 | [diff] [blame] | 17 | import hashlib |
| 18 | import os |
| 19 | import socket |
| 20 | import sys |
| 21 | import base64 |
| 22 | import time |
| 23 | import json |
| 24 | #import threading |
| 25 | import subprocess |
| 26 | import random |
| 27 | import tempfile |
| 28 | #from sshtunnel import SSHTunnelForwarder |
| 29 | from django.db.models import F, Q |
| 30 | from xos.config import Config |
Murat Parlakisik | 638c65f | 2017-05-31 11:10:24 +0300 | [diff] [blame] | 31 | from synchronizers.new_base.syncstep import SyncStep |
| 32 | from synchronizers.new_base.ansible_helper import run_template |
| 33 | from synchronizers.new_base.ansible_helper import run_template_ssh |
| 34 | from synchronizers.new_base.SyncInstanceUsingAnsible import SyncInstanceUsingAnsible |
| 35 | from modelaccessor import * |
| 36 | #from core.models import Service, Slice |
| 37 | #from services.monitoring.models import CeilometerService, OpenStackServiceMonitoringPublisher |
rdudyala | 996d70b | 2016-10-13 17:40:55 +0000 | [diff] [blame] | 38 | from xos.logger import Logger, logging |
| 39 | |
| 40 | parentdir = os.path.join(os.path.dirname(__file__),"..") |
| 41 | sys.path.insert(0,parentdir) |
| 42 | |
| 43 | logger = Logger(level=logging.INFO) |
| 44 | |
| 45 | class SyncOpenStackMonitoringPublisher(SyncInstanceUsingAnsible): |
| 46 | provides=[OpenStackServiceMonitoringPublisher] |
| 47 | observes=OpenStackServiceMonitoringPublisher |
| 48 | requested_interval=0 |
| 49 | template_name = "sync_openstackmonitoringpublisher.yaml" |
| 50 | |
| 51 | def __init__(self, *args, **kwargs): |
| 52 | super(SyncOpenStackMonitoringPublisher, self).__init__(*args, **kwargs) |
| 53 | |
| 54 | def fetch_pending(self, deleted): |
| 55 | if (not deleted): |
| 56 | objs = OpenStackServiceMonitoringPublisher.get_tenant_objects().filter(Q(enacted__lt=F('updated')) | Q(enacted=None),Q(lazy_blocked=False)) |
| 57 | else: |
| 58 | objs = OpenStackServiceMonitoringPublisher.get_deleted_tenant_objects() |
| 59 | |
| 60 | return objs |
| 61 | |
| 62 | def sync_record(self, o): |
| 63 | logger.info("sync'ing object %s" % str(o),extra=o.tologdict()) |
| 64 | |
| 65 | self.prepare_record(o) |
| 66 | |
| 67 | ceilometer_services = CeilometerService.get_service_objects().filter(id=o.provider_service.id) |
| 68 | if not ceilometer_services: |
| 69 | raise "No associated Ceilometer service" |
| 70 | ceilometer_service = ceilometer_services[0] |
| 71 | service_instance = ceilometer_service.get_instance() |
| 72 | # sync only when the corresponding service instance is fully synced |
| 73 | if not service_instance: |
| 74 | self.defer_sync(o, "waiting on associated service instance") |
| 75 | return |
| 76 | if not service_instance.instance_name: |
| 77 | self.defer_sync(o, "waiting on associated service instance.instance_name") |
| 78 | return |
| 79 | |
| 80 | # Step1: Orchestrate UDP proxy agent on the compute node where monitoring service VM is spawned |
| 81 | |
| 82 | fields = { "hostname": ceilometer_service.ceilometer_rabbit_compute_node, |
| 83 | "baremetal_ssh": True, |
| 84 | "instance_name": "rootcontext", |
| 85 | "username": "root", |
| 86 | "container_name": None, |
| 87 | "rabbit_host": ceilometer_service.ceilometer_rabbit_host, |
| 88 | "rabbit_user": ceilometer_service.ceilometer_rabbit_user, |
| 89 | "rabbit_password": ceilometer_service.ceilometer_rabbit_password, |
| 90 | "listen_ip_addr": socket.gethostbyname(ceilometer_service.ceilometer_rabbit_compute_node) |
| 91 | } |
| 92 | |
| 93 | # If 'o' defines a 'sync_attributes' list, then we'll copy those |
| 94 | # attributes into the Ansible recipe's field list automatically. |
| 95 | if hasattr(o, "sync_attributes"): |
| 96 | for attribute_name in o.sync_attributes: |
| 97 | fields[attribute_name] = getattr(o, attribute_name) |
| 98 | |
| 99 | key_name = self.get_node_key(service_instance.node) |
| 100 | if not os.path.exists(key_name): |
| 101 | raise Exception("Node key %s does not exist" % key_name) |
| 102 | key = file(key_name).read() |
| 103 | fields["private_key"] = key |
| 104 | |
| 105 | template_name = "sync_openstackmonitoringpublisher.yaml" |
| 106 | fields["ansible_tag"] = o.__class__.__name__ + "_" + str(o.id) + "_step1" |
| 107 | |
| 108 | self.run_playbook(o, fields, template_name) |
| 109 | |
| 110 | # Step2: Orchestrate OpenStack publish agent |
| 111 | target_uri = "udp://" + ceilometer_service.ceilometer_rabbit_compute_node + ":4455" |
| 112 | fields = {} |
| 113 | agent_info = [] |
| 114 | if o.monitoring_agents: |
| 115 | for agent in o.monitoring_agents.all(): |
| 116 | body = {'target': target_uri} |
| 117 | if agent.start_url_json_data: |
| 118 | start_url_dict = json.loads(agent.start_url_json_data) |
Srikanth Vavilapalli | f291900 | 2016-12-19 18:32:20 +0000 | [diff] [blame] | 119 | body.update(start_url_dict) |
rdudyala | 996d70b | 2016-10-13 17:40:55 +0000 | [diff] [blame] | 120 | a = {'url': agent.start_url, 'body': json.dumps(body)} |
| 121 | agent_info.append(a) |
| 122 | |
| 123 | fields["agents"] = agent_info |
| 124 | #fields["private_key"] = "" |
| 125 | |
| 126 | template_name = "enable_monitoring_service.yaml" |
| 127 | fields["ansible_tag"] = o.__class__.__name__ + "_" + str(o.id) + "_step2" |
| 128 | |
Sapan Bhatia | 02c783e | 2017-02-04 09:26:30 -0800 | [diff] [blame] | 129 | run_template(template_name, fields, object=o) |
rdudyala | 996d70b | 2016-10-13 17:40:55 +0000 | [diff] [blame] | 130 | |
rdudyala | 996d70b | 2016-10-13 17:40:55 +0000 | [diff] [blame] | 131 | def map_delete_inputs(self, o): |
| 132 | fields = {"unique_id": o.id, |
| 133 | "delete": True} |
| 134 | return fields |
| 135 | |
| 136 | def delete_record(self, o): |
| 137 | pass |
| 138 | |