Esempio n. 1
0
    def execute(self, file_path, **kwargs):

        target_format = CONF.image_conversion.output_format
        # TODO(jokke): Once we support other schemas we need to take them into
        # account and handle the paths here.
        src_path = file_path.split('file://')[-1]
        dest_path = "%(path)s.%(target)s" % {'path': src_path,
                                             'target': target_format}
        self.dest_path = dest_path

        try:
            stdout, stderr = putils.trycmd("qemu-img", "info",
                                           "--output=json",
                                           src_path,
                                           prlimit=utils.QEMU_IMG_PROC_LIMITS,
                                           log_errors=putils.LOG_ALL_ERRORS,)
        except OSError as exc:
            with excutils.save_and_reraise_exception():
                exc_message = encodeutils.exception_to_unicode(exc)
                msg = ("Failed to do introspection as part of image "
                       "conversion for %(iid)s: %(err)s")
                LOG.error(msg, {'iid': self.image_id, 'err': exc_message})

        if stderr:
            raise RuntimeError(stderr)

        metadata = json.loads(stdout)
        source_format = metadata.get('format')
        virtual_size = metadata.get('virtual-size', 0)
        image = self.image_repo.get(self.image_id)
        image.virtual_size = virtual_size

        if source_format == target_format:
            LOG.debug("Source is already in target format, "
                      "not doing conversion for %s", self.image_id)
            self.image_repo.save(image)
            return file_path

        try:
            stdout, stderr = putils.trycmd('qemu-img', 'convert',
                                           '-f', source_format,
                                           '-O', target_format,
                                           src_path, dest_path,
                                           log_errors=putils.LOG_ALL_ERRORS)
        except OSError as exc:
            with excutils.save_and_reraise_exception():
                exc_message = encodeutils.exception_to_unicode(exc)
                msg = "Failed to do image conversion for %(iid)s: %(err)s"
                LOG.error(msg, {'iid': self.image_id, 'err': exc_message})

        if stderr:
            raise RuntimeError(stderr)

        image.disk_format = target_format
        image.container_format = 'bare'
        self.image_repo.save(image)

        os.remove(src_path)

        return "file://%s" % dest_path
def pre_confirm_flow(vlan):
    internal = "ovs-ofctl dump-flows br-int|grep 'dl_vlan=%s'" % vlan
    external = "ovs-ofctl dump-flows br-bond1|grep 'actions=mod_vlan_vid:%s'" % vlan
    if replace_n_r(processutils.trycmd(internal, shell=True)[0]).find(
            "dl_vlan=%s" % vlan) < 0 or replace_n_r(
                processutils.trycmd(external, shell=True)[0]).find(
                    "actions=mod_vlan_vid:%s" % vlan) < 0:
        raise Exception("not found vlan:%s" % vlan)
def pre_confirm_vm(instance_id):
    cmd = "echo `virsh dominfo %s|grep Name|awk -F ':' '{print $2}'`" % instance_id
    out, err = processutils.trycmd(cmd, shell=True)
    if replace_n_r(out).find("no domain with matching name") > -1:
        raise Exception(err)
    cmd = "ps aux|grep %s|grep -v 'grep'|awk -F ' ' '{print $8}'" % replace_n_r(
        out)
    out, err = processutils.trycmd(cmd, shell=True)
    if replace_n_r(out).find("D") > -1 or replace_n_r(out).find("Z") > -1:
        raise Exception(err)
def pre_confirm_nic(tap_name, qbr_name, qvb_name, qvo_name):
    nics = (tap_name, qbr_name, qvb_name, qvo_name)
    nic_check_cmd = "ifconfig -a |grep %s"
    for nic in nics:
        cmd = nic_check_cmd % nic
        out, err = processutils.trycmd(cmd, shell=True)
        if replace_n_r(out).find(nic) < 0 or (nic == qvo_name and replace_n_r(
                processutils.trycmd("ovs-ofctl show br-int|grep %s" % nic,
                                    shell=True)[0]).find(nic) < 0):
            raise Exception(err)
Esempio n. 5
0
    def execute(self, image_id, file_path):

        # NOTE(flaper87): A format must be explicitly
        # specified. There's no "sane" default for this
        # because the dest format may work differently depending
        # on the environment OpenStack is running in.
        conversion_format = CONF.taskflow_executor.conversion_format
        if conversion_format is None:
            if not _Convert.conversion_missing_warned:
                msg = (_LW('The conversion format is None, please add a value '
                           'for it in the config file for this task to '
                           'work: %s') %
                       self.task_id)
                LOG.warn(msg)
                _Convert.conversion_missing_warned = True
            return

        # TODO(flaper87): Check whether the image is in the desired
        # format already. Probably using `qemu-img` just like the
        # `Introspection` task.
        dest_path = "%s.converted"
        stdout, stderr = putils.trycmd('qemu-img', 'convert', '-O',
                                       conversion_format, file_path, dest_path,
                                       log_errors=putils.LOG_ALL_ERRORS)

        if stderr:
            raise RuntimeError(stderr)
Esempio n. 6
0
    def execute(self, image_id, file_path):

        # NOTE(flaper87): A format must be explicitly
        # specified. There's no "sane" default for this
        # because the dest format may work differently depending
        # on the environment OpenStack is running in.
        conversion_format = CONF.taskflow_executor.conversion_format
        if conversion_format is None:
            if not _Convert.conversion_missing_warned:
                msg = (_LW('The conversion format is None, please add a value '
                           'for it in the config file for this task to '
                           'work: %s') % self.task_id)
                LOG.warn(msg)
                _Convert.conversion_missing_warned = True
            return

        # TODO(flaper87): Check whether the image is in the desired
        # format already. Probably using `qemu-img` just like the
        # `Introspection` task.
        dest_path = os.path.join(CONF.task.work_dir, "%s.converted" % image_id)
        stdout, stderr = putils.trycmd('qemu-img',
                                       'convert',
                                       '-O',
                                       conversion_format,
                                       file_path,
                                       dest_path,
                                       log_errors=putils.LOG_ALL_ERRORS)

        if stderr:
            raise RuntimeError(stderr)

        os.rename(dest_path, file_path.split("file://")[-1])
        return file_path
Esempio n. 7
0
    def execute(self, image_id, file_path):
        """Does the actual introspection

        :param image_id: Glance image ID
        :param file_path: Path to the file being introspected
        """

        try:
            stdout, stderr = putils.trycmd(
                "qemu-img", "info", "--output=json", file_path, log_errors=putils.LOG_ALL_ERRORS
            )
        except OSError as exc:
            # NOTE(flaper87): errno == 2 means the executable file
            # was not found. For now, log an error and move forward
            # until we have a better way to enable/disable optional
            # tasks.
            if exc.errno != 2:
                with excutils.save_and_reraise_exception():
                    msg = _LE("Failed to execute introspection " "%(task_id)s: %(exc)s") % {
                        "task_id": self.task_id,
                        "exc": exc.message,
                    }
                    LOG.error(msg)
            return

        if stderr:
            raise RuntimeError(stderr)

        metadata = json.loads(stdout)
        new_image = self.image_repo.get(image_id)
        new_image.virtual_size = metadata.get("virtual-size", 0)
        new_image.disk_format = metadata.get("format")
        self.image_repo.save(new_image)
        LOG.debug("%(task_id)s: Introspection successful: %(file)s" % {"task_id": self.task_id, "file": file_path})
        return new_image
Esempio n. 8
0
 def test_discard_warnings(self):
     self.useFixture(fixtures.MonkeyPatch(
         'oslo_concurrency.processutils.execute', fake_execute))
     o, e = processutils.trycmd('this is a command'.split(' '),
                                discard_warnings=True)
     self.assertIsNotNone(o)
     self.assertEqual('', e)
 def test_keep_warnings(self):
     self.useFixture(
         fixtures.MonkeyPatch('oslo_concurrency.processutils.execute',
                              fake_execute))
     o, e = processutils.trycmd('this is a command'.split(' '))
     self.assertNotEqual('', o)
     self.assertNotEqual('', e)
 def test_discard_warnings(self):
     self.useFixture(fixtures.MonkeyPatch(
         'oslo_concurrency.processutils.execute', fake_execute))
     o, e = processutils.trycmd('this is a command'.split(' '),
                                discard_warnings=True)
     self.assertIsNotNone(o)
     self.assertEqual('', e)
Esempio n. 11
0
def trycmd(*args, **kwargs):
    """Convenience wrapper around oslo's trycmd() method."""
    if kwargs.get("run_as_root", False):
        if CONF.use_rootwrap_daemon:
            return RootwrapDaemonHelper(CONF.rootwrap_config).trycmd(*args, **kwargs)
        else:
            return RootwrapProcessHelper().trycmd(*args, **kwargs)
    return processutils.trycmd(*args, **kwargs)
Esempio n. 12
0
 def _execute_cmd(self, cmd=None):
     """Shell out and run the given command."""
     out, err = processutils.trycmd(cmd, shell=True)
     assert_equal('', err,
                  "Failed to run: '%(cmd)s' "
                  "Output: '%(stdout)s' "
                  "Error: '%(stderr)s'" %
                  {'cmd': cmd, 'stdout': out, 'stderr': err})
Esempio n. 13
0
def trycmd(*args, **kwargs):
    """Convenience wrapper around oslo's trycmd() method."""
    if kwargs.get('run_as_root', False):
        if CONF.use_rootwrap_daemon:
            return RootwrapDaemonHelper(CONF.rootwrap_config).trycmd(
                *args, **kwargs)
        else:
            return RootwrapProcessHelper().trycmd(*args, **kwargs)
    return processutils.trycmd(*args, **kwargs)
Esempio n. 14
0
 def _execute_cmd(self, cmd=None):
     """Shell out and run the given command."""
     out, err = processutils.trycmd(cmd, shell=True)
     assert_equal(
         "",
         err,
         "Failed to run: '%(cmd)s' "
         "Output: '%(stdout)s' "
         "Error: '%(stderr)s'" % {"cmd": cmd, "stdout": out, "stderr": err},
     )
def _extract_scexe_file(self, target_file, extract_path):
    """Extracts the scexe file.

    :param target_file: the firmware file to be extracted from
    :param extract_path: the path where extraction is supposed to happen
    """
    # Command to extract the smart component file.
    unpack_cmd = '--unpack=' + extract_path
    # os.path.isfile(target_file)
    cmd = [target_file, unpack_cmd]
    out, err = utils.trycmd(*cmd)
Esempio n. 16
0
 def _execute_cmd(self, cmd=None):
     """Shell out and run the given command."""
     out, err = processutils.trycmd(cmd, shell=True)
     assert_equal(
         '', err, "Failed to run: '%(cmd)s' "
         "Output: '%(stdout)s' "
         "Error: '%(stderr)s'" % {
             'cmd': cmd,
             'stdout': out,
             'stderr': err
         })
Esempio n. 17
0
    def execute(self, image_id, file_path):

        # NOTE(flaper87): A format must be explicitly
        # specified. There's no "sane" default for this
        # because the dest format may work differently depending
        # on the environment OpenStack is running in.
        abs_file_path = file_path.split("file://")[-1]
        conversion_format = CONF.taskflow_executor.conversion_format
        if conversion_format is None:
            if not _Convert.conversion_missing_warned:
                msg = _LW('The conversion format is None, please add a value '
                          'for it in the config file for this task to '
                          'work: %s')
                LOG.warn(msg, self.task_id)
                _Convert.conversion_missing_warned = True
            return

        image_obj = self.image_repo.get(image_id)
        src_format = image_obj.disk_format

        # TODO(flaper87): Check whether the image is in the desired
        # format already. Probably using `qemu-img` just like the
        # `Introspection` task.

        # NOTE(hemanthm): We add '-f' parameter to the convert command here so
        # that the image format need not be inferred by qemu utils. This
        # shields us from being vulnerable to an attack vector described here
        # https://bugs.launchpad.net/glance/+bug/1449062

        data_dir = CONF.task.work_dir
        # NOTE(abhishekk): Use reserved 'os_glance_tasks_store' for tasks.
        if CONF.enabled_backends:
            data_dir = getattr(
                CONF, 'os_glance_tasks_store').filesystem_store_datadir

        dest_path = os.path.join(data_dir, "%s.converted" % image_id)
        stdout, stderr = putils.trycmd('qemu-img',
                                       'convert',
                                       '-f',
                                       src_format,
                                       '-O',
                                       conversion_format,
                                       file_path,
                                       dest_path,
                                       log_errors=putils.LOG_ALL_ERRORS)

        if stderr:
            raise RuntimeError(stderr)

        os.unlink(abs_file_path)
        os.rename(dest_path, abs_file_path)
        return file_path
Esempio n. 18
0
 def _execute_cmd(self, cmd=None):
     """Shell out and run the given command."""
     out, err = processutils.trycmd(cmd, shell=True)
     # Until someone wants to rewrite this to avoid the warning
     # we need to handle it for newer versions of mysql
     valid_err = err == '' or \
         err == 'mysql: [Warning] Using a password on the ' \
                'command line interface can be insecure.\n'
     assert_true(valid_err,
                 "Failed to run: '%(cmd)s' "
                 "Output: '%(stdout)s' "
                 "Error: '%(stderr)s'" %
                 {'cmd': cmd, 'stdout': out, 'stderr': err})
Esempio n. 19
0
 def _execute_cmd(self, cmd=None):
     """Shell out and run the given command."""
     out, err = processutils.trycmd(cmd, shell=True)
     # Until someone wants to rewrite this to avoid the warning
     # we need to handle it for newer versions of mysql
     valid_err = err == '' or \
         err == 'mysql: [Warning] Using a password on the ' \
                'command line interface can be insecure.\n'
     assert_true(valid_err,
                 "Failed to run: '%(cmd)s' "
                 "Output: '%(stdout)s' "
                 "Error: '%(stderr)s'" %
                 {'cmd': cmd, 'stdout': out, 'stderr': err})
Esempio n. 20
0
 def _can_fallocate(self):
     """Check once per class, whether fallocate(1) is available,
        and that the instances directory supports fallocate(2).
     """
     can_fallocate = getattr(self.__class__, 'can_fallocate', None)
     if can_fallocate is None:
         test_path = self.path + '.fallocate_test'
         _out, err = processutils.trycmd('fallocate', '-l', '1', test_path)
         fileutils.delete_if_exists(test_path)
         can_fallocate = not err
         self.__class__.can_fallocate = can_fallocate
         if not can_fallocate:
             LOG.warning('Unable to preallocate image at path: %(path)s',
                         {'path': self.path})
     return can_fallocate
Esempio n. 21
0
 def _can_fallocate(self):
     """Check once per class, whether fallocate(1) is available,
        and that the instances directory supports fallocate(2).
     """
     can_fallocate = getattr(self.__class__, 'can_fallocate', None)
     if can_fallocate is None:
         test_path = self.path + '.fallocate_test'
         _out, err = processutils.trycmd('fallocate', '-l', '1', test_path)
         fileutils.delete_if_exists(test_path)
         can_fallocate = not err
         self.__class__.can_fallocate = can_fallocate
         if not can_fallocate:
             LOG.warning('Unable to preallocate image at path: %(path)s',
                         {'path': self.path})
     return can_fallocate
Esempio n. 22
0
    def execute(self, image_id):
        """Create temp file into store and return path to it

        :param image_id: Glance Image ID
        """
        # NOTE(flaper87): We've decided to use a separate `work_dir` for
        # this task - and tasks coming after this one - as a way to expect
        # users to configure a local store for pre-import works on the image
        # to happen.
        #
        # While using any path should be "technically" fine, it's not what
        # we recommend as the best solution. For more details on this, please
        # refer to the comment in the `_ImportToStore.execute` method.
        data = script_utils.get_image_data_iter(self.uri)

        path = self.store.add(image_id, data, 0, context=None)[0]

        try:
            # NOTE(flaper87): Consider moving this code to a common
            # place that other tasks can consume as well.
            stdout, stderr = putils.trycmd('qemu-img',
                                           'info',
                                           '--output=json',
                                           path,
                                           log_errors=putils.LOG_ALL_ERRORS)
        except OSError as exc:
            with excutils.save_and_reraise_exception():
                exc_message = encodeutils.exception_to_unicode(exc)
                msg = (_LE('Failed to execute security checks on the image '
                           '%(task_id)s: %(exc)s') % {
                               'task_id': self.task_id,
                               'exc': exc_message
                           })
                LOG.error(msg)

        metadata = json.loads(stdout)

        backing_file = metadata.get('backing-filename')
        if backing_file is not None:
            msg = _("File %(path)s has invalid backing file "
                    "%(bfile)s, aborting.") % {
                        'path': path,
                        'bfile': backing_file
                    }
            raise RuntimeError(msg)

        return path
Esempio n. 23
0
    def execute(self, image_id, file_path):
        """Does the actual introspection

        :param image_id: Glance image ID
        :param file_path: Path to the file being introspected
        """

        try:
            stdout, stderr = putils.trycmd('qemu-img',
                                           'info',
                                           '--output=json',
                                           file_path,
                                           prlimit=utils.QEMU_IMG_PROC_LIMITS,
                                           log_errors=putils.LOG_ALL_ERRORS)
        except OSError as exc:
            # NOTE(flaper87): errno == 2 means the executable file
            # was not found. For now, log an error and move forward
            # until we have a better way to enable/disable optional
            # tasks.
            if exc.errno != 2:
                with excutils.save_and_reraise_exception():
                    exc_message = encodeutils.exception_to_unicode(exc)
                    msg = _LE('Failed to execute introspection '
                              '%(task_id)s: %(exc)s')
                    LOG.error(msg, {
                        'task_id': self.task_id,
                        'exc': exc_message
                    })
            return

        if stderr:
            raise RuntimeError(stderr)

        metadata = json.loads(stdout)
        new_image = self.image_repo.get(image_id)
        new_image.virtual_size = metadata.get('virtual-size', 0)
        new_image.disk_format = metadata.get('format')
        self.image_repo.save(new_image)
        LOG.debug("%(task_id)s: Introspection successful: %(file)s", {
            'task_id': self.task_id,
            'file': file_path
        })
        return new_image
Esempio n. 24
0
    def execute(self, image_id):
        """Create temp file into store and return path to it

        :param image_id: Glance Image ID
        """
        # NOTE(flaper87): We've decided to use a separate `work_dir` for
        # this task - and tasks coming after this one - as a way to expect
        # users to configure a local store for pre-import works on the image
        # to happen.
        #
        # While using any path should be "technically" fine, it's not what
        # we recommend as the best solution. For more details on this, please
        # refer to the comment in the `_ImportToStore.execute` method.
        data = script_utils.get_image_data_iter(self.uri)

        path = self.store.add(image_id, data, 0, context=None)[0]

        try:
            # NOTE(flaper87): Consider moving this code to a common
            # place that other tasks can consume as well.
            stdout, stderr = putils.trycmd('qemu-img', 'info',
                                           '--output=json', path,
                                           prlimit=utils.QEMU_IMG_PROC_LIMITS,
                                           log_errors=putils.LOG_ALL_ERRORS)
        except OSError as exc:
            with excutils.save_and_reraise_exception():
                exc_message = encodeutils.exception_to_unicode(exc)
                msg = _LE('Failed to execute security checks on the image '
                          '%(task_id)s: %(exc)s')
                LOG.error(msg, {'task_id': self.task_id, 'exc': exc_message})

        metadata = json.loads(stdout)

        backing_file = metadata.get('backing-filename')
        if backing_file is not None:
            msg = _("File %(path)s has invalid backing file "
                    "%(bfile)s, aborting.") % {'path': path,
                                               'bfile': backing_file}
            raise RuntimeError(msg)

        return path
Esempio n. 25
0
    def execute(self, image_id, file_path):

        # NOTE(flaper87): A format must be explicitly
        # specified. There's no "sane" default for this
        # because the dest format may work differently depending
        # on the environment OpenStack is running in.
        conversion_format = CONF.taskflow_executor.conversion_format
        if conversion_format is None:
            if not _Convert.conversion_missing_warned:
                msg = _LW('The conversion format is None, please add a value '
                          'for it in the config file for this task to '
                          'work: %s')
                LOG.warn(msg, self.task_id)
                _Convert.conversion_missing_warned = True
            return

        image_obj = self.image_repo.get(image_id)
        src_format = image_obj.disk_format

        # TODO(flaper87): Check whether the image is in the desired
        # format already. Probably using `qemu-img` just like the
        # `Introspection` task.

        # NOTE(hemanthm): We add '-f' parameter to the convert command here so
        # that the image format need not be inferred by qemu utils. This
        # shields us from being vulnerable to an attack vector described here
        # https://bugs.launchpad.net/glance/+bug/1449062

        dest_path = os.path.join(CONF.task.work_dir, "%s.converted" % image_id)
        stdout, stderr = putils.trycmd('qemu-img', 'convert',
                                       '-f', src_format,
                                       '-O', conversion_format,
                                       file_path, dest_path,
                                       log_errors=putils.LOG_ALL_ERRORS)

        if stderr:
            raise RuntimeError(stderr)

        os.rename(dest_path, file_path.split("file://")[-1])
        return file_path
Esempio n. 26
0
    def execute(self, image_id, file_path):
        """Does the actual introspection

        :param image_id: Glance image ID
        :param file_path: Path to the file being introspected
        """

        try:
            stdout, stderr = putils.trycmd('qemu-img', 'info',
                                           '--output=json', file_path,
                                           prlimit=utils.QEMU_IMG_PROC_LIMITS,
                                           log_errors=putils.LOG_ALL_ERRORS)
        except OSError as exc:
            # NOTE(flaper87): errno == 2 means the executable file
            # was not found. For now, log an error and move forward
            # until we have a better way to enable/disable optional
            # tasks.
            if exc.errno != 2:
                with excutils.save_and_reraise_exception():
                    exc_message = encodeutils.exception_to_unicode(exc)
                    msg = _LE('Failed to execute introspection '
                              '%(task_id)s: %(exc)s')
                    LOG.error(msg, {'task_id': self.task_id,
                                    'exc': exc_message})
            return

        if stderr:
            raise RuntimeError(stderr)

        metadata = json.loads(stdout)
        new_image = self.image_repo.get(image_id)
        new_image.virtual_size = metadata.get('virtual-size', 0)
        new_image.disk_format = metadata.get('format')
        self.image_repo.save(new_image)
        LOG.debug("%(task_id)s: Introspection successful: %(file)s",
                  {'task_id': self.task_id, 'file': file_path})
        return new_image
Esempio n. 27
0
def trycmd(*args, **kwargs):
    """Convenience wrapper around oslo's trycmd() method."""
    if kwargs.get('run_as_root') and 'root_helper' not in kwargs:
        kwargs['root_helper'] = _get_root_helper()
    return processutils.trycmd(*args, **kwargs)
Esempio n. 28
0
def trycmd(*args, **kwargs):
    """Convenience wrapper around oslo's trycmd() function."""
    if 'run_as_root' in kwargs and 'root_helper' not in kwargs:
        kwargs['root_helper'] = _get_root_helper()
    return processutils.trycmd(*args, **kwargs)
Esempio n. 29
0
def trycmd(*args, **kwargs):
    """Convenience wrapper around oslo's trycmd() method."""
    return processutils.trycmd(*args, **kwargs)
Esempio n. 30
0
 def trycmd(self, *cmd, **kwargs):
     kwargs['root_helper'] = get_root_helper()
     return processutils.trycmd(*cmd, **kwargs)
Esempio n. 31
0
def trycmd(*args, **kwargs):
    """Convenience wrapper around oslo's trycmd() function."""
    if 'run_as_root' in kwargs and 'root_helper' not in kwargs:
        kwargs['root_helper'] = _get_root_helper()
    return processutils.trycmd(*args, **kwargs)
Esempio n. 32
0
def update_firmware(node):
    """Performs SUM based firmware update on the node.

    This method performs SUM firmware update by mounting the
    SPP ISO on the node. It performs firmware update on all or
    some of the firmware components.

    :param node: A node object of type dict.
    :returns: Operation Status string.
    :raises: SUMOperationError, when the vmedia device is not found or
        when the mount operation fails or when the image validation fails.
    :raises: IloConnectionError, when the iLO connection fails.
    :raises: IloError, when vmedia eject or insert operation fails.
    """
    sum_update_iso = node['clean_step']['args'].get('url')

    # Validates the http image reference for SUM update ISO.
    try:
        utils.validate_href(sum_update_iso)
    except exception.ImageRefValidationFailed as e:
        raise exception.SUMOperationError(reason=e)

    # Ejects the CDROM device in the iLO and inserts the SUM update ISO
    # to the CDROM device.
    info = node.get('driver_info')
    ilo_object = client.IloClient(info.get('ilo_address'),
                                  info.get('ilo_username'),
                                  info.get('ilo_password'))

    ilo_object.eject_virtual_media('CDROM')
    ilo_object.insert_virtual_media(sum_update_iso, 'CDROM')

    # Waits for the OS to detect the disk and update the label file. SPP ISO
    # is identified by matching its label.
    time.sleep(WAIT_TIME_DISK_LABEL_TO_BE_VISIBLE)
    vmedia_device_dir = "/dev/disk/by-label/"
    for file in os.listdir(vmedia_device_dir):
        if fnmatch.fnmatch(file, 'SPP*'):
            vmedia_device_file = os.path.join(vmedia_device_dir, file)

    if not os.path.exists(vmedia_device_file):
        msg = "Unable to find the virtual media device for SUM"
        raise exception.SUMOperationError(reason=msg)

    # Validates the SPP ISO image for any file corruption using the checksum
    # of the ISO file.
    expected_checksum = node['clean_step']['args'].get('checksum')
    try:
        utils.verify_image_checksum(vmedia_device_file, expected_checksum)
    except exception.ImageRefValidationFailed as e:
        raise exception.SUMOperationError(reason=e)

    # Mounts SPP ISO on a temporary directory.
    vmedia_mount_point = tempfile.mkdtemp()
    try:
        try:
            processutils.execute("mount", vmedia_device_file,
                                 vmedia_mount_point)
        except processutils.ProcessExecutionError as e:
            msg = ("Unable to mount virtual media device %(device)s: "
                   "%(error)s" % {
                       'device': vmedia_device_file,
                       'error': e
                   })
            raise exception.SUMOperationError(reason=msg)

        # Executes the SUM based firmware update by passing the 'smartupdate'
        # executable path if exists else 'hpsum' executable path and the
        # components specified (if any).
        sum_file_path = os.path.join(vmedia_mount_point, SUM_LOCATION)
        if not os.path.exists(sum_file_path):
            sum_file_path = os.path.join(vmedia_mount_point, HPSUM_LOCATION)

        components = node['clean_step']['args'].get('components')
        result = _execute_sum(sum_file_path,
                              vmedia_mount_point,
                              components=components)

        processutils.trycmd("umount", vmedia_mount_point)
    finally:
        shutil.rmtree(vmedia_mount_point, ignore_errors=True)

    return result
 def execute_cmd(self, cmd=None):
     out, err = processutils.trycmd(cmd, shell=True, discard_warnings=True)
     output = out or err
     LOG.debug(output)
     self.assertEqual('', err,
                      "Failed to run: %s\n%s" % (cmd, output))
from oslo_concurrency import processutils as putils

# you need copy id_rsa.pub to remote host
# you create file test_processutils on  remote host
# simple try cmd on remote host

private_key = '/home/wyp/.ssh/id_rsa'
remote_user = '******'
remote_ip = '10.117.170.20'

stdout, stderr = putils.trycmd('ssh',
                               '-i',
                               private_key,
                               '%s@%s' % (remote_user, remote_ip),
                               "touch /root/test_processutils",
                               log_errors=putils.LOG_ALL_ERRORS,
                               env_variables=None)

if stderr:
    print("Error, %s", stderr)
Esempio n. 35
0
 def trycmd(self, *cmd, **kwargs):
     kwargs['root_helper'] = get_root_helper()
     return processutils.trycmd(*cmd, **kwargs)
Esempio n. 36
0
 def execute_cmd(self, cmd=None):
     out, err = processutils.trycmd(cmd, shell=True, discard_warnings=True)
     output = out or err
     LOG.debug(output)
     self.assertEqual('', err, "Failed to run: %s\n%s" % (cmd, output))
 def test_keep_warnings(self):
     self.useFixture(fixtures.MonkeyPatch(
         'oslo_concurrency.processutils.execute', fake_execute))
     o, e = processutils.trycmd('this is a command'.split(' '))
     self.assertNotEqual('', o)
     self.assertNotEqual('', e)
Esempio n. 38
0
def trycmd(*args, **kwargs):
    """Convenience wrapper around oslo's trycmd() method."""
    if "run_as_root" in kwargs and "root_helper" not in kwargs:
        kwargs["root_helper"] = _get_root_helper()
    return processutils.trycmd(*args, **kwargs)
Esempio n. 39
0
def trycmd(*args, **kwargs):
    """Convenience wrapper around oslo's trycmd() method."""
    if kwargs.get('run_as_root') and 'root_helper' not in kwargs:
        kwargs['root_helper'] = _get_root_helper()
    return processutils.trycmd(*args, **kwargs)
Esempio n. 40
0
def trycmd(*args, **kwargs):
    """Convenience wrapper around oslo's trycmd() method."""
    return processutils.trycmd(*args, **kwargs)
Esempio n. 41
0
    def delete(self, name):
        """Delete logical volume or snapshot.

        :param name: Name of LV to delete

        """

        def run_udevadm_settle():
            self._execute('udevadm', 'settle',
                          root_helper=self._root_helper, run_as_root=True,
                          check_exit_code=False)

        # LV removal seems to be a race with other writers or udev in
        # some cases (see LP #1270192), so we enable retry deactivation
        LVM_CONFIG = 'activation { retry_deactivation = 1} '

        try:
            self._execute(
                'lvremove',
                '--config', LVM_CONFIG,
                '-f',
                '%s/%s' % (self.vg_name, name),
                root_helper=self._root_helper, run_as_root=True)
        except putils.ProcessExecutionError as err:
            LOG.info(('Error reported running lvremove: CMD: %(command)s, '
                      'RESPONSE: %(response)s'),
                     {'command': err.cmd, 'response': err.stderr})

            LOG.info('Attempting udev settle and retry of lvremove...')
            run_udevadm_settle()

            # The previous failing lvremove -f might leave behind
            # suspended devices; when lvmetad is not available, any
            # further lvm command will block forever.
            # Therefore we need to skip suspended devices on retry.
            LVM_CONFIG += 'devices { ignore_suspended_devices = 1}'

            try:
                self._execute(
                    'lvremove',
                    '--config', LVM_CONFIG,
                    '-f',
                    '%s/%s' % (self.vg_name, name),
                    root_helper=self._root_helper, run_as_root=True)
            except putils.ProcessExecutionError as err:
                if all(x in err.stderr for x in ['Logical volume', 'in use']):
                    # Get pids that keeps volume or snapshot open
                    mapper = "/dev/mapper/cinder--volumes-" + \
                        name.replace("-", "--")
                    root_helper = self._root_helper
                    (stdout, __) = putils.trycmd('fuser', mapper,
                                                 root_helper=root_helper,
                                                 run_as_root=True)
                    # Upon a successful run, 'fuser' will output the file name
                    # to stderr and the PIDs using the file to stdout. Hence we
                    # only need to split stdout into the pids list, no need to
                    # slice stdout for removing the file name
                    pids = stdout.split()
                    # Get process names that keep our volume or snapshot open
                    procs = []
                    for pid in pids:
                        pid = pid.strip()
                        (name, __) = putils.trycmd('ps', '-co', 'cmd= ', pid,
                                                   root_helper=root_helper,
                                                   run_as_root=True)
                        procs.append("%s (pid: %s)" % (name.rstrip(), pid))

                    raise LVMBackingStoreIsBusy(backing_vol=mapper,
                                                procs=", ".join(procs))
                raise
            LOG.debug('Successfully deleted volume: %s after '
                      'udev settle.', name)