-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhost.go
233 lines (214 loc) · 5.34 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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package herd
import (
"bytes"
"encoding/base64"
"encoding/json"
"fmt"
"hash/crc32"
"io"
"path/filepath"
"strings"
"github.com/sirupsen/logrus"
"github.com/spf13/cast"
"golang.org/x/crypto/ssh"
)
// Hosts can have attributes of any types, but querying is limited to strings,
// booleans, numbers, nil and slices of these values.
type HostAttributes map[string]interface{}
func (h HostAttributes) prefix(prefix string) HostAttributes {
ret := make(map[string]interface{})
for k, v := range h {
ret[prefix+k] = v
}
return ret
}
// A host represents a remote host. It can be instantiated manually, but is
// usually fetched from one or more Providers, which can all contribute to the
// hosts attributes.
type Host struct {
Name string
Address string
Attributes HostAttributes
Connection io.Closer `yaml:"-" json:"-"`
LastResult *Result `yaml:",omitempty" json:",omitempty"`
publicKeys []ssh.PublicKey
csum uint32
}
type host Host
func (h *Host) UnmarshalJSON(data []byte) error {
var h2 host
d := json.NewDecoder(bytes.NewReader(data))
d.UseNumber()
if err := d.Decode(&h2); err != nil {
return err
}
for k, v := range h2.Attributes {
if n, ok := v.(json.Number); ok {
if i, err := n.Int64(); err == nil {
h2.Attributes[k] = i
} else {
h2.Attributes[k], _ = n.Float64()
}
}
}
*h = Host(h2)
h.init()
return nil
}
func (h *Host) MarshalJSON() ([]byte, error) {
if len(h.publicKeys) > 0 {
keys := make([][]byte, len(h.publicKeys))
for i, key := range h.publicKeys {
keys[i] = key.Marshal()
}
h.Attributes["__publicKeys"] = keys
}
data, err := json.Marshal(host(*h))
delete(h.Attributes, "__publicKeys")
return data, err
}
// Hosts should be initialized with this function, which also initializes any
// internal data, without which SSH connections will not be possible.
func NewHost(name, address string, attributes HostAttributes) *Host {
h := &Host{
Name: name,
Address: address,
Attributes: attributes,
}
h.init()
return h
}
// Set all the defults and initialize ssh configuration for the host
func (h *Host) init() {
h.csum = crc32.ChecksumIEEE([]byte(h.Name))
h.publicKeys = make([]ssh.PublicKey, 0)
if h.Attributes == nil {
h.Attributes = make(HostAttributes)
}
parts := strings.SplitN(h.Name, ".", 2)
h.Attributes["hostname"] = parts[0]
if len(parts) == 2 {
h.Attributes["domainname"] = parts[1]
} else {
h.Attributes["domainname"] = ""
}
if keys, ok := h.Attributes["__publicKeys"]; ok {
for _, k := range keys.([]any) {
b, err := base64.StdEncoding.DecodeString(k.(string))
if err != nil {
logrus.Errorf("Unable to decode marshaledpublic key for %s: %s", h.Name, err)
continue
}
key, err := ssh.ParsePublicKey(b)
if err != nil {
logrus.Errorf("Unable to parse public key for %s: %s", h.Name, err)
}
h.AddPublicKey(key)
}
delete(h.Attributes, "__publicKeys")
}
}
func (h Host) String() string {
return fmt.Sprintf("Host{Name: %s, Keys: %d, Attributes: %s}", h.Name, len(h.publicKeys), h.Attributes)
}
// Adds a public key to a host, it will be used by the SSH client when connecting
func (h *Host) AddPublicKey(k ssh.PublicKey) {
h.publicKeys = append(h.publicKeys, k)
}
func (h *Host) PublicKeys() []ssh.PublicKey {
return h.publicKeys
}
func (h *Host) Match(hostnameGlob string, attributes MatchAttributes) bool {
if hostnameGlob != "" {
ok, err := filepath.Match(hostnameGlob, h.Name)
if !ok || err != nil {
return false
}
}
for _, attribute := range attributes {
name := attribute.Name
value, ok := h.GetAttribute(name)
if !ok && !attribute.Negate {
return false
}
if ok && !attribute.Match(value) {
return false
}
}
return true
}
func (h *Host) GetAttribute(key string) (interface{}, bool) {
value, ok := h.Attributes[key]
if ok {
return value, ok
}
r := h.LastResult
if r == nil {
r = &Result{ExitStatus: -1}
}
switch key {
case "name":
return h.Name, true
case "random":
return h.csum, true
case "address":
return h.Address, true
case "stdout":
return string(r.Stdout), true
case "stderr":
return string(r.Stderr), true
case "exitstatus":
return r.ExitStatus, true
case "err":
return r.Err, true
}
return nil, false
}
func (h *Host) Amend(h2 *Host) {
if h.Address == "" {
h.Address = h2.Address
}
if h2.Attributes["herd_provider"] != nil {
if h.Attributes["herd_provider"] == nil {
h.Attributes["herd_provider"] = make([]string, 0)
}
h.Attributes["herd_provider"] = append(h.Attributes["herd_provider"].([]string), h2.Attributes["herd_provider"].([]string)[0])
}
for attr, value := range h2.Attributes {
if attr == "herd_provider" {
continue
}
h.Attributes[attr] = value
}
for _, k := range h2.publicKeys {
h.AddPublicKey(k)
}
}
func (h *Host) less(h2 *Host, attributes []string) bool {
for _, attr := range attributes {
v1, ok1 := h.GetAttribute(attr)
v2, ok2 := h2.GetAttribute(attr)
// Sort nodes that are missing the attribute last
if ok1 && !ok2 {
return true
}
if !ok1 && ok2 {
return false
}
if !ok1 && !ok2 {
continue
}
// Compare the string values, this way we don't need to check a matrix of types
s1, err1 := cast.ToStringE(v1)
s2, err2 := cast.ToStringE(v2)
if err1 != nil || err2 != nil {
continue
}
// When equal, continue to the next field
if s1 == s2 {
continue
}
return s1 < s2
}
return h.Name < h2.Name
}