-
Notifications
You must be signed in to change notification settings - Fork 124
/
api_spawn.go
180 lines (155 loc) · 4.79 KB
/
api_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
package service
import (
"context"
"fmt"
"net/http"
"strings"
"github.com/evergreen-ci/evergreen"
"github.com/evergreen-ci/evergreen/cloud"
"github.com/evergreen-ci/evergreen/model/distro"
"github.com/evergreen-ci/evergreen/model/host"
"github.com/evergreen-ci/evergreen/rest/data"
"github.com/evergreen-ci/evergreen/rest/model"
"github.com/evergreen-ci/gimlet"
"github.com/evergreen-ci/utility"
"github.com/pkg/errors"
)
type spawnResponse struct {
Hosts []host.Host `json:"hosts,omitempty"`
HostInfo host.Host `json:"host_info,omitempty"`
Distros []string `json:"distros,omitempty"`
// empty if the request succeeded
ErrorMessage string `json:"error_message,omitempty"`
}
func (as *APIServer) listDistros(w http.ResponseWriter, r *http.Request) {
distros, err := distro.Find(r.Context(), distro.BySpawnAllowed())
if err != nil {
as.LoggedError(w, r, http.StatusInternalServerError, err)
return
}
distroList := []string{}
for _, d := range distros {
distroList = append(distroList, d.Id)
}
gimlet.WriteJSON(w, spawnResponse{Distros: distroList})
}
func (as *APIServer) requestHost(w http.ResponseWriter, r *http.Request) {
user := MustHaveUser(r)
hostRequest := struct {
Distro string `json:"distro"`
PublicKey string `json:"public_key"`
Region string `json:"region"`
}{}
err := utility.ReadJSON(utility.NewRequestReader(r), &hostRequest)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if hostRequest.Distro == "" {
http.Error(w, "distro may not be blank", http.StatusBadRequest)
return
}
if hostRequest.PublicKey == "" {
http.Error(w, "public key may not be blank", http.StatusBadRequest)
return
}
options := &model.HostRequestOptions{
DistroID: hostRequest.Distro,
KeyName: hostRequest.PublicKey,
Region: hostRequest.Region,
TaskID: "",
UserData: "",
InstanceTags: nil,
InstanceType: "",
}
ctx, cancel := as.env.Context()
defer cancel()
spawnHost, err := data.NewIntentHost(ctx, options, user, as.env)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if spawnHost == nil {
http.Error(w, "spawned host is nil", http.StatusBadRequest)
return
}
gimlet.WriteJSON(w, "")
}
// returns info on the host specified
func (as *APIServer) hostInfo(w http.ResponseWriter, r *http.Request) {
instanceId := gimlet.GetVars(r)["instance_id"]
h, err := host.FindOne(r.Context(), host.ById(instanceId))
if err != nil {
as.LoggedError(w, r, http.StatusInternalServerError, err)
return
}
if h == nil {
http.Error(w, "not found", http.StatusNotFound)
return
}
gimlet.WriteJSON(w, spawnResponse{HostInfo: *h})
}
// returns info on all of the hosts spawned by a user
func (as *APIServer) hostsInfoForUser(w http.ResponseWriter, r *http.Request) {
user := gimlet.GetVars(r)["user"]
hosts, err := host.Find(r.Context(), host.ByUserWithUnterminatedStatus(user))
if err != nil {
as.LoggedError(w, r, http.StatusInternalServerError, err)
return
}
gimlet.WriteJSON(w, spawnResponse{Hosts: hosts})
}
func (as *APIServer) modifyHost(w http.ResponseWriter, r *http.Request) {
instanceId := gimlet.GetVars(r)["instance_id"]
hostAction := r.FormValue("action")
h, err := host.FindOne(r.Context(), host.ById(instanceId))
if err != nil {
as.LoggedError(w, r, http.StatusInternalServerError, err)
return
}
if h == nil {
http.Error(w, "not found", http.StatusNotFound)
return
}
ctx, cancel := context.WithCancel(r.Context())
defer cancel()
user := gimlet.GetUser(ctx)
if user == nil || user.Username() != h.StartedBy {
message := fmt.Sprintf("Only %v is authorized to terminate this host", h.StartedBy)
http.Error(w, message, http.StatusUnauthorized)
return
}
switch hostAction {
case "terminate":
if h.Status == evergreen.HostTerminated {
message := fmt.Sprintf("Host %v is already terminated", h.Id)
http.Error(w, message, http.StatusBadRequest)
return
}
cloudHost, err := cloud.GetCloudHost(ctx, h, as.env)
if err != nil {
as.LoggedError(w, r, http.StatusInternalServerError, err)
return
}
if err = cloudHost.TerminateInstance(ctx, user.Username(), fmt.Sprintf("terminated via API by %s", user.Username())); err != nil {
as.LoggedError(w, r, http.StatusInternalServerError, errors.Wrap(err, "Failed to terminate spawn host"))
return
}
gimlet.WriteJSON(w, spawnResponse{HostInfo: *h})
default:
http.Error(w, fmt.Sprintf("Unrecognized action %v", hostAction), http.StatusBadRequest)
}
}
// returns dockerfle as text
func getDockerfile(w http.ResponseWriter, r *http.Request) {
parts := []string{
"ARG BASE_IMAGE",
"FROM $BASE_IMAGE",
"ARG URL",
"ARG EXECUTABLE_SUB_PATH",
"ARG BINARY_NAME",
"ADD ${URL}/clients/${EXECUTABLE_SUB_PATH} /",
"RUN chmod 0777 /${BINARY_NAME}",
}
gimlet.WriteText(w, strings.Join(parts, "\n"))
}