-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathinternal_api.go
235 lines (211 loc) · 6.23 KB
/
internal_api.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"regexp"
"time"
"gopkg.in/yaml.v2"
)
type InternalApi struct {
Env string
Vault *Vault
Broker *Broker
}
func (api *InternalApi) ServeHTTP(w http.ResponseWriter, req *http.Request) {
if req.URL.Path == "/b/status" {
idx, err := api.Vault.GetIndex("db")
if err != nil {
w.WriteHeader(500)
fmt.Fprintf(w, "error: %s\n", err)
return
}
out := struct {
Env string `json:"env"`
Instances interface{} `json:"instances"`
Plans interface{} `json:"plans"`
Log string `json:"log"`
}{
Env: api.Env,
Instances: idx.Data,
Plans: deinterface(api.Broker.Plans),
Log: Logger.String(),
}
b, err := json.Marshal(out)
if err != nil {
w.WriteHeader(500)
fmt.Fprintf(w, "error: %s\n", err)
return
}
w.Header().Set("Content-type", "application/json")
fmt.Fprintf(w, "%s\n", string(b))
return
}
if req.URL.Path == "/b/cleanup" {
task, err := api.Broker.BOSH.Cleanup(false)
if err != nil {
w.WriteHeader(500)
fmt.Fprintf(w, "error: %s\n", err)
return
}
//return a 200 and a task id for the cleanup task
cleanups, err := api.Broker.serviceWithNoDeploymentCheck()
if err != nil {
w.WriteHeader(500)
fmt.Fprintf(w, "error: %s\n", err)
return
}
out := struct {
TaskID int `json:"task_id"`
Cleanups []string `json:"cleanups"`
}{
TaskID: task.ID,
Cleanups: cleanups,
}
js, err := json.Marshal(out)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(200)
fmt.Fprintf(w, "%s\n", string(js))
return
}
pattern := regexp.MustCompile("^/b/([^/]+)/manifest\\.yml$")
if m := pattern.FindStringSubmatch(req.URL.Path); m != nil {
l := Logger.Wrap("manifest.yml")
l.Debug("looking up BOSH manifest for %s", m[1])
manifest := struct {
Manifest string `json:"manifest"`
}{}
exists, err := api.Vault.Get(fmt.Sprintf("%s/manifest", m[1]), &manifest)
if err != nil || !exists {
l.Error("unable to find service instance %s in vault index", m[1])
w.WriteHeader(404)
return
}
w.Header().Set("Content-Type", "text/plain")
w.Write([]byte(manifest.Manifest + "\n"))
return
}
pattern = regexp.MustCompile("^/b/([^/]+)/redeploy$")
if m := pattern.FindStringSubmatch(req.URL.Path); m != nil {
l := Logger.Wrap("update-service")
l.Debug("looking up BOSH manifest for %s", m[1])
manifest := struct {
Manifest string `json:"manifest"`
}{}
exists, err := api.Vault.Get(fmt.Sprintf("%s/manifest", m[1]), &manifest)
if err != nil || !exists {
l.Error("unable to find service instance %s in vault index", m[1])
w.WriteHeader(404)
return
}
task, err := api.Broker.BOSH.CreateDeployment(manifest.Manifest)
if err != nil {
w.WriteHeader(500)
fmt.Fprintf(w, "error: %s\n", err)
return
}
w.Header().Set("Content-Type", "application/json")
fmt.Fprintf(w, "{\"message\": \"Redeploy of %s requested in task %d\"}", m[1], task.ID)
return
}
pattern = regexp.MustCompile("^/b/([^/]+)/creds\\.yml$")
if m := pattern.FindStringSubmatch(req.URL.Path); m != nil {
l := Logger.Wrap("creds.yml")
l.Debug("looking up credentials for %s", m[1])
inst, exists, err := api.Vault.FindInstance(m[1])
if err != nil || !exists {
l.Error("unable to find service instance %s in vault index", m[1])
} else {
w.Header().Set("Content-type", "text/plain")
l.Debug("looking up service '%s' / plan '%s' in catalog", inst.ServiceID, inst.PlanID)
plan, err := api.Broker.FindPlan(inst.ServiceID, inst.PlanID)
if err != nil {
w.WriteHeader(500)
fmt.Fprintf(w, "---\nerror: unable to find plan '%s'\n\n", inst.PlanID)
return
}
creds, err := GetCreds(m[1], plan, api.Broker.BOSH, l)
if err != nil {
w.WriteHeader(500)
fmt.Fprintf(w, "---\nerror: unable to retrieve credentials\n\n")
return
}
b, err := yaml.Marshal(creds)
if err != nil {
w.WriteHeader(500)
fmt.Fprintf(w, "---\nerror: unable to yamlify credentials\n\n")
return
}
fmt.Fprintf(w, "%s\n", string(b))
return
}
w.WriteHeader(404)
return
}
pattern = regexp.MustCompile("^/b/([^/]+)/task\\.log$")
if m := pattern.FindStringSubmatch(req.URL.Path); m != nil {
l := Logger.Wrap("task.log")
l.Debug("looking up task log for %s", m[1])
_, id, _, err := api.Vault.State(m[1])
if err != nil {
l.Error("unable to find service instance %s in vault index", m[1])
w.WriteHeader(404)
return
}
if id == 0 {
l.Error("'task' key not found in vault index for service instance %s; perhaps vault is corrupted?", m[1])
w.WriteHeader(404)
return
}
events, err := api.Broker.BOSH.GetTaskEvents(id)
if err != nil {
w.WriteHeader(500)
fmt.Fprintf(w, "error: %s\n", err)
return
}
w.Header().Set("Content-type", "text/plain")
for _, event := range events {
ts := time.Unix(int64(event.Time), 0)
if event.Task != "" {
fmt.Fprintf(w, "Task %d | %s | %s: %s %s\n", id, ts.Format("15:04:05"), event.Stage, event.Task, event.State)
} else if event.Error.Code != 0 {
fmt.Fprintf(w, "Task %d | %s | ERROR: [%d] %s\n", id, ts.Format("15:04:05"), event.Error.Code, event.Error.Message)
}
}
return
}
pattern = regexp.MustCompile("^/b/([^/]+)/update-manifest$")
if m := pattern.FindStringSubmatch(req.URL.Path); m != nil {
if req.Method != http.MethodPost {
w.WriteHeader(http.StatusMethodNotAllowed)
fmt.Fprint(w, "Method Not Allowed")
return
}
l := Logger.Wrap("update-manifest")
l.Debug("Updating BOSH manifest for %s", m[1])
newManifest, err := ioutil.ReadAll(req.Body)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "error reading request body: %s\n", err)
return
}
vaultPath := fmt.Sprintf("%s/manifest", m[1])
manifestData := struct {
Manifest string `json:"manifest"`
}{
Manifest: string(newManifest),
}
err = api.Vault.Put(vaultPath, &manifestData)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "error updating manifest in vault: %s\n", err)
return
}
w.Header().Set("Content-Type", "application/json")
fmt.Fprintf(w, "{\"message\": \"Manifest for %s updated successfully\"}", m[1])
return
}
w.WriteHeader(404)
fmt.Fprintf(w, "endpoint not found...\n")
}