github.com/choria-io/go-choria@v0.28.1-0.20240416190746-b3bf9c7d5a45/aagent/data/machinedata/machine.go (about) 1 // Copyright (c) 2021, R.I. Pienaar and the Choria Project contributors 2 // 3 // SPDX-License-Identifier: Apache-2.0 4 5 package machinedata 6 7 import ( 8 "context" 9 "fmt" 10 11 "github.com/choria-io/go-choria/build" 12 "github.com/choria-io/go-choria/providers/agent/mcorpc/ddl/common" 13 "github.com/choria-io/go-choria/providers/data" 14 "github.com/choria-io/go-choria/providers/data/ddl" 15 "github.com/choria-io/go-choria/providers/data/plugin" 16 "github.com/choria-io/go-choria/server/agents" 17 ) 18 19 type MachineData struct{} 20 21 func ChoriaPlugin() *plugin.DataPlugin { 22 return plugin.NewDataPlugin("machine_state", New) 23 } 24 25 func New(_ data.Framework) (data.Plugin, error) { 26 return &MachineData{}, nil 27 } 28 29 func (s *MachineData) Run(_ context.Context, q data.Query, si agents.ServerInfoSource) (map[string]data.OutputItem, error) { 30 machines, err := si.MachinesStatus() 31 if err != nil { 32 return nil, err 33 } 34 35 query := q.(string) 36 for _, m := range machines { 37 if m.ID == query || m.Path == query || m.Name == query { 38 response := map[string]data.OutputItem{ 39 "name": m.Name, 40 "version": m.Version, 41 "state": m.State, 42 "path": m.Path, 43 "id": m.ID, 44 "start_time": m.StartTimeUTC, 45 "available_transitions": m.AvailableTransitions, 46 "scout": m.Scout, 47 } 48 49 if m.Scout { 50 response["current_state"] = m.ScoutState 51 } 52 53 return response, nil 54 } 55 } 56 57 return nil, fmt.Errorf("no machine matching %q found", query) 58 } 59 60 func (s *MachineData) DLL() (*ddl.DDL, error) { 61 sddl := &ddl.DDL{ 62 Metadata: ddl.Metadata{ 63 License: "Apache-2.0", 64 Author: "R.I.Pienaar <rip@devco.net>", 65 Timeout: 1, 66 Name: "machine_state", 67 Version: build.Version, 68 URL: "https://choria.io", 69 Description: "Data about a Choria Autonomous Agent", 70 Provider: "golang", 71 }, 72 Query: &common.InputItem{ 73 Prompt: "Machine", 74 Description: "The machine name, path or ID to match", 75 Type: common.InputTypeString, 76 Default: "", 77 Optional: false, 78 Validation: ".+", 79 MaxLength: 256, 80 }, 81 82 Output: map[string]*common.OutputItem{ 83 "name": { 84 Description: "The machine name", 85 DisplayAs: "Name", 86 Type: common.OutputTypeString, 87 }, 88 89 "version": { 90 Description: "The machine version", 91 DisplayAs: "Version", 92 Type: common.OutputTypeString, 93 }, 94 95 "state": { 96 Description: "The state the machine is in currently", 97 DisplayAs: "Current State", 98 Type: common.OutputTypeString, 99 }, 100 101 "path": { 102 Description: "The path the machine is stored in on disk", 103 DisplayAs: "Path", 104 Type: common.OutputTypeString, 105 }, 106 107 "id": { 108 Description: "The unique instance id", 109 DisplayAs: "ID", 110 Type: common.OutputTypeString, 111 }, 112 113 "start_time": { 114 Description: "The time this machine started, seconds since 1970", 115 DisplayAs: "Path", 116 Type: common.OutputTypeInteger, 117 }, 118 119 "available_transitions": { 120 Description: "The names of transition events that's valid for this machine", 121 DisplayAs: "Available Transitions", 122 Type: common.OutputTypeArray, 123 }, 124 125 "scout": { 126 Description: "Indicates if this machine is a Scout check", 127 DisplayAs: "Scout", 128 Type: common.OutputTypeBoolean, 129 }, 130 131 "current_state": { 132 Description: "For Scout checks, this is the extended Scout state", 133 DisplayAs: "Scout State", 134 Type: common.OutputTypeHash, 135 }, 136 }, 137 } 138 139 return sddl, nil 140 }