def __init__(self, cv, ci, cr):
     if cv:
         self.version = cv.version
         self.major = cv.major
         self.minor = cv.minor
         self.is_els = cv.is_els
         self.downstream_release = cv.downstream_release
         self.upstream_version = cv.upstream_version
     elif ci:
         context = Context(
             content=ci.data["version"]["full"].strip().splitlines())
         cv = CephV(context)
         self.version = cv.version
         self.major = cv.major
         self.minor = cv.minor
         self.is_els = cv.is_els
         self.downstream_release = cv.downstream_release
         self.upstream_version = cv.upstream_version
     else:
         context = Context(content=cr["version"].strip().splitlines())
         cv = CephV(context)
         self.version = cv.version
         self.major = cv.major
         self.minor = cv.minor
         self.is_els = cv.is_els
         self.downstream_release = cv.downstream_release
         self.upstream_version = cv.upstream_version
Exemple #2
0
def os_release_info():
    '''
    Use insights-core to fetch the os-release or redhat-release info

    Returns a tuple of OS name and version
    '''
    os_family = "Unknown"
    os_release = ""
    for p in ["/etc/os-release", "/etc/redhat-release"]:
        try:
            with open(p) as f:
                data = f.readlines()

            ctx = Context(content=data, path=p, relative_path=p)
            if p == "/etc/os-release":
                rls = OsRelease(ctx)
                os_family = rls.data.get("NAME")
                os_release = rls.data.get("VERSION_ID")
            elif p == "/etc/redhat-release":
                rls = RedhatRelease(ctx)
                os_family = rls.product
                os_release = rls.version
            break
        except IOError:
            continue
        except Exception as e:
            logger.warning("Failed to detect OS version: %s", e)
    return (os_family, os_release)
Exemple #3
0
def context_wrap(lines,
                 path="path",
                 hostname=DEFAULT_HOSTNAME,
                 release=DEFAULT_RELEASE,
                 version="-1.-1",
                 machine_id="machine_id",
                 strip=True,
                 split=True,
                 filtered_spec=None,
                 **kwargs):
    if isinstance(lines, six.string_types):
        if strip:
            lines = lines.strip()
        if split:
            lines = lines.splitlines()

    if filtered_spec is not None and filtered_spec in filters.FILTERS:
        lines = [
            l for l in lines
            if any([f in l for f in filters.FILTERS[filtered_spec]])
        ]

    return Context(content=lines,
                   path=path,
                   hostname=hostname,
                   release=release,
                   version=version.split("."),
                   machine_id=machine_id,
                   relative_path=path,
                   **kwargs)
    def user_agent(self):
        """
        Generates and returns a string suitable for use as a request user-agent
        """
        core_version = "insights-core"
        pkg = pkg_resources.working_set.find(pkg_resources.Requirement.parse(core_version))
        if pkg is not None:
            core_version = "%s %s" % (pkg.project_name, pkg.version)
        else:
            core_version = "Core %s" % package_info["VERSION"]

        client_version = "insights-client"
        pkg = pkg_resources.working_set.find(pkg_resources.Requirement.parse(client_version))
        if pkg is not None:
            client_version = "%s/%s" % (pkg.project_name, pkg.version)

        requests_version = None
        pkg = pkg_resources.working_set.find(pkg_resources.Requirement.parse("requests"))
        if pkg is not None:
            requests_version = "%s %s" % (pkg.project_name, pkg.version)

        python_version = "%s %s" % (platform.python_implementation(), platform.python_version())

        os_family = "Unknown"
        os_release = ""
        for p in ["/etc/os-release", "/etc/redhat-release"]:
            try:
                with open(p) as f:
                    data = f.readlines()

                ctx = Context(content=data, path=p, relative_path=p)
                if p == "/etc/os-release":
                    rls = OsRelease(ctx)
                    os_family = rls.data.get("NAME")
                    os_release = rls.data.get("VERSION_ID")
                elif p == "/etc/redhat-release":
                    rls = RedhatRelease(ctx)
                    os_family = rls.product
                    os_release = rls.version
                break
            except IOError:
                continue
            except Exception as e:
                logger.warning("Failed to detect OS version: %s", e)
        kernel_version = "%s %s" % (platform.system(), platform.release())

        ua = "{client_version} ({core_version}; {requests_version}) {os_family} {os_release} ({python_version}; {kernel_version})".format(
            client_version=client_version,
            core_version=core_version,
            python_version=python_version,
            os_family=os_family,
            os_release=os_release,
            kernel_version=kernel_version,
            requests_version=requests_version,
        )

        return ua
Exemple #5
0
    def from_uname_str(cls, uname_str):
        """
        Create a Uname object from a string containing the output of 'uname -a'.

        :Parameters:
            - `uname_str` - the string output of `uname -a`
        """
        if len(uname_str.split(' ')) == 1:
            uname_str = "Linux " + "hostname " + uname_str
        return cls(Context(content=[uname_str.strip()], path=None))
Exemple #6
0
def context_wrap(lines,
                 path="path",
                 hostname=DEFAULT_HOSTNAME,
                 release=DEFAULT_RELEASE,
                 version="-1.-1",
                 machine_id="machine_id",
                 **kwargs):
    if isinstance(lines, basestring):
        lines = lines.strip().splitlines()
    return Context(content=lines,
                   path=path,
                   hostname=hostname,
                   release=release,
                   version=version.split("."),
                   machine_id=machine_id,
                   **kwargs)
Exemple #7
0
def context_wrap(lines,
                 path="path",
                 hostname=DEFAULT_HOSTNAME,
                 release=DEFAULT_RELEASE,
                 version="-1.-1",
                 machine_id="machine_id",
                 strip=True,
                 **kwargs):
    if isinstance(lines, six.string_types):
        if strip:
            lines = lines.strip()
        lines = lines.splitlines()

    return Context(content=lines,
                   path=path, hostname=hostname,
                   release=release, version=version.split("."),
                   machine_id=machine_id, relative_path=path, **kwargs)
Exemple #8
0
    def from_kernel(cls, kernel):
        """
        Create a Uname object from a kernel NVR (e.g. '2.6.32-504.el6.x86_64').

        :Parameters:
            - `kernel` - the kernel version and release string.
        """
        data = cls.parse_nvr(kernel, arch=False)
        content = ["{name} {nodename} {kernel} {kernel_type} {kernel_date} {machine} {processor} {hw_platform} {os}".format(
            name="Linux",
            nodename=data['nodename'],
            kernel=kernel,
            kernel_type=data['kernel_type'],
            kernel_date=data['kernel_date'],
            machine=data['machine'],
            processor=data['processor'],
            hw_platform=data['hw_platform'],
            os=data['os'])]
        return cls(Context(content=content, path=None))