// https://github.com/serverspec/specinfra/blob/master/lib/specinfra/helper/detect_os/redhat.rb func detectRedhat(c config.ServerInfo) (itsMe bool, red osTypeInterface) { red = newRedhat(c) // set sudo option flag c.SudoOpt = config.SudoOption{ExecBySudo: true} red.setServerInfo(c) if r := sshExec(c, "ls /etc/fedora-release", noSudo); r.isSuccess() { red.setDistributionInfo("fedora", "unknown") Log.Warn("Fedora not tested yet. Host: %s:%s", c.Host, c.Port) return true, red } if r := sshExec(c, "ls /etc/redhat-release", noSudo); r.isSuccess() { // https://www.rackaid.com/blog/how-to-determine-centos-or-red-hat-version/ // e.g. // $ cat /etc/redhat-release // CentOS release 6.5 (Final) if r := sshExec(c, "cat /etc/redhat-release", noSudo); r.isSuccess() { re, _ := regexp.Compile(`(.*) release (\d[\d.]*)`) result := re.FindStringSubmatch(strings.TrimSpace(r.Stdout)) if len(result) != 3 { Log.Warn( "Failed to parse RedHat/CentOS version. stdout: %s, Host: %s:%s", r.Stdout, c.Host, c.Port) return true, red } release := result[2] switch strings.ToLower(result[1]) { case "centos", "centos linux": red.setDistributionInfo("centos", release) default: red.setDistributionInfo("rhel", release) } return true, red } return true, red } if r := sshExec(c, "ls /etc/system-release", noSudo); r.isSuccess() { family := "amazon" release := "unknown" if r := sshExec(c, "cat /etc/system-release", noSudo); r.isSuccess() { fields := strings.Fields(r.Stdout) if len(fields) == 5 { release = fields[4] } } red.setDistributionInfo(family, release) return true, red } Log.Debugf("Not RedHat like Linux. Host: %s:%s", c.Host, c.Port) return false, red }
// Ubuntu, Debian // https://github.com/serverspec/specinfra/blob/master/lib/specinfra/helper/detect_os/debian.rb func detectDebian(c config.ServerInfo) (itsMe bool, deb osTypeInterface, err error) { deb = newDebian(c) // set sudo option flag c.SudoOpt = config.SudoOption{ExecBySudo: true} deb.setServerInfo(c) if r := sshExec(c, "ls /etc/debian_version", noSudo); !r.isSuccess() { if r.ExitStatus == 255 { return false, deb, fmt.Errorf( "Unable to connect via SSH. Check SSH settings. servername: %s, %s@%s:%s, status: %d, stdout: %s, stderr: %s", c.ServerName, c.User, c.Host, c.Port, r.ExitStatus, r.Stdout, r.Stderr, ) } Log.Debugf("Not Debian like Linux. Host: %s:%s", c.Host, c.Port) return false, deb, nil } if r := sshExec(c, "lsb_release -ir", noSudo); r.isSuccess() { // e.g. // root@fa3ec524be43:/# lsb_release -ir // Distributor ID: Ubuntu // Release: 14.04 re, _ := regexp.Compile( `(?s)^Distributor ID:\s*(.+?)\n*Release:\s*(.+?)$`) result := re.FindStringSubmatch(trim(r.Stdout)) if len(result) == 0 { deb.setDistributionInfo("debian/ubuntu", "unknown") Log.Warnf( "Unknown Debian/Ubuntu version. lsb_release -ir: %s, Host: %s:%s", r.Stdout, c.Host, c.Port) } else { distro := strings.ToLower(trim(result[1])) deb.setDistributionInfo(distro, trim(result[2])) } return true, deb, nil } if r := sshExec(c, "cat /etc/lsb-release", noSudo); r.isSuccess() { // e.g. // DISTRIB_ID=Ubuntu // DISTRIB_RELEASE=14.04 // DISTRIB_CODENAME=trusty // DISTRIB_DESCRIPTION="Ubuntu 14.04.2 LTS" re, _ := regexp.Compile( `(?s)^DISTRIB_ID=(.+?)\n*DISTRIB_RELEASE=(.+?)\n.*$`) result := re.FindStringSubmatch(trim(r.Stdout)) if len(result) == 0 { Log.Warnf( "Unknown Debian/Ubuntu. cat /etc/lsb-release: %s, Host: %s:%s", r.Stdout, c.Host, c.Port) deb.setDistributionInfo("debian/ubuntu", "unknown") } else { distro := strings.ToLower(trim(result[1])) deb.setDistributionInfo(distro, trim(result[2])) } return true, deb, nil } // Debian cmd := "cat /etc/debian_version" if r := sshExec(c, cmd, noSudo); r.isSuccess() { deb.setDistributionInfo("debian", trim(r.Stdout)) return true, deb, nil } Log.Debugf("Not Debian like Linux. Host: %s:%s", c.Host, c.Port) return false, deb, nil }