-
Notifications
You must be signed in to change notification settings - Fork 5
/
fact.go
124 lines (92 loc) · 2.11 KB
/
fact.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package gossh
import (
"fmt"
"strings"
"github.com/pkg/errors"
)
// Fact is a key
type Fact int
//go:generate stringer -type=Fact
const (
// OS is the operating system
OS Fact = iota
// OSFamily is the family of operating systems
OSFamily
// OSVersion is the os version
OSVersion
)
// Facts are gathered from the machine
type Facts struct {
kv map[Fact]string
gathered bool
}
// Gather gathers facts about the machine
func (f *Facts) Gather(m *Host) error {
if f.kv == nil {
f.kv = map[Fact]string{}
}
r, err := m.RunCheck(`cat /etc/*release | tr '[:upper:]' '[:lower:]'`, "", "")
if err != nil {
return errors.Wrap(err, "getting release info errored")
}
if !r.Success() {
return fmt.Errorf("getting release info failed: %s", r.Stderr)
}
kv := parseINI(r.Stdout)
for _, item := range []struct {
key string
fact Fact
}{
{"id", OS},
{"id_like", OSFamily},
{"version_id", OSVersion},
} {
value, ok := kv[item.key]
if ok {
f.kv[item.fact] = value
}
}
// Ensure that debian also set os family debian
if v, ok := f.kv[OS]; ok && v == "debian" {
f.kv[OSFamily] = "debian"
}
// Ensure that fedora also set os family rhel
if v, ok := f.kv[OS]; ok && v == "fedora" {
f.kv[OSFamily] = "rhel"
}
// Set rhel consistently when fedora
if v, ok := f.kv[OSFamily]; ok && strings.Contains(v, "fedora") {
f.kv[OSFamily] = "rhel"
}
// Only return majorversion
f.kv[OSVersion] = majorVersion(f.kv[OSVersion])
f.gathered = true
return nil
}
// parseINI is used to parse single-level ini-type files
func parseINI(in string) map[string]string {
kv := map[string]string{}
lines := strings.Split(in, "\n")
for _, l := range lines {
if len(l) < 1 || l[0] == '#' {
continue
}
parts := strings.SplitN(l, "=", 2)
if len(parts) != 2 {
continue
}
kv[parts[0]] = strings.Trim(parts[1], `"`)
}
return kv
}
// majorversion is a simple util to ensure that only the major version is returned
func majorVersion(in string) string {
if !strings.Contains(in, ".") {
return in
}
parts := strings.Split(in, ".")
if len(parts) == 0 {
return in
}
return parts[0]
}