-
Notifications
You must be signed in to change notification settings - Fork 44
/
host.go
79 lines (67 loc) · 2.13 KB
/
host.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package distro
import (
"bufio"
"errors"
"fmt"
"io"
"os"
"path"
"strings"
)
// variable so that it can be overridden in tests
var getHostDistroNameTree = "/"
// GetHostDistroName returns the name of the host distribution, such as
// "fedora-32" or "rhel-8.2". It does so by reading the /etc/os-release file.
func GetHostDistroName() (string, error) {
osrelease, err := ReadOSReleaseFromTree(getHostDistroNameTree)
if err != nil {
return "", fmt.Errorf("cannot get the host distro name: %w", err)
}
if _, ok := osrelease["ID"]; !ok {
return "", errors.New("cannot get the host distro name: missing ID field in os-release")
}
if _, ok := osrelease["VERSION_ID"]; !ok {
return "", errors.New("cannot get the host distro name: missing VERSION_ID field in os-release")
}
name := osrelease["ID"] + "-" + osrelease["VERSION_ID"]
return name, nil
}
func readOSRelease(r io.Reader) (map[string]string, error) {
osrelease := make(map[string]string)
scanner := bufio.NewScanner(r)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if len(line) == 0 {
continue
}
parts := strings.SplitN(line, "=", 2)
if len(parts) != 2 {
return nil, errors.New("readOSRelease: invalid input")
}
key := strings.TrimSpace(parts[0])
// drop all surrounding whitespace and double-quotes
value := strings.Trim(strings.TrimSpace(parts[1]), "\"")
osrelease[key] = value
}
return osrelease, nil
}
// ReadOSReleaseFromTree reads the os-release file from the given root directory.
//
// According to os-release(5), the os-release file should be located in either /etc/os-release or /usr/lib/os-release,
// so both locations are tried, with the former taking precedence.
func ReadOSReleaseFromTree(root string) (map[string]string, error) {
locations := []string{
"etc/os-release",
"usr/lib/os-release",
}
var errs []string
for _, location := range locations {
f, err := os.Open(path.Join(root, location))
if err == nil {
defer f.Close()
return readOSRelease(f)
}
errs = append(errs, fmt.Sprintf("cannot read %s: %v", location, err))
}
return nil, fmt.Errorf("failed to read os-release:\n%s", strings.Join(errs, "\n"))
}