forked from evergreen-ci/evergreen
/
host_spawn.go
305 lines (256 loc) · 7.52 KB
/
host_spawn.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
package route
import (
"context"
"fmt"
"net/http"
"strconv"
"strings"
"time"
"github.com/evergreen-ci/evergreen"
"github.com/evergreen-ci/evergreen/cloud"
"github.com/evergreen-ci/evergreen/rest/data"
"github.com/evergreen-ci/evergreen/rest/model"
"github.com/evergreen-ci/evergreen/util"
"github.com/evergreen-ci/gimlet"
"github.com/pkg/errors"
)
func makeSpawnHostCreateRoute(sc data.Connector) gimlet.RouteHandler {
return &hostPostHandler{
sc: sc,
}
}
type hostPostHandler struct {
Distro string `json:"distro"`
KeyName string `json:"keyname"`
sc data.Connector
}
func (hph *hostPostHandler) Factory() gimlet.RouteHandler {
return &hostPostHandler{
sc: hph.sc,
}
}
func (hph *hostPostHandler) Parse(ctx context.Context, r *http.Request) error {
return errors.WithStack(util.ReadJSONInto(r.Body, hph))
}
func (hph *hostPostHandler) Run(ctx context.Context) gimlet.Responder {
user := MustHaveUser(ctx)
intentHost, err := hph.sc.NewIntentHost(hph.Distro, hph.KeyName, "", user)
if err != nil {
return gimlet.MakeJSONErrorResponder(errors.Wrap(err, "error spawning host"))
}
hostModel := &model.APIHost{}
err = hostModel.BuildFromService(intentHost)
if err != nil {
return gimlet.MakeJSONErrorResponder(errors.Wrap(err, "API model error"))
}
return gimlet.NewJSONResponse(hostModel)
}
func getHostTerminateRouteManager(route string, version int) *RouteManager {
return &RouteManager{
Route: route,
Version: version,
Methods: []MethodHandler{
{
MethodType: http.MethodPost,
Authenticator: &RequireUserAuthenticator{},
RequestHandler: &hostTerminateHandler{},
},
},
}
}
type hostTerminateHandler struct {
hostID string
}
func (h *hostTerminateHandler) Handler() RequestHandler {
return &hostTerminateHandler{}
}
func (h *hostTerminateHandler) ParseAndValidate(ctx context.Context, r *http.Request) error {
var err error
h.hostID, err = validateHostID(gimlet.GetVars(r)["host_id"])
return err
}
func (h *hostTerminateHandler) Execute(ctx context.Context, sc data.Connector) (ResponseData, error) {
u := MustHaveUser(ctx)
host, err := sc.FindHostByIdWithOwner(h.hostID, u)
if err != nil {
return ResponseData{}, err
}
if host.Status == evergreen.HostTerminated {
return ResponseData{}, gimlet.ErrorResponse{
StatusCode: http.StatusBadRequest,
Message: fmt.Sprintf("Host %s is already terminated", host.Id),
}
} else if host.Status == evergreen.HostUninitialized {
if err := sc.SetHostStatus(host, evergreen.HostTerminated, u.Id); err != nil {
return ResponseData{}, gimlet.ErrorResponse{
StatusCode: http.StatusInternalServerError,
Message: err.Error(),
}
}
} else {
if err := sc.TerminateHost(ctx, host, u.Id); err != nil {
return ResponseData{}, gimlet.ErrorResponse{
StatusCode: http.StatusInternalServerError,
Message: err.Error(),
}
}
}
return ResponseData{}, nil
}
func getHostChangeRDPPasswordRouteManager(route string, version int) *RouteManager {
return &RouteManager{
Route: route,
Version: version,
Methods: []MethodHandler{
{
MethodType: http.MethodPost,
Authenticator: &RequireUserAuthenticator{},
RequestHandler: &hostChangeRDPPasswordHandler{},
},
},
}
}
type hostChangeRDPPasswordHandler struct {
hostID string
rdpPassword string
}
func (h *hostChangeRDPPasswordHandler) Handler() RequestHandler {
return &hostChangeRDPPasswordHandler{}
}
func (h *hostChangeRDPPasswordHandler) ParseAndValidate(ctx context.Context, r *http.Request) error {
hostModify := model.APISpawnHostModify{}
if err := util.ReadJSONInto(util.NewRequestReader(r), &hostModify); err != nil {
return err
}
var err error
h.hostID, err = validateHostID(gimlet.GetVars(r)["host_id"])
if err != nil {
return err
}
h.rdpPassword = model.FromAPIString(hostModify.RDPPwd)
if !cloud.ValidateRDPPassword(h.rdpPassword) {
return gimlet.ErrorResponse{
StatusCode: http.StatusBadRequest,
Message: "invalid password",
}
}
return nil
}
func (h *hostChangeRDPPasswordHandler) Execute(ctx context.Context, sc data.Connector) (ResponseData, error) {
u := MustHaveUser(ctx)
host, err := sc.FindHostByIdWithOwner(h.hostID, u)
if err != nil {
return ResponseData{}, err
}
if !host.Distro.IsWindows() {
return ResponseData{}, gimlet.ErrorResponse{
StatusCode: http.StatusBadRequest,
Message: "RDP passwords can only be set on Windows hosts",
}
}
if host.Status != evergreen.HostRunning {
return ResponseData{}, gimlet.ErrorResponse{
StatusCode: http.StatusBadRequest,
Message: "RDP passwords can only be set on running hosts",
}
}
if err := cloud.SetHostRDPPassword(ctx, host, h.rdpPassword); err != nil {
return ResponseData{}, gimlet.ErrorResponse{
StatusCode: http.StatusInternalServerError,
Message: err.Error(),
}
}
return ResponseData{}, nil
}
func getHostExtendExpirationRouteManager(route string, version int) *RouteManager {
return &RouteManager{
Route: route,
Version: version,
Methods: []MethodHandler{
{
MethodType: http.MethodPost,
Authenticator: &RequireUserAuthenticator{},
RequestHandler: &hostExtendExpirationHandler{},
},
},
}
}
type hostExtendExpirationHandler struct {
hostID string
addHours time.Duration
}
func (h *hostExtendExpirationHandler) Handler() RequestHandler {
return &hostExtendExpirationHandler{}
}
func (h *hostExtendExpirationHandler) ParseAndValidate(ctx context.Context, r *http.Request) error {
hostModify := model.APISpawnHostModify{}
if err := util.ReadJSONInto(util.NewRequestReader(r), &hostModify); err != nil {
return err
}
var err error
h.hostID, err = validateHostID(gimlet.GetVars(r)["host_id"])
if err != nil {
return err
}
addHours, err := strconv.Atoi(model.FromAPIString(hostModify.AddHours))
if err != nil {
return gimlet.ErrorResponse{
StatusCode: http.StatusBadRequest,
Message: "expiration not a number",
}
}
h.addHours = time.Duration(addHours) * time.Hour
if h.addHours <= 0 {
return gimlet.ErrorResponse{
StatusCode: http.StatusBadRequest,
Message: "must add more than 0 hours to expiration",
}
}
if h.addHours > cloud.MaxSpawnHostExpirationDurationHours {
return gimlet.ErrorResponse{
StatusCode: http.StatusBadRequest,
Message: fmt.Sprintf("cannot add more than %s", cloud.MaxSpawnHostExpirationDurationHours.String()),
}
}
return nil
}
func (h *hostExtendExpirationHandler) Execute(ctx context.Context, sc data.Connector) (ResponseData, error) {
u := MustHaveUser(ctx)
host, err := sc.FindHostByIdWithOwner(h.hostID, u)
if err != nil {
return ResponseData{}, err
}
if host.Status == evergreen.HostTerminated {
return ResponseData{}, gimlet.ErrorResponse{
StatusCode: http.StatusBadRequest,
Message: "cannot extend expiration of a terminated host",
}
}
var newExp time.Time
newExp, err = cloud.MakeExtendedSpawnHostExpiration(host, h.addHours)
if err != nil {
return ResponseData{}, gimlet.ErrorResponse{
StatusCode: http.StatusBadRequest,
Message: err.Error(),
}
}
if err := sc.SetHostExpirationTime(host, newExp); err != nil {
return ResponseData{}, gimlet.ErrorResponse{
StatusCode: http.StatusInternalServerError,
Message: err.Error(),
}
}
return ResponseData{}, nil
}
////////////////////////////////////////////////////////////////////////
//
// utility functions
func validateHostID(hostID string) (string, error) {
if strings.TrimSpace(hostID) == "" {
return "", gimlet.ErrorResponse{
StatusCode: http.StatusBadRequest,
Message: "missing/empty host id",
}
}
return hostID, nil
}