Migrate to oslo.i18n

This replaces use of openstack.common.gettextutils with oslo.i18n.
It switches to using the finer grained message catalogs and classifies
log msgs that are currently translated into their respective catalog.

Change-Id: Ia8abe17e5b88ba7994d8dd29763375f3eeb57ed6
This commit is contained in:
Adam Gandelman 2015-06-16 12:28:45 -07:00
parent 1696da438a
commit dd401f7618
9 changed files with 67 additions and 33 deletions

24
akanda/rug/common/i18n.py Normal file
View File

@ -0,0 +1,24 @@
# Copyright 2014 Akanda, Inc.
#
# Author: Akanda, Inc.
#
# 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
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import oslo_i18n
_translators = oslo_i18n.TranslatorFactory(domain='akanda-rug')
_ = _translators.primary
_LI = _translators.log_info
_LW = _translators.log_warning
_LE = _translators.log_error

View File

@ -39,10 +39,10 @@ import netaddr
from oslo_config import cfg
from oslo_log import log as logging
from akanda.rug.common.i18n import _, _LE, _LW
from akanda.rug.common.linux import ip_lib
from akanda.rug.common.linux import ovs_lib
from akanda.rug.common.linux import utils
from akanda.rug.openstack.common.gettextutils import _
LOG = logging.getLogger(__name__)
@ -200,7 +200,7 @@ class OVSInterfaceDriver(LinuxInterfaceDriver):
if self.conf.ovs_use_veth:
root_dev.link.set_up()
else:
LOG.warn(_("Device %s already exists"), device_name)
LOG.warn(_LW("Device %s already exists"), device_name)
def unplug(self, device_name, bridge=None, namespace=None, prefix=None):
"""Unplug the interface."""
@ -220,8 +220,7 @@ class OVSInterfaceDriver(LinuxInterfaceDriver):
device.link.delete()
LOG.debug(_("Unplugged interface '%s'"), device_name)
except RuntimeError:
LOG.error(_("Failed unplugging interface '%s'"),
device_name)
LOG.exception(_LE("Failed unplugging interface '%s'"), device_name)
class BridgeInterfaceDriver(LinuxInterfaceDriver):
@ -257,7 +256,7 @@ class BridgeInterfaceDriver(LinuxInterfaceDriver):
ns_veth.link.set_up()
else:
LOG.warn(_("Device %s already exists"), device_name)
LOG.warn(_LW("Device %s already exists"), device_name)
def unplug(self, device_name, bridge=None, namespace=None, prefix=None):
"""Unplug the interface."""
@ -266,5 +265,5 @@ class BridgeInterfaceDriver(LinuxInterfaceDriver):
device.link.delete()
LOG.debug(_("Unplugged interface '%s'"), device_name)
except RuntimeError:
LOG.error(_("Failed unplugging interface '%s'"),
device_name)
LOG.exception(_LE(
"Failed unplugging interface '%s'"), device_name)

View File

@ -34,11 +34,14 @@
import netaddr
from akanda.rug.common.linux import utils
from akanda.rug.openstack.common.gettextutils import _
from akanda.rug.common.i18n import _
from oslo_log import log
LOOPBACK_DEVNAME = 'lo'
LOG = log.getLogger(__name__)
class SubProcessBase(object):
def __init__(self, root_helper=None, namespace=None):
@ -413,9 +416,13 @@ class IpNetnsCommand(IpCommandBase):
def execute(self, cmds, addl_env={}, check_exit_code=True):
if not self._parent.root_helper:
raise Exception('Sudo is required to run this command')
m = _('sudo is required to run this command')
LOG.error(m)
raise Exception(m)
elif not self._parent.namespace:
raise Exception(_('No namespace defined for parent'))
m = _('No namespace defined for parent')
LOG.error(m)
raise Exception(m)
else:
return utils.execute(
['%s=%s' % pair for pair in addl_env.items()] +

View File

@ -38,7 +38,7 @@
import re
from akanda.rug.common.linux import utils
from akanda.rug.openstack.common.gettextutils import _
from akanda.rug.common.i18n import _, _LE, _LW
from oslo_log import log as logging
@ -81,8 +81,9 @@ class OVSBridge:
try:
return utils.execute(full_args, root_helper=self.root_helper)
except Exception, e:
LOG.error(_("Unable to execute %(cmd)s. Exception: %(exception)s"),
{'cmd': full_args, 'exception': e})
LOG.error(_LE(
"Unable to execute %(cmd)s. Exception: %(exception)s"),
{'cmd': full_args, 'exception': e})
def reset_bridge(self):
self.run_vsctl(["--", "--if-exists", "del-br", self.br_name])
@ -110,8 +111,9 @@ class OVSBridge:
try:
return utils.execute(full_args, root_helper=self.root_helper)
except Exception, e:
LOG.error(_("Unable to execute %(cmd)s. Exception: %(exception)s"),
{'cmd': full_args, 'exception': e})
LOG.error(_LE(
"Unable to execute %(cmd)s. Exception: %(exception)s"),
{'cmd': full_args, 'exception': e})
def count_flows(self):
flow_list = self.run_ofctl("dump-flows", []).split("\n")[1:]
@ -232,8 +234,9 @@ class OVSBridge:
try:
return utils.execute(args, root_helper=self.root_helper).strip()
except Exception, e:
LOG.error(_("Unable to execute %(cmd)s. Exception: %(exception)s"),
{'cmd': args, 'exception': e})
LOG.error(_LE(
"Unable to execute %(cmd)s. Exception: %(exception)s"),
{'cmd': args, 'exception': e})
# returns a VIF object for each VIF port
def get_vif_ports(self):
@ -287,7 +290,7 @@ class OVSBridge:
ofport = int(match.group('ofport'))
return VifPort(port_name, ofport, vif_id, vif_mac, self)
except Exception, e:
LOG.info(_("Unable to parse regex results. Exception: %s"), e)
LOG.warning(_LW("Unable to parse regex results. Exception: %s"), e)
return
def delete_ports(self, all_ports=False):
@ -304,9 +307,8 @@ def get_bridge_for_iface(root_helper, iface):
args = ["ovs-vsctl", "--timeout=2", "iface-to-br", iface]
try:
return utils.execute(args, root_helper=root_helper).strip()
except Exception, e:
LOG.exception(_("Interface %(iface)s not found. Exception: %(e)s"),
locals())
except Exception:
LOG.exception(_LE("Interface %s not found."), iface)
return None
@ -314,6 +316,6 @@ def get_bridges(root_helper):
args = ["ovs-vsctl", "--timeout=2", "list-br"]
try:
return utils.execute(args, root_helper=root_helper).strip().split("\n")
except Exception, e:
LOG.exception(_("Unable to retrieve bridges. Exception: %s"), e)
except Exception:
LOG.exception(_LE("Unable to retrieve bridges."))
return []

View File

@ -44,7 +44,7 @@ import tempfile
from eventlet.green import subprocess
from akanda.rug.openstack.common.gettextutils import _
from akanda.rug.common.i18n import _
from oslo_log import log as logging

View File

@ -26,6 +26,7 @@ import threading
from oslo_config import cfg
from oslo_log import log
from akanda.rug.common.i18n import _, _LE, _LI
from akanda.rug.common import config as ak_cfg
from akanda.rug import daemon
from akanda.rug import health
@ -76,14 +77,15 @@ def shuffle_notifications(notification_queue, sched):
# meantime waiting for a better solution.
pass
except KeyboardInterrupt:
LOG.info('got Ctrl-C')
LOG.info(_LI('got Ctrl-C'))
break
except:
LOG.exception('unhandled exception processing message')
LOG.exception(_LE('unhandled exception processing message'))
def main(argv=sys.argv[1:]):
# Change the process and thread name so the logs are cleaner.
p = multiprocessing.current_process()
p.name = 'pmain'
t = threading.current_thread()
@ -175,11 +177,11 @@ def main(argv=sys.argv[1:]):
shuffle_notifications(notification_queue, sched)
finally:
# Terminate the scheduler and its workers
LOG.info('stopping processing')
LOG.info(_LI('stopping processing'))
sched.stop()
# Terminate the listening process
LOG.debug('stopping %s', notification_proc.name)
LOG.debug(_('stopping %s'), notification_proc.name)
notification_proc.terminate()
LOG.debug('stopping %s', metadata_proc.name)
LOG.debug(_('stopping %s'), metadata_proc.name)
metadata_proc.terminate()
LOG.info('exiting')
LOG.info(_LI('exiting'))

View File

@ -24,7 +24,6 @@ from oslo_config import cfg
from oslo_log import log
from akanda.rug import manager
from akanda.rug.openstack.common.gettextutils import _
from akanda.rug.openstack.common.rpc import service as rpc_service
from akanda.rug.openstack.common import service
@ -36,7 +35,7 @@ cfg.CONF.register_opts([
help='seconds between periodic task runs (ie health check)'),
cfg.StrOpt('host',
default=socket.getfqdn(),
help=_("The hostname Neutron is running on")),
help="The hostname Neutron is running on"),
])

View File

@ -246,7 +246,7 @@ class TestBridgeInterfaceDriver(TestBase):
br = interface.BridgeInterfaceDriver(self.conf)
br.unplug('tap0')
[mock.call(), mock.call('tap0', 'sudo'), mock.call().link.delete()]
self.assertEqual(log.error.call_count, 1)
self.assertEqual(log.exception.call_count, 1)
def test_unplug(self):
self.device_exists.return_value = True

View File

@ -3,6 +3,7 @@ httplib2
python-neutronclient>=2.3.0,<3
oslo.config>=1.2.0
oslo.context>=0.2.0,<0.3.0
oslo.i18n>=1.5.0
oslo.log>=1.2.0
oslo.messaging>=1.8.0
oslo.serialization>=1.4.0,<1.5.0