forked from evergreen-ci/evergreen
/
host.go
205 lines (174 loc) · 5.39 KB
/
host.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
package service
import (
"fmt"
"net/http"
"strconv"
"strings"
"github.com/evergreen-ci/evergreen"
"github.com/evergreen-ci/evergreen/model/event"
"github.com/evergreen-ci/evergreen/model/host"
"github.com/evergreen-ci/evergreen/model/task"
"github.com/evergreen-ci/evergreen/model/user"
"github.com/evergreen-ci/evergreen/util"
"github.com/gorilla/mux"
"github.com/pkg/errors"
)
var (
validUpdateToStatuses = []string{
evergreen.HostRunning,
evergreen.HostQuarantined,
evergreen.HostDecommissioned,
}
)
const (
IncludeSpawnedHosts = "includeSpawnedHosts"
)
type uiParams struct {
Action string `json:"action"`
// the host ids for which an action applies
HostIds []string `json:"host_ids"`
// for the update status option
Status string `json:"status"`
}
func (uis *UIServer) hostPage(w http.ResponseWriter, r *http.Request) {
projCtx := MustHaveProjectContext(r)
vars := mux.Vars(r)
id := vars["host_id"]
h, err := host.FindOne(host.ById(id))
if err != nil {
uis.LoggedError(w, r, http.StatusInternalServerError, err)
return
}
if h == nil {
http.Error(w, "Host not found", http.StatusNotFound)
return
}
events, err := event.Find(event.AllLogCollection, event.MostRecentHostEvents(id, 50))
if err != nil {
uis.LoggedError(w, r, http.StatusInternalServerError, err)
return
}
runningTask := &task.Task{}
if h.RunningTask != "" {
runningTask, err = task.FindOne(task.ById(h.RunningTask))
if err != nil {
uis.LoggedError(w, r, http.StatusInternalServerError, err)
return
}
}
flashes := PopFlashes(uis.CookieStore, r, w)
uis.WriteHTML(w, http.StatusOK, struct {
Flashes []interface{}
Events []event.Event
Host *host.Host
RunningTask *task.Task
User *user.DBUser
ProjectData projectContext
}{flashes, events, h, runningTask, GetUser(r), projCtx},
"base", "host.html", "base_angular.html", "menu.html")
}
func (uis *UIServer) hostsPage(w http.ResponseWriter, r *http.Request) {
projCtx := MustHaveProjectContext(r)
includeSpawnedHosts, _ := strconv.ParseBool(r.FormValue(IncludeSpawnedHosts))
hosts, err := getHostsData(includeSpawnedHosts)
if err != nil {
uis.LoggedError(w, r, http.StatusInternalServerError, err)
return
}
flashes := PopFlashes(uis.CookieStore, r, w)
uis.WriteHTML(w, http.StatusOK, struct {
Flashes []interface{}
Hosts *hostsData
IncludeSpawnedHosts bool
User *user.DBUser
ProjectData projectContext
}{flashes, hosts, includeSpawnedHosts, GetUser(r), projCtx},
"base", "hosts.html", "base_angular.html", "menu.html")
}
func (uis *UIServer) modifyHost(w http.ResponseWriter, r *http.Request) {
_ = MustHaveUser(r)
vars := mux.Vars(r)
id := vars["host_id"]
host, err := host.FindOne(host.ById(id))
if err != nil {
uis.LoggedError(w, r, http.StatusInternalServerError, err)
return
}
if host == nil {
http.Error(w, "Host not found", http.StatusNotFound)
return
}
opts := &uiParams{}
if err = util.ReadJSONInto(util.NewRequestReader(r), opts); err != nil {
uis.LoggedError(w, r, http.StatusBadRequest, err)
return
}
// determine what action needs to be taken
switch opts.Action {
case "updateStatus":
currentStatus := host.Status
newStatus := opts.Status
if !util.SliceContains(validUpdateToStatuses, newStatus) {
http.Error(w, fmt.Sprintf("'%v' is not a valid status", newStatus), http.StatusBadRequest)
return
}
err := host.SetStatus(newStatus)
if err != nil {
uis.LoggedError(w, r, http.StatusInternalServerError, errors.Wrap(err, "Error updating host"))
return
}
msg := NewSuccessFlash(fmt.Sprintf("Host status successfully updated from '%v' to '%v'", currentStatus, host.Status))
PushFlash(uis.CookieStore, r, w, msg)
uis.WriteJSON(w, http.StatusOK, "Successfully updated host status")
default:
uis.WriteJSON(w, http.StatusBadRequest, fmt.Sprintf("Unrecognized action: %v", opts.Action))
}
}
func (uis *UIServer) modifyHosts(w http.ResponseWriter, r *http.Request) {
_ = MustHaveUser(r)
opts := &uiParams{}
if err := util.ReadJSONInto(util.NewRequestReader(r), opts); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
hostIds := opts.HostIds
if len(hostIds) == 1 && strings.TrimSpace(hostIds[0]) == "" {
http.Error(w, "No host ID's found in request", http.StatusBadRequest)
return
}
// fetch all relevant hosts
hosts, err := host.Find(host.ByIds(hostIds))
if err != nil {
uis.LoggedError(w, r, http.StatusInternalServerError, errors.Wrap(err, "Error finding hosts"))
return
}
if len(hosts) == 0 {
http.Error(w, "No matching hosts found.", http.StatusBadRequest)
return
}
// determine what action needs to be taken
switch opts.Action {
case "updateStatus":
newStatus := opts.Status
if !util.SliceContains(validUpdateToStatuses, newStatus) {
http.Error(w, fmt.Sprintf("Invalid status: %v", opts.Status), http.StatusBadRequest)
return
}
numHostsUpdated := 0
for _, host := range hosts {
err := host.SetStatus(newStatus)
if err != nil {
uis.LoggedError(w, r, http.StatusInternalServerError, errors.Wrap(err, "Error updating host"))
return
}
numHostsUpdated += 1
}
msg := NewSuccessFlash(fmt.Sprintf("%v host(s) status successfully updated to '%v'",
numHostsUpdated, newStatus))
PushFlash(uis.CookieStore, r, w, msg)
return
default:
http.Error(w, fmt.Sprintf("Unrecognized action: %v", opts.Action), http.StatusBadRequest)
return
}
}