Beispiel #1
0
def system_info(client):
    """Run ohai-solo on a remote system and gather the output.

    :param client: :class:`ssh.SSH` instance
    :returns: dict -- system information from ohai-solo
    :raises: SystemInfoCommandMissing, SystemInfoCommandOld, SystemInfoNotJson
             SystemInfoMissingJson

        SystemInfoCommandMissing if `ohai` is not installed.
        SystemInfoCommandOld if `ohai` is not the latest.
        SystemInfoNotJson if `ohai` does not return valid JSON.
        SystemInfoMissingJson if `ohai` does not return any JSON.
    """
    if client.is_windows():
        return
    else:
        output = client.execute("sudo -i ohai-solo")
        not_found_msgs = ["command not found", "Could not find ohai"]
        if any(m in k for m in not_found_msgs for k in list(output.values())
               if isinstance(k, six.string_types)):
            LOG.warning("SystemInfoCommandMissing on host: [%s]", client.host)
            raise errors.SystemInfoCommandMissing("ohai-solo missing on %s",
                                                  client.host)
        unicode_output = unicode(output['stdout'], errors='replace')
        try:
            results = json.loads(unicode_output)
        except ValueError as exc:
            try:
                clean_output = get_json(unicode_output)
                results = json.loads(clean_output)
            except ValueError as exc:
                raise errors.SystemInfoNotJson(exc)
        return results
Beispiel #2
0
def system_info(client, with_install=False, install_dir=None):
    """Run ohai-solo on a remote system and gather the output.

    :param client: :class:`ssh.SSH` instance
    :param with_install Will install ohai-solo if set to True
    :param install_dir string containing directory to install to
    :returns: dict -- system information from ohai-solo
    :raises: SystemInfoCommandMissing, SystemInfoCommandOld, SystemInfoNotJson
             SystemInfoMissingJson

        SystemInfoCommandMissing if `ohai` is not installed.
        SystemInfoCommandOld if `ohai` is not the latest.
        SystemInfoNotJson if `ohai` does not return valid JSON.
        SystemInfoMissingJson if `ohai` does not return any JSON.
    """
    if with_install:
        perform_install(client, install_dir=install_dir)

    if client.is_windows():
        raise errors.UnsupportedPlatform(
            "ohai-solo is a linux-only sytem info provider. "
            "Target platform was %s", client.platform_info['dist'])

    ohai_solo_prefix = (install_dir or '/opt')
    ohai_solo_command = six.moves.shlex_quote("%s/ohai-solo/bin/ohai-solo"
                                              % ohai_solo_prefix)
    command = ("unset GEM_CACHE GEM_HOME GEM_PATH && "
               "sudo %s" % ohai_solo_command)

    output = client.execute(command, escalate=True, allow_many=False)
    not_found_msgs = ["command not found", "Could not find ohai"]
    if any(m in k for m in not_found_msgs
           for k in list(output.values()) if isinstance(k,
                                                        six.string_types)):
        LOG.warning("SystemInfoCommandMissing on host: [%s]", client.host)
        raise errors.SystemInfoCommandMissing("ohai-solo missing on %s" %
                                              client.host)
    # use string formatting to handle unicode
    unicode_output = "%s" % output['stdout']
    try:
        results = json.loads(unicode_output)
    except ValueError as exc:
        try:
            clean_output = get_json(unicode_output)
            results = json.loads(clean_output)
        except ValueError as exc:
            raise errors.SystemInfoNotJson(exc)
    return results