Added some oslo modules
Added some oslo modules which are needed for generating config samples and for running os commands. Re-synced oslo from b7ad6ddab8b1d61bf4f52ccaa461a9d68809747b which is master. Change-Id: I6e35ba1f0df007876f4dc25e7e2bd553986bfa8d
This commit is contained in:
parent
826edb688f
commit
51bb147e47
|
@ -0,0 +1,307 @@
|
||||||
|
# Copyright 2012 SINA Corporation
|
||||||
|
# Copyright 2014 Cisco Systems, Inc.
|
||||||
|
# All Rights Reserved.
|
||||||
|
#
|
||||||
|
# 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.
|
||||||
|
#
|
||||||
|
|
||||||
|
"""Extracts OpenStack config option info from module(s)."""
|
||||||
|
|
||||||
|
from __future__ import print_function
|
||||||
|
|
||||||
|
import argparse
|
||||||
|
import imp
|
||||||
|
import os
|
||||||
|
import re
|
||||||
|
import socket
|
||||||
|
import sys
|
||||||
|
import textwrap
|
||||||
|
|
||||||
|
from oslo.config import cfg
|
||||||
|
import six
|
||||||
|
import stevedore.named
|
||||||
|
|
||||||
|
from ironic_python_agent.openstack.common import gettextutils
|
||||||
|
from ironic_python_agent.openstack.common import importutils
|
||||||
|
|
||||||
|
gettextutils.install('ironic_python_agent')
|
||||||
|
|
||||||
|
STROPT = "StrOpt"
|
||||||
|
BOOLOPT = "BoolOpt"
|
||||||
|
INTOPT = "IntOpt"
|
||||||
|
FLOATOPT = "FloatOpt"
|
||||||
|
LISTOPT = "ListOpt"
|
||||||
|
DICTOPT = "DictOpt"
|
||||||
|
MULTISTROPT = "MultiStrOpt"
|
||||||
|
|
||||||
|
OPT_TYPES = {
|
||||||
|
STROPT: 'string value',
|
||||||
|
BOOLOPT: 'boolean value',
|
||||||
|
INTOPT: 'integer value',
|
||||||
|
FLOATOPT: 'floating point value',
|
||||||
|
LISTOPT: 'list value',
|
||||||
|
DICTOPT: 'dict value',
|
||||||
|
MULTISTROPT: 'multi valued',
|
||||||
|
}
|
||||||
|
|
||||||
|
OPTION_REGEX = re.compile(r"(%s)" % "|".join([STROPT, BOOLOPT, INTOPT,
|
||||||
|
FLOATOPT, LISTOPT, DICTOPT,
|
||||||
|
MULTISTROPT]))
|
||||||
|
|
||||||
|
PY_EXT = ".py"
|
||||||
|
BASEDIR = os.path.abspath(os.path.join(os.path.dirname(__file__),
|
||||||
|
"../../../../"))
|
||||||
|
WORDWRAP_WIDTH = 60
|
||||||
|
|
||||||
|
|
||||||
|
def raise_extension_exception(extmanager, ep, err):
|
||||||
|
raise
|
||||||
|
|
||||||
|
|
||||||
|
def generate(argv):
|
||||||
|
parser = argparse.ArgumentParser(
|
||||||
|
description='generate sample configuration file',
|
||||||
|
)
|
||||||
|
parser.add_argument('-m', dest='modules', action='append')
|
||||||
|
parser.add_argument('-l', dest='libraries', action='append')
|
||||||
|
parser.add_argument('srcfiles', nargs='*')
|
||||||
|
parsed_args = parser.parse_args(argv)
|
||||||
|
|
||||||
|
mods_by_pkg = dict()
|
||||||
|
for filepath in parsed_args.srcfiles:
|
||||||
|
pkg_name = filepath.split(os.sep)[1]
|
||||||
|
mod_str = '.'.join(['.'.join(filepath.split(os.sep)[:-1]),
|
||||||
|
os.path.basename(filepath).split('.')[0]])
|
||||||
|
mods_by_pkg.setdefault(pkg_name, list()).append(mod_str)
|
||||||
|
# NOTE(lzyeval): place top level modules before packages
|
||||||
|
pkg_names = sorted(pkg for pkg in mods_by_pkg if pkg.endswith(PY_EXT))
|
||||||
|
ext_names = sorted(pkg for pkg in mods_by_pkg if pkg not in pkg_names)
|
||||||
|
pkg_names.extend(ext_names)
|
||||||
|
|
||||||
|
# opts_by_group is a mapping of group name to an options list
|
||||||
|
# The options list is a list of (module, options) tuples
|
||||||
|
opts_by_group = {'DEFAULT': []}
|
||||||
|
|
||||||
|
if parsed_args.modules:
|
||||||
|
for module_name in parsed_args.modules:
|
||||||
|
module = _import_module(module_name)
|
||||||
|
if module:
|
||||||
|
for group, opts in _list_opts(module):
|
||||||
|
opts_by_group.setdefault(group, []).append((module_name,
|
||||||
|
opts))
|
||||||
|
|
||||||
|
# Look for entry points defined in libraries (or applications) for
|
||||||
|
# option discovery, and include their return values in the output.
|
||||||
|
#
|
||||||
|
# Each entry point should be a function returning an iterable
|
||||||
|
# of pairs with the group name (or None for the default group)
|
||||||
|
# and the list of Opt instances for that group.
|
||||||
|
if parsed_args.libraries:
|
||||||
|
loader = stevedore.named.NamedExtensionManager(
|
||||||
|
'oslo.config.opts',
|
||||||
|
names=list(set(parsed_args.libraries)),
|
||||||
|
invoke_on_load=False,
|
||||||
|
on_load_failure_callback=raise_extension_exception
|
||||||
|
)
|
||||||
|
for ext in loader:
|
||||||
|
for group, opts in ext.plugin():
|
||||||
|
opt_list = opts_by_group.setdefault(group or 'DEFAULT', [])
|
||||||
|
opt_list.append((ext.name, opts))
|
||||||
|
|
||||||
|
for pkg_name in pkg_names:
|
||||||
|
mods = mods_by_pkg.get(pkg_name)
|
||||||
|
mods.sort()
|
||||||
|
for mod_str in mods:
|
||||||
|
if mod_str.endswith('.__init__'):
|
||||||
|
mod_str = mod_str[:mod_str.rfind(".")]
|
||||||
|
|
||||||
|
mod_obj = _import_module(mod_str)
|
||||||
|
if not mod_obj:
|
||||||
|
raise RuntimeError("Unable to import module %s" % mod_str)
|
||||||
|
|
||||||
|
for group, opts in _list_opts(mod_obj):
|
||||||
|
opts_by_group.setdefault(group, []).append((mod_str, opts))
|
||||||
|
|
||||||
|
print_group_opts('DEFAULT', opts_by_group.pop('DEFAULT', []))
|
||||||
|
for group in sorted(opts_by_group.keys()):
|
||||||
|
print_group_opts(group, opts_by_group[group])
|
||||||
|
|
||||||
|
|
||||||
|
def _import_module(mod_str):
|
||||||
|
try:
|
||||||
|
if mod_str.startswith('bin.'):
|
||||||
|
imp.load_source(mod_str[4:], os.path.join('bin', mod_str[4:]))
|
||||||
|
return sys.modules[mod_str[4:]]
|
||||||
|
else:
|
||||||
|
return importutils.import_module(mod_str)
|
||||||
|
except Exception as e:
|
||||||
|
sys.stderr.write("Error importing module %s: %s\n" % (mod_str, str(e)))
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def _is_in_group(opt, group):
|
||||||
|
"Check if opt is in group."
|
||||||
|
for value in group._opts.values():
|
||||||
|
# NOTE(llu): Temporary workaround for bug #1262148, wait until
|
||||||
|
# newly released oslo.config support '==' operator.
|
||||||
|
if not(value['opt'] != opt):
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
def _guess_groups(opt, mod_obj):
|
||||||
|
# is it in the DEFAULT group?
|
||||||
|
if _is_in_group(opt, cfg.CONF):
|
||||||
|
return 'DEFAULT'
|
||||||
|
|
||||||
|
# what other groups is it in?
|
||||||
|
for value in cfg.CONF.values():
|
||||||
|
if isinstance(value, cfg.CONF.GroupAttr):
|
||||||
|
if _is_in_group(opt, value._group):
|
||||||
|
return value._group.name
|
||||||
|
|
||||||
|
raise RuntimeError(
|
||||||
|
"Unable to find group for option %s, "
|
||||||
|
"maybe it's defined twice in the same group?"
|
||||||
|
% opt.name
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def _list_opts(obj):
|
||||||
|
def is_opt(o):
|
||||||
|
return (isinstance(o, cfg.Opt) and
|
||||||
|
not isinstance(o, cfg.SubCommandOpt))
|
||||||
|
|
||||||
|
opts = list()
|
||||||
|
for attr_str in dir(obj):
|
||||||
|
attr_obj = getattr(obj, attr_str)
|
||||||
|
if is_opt(attr_obj):
|
||||||
|
opts.append(attr_obj)
|
||||||
|
elif (isinstance(attr_obj, list) and
|
||||||
|
all(map(lambda x: is_opt(x), attr_obj))):
|
||||||
|
opts.extend(attr_obj)
|
||||||
|
|
||||||
|
ret = {}
|
||||||
|
for opt in opts:
|
||||||
|
ret.setdefault(_guess_groups(opt, obj), []).append(opt)
|
||||||
|
return ret.items()
|
||||||
|
|
||||||
|
|
||||||
|
def print_group_opts(group, opts_by_module):
|
||||||
|
print("[%s]" % group)
|
||||||
|
print('')
|
||||||
|
for mod, opts in opts_by_module:
|
||||||
|
print('#')
|
||||||
|
print('# Options defined in %s' % mod)
|
||||||
|
print('#')
|
||||||
|
print('')
|
||||||
|
for opt in opts:
|
||||||
|
_print_opt(opt)
|
||||||
|
print('')
|
||||||
|
|
||||||
|
|
||||||
|
def _get_my_ip():
|
||||||
|
try:
|
||||||
|
csock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||||
|
csock.connect(('8.8.8.8', 80))
|
||||||
|
(addr, port) = csock.getsockname()
|
||||||
|
csock.close()
|
||||||
|
return addr
|
||||||
|
except socket.error:
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def _sanitize_default(name, value):
|
||||||
|
"""Set up a reasonably sensible default for pybasedir, my_ip and host."""
|
||||||
|
if value.startswith(sys.prefix):
|
||||||
|
# NOTE(jd) Don't use os.path.join, because it is likely to think the
|
||||||
|
# second part is an absolute pathname and therefore drop the first
|
||||||
|
# part.
|
||||||
|
value = os.path.normpath("/usr/" + value[len(sys.prefix):])
|
||||||
|
elif value.startswith(BASEDIR):
|
||||||
|
return value.replace(BASEDIR, '/usr/lib/python/site-packages')
|
||||||
|
elif BASEDIR in value:
|
||||||
|
return value.replace(BASEDIR, '')
|
||||||
|
elif value == _get_my_ip():
|
||||||
|
return '10.0.0.1'
|
||||||
|
elif value in (socket.gethostname(), socket.getfqdn()) and 'host' in name:
|
||||||
|
return 'ironic_python_agent'
|
||||||
|
elif value.strip() != value:
|
||||||
|
return '"%s"' % value
|
||||||
|
return value
|
||||||
|
|
||||||
|
|
||||||
|
def _print_opt(opt):
|
||||||
|
opt_name, opt_default, opt_help = opt.dest, opt.default, opt.help
|
||||||
|
if not opt_help:
|
||||||
|
sys.stderr.write('WARNING: "%s" is missing help string.\n' % opt_name)
|
||||||
|
opt_help = ""
|
||||||
|
opt_type = None
|
||||||
|
try:
|
||||||
|
opt_type = OPTION_REGEX.search(str(type(opt))).group(0)
|
||||||
|
except (ValueError, AttributeError) as err:
|
||||||
|
sys.stderr.write("%s\n" % str(err))
|
||||||
|
sys.exit(1)
|
||||||
|
opt_help = u'%s (%s)' % (opt_help,
|
||||||
|
OPT_TYPES[opt_type])
|
||||||
|
print('#', "\n# ".join(textwrap.wrap(opt_help, WORDWRAP_WIDTH)))
|
||||||
|
if opt.deprecated_opts:
|
||||||
|
for deprecated_opt in opt.deprecated_opts:
|
||||||
|
if deprecated_opt.name:
|
||||||
|
deprecated_group = (deprecated_opt.group if
|
||||||
|
deprecated_opt.group else "DEFAULT")
|
||||||
|
print('# Deprecated group/name - [%s]/%s' %
|
||||||
|
(deprecated_group,
|
||||||
|
deprecated_opt.name))
|
||||||
|
try:
|
||||||
|
if opt_default is None:
|
||||||
|
print('#%s=<None>' % opt_name)
|
||||||
|
elif opt_type == STROPT:
|
||||||
|
assert(isinstance(opt_default, six.string_types))
|
||||||
|
print('#%s=%s' % (opt_name, _sanitize_default(opt_name,
|
||||||
|
opt_default)))
|
||||||
|
elif opt_type == BOOLOPT:
|
||||||
|
assert(isinstance(opt_default, bool))
|
||||||
|
print('#%s=%s' % (opt_name, str(opt_default).lower()))
|
||||||
|
elif opt_type == INTOPT:
|
||||||
|
assert(isinstance(opt_default, int) and
|
||||||
|
not isinstance(opt_default, bool))
|
||||||
|
print('#%s=%s' % (opt_name, opt_default))
|
||||||
|
elif opt_type == FLOATOPT:
|
||||||
|
assert(isinstance(opt_default, float))
|
||||||
|
print('#%s=%s' % (opt_name, opt_default))
|
||||||
|
elif opt_type == LISTOPT:
|
||||||
|
assert(isinstance(opt_default, list))
|
||||||
|
print('#%s=%s' % (opt_name, ','.join(opt_default)))
|
||||||
|
elif opt_type == DICTOPT:
|
||||||
|
assert(isinstance(opt_default, dict))
|
||||||
|
opt_default_strlist = [str(key) + ':' + str(value)
|
||||||
|
for (key, value) in opt_default.items()]
|
||||||
|
print('#%s=%s' % (opt_name, ','.join(opt_default_strlist)))
|
||||||
|
elif opt_type == MULTISTROPT:
|
||||||
|
assert(isinstance(opt_default, list))
|
||||||
|
if not opt_default:
|
||||||
|
opt_default = ['']
|
||||||
|
for default in opt_default:
|
||||||
|
print('#%s=%s' % (opt_name, default))
|
||||||
|
print('')
|
||||||
|
except Exception:
|
||||||
|
sys.stderr.write('Error in option "%s"\n' % opt_name)
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
generate(sys.argv[1:])
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
|
@ -0,0 +1,267 @@
|
||||||
|
# Copyright 2011 OpenStack Foundation.
|
||||||
|
# All Rights Reserved.
|
||||||
|
#
|
||||||
|
# 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.
|
||||||
|
|
||||||
|
"""
|
||||||
|
System-level utilities and helper functions.
|
||||||
|
"""
|
||||||
|
|
||||||
|
import errno
|
||||||
|
import logging as stdlib_logging
|
||||||
|
import os
|
||||||
|
import random
|
||||||
|
import shlex
|
||||||
|
import signal
|
||||||
|
|
||||||
|
from eventlet.green import subprocess
|
||||||
|
from eventlet import greenthread
|
||||||
|
import six
|
||||||
|
|
||||||
|
from ironic_python_agent.openstack.common.gettextutils import _
|
||||||
|
from ironic_python_agent.openstack.common import log as logging
|
||||||
|
|
||||||
|
|
||||||
|
LOG = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
class InvalidArgumentError(Exception):
|
||||||
|
def __init__(self, message=None):
|
||||||
|
super(InvalidArgumentError, self).__init__(message)
|
||||||
|
|
||||||
|
|
||||||
|
class UnknownArgumentError(Exception):
|
||||||
|
def __init__(self, message=None):
|
||||||
|
super(UnknownArgumentError, self).__init__(message)
|
||||||
|
|
||||||
|
|
||||||
|
class ProcessExecutionError(Exception):
|
||||||
|
def __init__(self, stdout=None, stderr=None, exit_code=None, cmd=None,
|
||||||
|
description=None):
|
||||||
|
self.exit_code = exit_code
|
||||||
|
self.stderr = stderr
|
||||||
|
self.stdout = stdout
|
||||||
|
self.cmd = cmd
|
||||||
|
self.description = description
|
||||||
|
|
||||||
|
if description is None:
|
||||||
|
description = _("Unexpected error while running command.")
|
||||||
|
if exit_code is None:
|
||||||
|
exit_code = '-'
|
||||||
|
message = _('%(description)s\n'
|
||||||
|
'Command: %(cmd)s\n'
|
||||||
|
'Exit code: %(exit_code)s\n'
|
||||||
|
'Stdout: %(stdout)r\n'
|
||||||
|
'Stderr: %(stderr)r') % {'description': description,
|
||||||
|
'cmd': cmd,
|
||||||
|
'exit_code': exit_code,
|
||||||
|
'stdout': stdout,
|
||||||
|
'stderr': stderr}
|
||||||
|
super(ProcessExecutionError, self).__init__(message)
|
||||||
|
|
||||||
|
|
||||||
|
class NoRootWrapSpecified(Exception):
|
||||||
|
def __init__(self, message=None):
|
||||||
|
super(NoRootWrapSpecified, self).__init__(message)
|
||||||
|
|
||||||
|
|
||||||
|
def _subprocess_setup():
|
||||||
|
# Python installs a SIGPIPE handler by default. This is usually not what
|
||||||
|
# non-Python subprocesses expect.
|
||||||
|
signal.signal(signal.SIGPIPE, signal.SIG_DFL)
|
||||||
|
|
||||||
|
|
||||||
|
def execute(*cmd, **kwargs):
|
||||||
|
"""Helper method to shell out and execute a command through subprocess.
|
||||||
|
|
||||||
|
Allows optional retry.
|
||||||
|
|
||||||
|
:param cmd: Passed to subprocess.Popen.
|
||||||
|
:type cmd: string
|
||||||
|
:param process_input: Send to opened process.
|
||||||
|
:type process_input: string
|
||||||
|
:param check_exit_code: Single bool, int, or list of allowed exit
|
||||||
|
codes. Defaults to [0]. Raise
|
||||||
|
:class:`ProcessExecutionError` unless
|
||||||
|
program exits with one of these code.
|
||||||
|
:type check_exit_code: boolean, int, or [int]
|
||||||
|
:param delay_on_retry: True | False. Defaults to True. If set to True,
|
||||||
|
wait a short amount of time before retrying.
|
||||||
|
:type delay_on_retry: boolean
|
||||||
|
:param attempts: How many times to retry cmd.
|
||||||
|
:type attempts: int
|
||||||
|
:param run_as_root: True | False. Defaults to False. If set to True,
|
||||||
|
the command is prefixed by the command specified
|
||||||
|
in the root_helper kwarg.
|
||||||
|
:type run_as_root: boolean
|
||||||
|
:param root_helper: command to prefix to commands called with
|
||||||
|
run_as_root=True
|
||||||
|
:type root_helper: string
|
||||||
|
:param shell: whether or not there should be a shell used to
|
||||||
|
execute this command. Defaults to false.
|
||||||
|
:type shell: boolean
|
||||||
|
:param loglevel: log level for execute commands.
|
||||||
|
:type loglevel: int. (Should be stdlib_logging.DEBUG or
|
||||||
|
stdlib_logging.INFO)
|
||||||
|
:returns: (stdout, stderr) from process execution
|
||||||
|
:raises: :class:`UnknownArgumentError` on
|
||||||
|
receiving unknown arguments
|
||||||
|
:raises: :class:`ProcessExecutionError`
|
||||||
|
"""
|
||||||
|
|
||||||
|
process_input = kwargs.pop('process_input', None)
|
||||||
|
check_exit_code = kwargs.pop('check_exit_code', [0])
|
||||||
|
ignore_exit_code = False
|
||||||
|
delay_on_retry = kwargs.pop('delay_on_retry', True)
|
||||||
|
attempts = kwargs.pop('attempts', 1)
|
||||||
|
run_as_root = kwargs.pop('run_as_root', False)
|
||||||
|
root_helper = kwargs.pop('root_helper', '')
|
||||||
|
shell = kwargs.pop('shell', False)
|
||||||
|
loglevel = kwargs.pop('loglevel', stdlib_logging.DEBUG)
|
||||||
|
|
||||||
|
if isinstance(check_exit_code, bool):
|
||||||
|
ignore_exit_code = not check_exit_code
|
||||||
|
check_exit_code = [0]
|
||||||
|
elif isinstance(check_exit_code, int):
|
||||||
|
check_exit_code = [check_exit_code]
|
||||||
|
|
||||||
|
if kwargs:
|
||||||
|
raise UnknownArgumentError(_('Got unknown keyword args '
|
||||||
|
'to utils.execute: %r') % kwargs)
|
||||||
|
|
||||||
|
if run_as_root and hasattr(os, 'geteuid') and os.geteuid() != 0:
|
||||||
|
if not root_helper:
|
||||||
|
raise NoRootWrapSpecified(
|
||||||
|
message=_('Command requested root, but did not '
|
||||||
|
'specify a root helper.'))
|
||||||
|
cmd = shlex.split(root_helper) + list(cmd)
|
||||||
|
|
||||||
|
cmd = map(str, cmd)
|
||||||
|
|
||||||
|
while attempts > 0:
|
||||||
|
attempts -= 1
|
||||||
|
try:
|
||||||
|
LOG.log(loglevel, 'Running cmd (subprocess): %s',
|
||||||
|
' '.join(cmd))
|
||||||
|
_PIPE = subprocess.PIPE # pylint: disable=E1101
|
||||||
|
|
||||||
|
if os.name == 'nt':
|
||||||
|
preexec_fn = None
|
||||||
|
close_fds = False
|
||||||
|
else:
|
||||||
|
preexec_fn = _subprocess_setup
|
||||||
|
close_fds = True
|
||||||
|
|
||||||
|
obj = subprocess.Popen(cmd,
|
||||||
|
stdin=_PIPE,
|
||||||
|
stdout=_PIPE,
|
||||||
|
stderr=_PIPE,
|
||||||
|
close_fds=close_fds,
|
||||||
|
preexec_fn=preexec_fn,
|
||||||
|
shell=shell)
|
||||||
|
result = None
|
||||||
|
for _i in six.moves.range(20):
|
||||||
|
# NOTE(russellb) 20 is an arbitrary number of retries to
|
||||||
|
# prevent any chance of looping forever here.
|
||||||
|
try:
|
||||||
|
if process_input is not None:
|
||||||
|
result = obj.communicate(process_input)
|
||||||
|
else:
|
||||||
|
result = obj.communicate()
|
||||||
|
except OSError as e:
|
||||||
|
if e.errno in (errno.EAGAIN, errno.EINTR):
|
||||||
|
continue
|
||||||
|
raise
|
||||||
|
break
|
||||||
|
obj.stdin.close() # pylint: disable=E1101
|
||||||
|
_returncode = obj.returncode # pylint: disable=E1101
|
||||||
|
LOG.log(loglevel, 'Result was %s' % _returncode)
|
||||||
|
if not ignore_exit_code and _returncode not in check_exit_code:
|
||||||
|
(stdout, stderr) = result
|
||||||
|
raise ProcessExecutionError(exit_code=_returncode,
|
||||||
|
stdout=stdout,
|
||||||
|
stderr=stderr,
|
||||||
|
cmd=' '.join(cmd))
|
||||||
|
return result
|
||||||
|
except ProcessExecutionError:
|
||||||
|
if not attempts:
|
||||||
|
raise
|
||||||
|
else:
|
||||||
|
LOG.log(loglevel, '%r failed. Retrying.', cmd)
|
||||||
|
if delay_on_retry:
|
||||||
|
greenthread.sleep(random.randint(20, 200) / 100.0)
|
||||||
|
finally:
|
||||||
|
# NOTE(termie): this appears to be necessary to let the subprocess
|
||||||
|
# call clean something up in between calls, without
|
||||||
|
# it two execute calls in a row hangs the second one
|
||||||
|
greenthread.sleep(0)
|
||||||
|
|
||||||
|
|
||||||
|
def trycmd(*args, **kwargs):
|
||||||
|
"""A wrapper around execute() to more easily handle warnings and errors.
|
||||||
|
|
||||||
|
Returns an (out, err) tuple of strings containing the output of
|
||||||
|
the command's stdout and stderr. If 'err' is not empty then the
|
||||||
|
command can be considered to have failed.
|
||||||
|
|
||||||
|
:discard_warnings True | False. Defaults to False. If set to True,
|
||||||
|
then for succeeding commands, stderr is cleared
|
||||||
|
|
||||||
|
"""
|
||||||
|
discard_warnings = kwargs.pop('discard_warnings', False)
|
||||||
|
|
||||||
|
try:
|
||||||
|
out, err = execute(*args, **kwargs)
|
||||||
|
failed = False
|
||||||
|
except ProcessExecutionError as exn:
|
||||||
|
out, err = '', str(exn)
|
||||||
|
failed = True
|
||||||
|
|
||||||
|
if not failed and discard_warnings and err:
|
||||||
|
# Handle commands that output to stderr but otherwise succeed
|
||||||
|
err = ''
|
||||||
|
|
||||||
|
return out, err
|
||||||
|
|
||||||
|
|
||||||
|
def ssh_execute(ssh, cmd, process_input=None,
|
||||||
|
addl_env=None, check_exit_code=True):
|
||||||
|
LOG.debug('Running cmd (SSH): %s', cmd)
|
||||||
|
if addl_env:
|
||||||
|
raise InvalidArgumentError(_('Environment not supported over SSH'))
|
||||||
|
|
||||||
|
if process_input:
|
||||||
|
# This is (probably) fixable if we need it...
|
||||||
|
raise InvalidArgumentError(_('process_input not supported over SSH'))
|
||||||
|
|
||||||
|
stdin_stream, stdout_stream, stderr_stream = ssh.exec_command(cmd)
|
||||||
|
channel = stdout_stream.channel
|
||||||
|
|
||||||
|
# NOTE(justinsb): This seems suspicious...
|
||||||
|
# ...other SSH clients have buffering issues with this approach
|
||||||
|
stdout = stdout_stream.read()
|
||||||
|
stderr = stderr_stream.read()
|
||||||
|
stdin_stream.close()
|
||||||
|
|
||||||
|
exit_status = channel.recv_exit_status()
|
||||||
|
|
||||||
|
# exit_status == -1 if no exit code was returned
|
||||||
|
if exit_status != -1:
|
||||||
|
LOG.debug('Result was %s' % exit_status)
|
||||||
|
if check_exit_code and exit_status != 0:
|
||||||
|
raise ProcessExecutionError(exit_code=exit_status,
|
||||||
|
stdout=stdout,
|
||||||
|
stderr=stderr,
|
||||||
|
cmd=cmd)
|
||||||
|
|
||||||
|
return (stdout, stderr)
|
|
@ -1,7 +1,10 @@
|
||||||
[DEFAULT]
|
[DEFAULT]
|
||||||
|
|
||||||
# The list of modules to copy from oslo-incubator
|
# The list of modules to copy from oslo-incubator
|
||||||
|
module=config.generator
|
||||||
|
module=gettextutils
|
||||||
module=log
|
module=log
|
||||||
module=loopingcall
|
module=loopingcall
|
||||||
|
module=processutils
|
||||||
# The base module to hold the copy of openstack.common
|
# The base module to hold the copy of openstack.common
|
||||||
base=ironic_python_agent
|
base=ironic_python_agent
|
||||||
|
|
Loading…
Reference in New Issue