-
Notifications
You must be signed in to change notification settings - Fork 453
Expand file tree
/
Copy pathnode.go
More file actions
249 lines (223 loc) · 7.81 KB
/
node.go
File metadata and controls
249 lines (223 loc) · 7.81 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
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
// Copyright 2015 Vadim Kravcenko
//
// 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 gojenkins
import (
"context"
"errors"
)
// Computers represents a collection of Jenkins nodes (agents).
type Computers struct {
BusyExecutors int `json:"busyExecutors"`
Computers []*NodeResponse `json:"computer"`
DisplayName string `json:"displayName"`
TotalExecutors int `json:"totalExecutors"`
}
// Node represents a Jenkins agent (slave) node.
type Node struct {
Raw *NodeResponse
Jenkins *Jenkins
Base string
}
// NodeResponse represents the JSON response from the Jenkins API for a node.
type NodeResponse struct {
Class string `json:"_class"`
Actions []interface{} `json:"actions"`
DisplayName string `json:"displayName"`
Executors []struct {
CurrentExecutable struct {
Number int `json:"number"`
URL string `json:"url"`
SubBuilds []struct {
Abort bool `json:"abort"`
Build interface{} `json:"build"`
BuildNumber int `json:"buildNumber"`
Duration string `json:"duration"`
Icon string `json:"icon"`
JobName string `json:"jobName"`
ParentBuildNumber int `json:"parentBuildNumber"`
ParentJobName string `json:"parentJobName"`
PhaseName string `json:"phaseName"`
Result string `json:"result"`
Retry bool `json:"retry"`
URL string `json:"url"`
} `json:"subBuilds"`
} `json:"currentExecutable"`
} `json:"executors"`
Icon string `json:"icon"`
IconClassName string `json:"iconClassName"`
Idle bool `json:"idle"`
JnlpAgent bool `json:"jnlpAgent"`
LaunchSupported bool `json:"launchSupported"`
LoadStatistics struct{} `json:"loadStatistics"`
ManualLaunchAllowed bool `json:"manualLaunchAllowed"`
MonitorData struct {
Hudson_NodeMonitors_ArchitectureMonitor interface{} `json:"hudson.node_monitors.ArchitectureMonitor"`
Hudson_NodeMonitors_ClockMonitor interface{} `json:"hudson.node_monitors.ClockMonitor"`
Hudson_NodeMonitors_DiskSpaceMonitor interface{} `json:"hudson.node_monitors.DiskSpaceMonitor"`
Hudson_NodeMonitors_ResponseTimeMonitor struct {
Average int64 `json:"average"`
} `json:"hudson.node_monitors.ResponseTimeMonitor"`
Hudson_NodeMonitors_SwapSpaceMonitor interface{} `json:"hudson.node_monitors.SwapSpaceMonitor"`
Hudson_NodeMonitors_TemporarySpaceMonitor interface{} `json:"hudson.node_monitors.TemporarySpaceMonitor"`
} `json:"monitorData"`
NumExecutors int64 `json:"numExecutors"`
Offline bool `json:"offline"`
OfflineCause struct{} `json:"offlineCause"`
OfflineCauseReason string `json:"offlineCauseReason"`
OneOffExecutors []interface{} `json:"oneOffExecutors"`
TemporarilyOffline bool `json:"temporarilyOffline"`
}
// Info returns the node details after polling for the latest data.
func (n *Node) Info(ctx context.Context) (*NodeResponse, error) {
_, err := n.Poll(ctx)
if err != nil {
return nil, err
}
return n.Raw, nil
}
// GetName returns the display name of the node.
func (n *Node) GetName() string {
return n.Raw.DisplayName
}
// Delete removes the node from Jenkins.
func (n *Node) Delete(ctx context.Context) (bool, error) {
resp, err := n.Jenkins.Requester.Post(ctx, n.Base+"/doDelete", nil, nil, nil)
if err != nil {
return false, err
}
return resp.StatusCode == 200, nil
}
// IsOnline returns true if the node is online and available.
func (n *Node) IsOnline(ctx context.Context) (bool, error) {
_, err := n.Poll(ctx)
if err != nil {
return false, err
}
return !n.Raw.Offline, nil
}
// IsTemporarilyOffline returns true if the node is temporarily marked offline.
func (n *Node) IsTemporarilyOffline(ctx context.Context) (bool, error) {
_, err := n.Poll(ctx)
if err != nil {
return false, err
}
return n.Raw.TemporarilyOffline, nil
}
// IsIdle returns true if the node has no builds running.
func (n *Node) IsIdle(ctx context.Context) (bool, error) {
_, err := n.Poll(ctx)
if err != nil {
return false, err
}
return n.Raw.Idle, nil
}
// IsJnlpAgent returns true if the node is a JNLP (Java Web Start) agent.
func (n *Node) IsJnlpAgent(ctx context.Context) (bool, error) {
_, err := n.Poll(ctx)
if err != nil {
return false, err
}
return n.Raw.JnlpAgent, nil
}
// SetOnline brings a temporarily offline node back online.
func (n *Node) SetOnline(ctx context.Context) (bool, error) {
_, err := n.Poll(ctx)
if err != nil {
return false, err
}
if n.Raw.Offline && !n.Raw.TemporarilyOffline {
return false, errors.New("Node is Permanently offline, can't bring it up")
}
if n.Raw.Offline && n.Raw.TemporarilyOffline {
return n.ToggleTemporarilyOffline(ctx)
}
return true, nil
}
// SetOffline marks the node as temporarily offline with an optional message.
func (n *Node) SetOffline(ctx context.Context, options ...interface{}) (bool, error) {
if !n.Raw.Offline {
return n.ToggleTemporarilyOffline(ctx, options...)
}
return false, errors.New("Node already Offline")
}
// ToggleTemporarilyOffline toggles the temporarily offline status of the node.
func (n *Node) ToggleTemporarilyOffline(ctx context.Context, options ...interface{}) (bool, error) {
state_before, err := n.IsTemporarilyOffline(ctx)
if err != nil {
return false, err
}
qr := map[string]string{"offlineMessage": "requested from gojenkins"}
if len(options) > 0 {
qr["offlineMessage"] = options[0].(string)
}
_, err = n.Jenkins.Requester.Post(ctx, n.Base+"/toggleOffline", nil, nil, qr)
if err != nil {
return false, err
}
new_state, err := n.IsTemporarilyOffline(ctx)
if err != nil {
return false, err
}
if state_before == new_state {
return false, errors.New("Node state not changed")
}
return true, nil
}
// Poll fetches the latest node data from Jenkins.
func (n *Node) Poll(ctx context.Context) (int, error) {
response, err := n.Jenkins.Requester.GetJSON(ctx, n.Base, n.Raw, nil)
if err != nil {
return 0, err
}
return response.StatusCode, nil
}
// LaunchNodeBySSH launches the node's agent via SSH.
func (n *Node) LaunchNodeBySSH(ctx context.Context) (int, error) {
qr := map[string]string{
"json": "",
"Submit": "Launch slave agent",
}
response, err := n.Jenkins.Requester.Post(ctx, n.Base+"/launchSlaveAgent", nil, nil, qr)
if err != nil {
return 0, err
}
return response.StatusCode, nil
}
// Disconnect disconnects the node from Jenkins.
func (n *Node) Disconnect(ctx context.Context) (int, error) {
qr := map[string]string{
"offlineMessage": "",
"json": makeJson(map[string]string{"offlineMessage": ""}),
"Submit": "Yes",
}
response, err := n.Jenkins.Requester.Post(ctx, n.Base+"/doDisconnect", nil, nil, qr)
if err != nil {
return 0, err
}
return response.StatusCode, nil
}
// GetLogText returns the agent log text for the node.
func (n *Node) GetLogText(ctx context.Context) (string, error) {
var log string
_, err := n.Jenkins.Requester.Post(ctx, n.Base+"/log", nil, nil, nil)
if err != nil {
return "", err
}
qr := map[string]string{"start": "0"}
_, err = n.Jenkins.Requester.GetJSON(ctx, n.Base+"/logText/progressiveHtml/", &log, qr)
if err != nil {
return "", nil
}
return log, nil
}