Python oslo_concurrency.processutils.execute() Examples

The following are 30 code examples of oslo_concurrency.processutils.execute(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module oslo_concurrency.processutils , or try the search function .
Example #1
Source File: utils.py    From magnum with Apache License 2.0 6 votes vote down vote up
def execute(*cmd, **kwargs):
    """Convenience wrapper around oslo's execute() method.

    :param cmd: Passed to processutils.execute.
    :param use_standard_locale: True | False. Defaults to False. If set to
                                True, execute command with standard locale
                                added to environment variables.
    :returns: (stdout, stderr) from process execution
    :raises: UnknownArgumentError
    :raises: ProcessExecutionError
    """

    use_standard_locale = kwargs.pop('use_standard_locale', False)
    if use_standard_locale:
        env = kwargs.pop('env_variables', os.environ.copy())
        env['LC_ALL'] = 'C'
        kwargs['env_variables'] = env
    if kwargs.get('run_as_root') and 'root_helper' not in kwargs:
        kwargs['root_helper'] = _get_root_helper()
    result = processutils.execute(*cmd, **kwargs)
    LOG.debug('Execution completed, command line is "%s"',
              ' '.join(map(str, cmd)))
    LOG.debug('Command stdout is: "%s"', result[0])
    LOG.debug('Command stderr is: "%s"', result[1])
    return result 
Example #2
Source File: cinder.py    From glance_store with Apache License 2.0 6 votes vote down vote up
def temporary_chown(self, path):
        owner_uid = os.getuid()
        orig_uid = os.stat(path).st_uid

        if orig_uid != owner_uid:
            processutils.execute(
                'chown', owner_uid, path,
                run_as_root=True,
                root_helper=self.get_root_helper())
        try:
            yield
        finally:
            if orig_uid != owner_uid:
                processutils.execute(
                    'chown', orig_uid, path,
                    run_as_root=True,
                    root_helper=self.get_root_helper()) 
Example #3
Source File: fs_mount.py    From glance_store with Apache License 2.0 6 votes vote down vote up
def _real_umount(self, mountpoint, rootwrap_helper):
        # Unmount and delete a mountpoint.
        # Return mount state after umount (i.e. True means still mounted)
        LOG.debug('Unmounting %(mountpoint)s', {'mountpoint': mountpoint})

        try:
            processutils.execute('umount', mountpoint, run_as_root=True,
                                 attempts=3, delay_on_retry=True,
                                 root_helper=rootwrap_helper)
        except processutils.ProcessExecutionError as ex:
            LOG.error(_LE("Couldn't unmount %(mountpoint)s: %(reason)s"),
                      {'mountpoint': mountpoint, 'reason': ex})

        if not os.path.ismount(mountpoint):
            try:
                os.rmdir(mountpoint)
            except Exception as ex:
                LOG.error(_LE("Couldn't remove directory %(mountpoint)s: "
                              "%(reason)s"),
                          {'mountpoint': mountpoint,
                           'reason': ex})
            return False

        return True 
Example #4
Source File: utils.py    From os-net-config with Apache License 2.0 6 votes vote down vote up
def get_pci_address(ifname, noop):
    # TODO(skramaja): Validate if the given interface supports dpdk
    if not noop:
        try:
            out, err = processutils.execute('ethtool', '-i', ifname)
            if not err:
                for item in out.split('\n'):
                    if 'bus-info' in item:
                        return item.split(' ')[1]
        except processutils.ProcessExecutionError:
            # If ifname is already bound, then ethtool will not be able to
            # list the device, in which case, binding is already done, proceed
            # with scripts generation.
            return

    else:
        logger.info('Fetch the PCI address of the interface %s using '
                    'ethtool' % ifname) 
Example #5
Source File: __init__.py    From os-net-config with Apache License 2.0 6 votes vote down vote up
def ovs_appctl(self, action, *parameters):
        """Run 'ovs-appctl' with the specified action

         Its possible the command may fail due to timing if, for example,
         the command affects an interface and it the prior ifup command
         has not completed.  So retry the command and if a failures still
         occurs save the error for later handling.

         :param action: The ovs-appctl action.
         :param parameters: Parameters to pass to ovs-appctl.
         """
        msg = 'Running ovs-appctl %s %s' % (action, parameters)
        try:
            self.execute(msg, '/bin/ovs-appctl', action, *parameters,
                         delay_on_retry=True, attempts=5)
        except processutils.ProcessExecutionError as e:
            self.errors.append(e) 
Example #6
Source File: test_brick_lvm.py    From os-brick with Apache License 2.0 6 votes vote down vote up
def setUp(self):
        super(BrickLvmTestCase, self).setUp()
        if not hasattr(self, 'configuration'):
            self.configuration = mock.Mock()
            self.configuration.lvm_suppress_fd_warnings = False
        self.volume_group_name = 'fake-vg'

        # Stub processutils.execute for static methods
        self.mock_object(priv_rootwrap, 'execute',
                         self.fake_execute)
        self.vg = brick.LVM(
            self.volume_group_name,
            'sudo',
            create_vg=False,
            physical_volumes=None,
            lvm_type='default',
            executor=self.fake_execute,
            suppress_fd_warn=self.configuration.lvm_suppress_fd_warnings) 
Example #7
Source File: test_processutils.py    From oslo.concurrency with Apache License 2.0 6 votes vote down vote up
def test_execute_with_callback_and_errors(self, mock_comm):
        on_execute_callback = mock.Mock()
        on_completion_callback = mock.Mock()

        def fake_communicate(*args, timeout=None):
            raise IOError("Broken pipe")

        mock_comm.side_effect = fake_communicate

        self.assertRaises(IOError,
                          processutils.execute,
                          TRUE_UTILITY,
                          on_execute=on_execute_callback,
                          on_completion=on_completion_callback)
        self.assertEqual(1, on_execute_callback.call_count)
        self.assertEqual(1, on_completion_callback.call_count) 
Example #8
Source File: zaqar_notification_example_consumer.py    From manila with Apache License 2.0 6 votes vote down vote up
def mount_share(export_location, access_to):
    data = {
        'mount_point': os.path.join(CONF.zaqar.mount_dir,
                                    export_location.split('/')[-1]),
        'export_location': export_location,
    }
    if (rule_affects_me(access_to) and
            not is_share_mounted(data['mount_point'])):
        print_with_time(
            "Mounting '%(export_location)s' share to %(mount_point)s.")
        execute('sudo mkdir -p %(mount_point)s' % data)
        stdout, stderr = execute(
            'sudo mount.nfs %(export_location)s %(mount_point)s' % data)
        if stderr:
            print_with_time("Mount operation failed.")
        else:
            print_with_time("Mount operation went OK.") 
Example #9
Source File: test_image.py    From ec2-api with Apache License 2.0 6 votes vote down vote up
def test_deregister_image(self):
        self._setup_model()

        # normal flow
        resp = self.execute('DeregisterImage',
                            {'ImageId': fakes.ID_EC2_IMAGE_1})
        self.assertThat(resp, matchers.DictMatches({'return': True}))
        self.db_api.delete_item.assert_called_once_with(
            mock.ANY, fakes.ID_EC2_IMAGE_1)
        self.glance.images.delete.assert_called_once_with(
            fakes.ID_OS_IMAGE_1)

        # deregister image which failed on asynchronously creation
        self.glance.reset_mock()
        image_id = fakes.random_ec2_id('ami')
        self.add_mock_db_items({'id': image_id,
                                'os_id': None,
                                'state': 'failed'})
        resp = self.execute('DeregisterImage',
                            {'ImageId': image_id})
        self.assertThat(resp, matchers.DictMatches({'return': True}))
        self.db_api.delete_item.assert_called_with(mock.ANY, image_id)
        self.assertFalse(self.glance.images.delete.called) 
Example #10
Source File: utils.py    From manila with Apache License 2.0 6 votes vote down vote up
def _update_info_from_rpm(self):
        LOG.debug('Trying rpm command.')
        try:
            out, err = putils.execute("rpm", "-q", "--queryformat",
                                      "'%{version}\t%{release}\t%{vendor}'",
                                      self.PACKAGE_NAME)
            if not out:
                LOG.info('No rpm info found for %(pkg)s package.', {
                    'pkg': self.PACKAGE_NAME})
                return False
            parts = out.split()
            self._version = parts[0]
            self._release = parts[1]
            self._vendor = ' '.join(parts[2::])
            return True
        except Exception as e:
            LOG.info('Could not run rpm command: %(msg)s.', {
                'msg': e})
            return False

    # Ubuntu, Mirantis on Ubuntu. 
Example #11
Source File: linux_net.py    From networking-midonet with Apache License 2.0 6 votes vote down vote up
def create_tap_dev(dev, mac_address=None, multiqueue=False):
    if not device_exists(dev):
        try:
            # First, try with 'ip'
            cmd = ('ip', 'tuntap', 'add', dev, 'mode', 'tap')
            if multiqueue:
                cmd = cmd + ('multi_queue', )
            execute(*cmd, check_exit_code=[0, 2, 254])
        except processutils.ProcessExecutionError:
            if multiqueue:
                LOG.warning(
                    'Failed to create a tap device with ip tuntap. '
                    'tunctl does not support creation of multi-queue '
                    'enabled devices, skipping fallback.')
                raise

            # Second option: tunctl
            execute('tunctl', '-b', '-t', dev)
        if mac_address:
            execute('ip', 'link', 'set', dev, 'address', mac_address,
                    check_exit_code=[0, 2, 254])
        execute('ip', 'link', 'set', dev, 'up', check_exit_code=[0, 2, 254]) 
Example #12
Source File: impl_vsctl.py    From os-vif with Apache License 2.0 5 votes vote down vote up
def execute(self, check_error=False, log_errors=True):
        with Transaction(self.context, check_error=check_error,
                         log_errors=log_errors) as txn:
            txn.add(self)
        return self.result 
Example #13
Source File: test_processutils.py    From oslo.concurrency with Apache License 2.0 5 votes vote down vote up
def test_process_input_with_string(self):
        code = ';'.join(('import sys',
                         'print(len(sys.stdin.readlines()))'))
        args = [sys.executable, '-c', code]
        input = "\n".join(['foo', 'bar', 'baz'])
        stdout, stderr = processutils.execute(*args, process_input=input)
        self.assertEqual("3", stdout.rstrip()) 
Example #14
Source File: test_processutils.py    From oslo.concurrency with Apache License 2.0 5 votes vote down vote up
def test_unknown_kwargs_raises_error(self):
        self.assertRaises(processutils.UnknownArgumentError,
                          processutils.execute,
                          '/usr/bin/env', 'true',
                          this_is_not_a_valid_kwarg=True) 
Example #15
Source File: test_processutils.py    From oslo.concurrency with Apache License 2.0 5 votes vote down vote up
def _test_windows_execute(self, mock_tpool, mock_popen,
                              use_eventlet=False):
        # We want to ensure that if eventlet is used on Windows,
        # 'communicate' calls are wrapped with eventlet.tpool.execute.
        mock_comm = mock_popen.return_value.communicate
        mock_comm.return_value = None
        mock_tpool.execute.return_value = mock_comm.return_value

        fake_pinput = 'fake pinput'.encode('utf-8')

        with mock.patch.object(processutils, 'eventlet_patched',
                               use_eventlet):
            processutils.execute(
                TRUE_UTILITY,
                process_input=fake_pinput,
                check_exit_code=False)

        mock_popen.assert_called_once_with(
            [TRUE_UTILITY],
            stdin=mock.ANY, stdout=mock.ANY,
            stderr=mock.ANY, close_fds=mock.ANY,
            preexec_fn=mock.ANY, shell=mock.ANY,
            cwd=mock.ANY, env=mock.ANY)

        if use_eventlet:
            mock_tpool.execute.assert_called_once_with(
                mock_comm, fake_pinput, timeout=None)
        else:
            mock_comm.assert_called_once_with(fake_pinput, timeout=None) 
Example #16
Source File: test_processutils.py    From oslo.concurrency with Apache License 2.0 5 votes vote down vote up
def test_execute_with_preexec_fn(self):
        # NOTE(dims): preexec_fn is set to a callable object, this object
        # will be called in the child process just before the child is
        # executed. So we cannot pass share variables etc, simplest is to
        # check if a specific exception is thrown which can be caught here.
        def preexec_fn():
            raise processutils.InvalidArgumentError()

        processutils.execute(TRUE_UTILITY)

        try:
            processutils.execute(TRUE_UTILITY, preexec_fn=preexec_fn)
        except Exception as e:
            if type(e).__name__ != 'SubprocessError':
                raise 
Example #17
Source File: test_processutils.py    From oslo.concurrency with Apache License 2.0 5 votes vote down vote up
def test_execute_with_callback(self):
        on_execute_callback = mock.Mock()
        on_completion_callback = mock.Mock()
        processutils.execute(TRUE_UTILITY)
        self.assertEqual(0, on_execute_callback.call_count)
        self.assertEqual(0, on_completion_callback.call_count)

        processutils.execute(TRUE_UTILITY, on_execute=on_execute_callback,
                             on_completion=on_completion_callback)
        self.assertEqual(1, on_execute_callback.call_count)
        self.assertEqual(1, on_completion_callback.call_count) 
Example #18
Source File: test_processutils.py    From oslo.concurrency with Apache License 2.0 5 votes vote down vote up
def test_check_exit_code_boolean(self):
        processutils.execute('/usr/bin/env', 'false', check_exit_code=False)
        self.assertRaises(processutils.ProcessExecutionError,
                          processutils.execute,
                          '/usr/bin/env', 'false', check_exit_code=True) 
Example #19
Source File: iptables.py    From os-vif with Apache License 2.0 5 votes vote down vote up
def ip6tables_restore(input):
    return processutils.execute('ip6tables-restore',
                                '-c', attempts=5,
                                process_input=input) 
Example #20
Source File: iptables.py    From os-vif with Apache License 2.0 5 votes vote down vote up
def iptables_restore(input):
    return processutils.execute('iptables-restore',
                                '-c', attempts=5,
                                process_input=input) 
Example #21
Source File: iptables.py    From os-vif with Apache License 2.0 5 votes vote down vote up
def ip6tables_save():
    return processutils.execute('ip6tables-save',
                                '-c', attempts=5) 
Example #22
Source File: iptables.py    From os-vif with Apache License 2.0 5 votes vote down vote up
def iptables_save():
    return processutils.execute('iptables-save',
                                '-c', attempts=5) 
Example #23
Source File: zaqar_notification_example_consumer.py    From manila with Apache License 2.0 5 votes vote down vote up
def execute(cmd):
    try:
        print_with_time('Executing following command: \n%s' % cmd)
        cmd = cmd.split()
        stdout, stderr = processutils.execute(*cmd)
        if stderr:
            print_with_time('Got error: %s' % stderr)
        return stdout, stderr
    except Exception as e:
        print_with_time('Got following error: %s' % e)
        return False, True 
Example #24
Source File: linux_net.py    From os-vif with Apache License 2.0 5 votes vote down vote up
def _update_bridge_routes(interface, bridge):
    """Updates routing table for a given bridge and interface.
    :param interface: string interface name
    :param bridge: string bridge name
    """
    # TODO(sean-k-mooney): investigate deleting all this route
    # handling code. The vm tap devices should never have an ip,
    # this is old nova networks code and i dont think it will ever
    # be needed in os-vif.
    # NOTE(vish): This will break if there is already an ip on the
    #             interface, so we move any ips to the bridge
    # NOTE(danms): We also need to copy routes to the bridge so as
    #              not to break existing connectivity on the interface
    old_routes = []
    out, _ = processutils.execute('ip', 'route', 'show', 'dev',
                                  interface)
    for line in out.split('\n'):
        fields = line.split()
        if fields and 'via' in fields:
            old_routes.append(fields)
            processutils.execute('ip', 'route', 'del', *fields)

    out, _ = processutils.execute('ip', 'addr', 'show', 'dev',
                                  interface, 'scope', 'global')
    for line in out.split('\n'):
        fields = line.split()
        if fields and fields[0] == 'inet':
            if fields[-2] in ('secondary', 'dynamic', ):
                params = fields[1:-2]
            else:
                params = fields[1:-1]
                processutils.execute(*_ip_bridge_cmd('del', params,
                                                     fields[-1]),
                                     check_exit_code=[0, 2, 254])
                processutils.execute(*_ip_bridge_cmd('add', params,
                                                     bridge),
                                     check_exit_code=[0, 2, 254])
    for fields in old_routes:
        processutils.execute('ip', 'route', 'add', *fields) 
Example #25
Source File: utils.py    From manila with Apache License 2.0 5 votes vote down vote up
def _update_info_from_dpkg(self):
        LOG.debug('Trying dpkg-query command.')
        try:
            _vendor = None
            out, err = putils.execute("dpkg-query", "-W", "-f='${Version}'",
                                      self.PACKAGE_NAME)
            if not out:
                LOG.info(
                    'No dpkg-query info found for %(pkg)s package.', {
                        'pkg': self.PACKAGE_NAME})
                return False
            # Debian format: [epoch:]upstream_version[-debian_revision]
            deb_version = out
            # In case epoch or revision is missing, copy entire string.
            _release = deb_version
            if ':' in deb_version:
                deb_epoch, upstream_version = deb_version.split(':')
                _release = upstream_version
            if '-' in deb_version:
                deb_revision = deb_version.split('-')[1]
                _vendor = deb_revision
            self._release = _release
            if _vendor:
                self._vendor = _vendor
            return True
        except Exception as e:
            LOG.info('Could not run dpkg-query command: %(msg)s.', {
                'msg': e})
            return False 
Example #26
Source File: utils.py    From python-tripleoclient with Apache License 2.0 5 votes vote down vote up
def cleanup_tripleo_ansible_inventory_file(path):
    """Remove the static tripleo-ansible-inventory file from disk"""
    if os.path.exists(path):
        processutils.execute('/usr/bin/rm', '-f', path) 
Example #27
Source File: utils.py    From python-tripleoclient with Apache License 2.0 5 votes vote down vote up
def get_tripleo_ansible_inventory(inventory_file=None,
                                  ssh_user='tripleo-admin',
                                  stack='overcloud',
                                  undercloud_connection='ssh',
                                  return_inventory_file_path=False):
    if not inventory_file:
        inventory_file = os.path.join(
            constants.CLOUD_HOME_DIR,
            'tripleo-ansible-inventory.yaml'
        )
        try:
            processutils.execute(
                '/usr/bin/tripleo-ansible-inventory',
                '--stack', stack,
                '--ansible_ssh_user', ssh_user,
                '--undercloud-connection', undercloud_connection,
                '--undercloud-key-file',
                get_key(stack=stack),
                '--static-yaml-inventory', inventory_file)
        except processutils.ProcessExecutionError as e:
            message = _("Failed to generate inventory: %s") % str(e)
            raise exceptions.InvalidConfiguration(message)
    if os.path.exists(inventory_file):
        if return_inventory_file_path:
            return inventory_file

        with open(inventory_file, "r") as f:
            inventory = f.read()
        return inventory
    else:
        raise exceptions.InvalidConfiguration(_(
            "Inventory file %s can not be found.") % inventory_file) 
Example #28
Source File: zaqar_notification_example_consumer.py    From manila with Apache License 2.0 5 votes vote down vote up
def unmount_share(export_location, access_to):
    if rule_affects_me(access_to) and is_share_mounted(export_location):
        print_with_time("Unmounting '%(export_location)s' share.")
        stdout, stderr = execute('sudo umount %s' % export_location)
        if stderr:
            print_with_time("Unmount operation failed.")
        else:
            print_with_time("Unmount operation went OK.") 
Example #29
Source File: utils.py    From manila with Apache License 2.0 5 votes vote down vote up
def execute(*cmd, **kwargs):
    """Convenience wrapper around oslo's execute() function."""
    if 'run_as_root' in kwargs and 'root_helper' not in kwargs:
        kwargs['root_helper'] = _get_root_helper()
    if hasattr('CONF', 'debug') and CONF.debug:
        kwargs['loglevel'] = logging.DEBUG
    return processutils.execute(*cmd, **kwargs) 
Example #30
Source File: utils.py    From manila with Apache License 2.0 5 votes vote down vote up
def write_local_file(filename, contents, as_root=False):
    tmp_filename = "%s.tmp" % filename
    if as_root:
        execute('tee', tmp_filename, run_as_root=True, process_input=contents)
        execute('mv', '-f', tmp_filename, filename, run_as_root=True)
    else:
        with open(tmp_filename, 'w') as f:
            f.write(contents)
        os.rename(tmp_filename, filename)