示例#1
0
def get_hostname(ns):
    """
    :returns: System hostname.
    :rtype: String
    """
    i = get_computer_system(ns)
    return i.Name
示例#2
0
def get_hostname(ns):
    """
    :returns: Tabular data of system hostname.
    :rtype: List of tuples
    """
    i = get_computer_system(ns)
    return [('Computer Name:', i.Name)]
示例#3
0
def install_from_uri(ns, uri, force=False, update=False):
    """
    Install package from *URI* on remote system.

    :param string uri: Identifier of *RPM* package available via http, https,
        or ftp service.
    :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.
    """
    if not isinstance(uri, basestring):
        raise TypeError("uri must be a string")
    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
    results = service.SyncInstallFromURI(URI=uri,
                                         Target=get_computer_system(ns).path,
                                         InstallOptions=options)
    if results.rval != 0:
        msg = 'Failed to %s package from uri (rval=%d).' % (
            'update' if update else 'install', results.rval)
        if results.errorstr:
            msg += ': ' + results.errorstr
        raise LmiFailed(msg)
    else:
        LOG().info('Installed package from uri %s.', uri)
示例#4
0
def get_service(ns, service):
    """
    Return :py:class:`lmi.shell.LMIInstance` object matching the given
    service name.

    :param string service: Service name.
    """
    if isinstance(service, basestring):
        cs = get_computer_system(ns)
        iname = ns.LMI_Service.new_instance_name({
            "Name": service,
            "CreationClassName" : "LMI_Service",
            "SystemName" : cs.Name,
            "SystemCreationClassName" : cs.CreationClassName
        })
        inst = iname.to_instance()
        if inst is None:
            raise LmiFailed('No such service "%s"' % service)
    elif isinstance(service, (LMIInstance, LMIInstanceName)):
        inst = service
        service = inst.Name
        if isinstance(inst, LMIInstanceName):
            inst = inst.to_instance()
    else:
        raise TypeError("service must be either string or ``LMIInstanceName``")

    return inst
示例#5
0
def install_from_uri(ns, uri, force=False, update=False):
    """
    Install package from *URI* on remote system.

    :param string uri: Identifier of *RPM* package available via http, https,
        or ftp service.
    :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.
    """
    if not isinstance(uri, basestring):
        raise TypeError("uri must be a string")
    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
    results = service.SyncInstallFromURI(
            URI=uri,
            Target=get_computer_system(ns).path,
            InstallOptions=options)
    if results.rval != 0:
        msg = 'Failed to %s package from uri (rval=%d).' % (
                'update' if update else 'install', results.rval)
        if results.errorstr:
            msg += ': ' + results.errorstr
        raise LmiFailed(msg)
    else:
        LOG().info('Installed package from uri.')
示例#6
0
def get_system_info(ns):
    """
    :returns: Tabular data of system info.
    :rtype: List of tuples
    """
    i = get_computer_system(ns)
    return [('Hostname:', i.Name)]
示例#7
0
def get_hostname(ns):
    """
    :returns: Tabular data of system hostname.
    :rtype: List of tuples
    """
    i = get_computer_system(ns)
    return [('Hostname:', i.Name)]
示例#8
0
def get_hostname(ns):
    """
    :returns: System hostname.
    :rtype: String
    """
    i = get_computer_system(ns)
    return i.Name
示例#9
0
def get_uf_name(ns, target):
    system = get_computer_system(ns)
    uf_name = ns.LMI_UnixFile.new_instance_name(
        {'CSCreationClassName':system.classname,
         'CSName':system.name,
         'LFCreationClassName':'ignored',
         'FSCreationClassName':'ignored',
         'FSName':'ignored',
         'LFName':target})
    return uf_name
示例#10
0
def get_uf_name(ns, target):
    system = get_computer_system(ns)
    uf_name = ns.LMI_UnixFile.new_instance_name({
        'CSCreationClassName': system.classname,
        'CSName': system.name,
        'LFCreationClassName': 'ignored',
        'FSCreationClassName': 'ignored',
        'FSName': 'ignored',
        'LFName': target
    })
    return uf_name
示例#11
0
def list_installed_packages(ns):
    """
    Yields instances of ``LMI_SoftwareIdentity`` representing installed packages.

    :rtype: generator
    """
    for identity in get_computer_system(ns).associators(
            Role="System",
            ResultRole="InstalledSoftware",
            AssocClass='LMI_InstalledSoftwareIdentity',
            ResultClass="LMI_SoftwareIdentity"):
        yield identity
示例#12
0
def list_installed_packages(ns):
    """
    Yields instances of ``LMI_SoftwareIdentity`` representing installed packages.

    :rtype: generator
    """
    for identity in get_computer_system(ns).associators(
            Role="System",
            ResultRole="InstalledSoftware",
            AssocClass='LMI_InstalledSoftwareIdentity',
            ResultClass="LMI_SoftwareIdentity"):
        yield identity
示例#13
0
def get_service(ns, service):
    """
    Return :py:class:`lmi.shell.LMIInstance` object matching the given
    service name.

    :param string service: Service name.
    """
    try:
        if isinstance(service, basestring):
            if not service.endswith('.service'):
                service += '.service'
            cs = get_computer_system(ns)
            iname = ns.LMI_Service.new_instance_name({
                "Name":
                service,
                "CreationClassName":
                "LMI_Service",
                "SystemName":
                cs.Name,
                "SystemCreationClassName":
                cs.CreationClassName
            })
            inst = iname.to_instance()

        elif isinstance(service, (LMIInstance, LMIInstanceName)):
            try:
                inst = service
                service = inst.Name
                if isinstance(inst, LMIInstanceName):
                    inst = inst.to_instance()
            except AttributeError:
                raise ValueError('Invalid service instance name. It\'s missing'
                                 ' Name property.')

        else:
            raise TypeError(
                "service must be either string or ``LMIInstanceName``")

    except wbem.CIMError as err:
        if err.args[0] == wbem.CIM_ERR_NOT_FOUND:
            raise LmiFailed('No such service "%s".' % service)
        else:
            raise LmiFailed('Failed to get service "%s": %s' %
                            (service, err.args[1]))

    if inst is None:
        raise LmiFailed('No such service "%s".' % service)

    return inst
示例#14
0
def get_directory_name_properties(ns, directory):
    """
    Retrieve object path of a directory.

    :type directory: string
    :param directory: Full path to the directory.
    :rtype: LMIInstanceName
    """
    system = get_computer_system(ns)
    return {'CSCreationClassName':system.classname,
            'CSName':system.name,
            'CreationClassName':'LMI_UnixDirectory',
            'FSCreationClassName':'LMI_LocalFileSystem',
            'FSName':'',
            'Name':directory}
示例#15
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
示例#16
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
示例#17
0
def get_service(ns, service):
    """
    Return :py:class:`lmi.shell.LMIInstance` object matching the given
    service name.

    :param string service: Service name.
    """
    try:
        if isinstance(service, basestring):
            if not service.endswith('.service'):
                service += '.service'
            cs = get_computer_system(ns)
            iname = ns.LMI_Service.new_instance_name({
                "Name": service,
                "CreationClassName" : "LMI_Service",
                "SystemName" : cs.Name,
                "SystemCreationClassName" : cs.CreationClassName
            })
            inst = iname.to_instance()

        elif isinstance(service, (LMIInstance, LMIInstanceName)):
            try:
                inst = service
                service = inst.Name
                if isinstance(inst, LMIInstanceName):
                    inst = inst.to_instance()
            except AttributeError:
                raise ValueError('Invalid service instance name. It\'s missing'
                        ' Name property.')

        else:
            raise TypeError("service must be either string or ``LMIInstanceName``")

    except wbem.CIMError as err:
        if err.args[0] == wbem.CIM_ERR_NOT_FOUND:
            raise LmiFailed('No such service "%s".' % service)
        else:
            raise LmiFailed('Failed to get service "%s": %s'
                    % (service, err.args[1]))

    if inst is None:
        raise LmiFailed('No such service "%s".' % service)

    return inst
示例#18
0
def lf_show(ns, target):
    """
    Show detailed information about the target.

    Target can be either a file or a directory.

    :type target: string
    :param target: Full path to the target.
    """
    system = get_computer_system(ns)
    uf_name = ns.LMI_UnixFile.new_instance_name({
        'CSCreationClassName': system.classname,
        'CSName': system.name,
        'LFCreationClassName': 'ignored',
        'FSCreationClassName': 'ignored',
        'FSName': 'ignored',
        'LFName': target
    })
    try:
        uf = uf_name.to_instance()
    except CIMError as err:
        raise LmiFailed('Could not get target "%s": %s' % (target, err))

    ident = uf.associators(AssocClass='LMI_FileIdentity')[0]

    yield fcmd.NewTableCommand(title=uf.Name)
    yield ('Type', get_file_identification(ident))
    yield ('Readable', ident.Readable)
    yield ('Writeable', ident.Writeable)
    yield ('Executable', ident.Executable)
    yield ('UserID', uf.UserID)
    yield ('GroupID', uf.GroupID)
    yield ('SaveText', uf.SaveText)
    yield ('SetGid', uf.SetGid)
    yield ('SetUid', uf.SetUid)
    yield ('FileSize', ident.FileSize)
    yield ('LastAccessed', ident.LastAccessed)
    yield ('LastModified', ident.LastModified)
    yield ('FileInodeNumber', uf.FileInodeNumber)
    yield ('SELinuxCurrentContext', uf.SELinuxCurrentContext)
    yield ('SELinuxExpectedContext', uf.SELinuxExpectedContext)
示例#19
0
def lf_show(ns, target):
    """
    Show detailed information about the target.

    Target can be either a file or a directory.

    :type target: string
    :param target: Full path to the target.
    """
    system = get_computer_system(ns)
    uf_name = ns.LMI_UnixFile.new_instance_name(
        {'CSCreationClassName':system.classname,
         'CSName':system.name,
         'LFCreationClassName':'ignored',
         'FSCreationClassName':'ignored',
         'FSName':'ignored',
         'LFName':target})
    try:
        uf = uf_name.to_instance()
    except CIMError as err:
        raise LmiFailed('Could not get target "%s": %s' % (target, err))

    ident = uf.associators(AssocClass='LMI_FileIdentity')[0]

    yield fcmd.NewTableCommand(title=uf.Name)
    yield('Type', get_file_identification(ident))
    yield('Readable', ident.Readable)
    yield('Writeable', ident.Writeable)
    yield('Executable', ident.Executable)
    yield('UserID', uf.UserID)
    yield('GroupID', uf.GroupID)
    yield('SaveText', uf.SaveText)
    yield('SetGid', uf.SetGid)
    yield('SetUid', uf.SetUid)
    yield('FileSize', ident.FileSize)
    yield('LastAccessed', ident.LastAccessed)
    yield('LastModified', ident.LastModified)
    yield('FileInodeNumber', uf.FileInodeNumber)
    yield('SELinuxCurrentContext', uf.SELinuxCurrentContext)
    yield('SELinuxExpectedContext', uf.SELinuxExpectedContext)