Exemple #1
0
        // For some distros, we don't want to use the full version from VERSION_ID. One example is
        // Red Hat Enterprise Linux, which includes a minor version in their VERSION_ID but minor
        // versions are backwards compatable.
        //
        // In this case, we'll normalized RIDs like 'rhel.7.2' and 'rhel.7.3' to a generic
        // 'rhel.7'. This brings RHEL in line with other distros like CentOS or Debian which
        // don't put minor version numbers in their VERSION_ID fields because all minor versions
        // are backwards compatible.
        private static DistroInfo NormalizeDistroInfo(DistroInfo distroInfo)
        {
            // Handle if VersionId is null by just setting the index to -1.
            int lastVersionNumberSeparatorIndex = distroInfo.VersionId?.IndexOf('.') ?? -1;

            if (lastVersionNumberSeparatorIndex != -1 && distroInfo.Id == "alpine")
            {
                // For Alpine, the version reported has three components, so we need to find the second version separator
                lastVersionNumberSeparatorIndex = distroInfo.VersionId.IndexOf('.', lastVersionNumberSeparatorIndex + 1);
            }

            if (lastVersionNumberSeparatorIndex != -1 && (distroInfo.Id == "rhel" || distroInfo.Id == "alpine"))
            {
                distroInfo.VersionId = distroInfo.VersionId.Substring(0, lastVersionNumberSeparatorIndex);
            }

            // In some distros/versions we cannot discover the distro version; return something valid.
            // Pick a high version number, since this seems to happen on newer distros.
            if (string.IsNullOrEmpty(distroInfo.VersionId))
            {
                distroInfo.VersionId = new Version(Int32.MaxValue, Int32.MaxValue).ToString();
            }

            return(distroInfo);
        }
Exemple #2
0
        private static DistroInfo LoadDistroInfo()
        {
            // Sample os-release file:
            //   NAME="Ubuntu"
            //   VERSION = "14.04.3 LTS, Trusty Tahr"
            //   ID = ubuntu
            //   ID_LIKE = debian
            //   PRETTY_NAME = "Ubuntu 14.04.3 LTS"
            //   VERSION_ID = "14.04"
            //   HOME_URL = "http://www.ubuntu.com/"
            //   SUPPORT_URL = "http://help.ubuntu.com/"
            //   BUG_REPORT_URL = "http://bugs.launchpad.net/ubuntu/"
            // We use ID and VERSION_ID

            if (File.Exists("/etc/os-release"))
            {
                var lines  = File.ReadAllLines("/etc/os-release");
                var result = new DistroInfo();
                foreach (var line in lines)
                {
                    if (line.StartsWith("ID=", StringComparison.Ordinal))
                    {
                        result.Id = line.Substring(3).Trim('"', '\'');
                    }
                    else if (line.StartsWith("VERSION_ID=", StringComparison.Ordinal))
                    {
                        result.VersionId = line.Substring(11).Trim('"', '\'');
                    }
                }
                return(result);
            }
            return(null);
        }
Exemple #3
0
        private static DistroInfo LoadDistroInfoFromIllumos()
        {
            DistroInfo result = null;
            // examples:
            //   on OmniOS
            //       SunOS 5.11 omnios-r151018-95eaa7e
            //   on OpenIndiana Hipster:
            //       SunOS 5.11 illumos-63878f749f
            //   on SmartOS:
            //       SunOS 5.11 joyent_20200408T231825Z
            var versionDescription = RuntimeInformation.OSDescription.Split(' ')[2];

            switch (versionDescription)
            {
            case string version when version.StartsWith("omnios"):
                result.Id = "OmniOS";

                result.VersionId = version.Substring("omnios-r".Length, 2);     // e.g. 15
                break;

            case string version when version.StartsWith("joyent"):
                result.Id = "SmartOS";

                result.VersionId = version.Substring("joyent_".Length, 4);     // e.g. 2020
                break;

            case string version when version.StartsWith("illumos"):
                result.Id = "OpenIndiana";

                // version-less
                break;
            }

            return(result);
        }
        private static DistroInfo GetDistroInfo()
        {
            DistroInfo result = new DistroInfo();

            if (IsFreeBSD)
            {
                result.Id = "FreeBSD";
                // example:
                // FreeBSD 11.0-RELEASE-p1 FreeBSD 11.0-RELEASE-p1 #0 r306420: Thu Sep 29 01:43:23 UTC 2016     [email protected]:/usr/obj/usr/src/sys/GENERIC
                // What we want is major release as minor releases should be compatible.
                result.VersionId = ToVersion(RuntimeInformation.OSDescription.Split()[1].Split('.')[0]);
            }
            else if (File.Exists("/etc/os-release"))
            {
                foreach (string line in File.ReadAllLines("/etc/os-release"))
                {
                    if (line.StartsWith("ID=", StringComparison.Ordinal))
                    {
                        result.Id = line.Substring(3).Trim('"', '\'');
                    }
                    else if (line.StartsWith("VERSION_ID=", StringComparison.Ordinal))
                    {
                        result.VersionId = ToVersion(line.Substring(11).Trim('"', '\''));
                    }
                }
            }

            result.Id ??= "Linux";
            result.VersionId ??= ToVersion(string.Empty);

            return(result);
        }
Exemple #5
0
        private static DistroInfo LoadDistroInfo()
        {
            DistroInfo result = null;

            // Sample os-release file:
            //   NAME="Ubuntu"
            //   VERSION = "14.04.3 LTS, Trusty Tahr"
            //   ID = ubuntu
            //   ID_LIKE = debian
            //   PRETTY_NAME = "Ubuntu 14.04.3 LTS"
            //   VERSION_ID = "14.04"
            //   HOME_URL = "http://www.ubuntu.com/"
            //   SUPPORT_URL = "http://help.ubuntu.com/"
            //   BUG_REPORT_URL = "http://bugs.launchpad.net/ubuntu/"
            // We use ID and VERSION_ID

            if (File.Exists("/etc/os-release"))
            {
                var lines = File.ReadAllLines("/etc/os-release");
                result = new DistroInfo();
                foreach (var line in lines)
                {
                    if (line.StartsWith("ID=", StringComparison.Ordinal))
                    {
                        result.Id = line.Substring(3).Trim('"', '\'');
                    }
                    else if (line.StartsWith("VERSION_ID=", StringComparison.Ordinal))
                    {
                        result.VersionId = line.Substring(11).Trim('"', '\'');
                    }
                }
            }
            else if (File.Exists("/etc/redhat-release"))
            {
                var lines = File.ReadAllLines("/etc/redhat-release");

                if (lines.Length >= 1)
                {
                    string line = lines[0];
                    if (line.StartsWith("Red Hat Enterprise Linux Server release 6.") ||
                        line.StartsWith("CentOS release 6."))
                    {
                        result           = new DistroInfo();
                        result.Id        = "rhel";
                        result.VersionId = "6";
                    }
                }
            }
            // possibly should fall back to /usr/lib/os-release here

            if (result != null)
            {
                result = NormalizeDistroInfo(result);
            }

            return(result);
        }
Exemple #6
0
        public static string GetDistroVersionString()
        {
            if (!RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
            {
                return("");
            }

            DistroInfo v = ParseOsReleaseFile();

            return("Distro=" + v.Id + " VersionId=" + v.VersionId + " Pretty=" + v.PrettyName + " Version=" + v.Version);
        }
Exemple #7
0
        public static string GetDistroVersionString()
        {
            if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
            {
                return("OSX Version=" + s_osxProductVersion.ToString());
            }

            DistroInfo v = GetDistroInfo();

            return("Distro=" + v.Id + " VersionId=" + v.VersionId);
        }
        // For some distros, we don't want to use the full version from VERSION_ID. One example is
        // Red Hat Enterprise Linux, which includes a minor version in their VERSION_ID but minor
        // versions are backwards compatable.
        //
        // In this case, we'll normalized RIDs like 'rhel.7.2' and 'rhel.7.3' to a generic
        // 'rhel.7'. This brings RHEL in line with other distros like CentOS or Debian which
        // don't put minor version numbers in their VERSION_ID fields because all minor versions
        // are backwards compatible.
        private static DistroInfo NormalizeDistroInfo(DistroInfo distroInfo)
        {
            int minorVersionNumberSeparatorIndex = distroInfo.VersionId.IndexOf('.');

            if (distroInfo.Id == "rhel" && minorVersionNumberSeparatorIndex != -1)
            {
                distroInfo.VersionId = distroInfo.VersionId.Substring(0, minorVersionNumberSeparatorIndex);
            }

            return(distroInfo);
        }
Exemple #9
0
        public static string GetDistroVersionString()
        {
            if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
            {
                return("OSX Version=" + s_osxProductVersion.ToString());
            }

            DistroInfo v = ParseOsReleaseFile();

            return("Distro=" + v.Id + " VersionId=" + v.VersionId + " Pretty=" + v.PrettyName + " Version=" + v.Version);
        }
Exemple #10
0
        // For some distros, we don't want to use the full version from VERSION_ID. One example is
        // Red Hat Enterprise Linux, which includes a minor version in their VERSION_ID but minor
        // versions are backwards compatible.
        //
        // In this case, we'll normalized RIDs like 'rhel.7.2' and 'rhel.7.3' to a generic
        // 'rhel.7'. This brings RHEL in line with other distros like CentOS or Debian which
        // don't put minor version numbers in their VERSION_ID fields because all minor versions
        // are backwards compatible.
        private static DistroInfo NormalizeDistroInfo(DistroInfo distroInfo)
        {
            // Handle if VersionId is null by just setting the index to -1.
            int minorVersionNumberSeparatorIndex = distroInfo.VersionId?.IndexOf('.') ?? -1;

            if (distroInfo.Id == "rhel" && minorVersionNumberSeparatorIndex != -1)
            {
                distroInfo.VersionId = distroInfo.VersionId.Substring(0, minorVersionNumberSeparatorIndex);
            }

            return(distroInfo);
        }
        private static bool IsDistroAndVersionOrHigher(Predicate <string> distroPredicate, int major = -1, int minor = -1, int build = -1, int revision = -1)
        {
            if (IsLinux)
            {
                DistroInfo v = GetDistroInfo();
                if (distroPredicate(v.Id) && VersionEquivalentToOrHigher(major, minor, build, revision, v.VersionId))
                {
                    return(true);
                }
            }

            return(false);
        }
Exemple #12
0
        private static bool IsDistroAndVersion(Predicate <string> distroPredicate, int major = -1, int minor = -1, int build = -1, int revision = -1)
        {
            if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
            {
                DistroInfo v = GetDistroInfo();
                if (distroPredicate(v.Id) && VersionEquivalentTo(major, minor, build, revision, v.VersionId))
                {
                    return(true);
                }
            }

            return(false);
        }
Exemple #13
0
        /// <summary>
        /// Get whether the OS platform matches the given Linux distro and optional version.
        /// </summary>
        /// <param name="distroId">The distribution id.</param>
        /// <param name="versionId">The distro version.  If omitted, compares the distro only.</param>
        /// <returns>Whether the OS platform matches the given Linux distro and optional version.</returns>
        private static bool IsDistroAndVersion(string distroId, string versionId = null)
        {
            if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
            {
                DistroInfo v = ParseOsReleaseFile();
                if (v.Id == distroId && (versionId == null || v.VersionId == versionId))
                {
                    return(true);
                }
            }

            return(false);
        }
Exemple #14
0
        private static DistroInfo NormalizeDistroInfo(
            DistroInfo distroInfo)
        {
            string versionId = distroInfo.VersionId;
            int    length    = versionId != null?versionId.IndexOf('.') : -1;

            if (length != -1 && distroInfo.Id == "alpine")
            {
                length = distroInfo.VersionId.IndexOf('.', length + 1);
            }
            if (length != -1 && (distroInfo.Id == "rhel" || distroInfo.Id == "alpine"))
            {
                distroInfo.VersionId = distroInfo.VersionId.Substring(0, length);
            }
            return(distroInfo);
        }
Exemple #15
0

        
Exemple #16
0
        // For some distros, we don't want to use the full version from VERSION_ID. One example is
        // Red Hat Enterprise Linux, which includes a minor version in their VERSION_ID but minor
        // versions are backwards compatable.
        //
        // In this case, we'll normalized RIDs like 'rhel.7.2' and 'rhel.7.3' to a generic
        // 'rhel.7'. This brings RHEL in line with other distros like CentOS or Debian which
        // don't put minor version numbers in their VERSION_ID fields because all minor versions
        // are backwards compatible.
        private static DistroInfo NormalizeDistroInfo(DistroInfo distroInfo)
        {
            // Handle if VersionId is null by just setting the index to -1.
            int lastVersionNumberSeparatorIndex = distroInfo.VersionId?.IndexOf('.') ?? -1;

            if (lastVersionNumberSeparatorIndex != -1 && distroInfo.Id == "alpine")
            {
                // For Alpine, the version reported has three components, so we need to find the second version separator
                lastVersionNumberSeparatorIndex = distroInfo.VersionId.IndexOf('.', lastVersionNumberSeparatorIndex + 1);
            }

            if (lastVersionNumberSeparatorIndex != -1 && (distroInfo.Id == "rhel" || distroInfo.Id == "alpine"))
            {
                distroInfo.VersionId = distroInfo.VersionId.Substring(0, lastVersionNumberSeparatorIndex);
            }

            return(distroInfo);
        }
Exemple #17
0
        private static DistroInfo LoadDistroInfo()
        {
            DistroInfo distroInfo = (DistroInfo)null;

            if (File.Exists("/etc/os-release"))
            {
                string[] strArray = File.ReadAllLines("/etc/os-release");
                distroInfo = new DistroInfo();
                foreach (string str in strArray)
                {
                    if (str.StartsWith("ID=", StringComparison.Ordinal))
                    {
                        distroInfo.Id = str.Substring(3).Trim('"', '\'');
                    }
                    else if (str.StartsWith("VERSION_ID=", StringComparison.Ordinal))
                    {
                        distroInfo.VersionId = str.Substring(11).Trim('"', '\'');
                    }
                }
            }
            else if (File.Exists("/etc/redhat-release"))
            {
                string[] strArray = File.ReadAllLines("/etc/redhat-release");
                if (strArray.Length >= 1)
                {
                    string str = strArray[0];
                    if (str.StartsWith("Red Hat Enterprise Linux Server release 6.") || str.StartsWith("CentOS release 6."))
                    {
                        distroInfo           = new DistroInfo();
                        distroInfo.Id        = "rhel";
                        distroInfo.VersionId = "6";
                    }
                }
            }
            if (distroInfo != null)
            {
                distroInfo = NormalizeDistroInfo(distroInfo);
            }
            return(distroInfo);
        }
Exemple #18
0
        private static DistroInfo ParseOsReleaseFile()
        {
            Debug.Assert(RuntimeInformation.IsOSPlatform(OSPlatform.Linux));

            DistroInfo ret = new DistroInfo();

            ret.Id         = "";
            ret.VersionId  = "";
            ret.Version    = "";
            ret.PrettyName = "";

            if (File.Exists("/etc/os-release"))
            {
                foreach (string line in File.ReadLines("/etc/os-release"))
                {
                    if (line.StartsWith("ID=", System.StringComparison.Ordinal))
                    {
                        ret.Id = RemoveQuotes(line.Substring("ID=".Length));
                    }
                    else if (line.StartsWith("VERSION_ID=", System.StringComparison.Ordinal))
                    {
                        ret.VersionId = RemoveQuotes(line.Substring("VERSION_ID=".Length));
                    }
                    else if (line.StartsWith("VERSION=", System.StringComparison.Ordinal))
                    {
                        ret.Version = RemoveQuotes(line.Substring("VERSION=".Length));
                    }
                    else if (line.StartsWith("PRETTY_NAME=", System.StringComparison.Ordinal))
                    {
                        ret.PrettyName = RemoveQuotes(line.Substring("PRETTY_NAME=".Length));
                    }
                }
            }

            return(ret);
        }
Exemple #19
0
        private static DistroInfo GetDistroInfo()
        {
            DistroInfo result = new DistroInfo();

            if (IsFreeBSD)
            {
                result.Id = "FreeBSD";
                // example:
                // FreeBSD 11.0-RELEASE-p1 FreeBSD 11.0-RELEASE-p1 #0 r306420: Thu Sep 29 01:43:23 UTC 2016     [email protected]:/usr/obj/usr/src/sys/GENERIC
                // What we want is major release as minor releases should be compatible.
                result.VersionId = ToVersion(RuntimeInformation.OSDescription.Split()[1].Split('.')[0]);
            }
            else if (IsIllumos)
            {
                // examples:
                //   on OmniOS
                //       SunOS 5.11 omnios-r151018-95eaa7e
                //   on OpenIndiana Hipster:
                //       SunOS 5.11 illumos-63878f749f
                //   on SmartOS:
                //       SunOS 5.11 joyent_20200408T231825Z
                var versionDescription = RuntimeInformation.OSDescription.Split(' ')[2];
                switch (versionDescription)
                {
                case string version when version.StartsWith("omnios"):
                    result.Id = "OmniOS";

                    result.VersionId = ToVersion(version.Substring("omnios-r".Length, 2));     // e.g. 15
                    break;

                case string version when version.StartsWith("joyent"):
                    result.Id = "SmartOS";

                    result.VersionId = ToVersion(version.Substring("joyent_".Length, 4));     // e.g. 2020
                    break;

                case string version when version.StartsWith("illumos"):
                    result.Id = "OpenIndiana";

                    // version-less
                    break;
                }
            }
            else if (IsSolaris)
            {
                // example:
                //   SunOS 5.11 11.3
                result.Id        = "Solaris";
                result.VersionId = ToVersion(RuntimeInformation.OSDescription.Split(' ')[2]); // e.g. 11.3
            }
            else if (File.Exists("/etc/os-release"))
            {
                foreach (string line in File.ReadAllLines("/etc/os-release"))
                {
                    if (line.StartsWith("ID=", StringComparison.Ordinal))
                    {
                        result.Id = line.Substring(3).Trim('"', '\'');
                    }
                    else if (line.StartsWith("VERSION_ID=", StringComparison.Ordinal))
                    {
                        result.VersionId = ToVersion(line.Substring(11).Trim('"', '\''));
                    }
                }
            }

            result.Id ??= "Linux";
            result.VersionId ??= ToVersion(string.Empty);

            return(result);
        }
Exemple #20
0
        private static DistroInfo ParseOsReleaseFile()
        {
            Debug.Assert(RuntimeInformation.IsOSPlatform(OSPlatform.Linux));

            DistroInfo ret = new DistroInfo();

            ret.Id         = "";
            ret.VersionId  = "";
            ret.Version    = "";
            ret.PrettyName = "";

            if (File.Exists("/etc/os-release"))
            {
                foreach (string line in File.ReadLines("/etc/os-release"))
                {
                    if (line.StartsWith("ID=", System.StringComparison.Ordinal))
                    {
                        ret.Id = RemoveQuotes(line.Substring("ID=".Length));
                    }
                    else if (line.StartsWith("VERSION_ID=", System.StringComparison.Ordinal))
                    {
                        ret.VersionId = RemoveQuotes(line.Substring("VERSION_ID=".Length));
                    }
                    else if (line.StartsWith("VERSION=", System.StringComparison.Ordinal))
                    {
                        ret.Version = RemoveQuotes(line.Substring("VERSION=".Length));
                    }
                    else if (line.StartsWith("PRETTY_NAME=", System.StringComparison.Ordinal))
                    {
                        ret.PrettyName = RemoveQuotes(line.Substring("PRETTY_NAME=".Length));
                    }
                }
            }
            else
            {
                string fileName = null;
                if (File.Exists("/etc/redhat-release"))
                {
                    fileName = "/etc/redhat-release";
                }

                if (fileName == null && File.Exists("/etc/system-release"))
                {
                    fileName = "/etc/system-release";
                }

                if (fileName != null)
                {
                    // Parse the format like the following line:
                    // Red Hat Enterprise Linux Server release 7.3 (Maipo)
                    using (StreamReader file = new StreamReader(fileName))
                    {
                        string line = file.ReadLine();
                        if (!String.IsNullOrEmpty(line))
                        {
                            if (line.StartsWith("Red Hat Enterprise Linux", StringComparison.OrdinalIgnoreCase))
                            {
                                ret.Id = "rhel";
                            }
                            else if (line.StartsWith("Centos", StringComparison.OrdinalIgnoreCase))
                            {
                                ret.Id = "centos";
                            }
                            else if (line.StartsWith("Red Hat", StringComparison.OrdinalIgnoreCase))
                            {
                                ret.Id = "rhl";
                            }
                            else
                            {
                                // automatically generate the distro label
                                string []     words = line.Split(' ');
                                StringBuilder sb    = new StringBuilder();

                                foreach (string word in words)
                                {
                                    if (word.Length > 0)
                                    {
                                        if (Char.IsNumber(word[0]) ||
                                            word.Equals("release", StringComparison.OrdinalIgnoreCase) ||
                                            word.Equals("server", StringComparison.OrdinalIgnoreCase))
                                        {
                                            break;
                                        }
                                        sb.Append(Char.ToLower(word[0]));
                                    }
                                }
                                ret.Id = sb.ToString();
                            }

                            int i = 0;
                            while (i < line.Length && !Char.IsNumber(line[i])) // stop at first number
                            {
                                i++;
                            }

                            if (i < line.Length)
                            {
                                int j = i + 1;
                                while (j < line.Length && (Char.IsNumber(line[j]) || line[j] == '.'))
                                {
                                    j++;
                                }

                                ret.VersionId = line.Substring(i, j - i);
                                ret.Version   = line.Substring(i, line.Length - i);
                            }

                            ret.PrettyName = line;
                        }
                    }
                }
            }

            return(ret);
        }
Exemple #21
0
        private static DistroInfo LoadDistroInfo()
        {
            // Sample os-release file:
            //   NAME="Ubuntu"
            //   VERSION = "14.04.3 LTS, Trusty Tahr"
            //   ID = ubuntu
            //   ID_LIKE = debian
            //   PRETTY_NAME = "Ubuntu 14.04.3 LTS"
            //   VERSION_ID = "14.04"
            //   HOME_URL = "http://www.ubuntu.com/"
            //   SUPPORT_URL = "http://help.ubuntu.com/"
            //   BUG_REPORT_URL = "http://bugs.launchpad.net/ubuntu/"
            // We use ID and VERSION_ID

            if (File.Exists("/etc/os-release"))
            {
                var lines = File.ReadAllLines("/etc/os-release");
                var result = new DistroInfo();
                foreach (var line in lines)
                {
                    if (line.StartsWith("ID=", StringComparison.Ordinal))
                    {
                        result.Id = line.Substring(3).Trim('"', '\'');
                    }
                    else if (line.StartsWith("VERSION_ID=", StringComparison.Ordinal))
                    {
                        result.VersionId = line.Substring(11).Trim('"', '\'');
                    }
                }
                return result;
            }
            return null;
        }