forked from evergreen-ci/evergreen
/
distros.go
233 lines (189 loc) · 6.54 KB
/
distros.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
package service
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"sort"
"github.com/evergreen-ci/evergreen/model/distro"
"github.com/evergreen-ci/evergreen/model/event"
"github.com/evergreen-ci/evergreen/model/host"
"github.com/evergreen-ci/evergreen/model/user"
"github.com/evergreen-ci/evergreen/util"
"github.com/evergreen-ci/evergreen/validator"
"github.com/gorilla/mux"
)
func (uis *UIServer) distrosPage(w http.ResponseWriter, r *http.Request) {
projCtx := MustHaveProjectContext(r)
distros, err := distro.Find(distro.All)
if err != nil {
message := fmt.Sprintf("error fetching distros: %v", err)
PushFlash(uis.CookieStore, r, w, NewErrorFlash(message))
http.Error(w, message, http.StatusInternalServerError)
return
}
sort.Sort(&sortableDistro{distros})
uis.WriteHTML(w, http.StatusOK, struct {
Distros []distro.Distro
Keys map[string]string
User *user.DBUser
ProjectData projectContext
Flashes []interface{}
}{distros, uis.Settings.Keys, GetUser(r), projCtx, PopFlashes(uis.CookieStore, r, w)},
"base", "distros.html", "base_angular.html", "menu.html")
}
func (uis *UIServer) modifyDistro(w http.ResponseWriter, r *http.Request) {
id := mux.Vars(r)["distro_id"]
shouldDeco := r.FormValue("deco") == "true"
u := MustHaveUser(r)
body := util.NewRequestReader(r)
defer body.Close()
b, err := ioutil.ReadAll(body)
if err != nil {
message := fmt.Sprintf("error reading request: %v", err)
PushFlash(uis.CookieStore, r, w, NewErrorFlash(message))
http.Error(w, message, http.StatusBadRequest)
return
}
oldDistro, err := distro.FindOne(distro.ById(id))
if err != nil {
message := fmt.Sprintf("error finding distro: %v", err)
PushFlash(uis.CookieStore, r, w, NewErrorFlash(message))
http.Error(w, message, http.StatusInternalServerError)
return
}
newDistro := *oldDistro
// attempt to unmarshal data into distros field for type validation
if err = json.Unmarshal(b, &newDistro); err != nil {
message := fmt.Sprintf("error unmarshaling request: %v", err)
PushFlash(uis.CookieStore, r, w, NewErrorFlash(message))
http.Error(w, message, http.StatusBadRequest)
return
}
// check that the resulting distro is valid
vErrs, err := validator.CheckDistro(&newDistro, &uis.Settings, false)
if err != nil {
message := fmt.Sprintf("error retrieving distroIds: %v", err)
PushFlash(uis.CookieStore, r, w, NewErrorFlash(message))
http.Error(w, message, http.StatusInternalServerError)
return
}
if len(vErrs) != 0 {
for _, e := range vErrs {
PushFlash(uis.CookieStore, r, w, NewErrorFlash(e.Error()))
}
uis.WriteJSON(w, http.StatusBadRequest, vErrs)
return
}
if err = newDistro.Update(); err != nil {
message := fmt.Sprintf("error updating distro: %v", err)
PushFlash(uis.CookieStore, r, w, NewErrorFlash(message))
http.Error(w, message, http.StatusBadRequest)
return
}
if shouldDeco {
err = host.DecommissionHostsWithDistroId(newDistro.Id)
if err != nil {
message := fmt.Sprintf("error decommissioning hosts: %v", err)
PushFlash(uis.CookieStore, r, w, NewErrorFlash(message))
http.Error(w, message, http.StatusBadRequest)
return
}
}
event.LogDistroModified(id, u.Username(), newDistro)
message := fmt.Sprintf("Distro %v successfully updated.", id)
if shouldDeco {
message = fmt.Sprintf("Distro %v successfully updated and running hosts decommissioned", id)
}
PushFlash(uis.CookieStore, r, w, NewSuccessFlash(message))
uis.WriteJSON(w, http.StatusOK, "distro successfully updated")
}
func (uis *UIServer) removeDistro(w http.ResponseWriter, r *http.Request) {
id := mux.Vars(r)["distro_id"]
u := MustHaveUser(r)
d, err := distro.FindOne(distro.ById(id))
if err != nil {
message := fmt.Sprintf("error finding distro: %v", err)
PushFlash(uis.CookieStore, r, w, NewErrorFlash(message))
http.Error(w, message, http.StatusInternalServerError)
return
}
if err = distro.Remove(id); err != nil {
message := fmt.Sprintf("error removing distro '%v': %v", id, err)
PushFlash(uis.CookieStore, r, w, NewErrorFlash(message))
http.Error(w, message, http.StatusInternalServerError)
return
}
event.LogDistroRemoved(id, u.Username(), d)
PushFlash(uis.CookieStore, r, w, NewSuccessFlash(fmt.Sprintf("Distro %v successfully removed.", id)))
uis.WriteJSON(w, http.StatusOK, "distro successfully removed")
}
func (uis *UIServer) getDistro(w http.ResponseWriter, r *http.Request) {
id := mux.Vars(r)["distro_id"]
d, err := distro.FindOne(distro.ById(id))
if err != nil {
message := fmt.Sprintf("error fetching distro '%v': %v", id, err)
PushFlash(uis.CookieStore, r, w, NewErrorFlash(message))
http.Error(w, message, http.StatusInternalServerError)
return
}
uis.WriteJSON(w, http.StatusOK, d)
}
func (uis *UIServer) addDistro(w http.ResponseWriter, r *http.Request) {
id, hasId := mux.Vars(r)["distro_id"]
u := MustHaveUser(r)
body := util.NewRequestReader(r)
defer body.Close()
b, err := ioutil.ReadAll(body)
if err != nil {
message := fmt.Sprintf("error adding distro: %v", err)
PushFlash(uis.CookieStore, r, w, NewErrorFlash(message))
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
var d distro.Distro
if err = json.Unmarshal(b, &d); err != nil {
message := fmt.Sprintf("error adding distro: %v", err)
PushFlash(uis.CookieStore, r, w, NewErrorFlash(message))
http.Error(w, message, http.StatusBadRequest)
return
}
if hasId {
d.Id = id
}
vErrs, err := validator.CheckDistro(&d, &uis.Settings, true)
if err != nil {
message := fmt.Sprintf("error retrieving distroIds: %v", err)
PushFlash(uis.CookieStore, r, w, NewErrorFlash(message))
http.Error(w, message, http.StatusInternalServerError)
return
}
if len(vErrs) != 0 {
for _, e := range vErrs {
PushFlash(uis.CookieStore, r, w, NewErrorFlash(e.Error()))
}
uis.WriteJSON(w, http.StatusBadRequest, vErrs)
return
}
if err = d.Insert(); err != nil {
message := fmt.Sprintf("error inserting distro '%v': %v", d.Id, err)
PushFlash(uis.CookieStore, r, w, NewErrorFlash(message))
uis.WriteJSON(w, http.StatusInternalServerError, err)
return
}
event.LogDistroAdded(d.Id, u.Username(), d)
PushFlash(uis.CookieStore, r, w, NewSuccessFlash(fmt.Sprintf("Distro %v successfully added.", d.Id)))
uis.WriteJSON(w, http.StatusOK, "distro successfully added")
}
type sortableDistro struct {
distros []distro.Distro
}
func (s *sortableDistro) Len() int {
return len(s.distros)
}
func (s *sortableDistro) Less(i, j int) bool {
return s.distros[i].Id < s.distros[j].Id
}
func (s *sortableDistro) Swap(i, j int) {
s.distros[i], s.distros[j] = s.distros[j], s.distros[i]
}