-
Notifications
You must be signed in to change notification settings - Fork 13
/
salt_node_manager.go
369 lines (321 loc) · 12.7 KB
/
salt_node_manager.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/*Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package saltnodemanager
import (
"errors"
"fmt"
"github.com/skyrings/skyring-common/conf"
"github.com/skyrings/skyring-common/db"
"github.com/skyrings/skyring-common/models"
"github.com/skyrings/skyring-common/monitoring"
"github.com/skyrings/skyring-common/tools/logger"
"github.com/skyrings/skyring-common/tools/uuid"
"github.com/skyrings/skyring/backend/salt"
"github.com/skyrings/skyring/nodemanager"
"gopkg.in/mgo.v2/bson"
"io"
"net"
)
const (
NodeManagerName = "SaltNodeManager"
)
var (
salt_backend = salt.New()
)
type SaltNodeManager struct {
}
func init() {
nodemanager.RegisterNodeManager(NodeManagerName, func(config io.Reader) (nodemanager.NodeManagerInterface, error) {
return NewSaltNodeManager(config)
})
}
func NewSaltNodeManager(config io.Reader) (*SaltNodeManager, error) {
return &SaltNodeManager{}, nil
}
func (a SaltNodeManager) AcceptNode(node string, fingerprint string, ctxt string) (bool, error) {
if status, err := salt_backend.AcceptNode(node, fingerprint, false, ctxt); err != nil {
return false, err
} else if !status {
return false, errors.New(fmt.Sprintf("Unable to accept the node: %s", node))
} else {
return true, nil
}
}
func (a SaltNodeManager) AddNode(master string, node string, port uint, fingerprint string, username string, password string, ctxt string) (bool, error) {
if status, err := salt_backend.AddNode(master, node, port, fingerprint, username, password, ctxt); err != nil {
return false, err
} else if !status {
return false, errors.New(fmt.Sprintf("Unable to add the node: %s", node))
} else {
return true, nil
}
}
func SetupSkynetService(hostname string, ctxt string) error {
if ok, err := salt_backend.SetupSkynetService(hostname, ctxt); err != nil || !ok {
logger.Get().Error("%s-could not setup skynet service on the node: %s", ctxt, hostname)
return errors.New(fmt.Sprintf("could not setup skynet service on the node: %s", hostname))
}
return nil
}
func GetStorageNodeInstance(hostname string, sProfiles []models.StorageProfile, ctxt string) (*models.Node, bool) {
var storage_node models.Node
storage_node.Hostname = hostname
storage_node.Enabled = true
storage_node.NodeId, _ = salt_backend.GetNodeID(hostname, ctxt)
networkInfo, err := salt_backend.GetNodeNetwork(hostname, ctxt)
if err != nil {
logger.Get().Error(fmt.Sprintf("%s-Error getting network details for node: %s. error: %v", ctxt, hostname, err))
return nil, false
}
storage_node.NetworkInfo = networkInfo
addrs, err := net.LookupHost(hostname)
if err != nil {
logger.Get().Error(fmt.Sprintf("%s-Error looking up node IP for: %s. error: %v", ctxt, hostname, err))
return nil, false
}
storage_node.ManagementIP4 = addrs[0]
ok, err := salt_backend.NodeUp(hostname, ctxt)
if err != nil {
logger.Get().Error(fmt.Sprintf("%s-Error getting status of node: %s. error: %v", ctxt, hostname, err))
return nil, false
}
if ok {
storage_node.Status = models.NODE_STATUS_OK
} else {
storage_node.Status = models.NODE_STATUS_ERROR
}
disks, err := salt_backend.GetNodeDisk(hostname, ctxt)
if err != nil {
logger.Get().Error(fmt.Sprintf("%s-Error getting disk details for node: %s. error: %v", ctxt, hostname, err))
return nil, false
}
for _, disk := range disks {
dId, err := uuid.New()
if err != nil {
logger.Get().Error(fmt.Sprintf("%s-Unable to generate uuid for disk : %s. error: %v", ctxt, disk.DevName, err))
return nil, false
}
disk.DiskId = *dId
applyStorageProfile(&disk, sProfiles)
storage_node.StorageDisks = append(storage_node.StorageDisks, disk)
}
cpus, err := salt_backend.GetNodeCpu(hostname, ctxt)
if err != nil {
logger.Get().Error(fmt.Sprintf("%s-Error getting cpu details for node: %s. error: %v", ctxt, hostname, err))
return nil, false
}
for _, cpu := range cpus {
storage_node.CPUs = append(storage_node.CPUs, cpu)
}
osInfo, err := salt_backend.GetNodeOs(hostname, ctxt)
if err != nil {
logger.Get().Error(fmt.Sprintf("%s-Error getting os details for node: %s", ctxt, hostname))
return nil, false
}
storage_node.OS = osInfo
memoryInfo, err := salt_backend.GetNodeMemory(hostname, ctxt)
if err != nil {
logger.Get().Error(fmt.Sprintf("%s-Error getting memory details for node: %s", ctxt, hostname))
return nil, false
}
storage_node.Memory = memoryInfo
if !storage_node.NodeId.IsZero() && len(storage_node.NetworkInfo.Subnet) != 0 && len(storage_node.StorageDisks) != 0 {
return &storage_node, true
} else {
return nil, false
}
}
func (a SaltNodeManager) IsNodeUp(hostname string, ctxt string) (bool, error) {
ok, err := salt_backend.NodeUp(hostname, ctxt)
if err != nil {
logger.Get().Error(fmt.Sprintf("%s-Error getting status of node: %s. error: %v", ctxt, hostname, err))
return false, nil
}
return ok, nil
}
func (a SaltNodeManager) NodeUptime(hostname string, ctxt string) (string, error) {
uptime, err := salt_backend.NodeUptime(hostname, ctxt)
if err != nil {
return "", err
}
return uptime, nil
}
func (a SaltNodeManager) GetUnmanagedNodes(ctxt string) (*models.UnmanagedNodes, error) {
if nodes, err := salt_backend.GetNodes(ctxt); err != nil {
return nil, err
} else {
var retNodes models.UnmanagedNodes
for _, node := range nodes.Unmanage {
var retNode models.UnmanagedNode
retNode.Name = node.Name
retNode.SaltFingerprint = node.Fingerprint
retNodes = append(retNodes, retNode)
}
return &retNodes, nil
}
}
func (a SaltNodeManager) SyncStorageDisks(node string, sProfiles []models.StorageProfile, ctxt string) (bool, error) {
disks, err := salt_backend.GetNodeDisk(node, ctxt)
if err != nil {
return false, err
}
sessionCopy := db.GetDatastore().Copy()
defer sessionCopy.Close()
coll := sessionCopy.DB(conf.SystemConfig.DBConfig.Database).C(models.COLL_NAME_STORAGE_NODES)
var storage_node models.Node
var updated_disks []models.Disk
if err := coll.Find(bson.M{"hostname": node}).One(&storage_node); err != nil {
logger.Get().Error("%s-Error updating the disk details for node: %s. error: %v", ctxt, node, err)
return false, err
}
var present bool
for _, disk := range disks {
present = false
for _, stored_disk := range storage_node.StorageDisks {
if disk.DevName == stored_disk.DevName {
present = true
updated_disks = append(updated_disks, stored_disk)
break
}
}
if !present {
dId, err := uuid.New()
if err != nil {
logger.Get().Error(fmt.Sprintf("%s-Unable to generate uuid for disk : %s. error: %v", ctxt, disk.DevName, err))
return false, err
}
disk.DiskId = *dId
applyStorageProfile(&disk, sProfiles)
updated_disks = append(updated_disks, disk)
}
}
if err := coll.Update(bson.M{"hostname": node}, bson.M{"$set": bson.M{"storagedisks": updated_disks}}); err != nil {
logger.Get().Error("%s-Error updating the disk details for node: %s. error: %v", ctxt, node, err)
return false, err
}
return true, nil
}
func (a SaltNodeManager) DisableNode(node string, ctxt string) (bool, error) {
if ok, err := salt_backend.DisableService(node, "collectd", true, ctxt); err != nil || !ok {
logger.Get().Error(fmt.Sprintf("%s-Error disabling services on node: %s. error: %v", ctxt, node, err))
return false, err
}
// Disable any POST actions for participating nodes
sessionCopy := db.GetDatastore().Copy()
defer sessionCopy.Close()
coll := sessionCopy.DB(conf.SystemConfig.DBConfig.Database).C(models.COLL_NAME_STORAGE_NODES)
if err := coll.Update(bson.M{"hostname": node}, bson.M{"$set": bson.M{"enabled": false}}); err != nil {
logger.Get().Error("%s-Error updating managed state of node: %s. error: %v", ctxt, node, err)
return false, err
}
return true, nil
}
func (a SaltNodeManager) EnableNode(node string, ctxt string) (bool, error) {
if ok, err := salt_backend.EnableService(node, "collectd", true, ctxt); err != nil || !ok {
logger.Get().Error("%s-Error enabling services on node: %s. error: %v", ctxt, node, err)
return false, err
}
// Enable any POST actions for node
sessionCopy := db.GetDatastore().Copy()
defer sessionCopy.Close()
coll := sessionCopy.DB(conf.SystemConfig.DBConfig.Database).C(models.COLL_NAME_STORAGE_NODES)
if err := coll.Update(bson.M{"hostname": node}, bson.M{"$set": bson.M{"enabled": true}}); err != nil {
logger.Get().Error("%s-Error updating manage state of node: %s. error: %v", ctxt, node, err)
return false, err
}
return true, nil
}
func (a SaltNodeManager) RemoveNode(node string, ctxt string) (bool, error) {
if ok, err := salt_backend.DisableService(node, "collectd", true, ctxt); err != nil || !ok {
return false, err
}
if ok, err := salt_backend.IgnoreNode(node, ctxt); err != nil || !ok {
return false, err
}
return true, nil
}
func (a SaltNodeManager) IgnoreNode(node string, ctxt string) (bool, error) {
if ok, err := salt_backend.IgnoreNode(node, ctxt); err != nil || !ok {
logger.Get().Error(fmt.Sprintf("%s-Error rejecting node: %s. error: %v", ctxt, node, err))
return false, err
}
return true, nil
}
func (a SaltNodeManager) SetUpMonitoring(node string, master string, ctxt string) (map[string]interface{}, error) {
// The plugins to be setup includes the editable plugins and also the write plugin
return a.EnforceMonitoring(
append(monitoring.SupportedMonitoringPlugins, monitoring.MonitoringWritePlugin),
[]string{node},
master,
monitoring.GetDefaultThresholdValues(),
ctxt)
}
func (a SaltNodeManager) EnforceMonitoring(plugin_names []string, nodes []string, master string, plugins []monitoring.Plugin, ctxt string) (map[string]interface{}, error) {
failed_nodes, err := salt_backend.AddMonitoringPlugin(plugin_names, nodes, master, monitoring.ToSaltPillarCompat(plugins), ctxt)
return failed_nodes, err
}
func (a SaltNodeManager) UpdateMonitoringConfiguration(nodes []string, config []monitoring.Plugin, ctxt string) (map[string]string, error) {
failed_nodes, err := salt_backend.UpdateMonitoringConfiguration(nodes, config, ctxt)
return failed_nodes, err
}
func (a SaltNodeManager) GetSingleValuedMetricFromCollectd(nodes []string, metricName string, ctxt string) (map[string]models.CollectdSingleValuedMetric, error) {
result, err := salt_backend.GetSingleValuedMetricFromCollectd(nodes, metricName, ctxt)
return result, err
}
func (a SaltNodeManager) GetCpuMetricFromCollectd(nodes []string, ctxt string) (result map[string]models.CollectdCpuMetric, err error) {
result, err = salt_backend.GetCpuMetricFromCollectd(nodes, ctxt)
return result, err
}
func (a SaltNodeManager) EnableMonitoringPlugin(nodes []string, pluginName string, ctxt string) (map[string]string, error) {
failed_nodes, err := salt_backend.EnableMonitoringPlugin(nodes, pluginName, ctxt)
return failed_nodes, err
}
func (a SaltNodeManager) DisableMonitoringPlugin(nodes []string, pluginName string, ctxt string) (map[string]string, error) {
failed_nodes, err := salt_backend.DisableMonitoringPlugin(nodes, pluginName, ctxt)
return failed_nodes, err
}
func (a SaltNodeManager) RemoveMonitoringPlugin(nodes []string, pluginName string, ctxt string) (map[string]string, error) {
failed_nodes, err := salt_backend.RemoveMonitoringPlugin(nodes, pluginName, ctxt)
return failed_nodes, err
}
func (a SaltNodeManager) AddMonitoringPlugin(nodes []string, master string, plugin monitoring.Plugin, ctxt string) (map[string]interface{}, error) {
failed_nodes, err := salt_backend.AddMonitoringPlugin([]string{plugin.Name}, nodes, "", monitoring.ToSaltPillarCompat([]monitoring.Plugin{plugin}), ctxt)
return failed_nodes, err
}
func (a SaltNodeManager) SyncModules(node string, ctxt string) (bool, error) {
if ok, err := salt_backend.SyncModules(node, ctxt); !ok || err != nil {
return false, err
}
return true, nil
}
func applyStorageProfile(disk *models.Disk, sProfiles []models.StorageProfile) error {
for _, sProfile := range sProfiles {
//TODO check the speed
//Check the disk type
//Now only check whether the disk is SSD or not. REST are supported
//once the API is avaialable
diskType := sProfile.Rule.Type
switch diskType {
case models.SSD:
if disk.SSD {
disk.StorageProfile = sProfile.Name
return nil
}
}
}
//Not Matched, so add to generic profile
disk.StorageProfile = models.DefaultProfile3
return nil
}
func (a SaltNodeManager) GetFingerPrint(hostname string, ctxt string) (string, error) {
fingerprint, err := salt_backend.GetFingerPrint(hostname, ctxt)
return fingerprint, err
}