Esempio n. 1
0
def _wait_for_job_finished(job):
    """
    This function waits for asynchronous job to be finished.

    :param job: Instance of ``LMI_SoftwareJob``.
    :type job: :py:class:`lmi.shell.LMIInstance`
    """
    if not isinstance(job, LMIInstance):
        raise TypeError("job must be an LMIInstance")
    LOG().debug('Waiting for a job "%s" to finish.', job.InstanceID)
    sleep_time = INITIAL_SLEEP_TIME
    connection_problem_count = 0
    while not LMIJob.lmi_is_job_finished(job):
        # Sleep, a bit longer in every iteration
        time.sleep(sleep_time)
        if sleep_time < LMIMethod._POLLING_ADAPT_MAX_WAITING_TIME:
            sleep_time = min(sleep_time * SLEEP_TIME_MULTIPLIER,
                    LMIMethod._POLLING_ADAPT_MAX_WAITING_TIME)
        try:
            (refreshed, _, errorstr) = job.refresh()
        except pywbem.CIMError as err:
            if      (   err.args[0] == 0
                    and err.args[1].lower().startswith('socket error')):
                if connection_problem_count >= MAX_CONNECTION_PROBLEM_COUNT:
                    raise
                LOG().warn("Connection problem: %s", err.args[1])
                if connection_problem_count == 0:
                    sleep_time = CONNECTION_PROBLEM_SLEEP_TIME
                connection_problem_count += 1
            else:
                raise

        if not refreshed:
            raise LMIExceptions.LMISynchroMethodCallError(errorstr)
Esempio n. 2
0
def _wait_for_job_finished(job):
    """
    This function waits for asynchronous job to be finished.

    :param job: Instance of ``LMI_SoftwareJob``.
    :type job: :py:class:`lmi.shell.LMIInstance`
    """
    if not isinstance(job, LMIInstance):
        raise TypeError("job must be an LMIInstance")
    LOG().debug('Waiting for a job "%s" to finish.', job.InstanceID)
    sleep_time = INITIAL_SLEEP_TIME
    connection_problem_count = 0
    while not LMIJob.lmi_is_job_finished(job):
        # Sleep, a bit longer in every iteration
        time.sleep(sleep_time)
        if sleep_time < LMIMethod._POLLING_ADAPT_MAX_WAITING_TIME:
            sleep_time = min(sleep_time * SLEEP_TIME_MULTIPLIER,
                             LMIMethod._POLLING_ADAPT_MAX_WAITING_TIME)
        try:
            (refreshed, _, errorstr) = job.refresh()
        except wbem.CIMError as err:
            if (err.args[0] == 0
                    and err.args[1].lower().startswith('socket error')):
                if connection_problem_count >= MAX_CONNECTION_PROBLEM_COUNT:
                    raise
                LOG().warn("Connection problem: %s", err.args[1])
                if connection_problem_count == 0:
                    sleep_time = CONNECTION_PROBLEM_SLEEP_TIME
                connection_problem_count += 1
            else:
                raise

        if not refreshed:
            raise LMIExceptions.LMISynchroMethodCallError(errorstr)
Esempio n. 3
0
def verify_package(ns, package):
    """
    Returns the instances of ``LMI_SoftwareIdentityFileCheck`` representing
    files, that did not pass the verification.

    :param package: Instance or instance name of
        ``LMI_SoftwareIdentity`` representing package to verify.
    :type package: :py:class:`lmi.shell.LMIInstance`
        or :py:class:`lmi.shell.LMIInstanceName`
    :returns: List of instances of ``LMI_SoftwareIdentityFileCheck``
        with non-empty ``FailedFlags`` property.
    :rtype: list
    """
    if not isinstance(package, (LMIInstance, LMIInstanceName)):
        raise TypeError("package must be an LMIInstance or LMIInstanceName")
    # we can not use synchronous invocation because the reference to a job is
    # needed - for enumerating of affected software identities
    service = ns.LMI_SoftwareInstallationService.first_instance()
    results = service.VerifyInstalledIdentity(
            Source=package.path
                if isinstance(package, LMIInstance) else package,
            Target=get_computer_system(ns).path)
    nevra = get_package_nevra(package)
    if results.rval != 4096:
        msg = 'Failed to verify package "%s (rval=%d)".' % (nevra, results.rval)
        if results.errorstr:
            msg += ': ' + results.errorstr
        raise LmiFailed(msg)

    job = results.rparams['Job'].to_instance()
    _wait_for_job_finished(job)
    if not LMIJob.lmi_is_job_completed(job):
        msg = 'Failed to verify package "%s".' % nevra
        if job.ErrorDescription:
            msg += ': ' + job.ErrorDescription
        raise LmiFailed(msg)
    LOG().debug('Verified package "%s" on remote host "%s".',
            nevra, ns.connection.uri)

    failed = job.associators(
            Role='AffectingElement',
            ResultRole='AffectedElement',
            AssocClass="LMI_AffectedSoftwareJobElement",
            ResultClass='LMI_SoftwareIdentityFileCheck')
    LOG().debug('Verified package "%s" on remote host "%s" with %d failures.',
            nevra, ns.connection.uri, len(failed))

    return failed
Esempio n. 4
0
def verify_package(ns, package):
    """
    Returns the instances of ``LMI_SoftwareIdentityFileCheck`` representing
    files, that did not pass the verification.

    :param package: Instance or instance name of
        ``LMI_SoftwareIdentity`` representing package to verify.
    :type package: :py:class:`lmi.shell.LMIInstance`
        or :py:class:`lmi.shell.LMIInstanceName`
    :returns: List of instances of ``LMI_SoftwareIdentityFileCheck``
        with non-empty ``FailedFlags`` property.
    :rtype: list
    """
    if not isinstance(package, (LMIInstance, LMIInstanceName)):
        raise TypeError("package must be an LMIInstance or LMIInstanceName")
    # we can not use synchronous invocation because the reference to a job is
    # needed - for enumerating of affected software identities
    service = ns.LMI_SoftwareInstallationService.first_instance()
    results = service.VerifyInstalledIdentity(
        Source=package.path if isinstance(package, LMIInstance) else package,
        Target=get_computer_system(ns).path)
    nevra = get_package_nevra(package)
    if results.rval != 4096:
        msg = 'Failed to verify package "%s (rval=%d)".' % (nevra,
                                                            results.rval)
        if results.errorstr:
            msg += ': ' + results.errorstr
        raise LmiFailed(msg)

    job = results.rparams['Job'].to_instance()
    _wait_for_job_finished(job)
    if not LMIJob.lmi_is_job_completed(job):
        msg = 'Failed to verify package "%s".' % nevra
        if job.ErrorDescription:
            msg += ': ' + job.ErrorDescription
        raise LmiFailed(msg)
    LOG().debug('Verified package "%s" on remote host "%s".', nevra,
                ns.connection.uri)

    failed = job.associators(Role='AffectingElement',
                             ResultRole='AffectedElement',
                             AssocClass="LMI_AffectedSoftwareJobElement",
                             ResultClass='LMI_SoftwareIdentityFileCheck')
    LOG().debug('Verified package "%s" on remote host "%s" with %d failures.',
                nevra, ns.connection.uri, len(failed))

    return failed
Esempio n. 5
0
def _wait_for_job_finished(job):
    """
    This function waits for asynchronous job to be finished.

    :param job: Instance of ``LMI_SoftwareJob``.
    :type job: :py:class:`lmi.shell.LMIInstance`
    """
    if not isinstance(job, LMIInstance):
        raise TypeError("job must be an LMIInstance")
    LOG().debug('waiting for a job "%s" to finish', job.InstanceId)
    sleep_time = 0.5
    while not LMIJob.lmi_is_job_finished(job):
        # Sleep, a bit longer in every iteration
        time.sleep(sleep_time)
        if sleep_time < LMIMethod._POLLING_ADAPT_MAX_WAITING_TIME:
            sleep_time *= 1.5
        (refreshed, _, errorstr) = job.refresh()
        if not refreshed:
            raise LMIExceptions.LMISynchroMethodCallError(errorstr)
Esempio n. 6
0
def _wait_for_job_finished(job):
    """
    This function waits for asynchronous job to be finished.

    :param job: Instance of ``LMI_SoftwareJob``.
    :type job: :py:class:`lmi.shell.LMIInstance`
    """
    if not isinstance(job, LMIInstance):
        raise TypeError("job must be an LMIInstance")
    LOG().debug('waiting for a job "%s" to finish', job.InstanceId)
    sleep_time = 0.5
    while not LMIJob.lmi_is_job_finished(job):
        # Sleep, a bit longer in every iteration
        time.sleep(sleep_time)
        if sleep_time < LMIMethod._POLLING_ADAPT_MAX_WAITING_TIME:
            sleep_time *= 1.5
        (refreshed, _, errorstr) = job.refresh()
        if not refreshed:
            raise LMIExceptions.LMISynchroMethodCallError(errorstr)
Esempio n. 7
0
def install_package(ns, package, force=False, update=False):
    """
    Install package on system.

    :param package: Instance or instance name of ``LMI_SoftwareIdentity``
        representing package to install.
    :type package: :py:class:`lmi.shell.LMIInstance`
        or :py:class:`lmi.shell.LMIInstanceName`
    :param boolean force: Whether the installation shall be done even if
        installing the same (reinstalling) or older version than already
        installed.
    :param boolean update: Whether this is an update. Update fails if
        package is not already installed on system.
    :returns: Software identity installed on remote system.
        It's an instance ``LMI_SoftwareIdentity``.
    :rtype: :py:class:`lmi.shell.LMIInstance`
    """
    if not isinstance(package, (LMIInstance, LMIInstanceName)):
        raise TypeError("package must be an LMIInstance or LMIInstanceName")
    service = ns.LMI_SoftwareInstallationService.first_instance()
    options = [4 if not update else 5]  # Install (4) or Update (5)
    if force:
        options.append(3)  # Force Installation
    # we can not use synchronous invocation because the reference to a job is
    # needed
    results = service.InstallFromSoftwareIdentity(
        Source=package.path if isinstance(package, LMIInstance) else package,
        Collection=ns.LMI_SystemSoftwareCollection.first_instance_name(),
        InstallOptions=options)
    nevra = get_package_nevra(package)
    if results.rval != 4096:
        msg = 'Failed to %s package "%s" (rval=%d).' % (
            'update' if update else 'install', nevra, results.rval)
        if results.errorstr:
            msg += ': ' + results.errorstr
        raise LmiFailed(msg)

    job = results.rparams['Job'].to_instance()
    _wait_for_job_finished(job)
    if not LMIJob.lmi_is_job_completed(job):
        if not update:
            if not isinstance(package, LMIInstance):
                try:
                    package = package.to_instance()
                except wbem.CIMError:
                    pass
            if getattr(package, 'InstallDate', None) is not None:
                LOG().info('Package "%s" is already installed.' % nevra)
                return
        msg = 'Failed to %s package "%s".' % ('update'
                                              if update else 'install', nevra)
        if job.ErrorDescription:
            msg += ': ' + job.ErrorDescription
        raise LmiFailed(msg)
    else:
        LOG().info('Installed package "%s".', nevra, ns.connection.uri)

    installed = job.associators(Role='AffectingElement',
                                ResultRole='AffectedElement',
                                AssocClass="LMI_AffectedSoftwareJobElement",
                                ResultClass='LMI_SoftwareIdentity')
    if len(installed) < 1:
        raise LmiFailed('Failed to find installed package "%s".' % nevra)
    if len(installed) > 1:
        LOG().warn('Expected just one affected software identity, got: %s',
                   {get_package_nevra(p)
                    for p in installed})

    return installed[-1]
Esempio n. 8
0
def install_package(ns, package, force=False, update=False):
    """
    Install package on system.

    :param package: Instance or instance name of ``LMI_SoftwareIdentity``
        representing package to install.
    :type package: :py:class:`lmi.shell.LMIInstance`
        or :py:class:`lmi.shell.LMIInstanceName`
    :param boolean force: Whether the installation shall be done even if
        installing the same (reinstalling) or older version than already
        installed.
    :param boolean update: Whether this is an update. Update fails if
        package is not already installed on system.
    :returns: Software identity installed on remote system.
        It's an instance ``LMI_SoftwareIdentity``.
    :rtype: :py:class:`lmi.shell.LMIInstance`
    """
    if not isinstance(package, (LMIInstance, LMIInstanceName)):
        raise TypeError("package must be an LMIInstance or LMIInstanceName")
    service = ns.LMI_SoftwareInstallationService.first_instance()
    options = [4 if not update else 5]  # Install (4) or Update (5)
    if force:
        options.append(3) # Force Installation
    # we can not use synchronous invocation because the reference to a job is
    # needed
    results = service.InstallFromSoftwareIdentity(
            Source=package.path
                if isinstance(package, LMIInstance) else package,
            Collection=ns.LMI_SystemSoftwareCollection.first_instance_name(),
            InstallOptions=options)
    nevra = get_package_nevra(package)
    if results.rval != 4096:
        msg = 'Failed to %s package "%s" (rval=%d).' % (
                'update' if update else 'install', nevra, results.rval)
        if results.errorstr:
            msg += ': ' + results.errorstr
        raise LmiFailed(msg)

    job = results.rparams['Job'].to_instance()
    _wait_for_job_finished(job)
    if not LMIJob.lmi_is_job_completed(job):
        if not update:
            if not isinstance(package, LMIInstance):
                try:
                    package = package.to_instance()
                except pywbem.CIMError:
                    pass
            if getattr(package, 'InstallDate', None) is not None:
                raise LmiFailed('Package "%s" is already installed!' % nevra)
        msg = 'Failed to %s package "%s".' % (
                'update' if update else 'install', nevra)
        if job.ErrorDescription:
            msg += ': ' + job.ErrorDescription
        raise LmiFailed(msg)
    else:
        LOG().info('Installed package "%s" on remote host "%s".',
                nevra, ns.connection.uri)

    installed = job.associators(
            Role='AffectingElement',
            ResultRole='AffectedElement',
            AssocClass="LMI_AffectedSoftwareJobElement",
            ResultClass='LMI_SoftwareIdentity')
    if len(installed) < 1:
        raise LmiFailed('Failed to find installed package "%s".' % nevra)
    if len(installed) > 1:
        LOG().warn('Expected just one affected software identity, got: %s',
                {get_package_nevra(p) for p in installed})

    return installed[-1]