Sync with oslo-incubator

This commit is contained in:
tengqm 2015-02-17 08:59:49 +08:00
parent 948bb4e8db
commit a56be33b3b
9 changed files with 119 additions and 94 deletions

View File

@ -16,25 +16,30 @@ See http://docs.openstack.org/developer/oslo.i18n/usage.html
""" """
import oslo_i18n try:
import oslo_i18n
# NOTE(dhellmann): This reference to o-s-l-o will be replaced by the
# application name when this module is synced into the separate
# repository. It is OK to have more than one translation function
# using the same domain, since there will still only be one message
# catalog.
_translators = oslo_i18n.TranslatorFactory(domain='oslo')
# NOTE(dhellmann): This reference to o-s-l-o will be replaced by the # The primary translation function using the well-known name "_"
# application name when this module is synced into the separate _ = _translators.primary
# repository. It is OK to have more than one translation function
# using the same domain, since there will still only be one message
# catalog.
_translators = oslo_i18n.TranslatorFactory(domain='senlin')
# The primary translation function using the well-known name "_" # Translators for log levels.
_ = _translators.primary #
# The abbreviated names are meant to reflect the usual use of a short
# Translators for log levels. # name like '_'. The "L" is for "log" and the other letter comes from
# # the level.
# The abbreviated names are meant to reflect the usual use of a short _LI = _translators.log_info
# name like '_'. The "L" is for "log" and the other letter comes from _LW = _translators.log_warning
# the level. _LE = _translators.log_error
_LI = _translators.log_info _LC = _translators.log_critical
_LW = _translators.log_warning except ImportError:
_LE = _translators.log_error # NOTE(dims): Support for cases where a project wants to use
_LC = _translators.log_critical # code from oslo-incubator, but is not ready to be internationalized
# (like tempest)
_ = _LI = _LW = _LE = _LC = lambda x: x

View File

@ -16,21 +16,21 @@
from __future__ import print_function from __future__ import print_function
import copy
import errno import errno
import gc import gc
import logging
import os import os
import pprint import pprint
import socket import socket
import sys import sys
import traceback import traceback
import eventlet
import eventlet.backdoor import eventlet.backdoor
import greenlet import greenlet
from oslo_config import cfg from oslo_config import cfg
from senlin.common.i18n import _LI from senlin.common._i18n import _LI
from senlin.openstack.common import log as logging
help_for_backdoor_port = ( help_for_backdoor_port = (
"Acceptable values are 0, <port>, and <start>:<end>, where 0 results " "Acceptable values are 0, <port>, and <start>:<end>, where 0 results "
@ -49,6 +49,12 @@ CONF.register_opts(eventlet_backdoor_opts)
LOG = logging.getLogger(__name__) LOG = logging.getLogger(__name__)
def list_opts():
"""Entry point for oslo-config-generator.
"""
return [(None, copy.deepcopy(eventlet_backdoor_opts))]
class EventletBackdoorConfigValueError(Exception): class EventletBackdoorConfigValueError(Exception):
def __init__(self, port_range, help_msg, ex): def __init__(self, port_range, help_msg, ex):
msg = ('Invalid backdoor_port configuration %(range)s: %(ex)s. ' msg = ('Invalid backdoor_port configuration %(range)s: %(ex)s. '
@ -143,7 +149,3 @@ def initialize_if_enabled():
eventlet.spawn_n(eventlet.backdoor.backdoor_server, sock, eventlet.spawn_n(eventlet.backdoor.backdoor_server, sock,
locals=backdoor_locals) locals=backdoor_locals)
return port return port
def list_opts():
yield None, eventlet_backdoor_opts

View File

@ -15,25 +15,27 @@
import contextlib import contextlib
import errno import errno
import logging
import os import os
import stat
import tempfile import tempfile
from oslo_utils import excutils from oslo_utils import excutils
from senlin.openstack.common import log as logging
LOG = logging.getLogger(__name__) LOG = logging.getLogger(__name__)
_FILE_CACHE = {} _FILE_CACHE = {}
DEFAULT_MODE = stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO
def ensure_tree(path): def ensure_tree(path, mode=DEFAULT_MODE):
"""Create a directory (and any ancestor directories required) """Create a directory (and any ancestor directories required)
:param path: Directory to create :param path: Directory to create
:param mode: Directory creation permissions
""" """
try: try:
os.makedirs(path) os.makedirs(path, mode)
except OSError as exc: except OSError as exc:
if exc.errno == errno.EEXIST: if exc.errno == errno.EEXIST:
if not os.path.isdir(path): if not os.path.isdir(path):

View File

@ -15,14 +15,14 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import logging
import sys import sys
import time import time
from eventlet import event from eventlet import event
from eventlet import greenthread from eventlet import greenthread
from senlin.common.i18n import _LE, _LW from openstack.common._i18n import _LE, _LW
from senlin.openstack.common import log as logging
LOG = logging.getLogger(__name__) LOG = logging.getLogger(__name__)
@ -84,9 +84,9 @@ class FixedIntervalLoopingCall(LoopingCallBase):
break break
delay = end - start - interval delay = end - start - interval
if delay > 0: if delay > 0:
LOG.warn(_LW('task %(func_name)s run outlasted ' LOG.warn(_LW('task %(func_name)r run outlasted '
'interval by %(delay).2f sec'), 'interval by %(delay).2f sec'),
{'func_name': repr(self.f), 'delay': delay}) {'func_name': self.f, 'delay': delay})
greenthread.sleep(-delay if delay < 0 else 0) greenthread.sleep(-delay if delay < 0 else 0)
except LoopingCallDone as e: except LoopingCallDone as e:
self.stop() self.stop()
@ -127,9 +127,9 @@ class DynamicLoopingCall(LoopingCallBase):
if periodic_interval_max is not None: if periodic_interval_max is not None:
idle = min(idle, periodic_interval_max) idle = min(idle, periodic_interval_max)
LOG.debug('Dynamic looping call %(func_name)s sleeping ' LOG.debug('Dynamic looping call %(func_name)r sleeping '
'for %(idle).02f seconds', 'for %(idle).02f seconds',
{'func_name': repr(self.f), 'idle': idle}) {'func_name': self.f, 'idle': idle})
greenthread.sleep(idle) greenthread.sleep(idle)
except LoopingCallDone as e: except LoopingCallDone as e:
self.stop() self.stop()

View File

@ -1,25 +1,25 @@
# #
# Licensed under the Apache License, Version 2.0 (the "License"); you may # Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain # not use this file except in compliance with the License. You may obtain
# a copy of the License at # a copy of the License at
# #
# http://www.apache.org/licenses/LICENSE-2.0 # http://www.apache.org/licenses/LICENSE-2.0
# #
# Unless required by applicable law or agreed to in writing, software # Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import copy import copy
import logging
import random import random
import time import time
from oslo_config import cfg from oslo_config import cfg
import six import six
from senlin.openstack.common._i18n import _, _LE, _LI from openstack.common._i18n import _, _LE, _LI
from senlin.openstack.common import log as logging
periodic_opts = [ periodic_opts = [
@ -55,14 +55,15 @@ def periodic_task(*args, **kwargs):
interval of 60 seconds. interval of 60 seconds.
2. With arguments: 2. With arguments:
@periodic_task(spacing=N [, run_immediately=[True|False]]) @periodic_task(spacing=N [, run_immediately=[True|False]]
[, name=[None|"string"])
this will be run on approximately every N seconds. If this number is this will be run on approximately every N seconds. If this number is
negative the periodic task will be disabled. If the run_immediately negative the periodic task will be disabled. If the run_immediately
argument is provided and has a value of 'True', the first run of the argument is provided and has a value of 'True', the first run of the
task will be shortly after task scheduler starts. If task will be shortly after task scheduler starts. If
run_immediately is omitted or set to 'False', the first time the run_immediately is omitted or set to 'False', the first time the
task runs will be approximately N seconds after the task scheduler task runs will be approximately N seconds after the task scheduler
starts. starts. If name is not provided, __name__ of function is used.
""" """
def decorator(f): def decorator(f):
# Test for old style invocation # Test for old style invocation
@ -76,6 +77,7 @@ def periodic_task(*args, **kwargs):
f._periodic_enabled = False f._periodic_enabled = False
else: else:
f._periodic_enabled = kwargs.pop('enabled', True) f._periodic_enabled = kwargs.pop('enabled', True)
f._periodic_name = kwargs.pop('name', f.__name__)
# Control frequency # Control frequency
f._periodic_spacing = kwargs.pop('spacing', 0) f._periodic_spacing = kwargs.pop('spacing', 0)
@ -105,6 +107,36 @@ def periodic_task(*args, **kwargs):
class _PeriodicTasksMeta(type): class _PeriodicTasksMeta(type):
def _add_periodic_task(cls, task):
"""Add a periodic task to the list of periodic tasks.
The task should already be decorated by @periodic_task.
:return: whether task was actually enabled
"""
name = task._periodic_name
if task._periodic_spacing < 0:
LOG.info(_LI('Skipping periodic task %(task)s because '
'its interval is negative'),
{'task': name})
return False
if not task._periodic_enabled:
LOG.info(_LI('Skipping periodic task %(task)s because '
'it is disabled'),
{'task': name})
return False
# A periodic spacing of zero indicates that this task should
# be run on the default interval to avoid running too
# frequently.
if task._periodic_spacing == 0:
task._periodic_spacing = DEFAULT_INTERVAL
cls._periodic_tasks.append((name, task))
cls._periodic_spacing[name] = task._periodic_spacing
return True
def __init__(cls, names, bases, dict_): def __init__(cls, names, bases, dict_):
"""Metaclass that allows us to collect decorated periodic tasks.""" """Metaclass that allows us to collect decorated periodic tasks."""
super(_PeriodicTasksMeta, cls).__init__(names, bases, dict_) super(_PeriodicTasksMeta, cls).__init__(names, bases, dict_)
@ -125,28 +157,7 @@ class _PeriodicTasksMeta(type):
for value in cls.__dict__.values(): for value in cls.__dict__.values():
if getattr(value, '_periodic_task', False): if getattr(value, '_periodic_task', False):
task = value cls._add_periodic_task(value)
name = task.__name__
if task._periodic_spacing < 0:
LOG.info(_LI('Skipping periodic task %(task)s because '
'its interval is negative'),
{'task': name})
continue
if not task._periodic_enabled:
LOG.info(_LI('Skipping periodic task %(task)s because '
'it is disabled'),
{'task': name})
continue
# A periodic spacing of zero indicates that this task should
# be run on the default interval to avoid running too
# frequently.
if task._periodic_spacing == 0:
task._periodic_spacing = DEFAULT_INTERVAL
cls._periodic_tasks.append((name, task))
cls._periodic_spacing[name] = task._periodic_spacing
def _nearest_boundary(last_run, spacing): def _nearest_boundary(last_run, spacing):
@ -178,6 +189,15 @@ class PeriodicTasks(object):
for name, task in self._periodic_tasks: for name, task in self._periodic_tasks:
self._periodic_last_run[name] = task._periodic_last_run self._periodic_last_run[name] = task._periodic_last_run
def add_periodic_task(self, task):
"""Add a periodic task to the list of periodic tasks.
The task should already be decorated by @periodic_task.
"""
if self.__class__._add_periodic_task(task):
self._periodic_last_run[task._periodic_name] = (
task._periodic_last_run)
def run_periodic_tasks(self, context, raise_on_error=False): def run_periodic_tasks(self, context, raise_on_error=False):
"""Tasks to be run at a periodic interval.""" """Tasks to be run at a periodic interval."""
idle_for = DEFAULT_INTERVAL idle_for = DEFAULT_INTERVAL

View File

@ -91,6 +91,7 @@ as it allows particular rules to be explicitly disabled.
import abc import abc
import ast import ast
import copy import copy
import logging
import os import os
import re import re
@ -100,9 +101,8 @@ import six
import six.moves.urllib.parse as urlparse import six.moves.urllib.parse as urlparse
import six.moves.urllib.request as urlrequest import six.moves.urllib.request as urlrequest
from senlin.common.i18n import _, _LE, _LI from openstack.common import fileutils
from senlin.openstack.common import fileutils from openstack.common._i18n import _, _LE, _LI
from senlin.openstack.common import log as logging
policy_opts = [ policy_opts = [
@ -132,7 +132,7 @@ _checks = {}
def list_opts(): def list_opts():
"""Entry point for oslo.config-generator.""" """Entry point for oslo-config-generator."""
return [(None, copy.deepcopy(policy_opts))] return [(None, copy.deepcopy(policy_opts))]
@ -960,7 +960,3 @@ class GenericCheck(Check):
except KeyError: except KeyError:
return False return False
return match == six.text_type(leftval) return match == six.text_type(leftval)
def list_opts():
yield None, policy_opts

View File

@ -18,7 +18,7 @@
"""Generic Node base class for all workers that run on hosts.""" """Generic Node base class for all workers that run on hosts."""
import errno import errno
import logging as std_logging import logging
import os import os
import random import random
import signal import signal
@ -37,11 +37,10 @@ import eventlet
from eventlet import event from eventlet import event
from oslo_config import cfg from oslo_config import cfg
from senlin.common.i18n import _LE, _LI, _LW from openstack.common import eventlet_backdoor
from senlin.openstack.common import eventlet_backdoor from openstack.common._i18n import _LE, _LI, _LW
from senlin.openstack.common import log as logging from openstack.common import systemd
from senlin.openstack.common import systemd from openstack.common import threadgroup
from senlin.openstack.common import threadgroup
CONF = cfg.CONF CONF = cfg.CONF
@ -163,7 +162,7 @@ class ServiceLauncher(Launcher):
signo = 0 signo = 0
LOG.debug('Full set of CONF:') LOG.debug('Full set of CONF:')
CONF.log_opt_values(LOG, std_logging.DEBUG) CONF.log_opt_values(LOG, logging.DEBUG)
try: try:
if ready_callback: if ready_callback:
@ -377,7 +376,7 @@ class ProcessLauncher(object):
systemd.notify_once() systemd.notify_once()
LOG.debug('Full set of CONF:') LOG.debug('Full set of CONF:')
CONF.log_opt_values(LOG, std_logging.DEBUG) CONF.log_opt_values(LOG, logging.DEBUG)
try: try:
while True: while True:
@ -397,7 +396,7 @@ class ProcessLauncher(object):
self.running = True self.running = True
self.sigcaught = None self.sigcaught = None
except eventlet.greenlet.GreenletExit: except eventlet.greenlet.GreenletExit:
LOG.info(_LI("Wait called after thread killed. Cleaning up.")) LOG.info(_LI("Wait called after thread killed. Cleaning up."))
self.stop() self.stop()
@ -434,8 +433,8 @@ class Service(object):
def start(self): def start(self):
pass pass
def stop(self): def stop(self, graceful=False):
self.tg.stop() self.tg.stop(graceful)
self.tg.wait() self.tg.wait()
# Signal that service cleanup is done: # Signal that service cleanup is done:
if not self._done.ready(): if not self._done.ready():

View File

@ -16,12 +16,11 @@
Helper module for systemd service readiness notification. Helper module for systemd service readiness notification.
""" """
import logging
import os import os
import socket import socket
import sys import sys
from senlin.openstack.common import log as logging
LOG = logging.getLogger(__name__) LOG = logging.getLogger(__name__)

View File

@ -11,13 +11,13 @@
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import logging
import threading import threading
import eventlet import eventlet
from eventlet import greenpool from eventlet import greenpool
from senlin.openstack.common import log as logging from openstack.common import loopingcall
from senlin.openstack.common import loopingcall
LOG = logging.getLogger(__name__) LOG = logging.getLogger(__name__)
@ -96,6 +96,8 @@ class ThreadGroup(object):
continue continue
try: try:
x.stop() x.stop()
except eventlet.greenlet.GreenletExit:
pass
except Exception as ex: except Exception as ex:
LOG.exception(ex) LOG.exception(ex)