/
data_source_uname.go
134 lines (122 loc) · 3.23 KB
/
data_source_uname.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
125
126
127
128
129
130
131
132
133
134
package sys
import (
"crypto/sha1"
"encoding/hex"
"strings"
"regexp"
"os/exec"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
var uname_all_regexp *regexp.Regexp
func init(){
uname_all_regexp = regexp.MustCompile(`^(\S+)\s+(\S+)\s+(\S+)\s+(.+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)$`)
}
func dataSourceUname() *schema.Resource {
return &schema.Resource{
Read: dataSourceUnameRead,
Description: "Return values from the uname executable",
Schema: map[string]*schema.Schema{
"flag": {
Type: schema.TypeString,
Default: "a",
Optional: true,
ForceNew: true,
Description: `Uname flag without the dash: a: all, s: kernel name, n: nodename, r: kernel release, v: kernel version, m: machine, p: processor, i: hardware platform, o: operating system`,
},
"output": {
Type: schema.TypeString,
Computed: true,
Description: `Output from uname command`,
},
"kernel_name": {
Type: schema.TypeString,
Computed: true,
Description: `uname -s`,
},
"nodename": {
Type: schema.TypeString,
Computed: true,
Description: `uname -n`,
},
"kernel_release": {
Type: schema.TypeString,
Computed: true,
Description: `uname -r`,
},
"kernel_version": {
Type: schema.TypeString,
Computed: true,
Description: `uname -v`,
},
"machine": {
Type: schema.TypeString,
Computed: true,
Description: `uname -m`,
},
"processor": {
Type: schema.TypeString,
Computed: true,
Description: `uname -p`,
},
"hardware_platform": {
Type: schema.TypeString,
Computed: true,
Description: `uname -i`,
},
"operating_system": {
Type: schema.TypeString,
Computed: true,
Description: `uname -o`,
},
},
}
}
func dataSourceUnameRead(d *schema.ResourceData, _ interface{}) error {
flag := d.Get("flag").(string)
if len(flag) == 0 {
flag = "-a"
} else if flag[0] != '-' {
if len(flag) > 1 {
flag = "--" + flag
} else {
flag = "-" + flag
}
}
out, err := exec.Command("uname", flag).Output()
if err != nil {
return err
}
out_line := strings.Trim(string(out), "\n\r\t ")
d.Set("output", out_line)
switch flag {
case "-a", "--all":
parts := uname_all_regexp.FindStringSubmatch(out_line)
d.Set("kernel_name", parts[1])
d.Set("nodename", parts[2])
d.Set("kernel_release", parts[3])
d.Set("kernel_version", parts[4])
d.Set("machine", parts[5])
d.Set("processor", parts[6])
d.Set("hardware_platform", parts[7])
d.Set("operating_system", parts[8])
case "-s", "--kernel-name":
d.Set("kernel_name", out_line)
case "-n", "--nodename":
d.Set("nodename", out_line)
case "-r", "--kernel-release":
d.Set("kernel_release", out_line)
case "-v", "--kernel-version":
d.Set("kernel_version", out_line)
case "-m", "--machine":
d.Set("machine", out_line)
case "-p", "--processor":
d.Set("processor", out_line)
case "-i", "--hardware-platform":
d.Set("hardware_platform", out_line)
case "-o", "--operating-system":
d.Set("operating_system", out_line)
}
checksum := sha1.Sum([]byte(flag + "\n" + out_line))
d.SetId(hex.EncodeToString(checksum[:]))
return nil
}