2015-07-25 08:35:07 +00:00
|
|
|
// Package operatingsystem provides helper function to get the operating system
|
|
|
|
// name for different platforms.
|
2014-07-24 15:42:38 +00:00
|
|
|
package operatingsystem
|
|
|
|
|
|
|
|
import (
|
2015-10-29 08:16:21 +00:00
|
|
|
"bufio"
|
2014-07-24 15:42:38 +00:00
|
|
|
"bytes"
|
2015-10-29 08:16:21 +00:00
|
|
|
"fmt"
|
2014-07-24 15:42:38 +00:00
|
|
|
"io/ioutil"
|
2015-10-29 08:16:21 +00:00
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/mattn/go-shellwords"
|
2014-07-24 15:42:38 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// file to use to detect if the daemon is running in a container
|
|
|
|
proc1Cgroup = "/proc/1/cgroup"
|
|
|
|
|
|
|
|
// file to check to determine Operating System
|
|
|
|
etcOsRelease = "/etc/os-release"
|
2015-11-17 00:19:14 +00:00
|
|
|
|
|
|
|
// used by stateless systems like Clear Linux
|
|
|
|
altOsRelease = "/usr/lib/os-release"
|
2014-07-24 15:42:38 +00:00
|
|
|
)
|
|
|
|
|
2015-07-25 08:35:07 +00:00
|
|
|
// GetOperatingSystem gets the name of the current operating system.
|
2014-07-24 15:42:38 +00:00
|
|
|
func GetOperatingSystem() (string, error) {
|
2015-10-29 08:16:21 +00:00
|
|
|
osReleaseFile, err := os.Open(etcOsRelease)
|
2014-07-24 15:42:38 +00:00
|
|
|
if err != nil {
|
2015-11-17 00:19:14 +00:00
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
return "", fmt.Errorf("Error opening %s: %v", etcOsRelease, err)
|
|
|
|
}
|
|
|
|
osReleaseFile, err = os.Open(altOsRelease)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("Error opening %s: %v", altOsRelease, err)
|
|
|
|
}
|
2014-07-24 15:42:38 +00:00
|
|
|
}
|
2015-10-29 08:16:21 +00:00
|
|
|
defer osReleaseFile.Close()
|
|
|
|
|
|
|
|
var prettyName string
|
|
|
|
scanner := bufio.NewScanner(osReleaseFile)
|
|
|
|
for scanner.Scan() {
|
|
|
|
line := scanner.Text()
|
|
|
|
if strings.HasPrefix(line, "PRETTY_NAME=") {
|
|
|
|
data := strings.SplitN(line, "=", 2)
|
|
|
|
prettyNames, err := shellwords.Parse(data[1])
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("PRETTY_NAME is invalid: %s", err.Error())
|
|
|
|
}
|
|
|
|
if len(prettyNames) != 1 {
|
|
|
|
return "", fmt.Errorf("PRETTY_NAME needs to be enclosed by quotes if they have spaces: %s", data[1])
|
|
|
|
}
|
|
|
|
prettyName = prettyNames[0]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if prettyName != "" {
|
|
|
|
return prettyName, nil
|
2014-07-24 15:42:38 +00:00
|
|
|
}
|
2015-10-29 08:16:21 +00:00
|
|
|
// If not set, defaults to PRETTY_NAME="Linux"
|
|
|
|
// c.f. http://www.freedesktop.org/software/systemd/man/os-release.html
|
|
|
|
return "Linux", nil
|
2014-07-24 15:42:38 +00:00
|
|
|
}
|
|
|
|
|
2015-07-25 08:35:07 +00:00
|
|
|
// IsContainerized returns true if we are running inside a container.
|
2014-07-24 15:42:38 +00:00
|
|
|
func IsContainerized() (bool, error) {
|
|
|
|
b, err := ioutil.ReadFile(proc1Cgroup)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
for _, line := range bytes.Split(b, []byte{'\n'}) {
|
2015-10-28 02:00:07 +00:00
|
|
|
if len(line) > 0 && !bytes.HasSuffix(line, []byte{'/'}) && !bytes.HasSuffix(line, []byte("init.scope")) {
|
2014-07-24 15:42:38 +00:00
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false, nil
|
|
|
|
}
|