import zmq from openwlee.exchange import base from openwlee.openstack.common import cfg from openwlee.openstack.common import log as logging AGENT_TOPIC = "wlee.agent" LOG = logging.getLogger('openwlee.exchange.zeromq') """ WARNING : THIS CAANOT BE USED NOW! THIS IS BAD! """ class ZeromqReceiver(base.Receiver): def __init__(self, *args, **kargs): super(ZeromqReceiver, self).__init__(*args, **kargs) self.context = zmq.Context() self.socket = self.context.socket(zmq.SUB) def start_event_loop(self): LOG.info("ZeromqReceiver would connect to tcp://localhost:9779") self.socket.connect("tcp://localhost:9779") self.socket.setsockopt(zmq.SUBSCRIBE, '') while True: data = self.receive() self.handle_receive_data(data) def receive(self):
import time import eventlet import extras import logging as std_logging from openwlee.openstack.common import cfg from openwlee.openstack.common import eventlet_backdoor from openwlee.openstack.common.gettextutils import _ from openwlee.openstack.common import log as logging from openwlee.openstack.common import threadgroup rpc = extras.try_import('openwlee.openstack.common.rpc') CONF = cfg.CONF LOG = logging.getLogger(__name__) class Launcher(object): """Launch one or more services and wait for them to complete.""" def __init__(self): """Initialize the service launcher. :returns: None """ self._services = threadgroup.ThreadGroup('launcher') eventlet_backdoor.initialize_if_enabled() @staticmethod
http_exchange_opts = [ cfg.StrOpt('http_receiver_listen_host', default="0.0.0.0", help="HTTPReceiver listen bind host"), cfg.IntOpt('http_receiver_listen_port', default=9778, help="HTTPReceiver listen port"), cfg.StrOpt('http_receiver_address', default='localhost', help='HTTP receiver address.'), cfg.StrOpt('http_report_path', default='/report', help='The absolute path of http report address') ] CONF = cfg.CONF CONF.register_opts(http_exchange_opts) LOG = logging.getLogger('openwlee.exchange.http') class HTTPReceiver(base.Receiver): def __init__(self, *args, **kargs): super(HTTPReceiver, self).__init__(*args, **kargs) self.listener = eventlet.listen((cfg.CONF.http_receiver_listen_host, cfg.CONF.http_receiver_listen_port)) self.report_path = cfg.CONF.http_report_path def start_event_loop(self): def on_request(env, start_response): if env['PATH_INFO'] != self.report_path: start_response('404 Not Found', [('Content-Type', 'text/plain')]) return ['Not Found\r\n'] response_data = 'OK\r\n' start_response('200 OK', [('Content-Type', 'text/plain'),
from openwlee.openstack.common import log as logging LOG = logging.getLogger("openwlee.exchange") class Reporter(object): """ Reporter worked in wlee agent to send message to receiver which in wlee daemon. """ def __init__(self, *args, **kargs): pass def report(self, data): """ Block or not, guarantee or not, that's a question.""" raise NotImplementedError class Receiver(object): """ A receiver worked in wlee daemon to receive from wlee agent. """ def __init__(self, receive_handler, *args, **kargs): """ receive_handler : would be invoked when receive report data from agent. """ self.receive_handler = receive_handler def handle_receive_data(self, data): """ subclass would call this function when receive data. """ try: self.receive_handler(data) except Exception as e: LOG.error("Caught an exception when handle receive reporter's " "report data. Exception : %s" % str(e))
import eventlet import datetime from openwlee import task from openwlee import exception from openwlee import exchange from openwlee.openstack.common import log as logging from openwlee.openstack.common import timeutils from openwlee.openstack.common import cfg LOG = logging.getLogger('openwlee.wlee_agent') class WleeAgentManager: """ Wlee Agent : collect data from agent host and send to wlee daemon.""" def __init__(self): self.reporter = exchange.Reporter() self.hostname = cfg.CONF.host self._tasks = {} self._schedulers = [] self.green_pool = eventlet.GreenPool() self.running = False self._setup_tasks() def _setup_tasks(self): self.add_agent_task(task.common.HeartbeatTask, 5) self.add_agent_task(task.instance.InstancePerformanceTask, 10) self.add_agent_task(task.instance.InstanceStatisticTask, 10) def add_agent_task(self, task_cls, interval=0): """ Add a agent task that would run every interval seconds,
import webob import routes from openwlee import utils from openwlee.api import instance from openwlee.openstack.common import wsgi from openwlee.openstack.common import log as logging LOG = logging.getLogger('openwlee.api') class APIRouter(wsgi.Router): def __init__(self, conf, **global_conf): self.conf = conf mapper = self.setup_mapper() super(APIRouter, self).__init__(mapper) def setup_mapper(self): mapper = routes.Mapper() instance_resource = instance.create_resource(self.conf) mapper.connect(None, "/instance/{instance_id}/performance", controller=instance_resource, action="performance", conditions={'method': 'GET'}) mapper.connect(None, "/instance/{instance_id}/statistic", controller=instance_resource, action="statistic", conditions={'method': 'GET'}) return mapper class Versions(): def __init__(self, conf, **global_conf):
from openwlee import utils from openwlee import exchange from openwlee.db import base as db_base from openwlee.openstack.common import jsonutils from openwlee.openstack.common import timeutils from openwlee.openstack.common import log as logging LOG = logging.getLogger("openwlee.wlee_daemon") class EventDispatcher(): """ Event Dispatcher : dispatch message """ def __init__(self, manager): self.manager = manager self.db = manager.db def dispatch(self, host, type, data, datetime): if type == "instance_perf": self.handle_instance_perf_data(data, datetime) elif type == "instance_statistic": self.handle_instance_statistic_data(data, datetime) elif type == "heartbeat": self.handle_heartbeat(host, datetime) def handle_heartbeat(self, host, datetime): self.db.update_agent_status(host, datetime) def handle_instance_perf_data(self, inst_perf_list, datetime): for inst_perf in inst_perf_list:
import json import socket import signal import inspect import traceback from eventlet import greenthread from eventlet.green import subprocess from openwlee import exception from openwlee.openstack.common import cfg from openwlee.openstack.common import log as logging from openwlee.openstack.common.jsonutils import to_primitive CONF = cfg.CONF LOG = logging.getLogger('openwlee.utils') def methods_with_decorator(cls, decorator_name): """ A method that find all functions with the given decorator_name """ method_names = [] sourcelines = inspect.getsourcelines(cls)[0] for i,line in enumerate(sourcelines): line = line.strip() if line.split('(')[0].strip() == '@'+decorator_name: # leaving a bit out nextLine = sourcelines[i+1] name = nextLine.split('def')[1].split('(')[0].strip() method_names.append(name) return method_names
from openwlee.openstack.common import cfg from openwlee.openstack.common import timeutils from openwlee.openstack.common import log as logging libvirt_opts = [ cfg.BoolOpt('libvirt_nonblocking', default=False, help='Use a separated OS thread pool to realize non-blocking' ' libvirt calls'), ] #FIXME : It statistic the wrong data if we use nonblocking mode. #So it has a bug here. We should fix this later. cfg.CONF.register_opts(libvirt_opts) LOG = logging.getLogger("openwlee.tools.libvirt") libvirt = None class DomainInfo(): """ Get domain info from libvirt, just support simple statistic info. """ def __init__(self, dom, libvirt_con): self.__dom = dom self.__conn = libvirt_con @classmethod def __all_properties(cls): if not hasattr(cls, "_properties"): cls._properties = openwlee_utils.methods_with_decorator( DomainInfo, 'property')