-
Notifications
You must be signed in to change notification settings - Fork 30
/
machine.go
140 lines (118 loc) · 3.52 KB
/
machine.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
// Copyright (c) 2021, R.I. Pienaar and the Choria Project contributors
//
// SPDX-License-Identifier: Apache-2.0
package machinedata
import (
"context"
"fmt"
"github.com/choria-io/go-choria/build"
"github.com/choria-io/go-choria/providers/agent/mcorpc/ddl/common"
"github.com/choria-io/go-choria/providers/data"
"github.com/choria-io/go-choria/providers/data/ddl"
"github.com/choria-io/go-choria/providers/data/plugin"
"github.com/choria-io/go-choria/server/agents"
)
type MachineData struct{}
func ChoriaPlugin() *plugin.DataPlugin {
return plugin.NewDataPlugin("machine_state", New)
}
func New(_ data.Framework) (data.Plugin, error) {
return &MachineData{}, nil
}
func (s *MachineData) Run(_ context.Context, q data.Query, si agents.ServerInfoSource) (map[string]data.OutputItem, error) {
machines, err := si.MachinesStatus()
if err != nil {
return nil, err
}
query := q.(string)
for _, m := range machines {
if m.ID == query || m.Path == query || m.Name == query {
response := map[string]data.OutputItem{
"name": m.Name,
"version": m.Version,
"state": m.State,
"path": m.Path,
"id": m.ID,
"start_time": m.StartTimeUTC,
"available_transitions": m.AvailableTransitions,
"scout": m.Scout,
}
if m.Scout {
response["current_state"] = m.ScoutState
}
return response, nil
}
}
return nil, fmt.Errorf("no machine matching %q found", query)
}
func (s *MachineData) DLL() (*ddl.DDL, error) {
sddl := &ddl.DDL{
Metadata: ddl.Metadata{
License: "Apache-2.0",
Author: "R.I.Pienaar <rip@devco.net>",
Timeout: 1,
Name: "machine_state",
Version: build.Version,
URL: "https://choria.io",
Description: "Data about a Choria Autonomous Agent",
Provider: "golang",
},
Query: &common.InputItem{
Prompt: "Machine",
Description: "The machine name, path or ID to match",
Type: common.InputTypeString,
Default: "",
Optional: false,
Validation: ".+",
MaxLength: 256,
},
Output: map[string]*common.OutputItem{
"name": {
Description: "The machine name",
DisplayAs: "Name",
Type: common.OutputTypeString,
},
"version": {
Description: "The machine version",
DisplayAs: "Version",
Type: common.OutputTypeString,
},
"state": {
Description: "The state the machine is in currently",
DisplayAs: "Current State",
Type: common.OutputTypeString,
},
"path": {
Description: "The path the machine is stored in on disk",
DisplayAs: "Path",
Type: common.OutputTypeString,
},
"id": {
Description: "The unique instance id",
DisplayAs: "ID",
Type: common.OutputTypeString,
},
"start_time": {
Description: "The time this machine started, seconds since 1970",
DisplayAs: "Path",
Type: common.OutputTypeInteger,
},
"available_transitions": {
Description: "The names of transition events that's valid for this machine",
DisplayAs: "Available Transitions",
Type: common.OutputTypeArray,
},
"scout": {
Description: "Indicates if this machine is a Scout check",
DisplayAs: "Scout",
Type: common.OutputTypeBoolean,
},
"current_state": {
Description: "For Scout checks, this is the extended Scout state",
DisplayAs: "Scout State",
Type: common.OutputTypeHash,
},
},
}
return sddl, nil
}