-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.go
More file actions
127 lines (102 loc) · 3.3 KB
/
script.go
File metadata and controls
127 lines (102 loc) · 3.3 KB
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
package fleet
import (
"net/http"
"strconv"
"time"
)
type Script struct {
ID int `json:"id"`
TeamID interface{} `json:"team_id"`
Name string `json:"name"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
// DeleteScript deletes the script with the corresponding ID.
func (s *Service) DeleteScript(id int) error {
if _, err := s.makeRequest(http.MethodDelete, "scripts/"+strconv.Itoa(id), nil, nil); err != nil {
return err
}
return nil
}
// FindScriptByID returns the script with the corresponding ID.
func (s *Service) FindScriptByID(id int) (*Script, error) {
resp := struct {
Script *Script `json:"script"`
}{}
if _, err := s.makeRequest(http.MethodGet, "scripts/"+strconv.Itoa(id), nil, &resp); err != nil {
return nil, err
}
return resp.Script, nil
}
// FindScriptsByHostID returns script metadata for the given host.
func (s *Service) FindScriptsByHostID(hostID int) ([]*Script, error) {
resp := struct {
Scripts []*Script `json:"scripts"`
}{}
if _, err := s.makeRequest(http.MethodGet, "hosts/"+strconv.Itoa(hostID)+"/scripts", nil, &resp); err != nil {
return nil, err
}
return resp.Scripts, nil
}
// FindScripts returns all scripts from the Fleet API.
func (s *Service) FindScripts() ([]*Script, error) {
resp := struct {
Scripts []*Script `json:"scripts"`
}{}
if _, err := s.makeRequest(http.MethodGet, "scripts", nil, &resp); err != nil {
return nil, err
}
return resp.Scripts, nil
}
// RunScript runs the given raw script on the given host, and returns the UUID of the script execution.
func (s *Service) RunScript(hostID int, scriptContents string) (string, error) {
body := struct {
HostID int `json:"host_id"`
ScriptContents string `json:"script_contents"`
}{
hostID, scriptContents,
}
resp := struct {
ExecutionID string `json:"execution_id"`
}{}
if _, err := s.makeRequest(http.MethodPost, "scripts/run", body, &resp); err != nil {
return "", err
}
return resp.ExecutionID, nil
}
// RunScriptID runs the given script on the given host, and returns the UUID of the script execution.
func (s *Service) RunScriptID(hostID int, scriptID int) (string, error) {
body := struct {
HostID int `json:"host_id"`
ScriptID int `json:"script_id"`
}{
hostID, scriptID,
}
resp := struct {
ExecutionID string `json:"execution_id"`
}{}
if _, err := s.makeRequest(http.MethodPost, "scripts/run", body, &resp); err != nil {
return "", err
}
return resp.ExecutionID, nil
}
type ScriptResult struct {
ScriptContents string `json:"script_contents"`
ExitCode int `json:"exit_code"`
Output string `json:"output"`
Message string `json:"message"`
Hostname string `json:"hostname"`
HostTimeout bool `json:"host_timeout"`
HostID int `json:"host_id"`
ID string `json:"execution_id"`
Runtime int `json:"runtime"`
CreatedAt time.Time `json:"created_at"`
}
// FindScriptResult returns the script result with the corresponding ID.
func (s *Service) FindScriptResult(executionID string) (*ScriptResult, error) {
scriptResult := ScriptResult{}
if _, err := s.makeRequest(http.MethodGet, "scripts/results/"+executionID, nil, &scriptResult); err != nil {
return nil, err
}
return &scriptResult, nil
}