blob: 6cd51cba49ad5b21510e2fbb4fc11eb458d5ec58 [file] [log] [blame]
rdudyala996d70b2016-10-13 17:40:55 +00001import hashlib
2import os
3import socket
4import sys
5import base64
6import time
7import json
8#import threading
9import subprocess
10import random
11import tempfile
12#from sshtunnel import SSHTunnelForwarder
13from django.db.models import F, Q
14from xos.config import Config
15from synchronizers.base.syncstep import SyncStep
Sapan Bhatia7e9f4d32017-01-24 19:32:59 +010016from synchronizers.base.ansible_helper import run_template
17from synchronizers.base.ansible_helper import run_template_ssh
rdudyala996d70b2016-10-13 17:40:55 +000018from synchronizers.base.SyncInstanceUsingAnsible import SyncInstanceUsingAnsible
19from core.models import Service, Slice
20from services.monitoring.models import CeilometerService, OpenStackServiceMonitoringPublisher
21from xos.logger import Logger, logging
22
23parentdir = os.path.join(os.path.dirname(__file__),"..")
24sys.path.insert(0,parentdir)
25
26logger = Logger(level=logging.INFO)
27
28class 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 Vavilapallif2919002016-12-19 18:32:20 +0000102 body.update(start_url_dict)
rdudyala996d70b2016-10-13 17:40:55 +0000103 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
Sapan Bhatia02c783e2017-02-04 09:26:30 -0800112 run_template(template_name, fields, object=o)
rdudyala996d70b2016-10-13 17:40:55 +0000113
rdudyala996d70b2016-10-13 17:40:55 +0000114 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