Example #1
0
def _execute_shell_cmd(cmd, options, *args, **kwargs):
    """Execute a given shell command passing it
    given options (flags) and arguments.

    Takes optional keyword arguments:
    :param as_root:        Execute as root.
    :type as_root:         boolean

    :param timeout:        Number of seconds if specified,
                           default if not.
                           There is no timeout if set to None.
    :type timeout:         integer

    :raises:               class:`UnknownArgumentError` if passed unknown args.
    """

    exec_args = {}
    if kwargs.pop('as_root', False):
        exec_args['run_as_root'] = True
        exec_args['root_helper'] = 'sudo'

    if 'timeout' in kwargs:
        exec_args['timeout'] = kwargs.pop('timeout')

    if kwargs:
        raise UnknownArgumentError(_("Got unknown keyword args: %r") % kwargs)

    cmd_flags = _build_command_options(options)
    cmd_args = cmd_flags + list(args)
    utils.execute_with_timeout(cmd, *cmd_args, **exec_args)
Example #2
0
def _execute_service_command(service_candidates, command_key, **kwargs):
    """
    :param service_candidates        List of possible system service names.
    :type service_candidates         list

    :param command_key               One of the actions returned by
                                     'service_discovery'.
    :type command_key                string

    :param timeout:                  Number of seconds if specified,
                                     default if not.
                                     There is no timeout if set to None.
    :type timeout:                   integer

    :raises:          :class:`UnknownArgumentError` if passed unknown args.
    :raises:          :class:`UnprocessableEntity` if no candidate names given.
    :raises:          :class:`RuntimeError` if command not found.
    """

    exec_args = {}
    if 'timeout' in kwargs:
        exec_args['timeout'] = kwargs.pop('timeout')

    if kwargs:
        raise UnknownArgumentError(_("Got unknown keyword args: %r") % kwargs)

    if service_candidates:
        service = service_discovery(service_candidates)
        if command_key in service:
            utils.execute_with_timeout(service[command_key],
                                       shell=True,
                                       **exec_args)
        else:
            raise RuntimeError(
                _("Service control command not available: %s") % command_key)
    else:
        raise exception.UnprocessableEntity(
            _("Candidate service names not "
              "specified."))