/
resource.go
421 lines (337 loc) · 9.4 KB
/
resource.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
package k8s
import (
"context"
"crypto/sha256"
"fmt"
"io"
"math/rand"
"net/url"
"os/exec"
"github.com/convox/convox/pkg/common"
"github.com/convox/convox/pkg/structs"
"github.com/creack/pty"
"github.com/pkg/errors"
am "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func (p *Provider) ResourceConsole(app, name string, rw io.ReadWriter, opts structs.ResourceConsoleOptions) error {
r, err := p.ResourceGet(app, name)
if err != nil {
return errors.WithStack(err)
}
u, err := p.resourceUrl(app, name)
if err != nil {
return err
}
cn, err := parseResourceURL(u)
if err != nil {
return errors.WithStack(err)
}
switch r.Type {
case "mariadb":
return resourceConsoleCommand(rw, opts, "mysql", "-h", cn.Host, "-P", cn.Port, "-u", cn.Username, fmt.Sprintf("-p%s", cn.Password), "-D", cn.Database)
case "memcached":
return resourceConsoleCommand(rw, opts, "telnet", cn.Host, cn.Port)
case "mysql":
return resourceConsoleCommand(rw, opts, "mysql", "-h", cn.Host, "-P", cn.Port, "-u", cn.Username, fmt.Sprintf("-p%s", cn.Password), "-D", cn.Database)
case "postgis":
return resourceConsoleCommand(rw, opts, "psql", u)
case "postgres":
return resourceConsoleCommand(rw, opts, "psql", u)
case "redis":
return resourceConsoleCommand(rw, opts, "redis-cli", "-u", u)
default:
return errors.WithStack(fmt.Errorf("console not available for resources of type: %s", r.Type))
}
}
func (p *Provider) ResourceExport(app, name string) (io.ReadCloser, error) {
r, err := p.ResourceGet(app, name)
if err != nil {
return nil, errors.WithStack(err)
}
u, err := p.resourceUrl(app, name)
if err != nil {
return nil, err
}
switch r.Type {
case "mariadb":
return resourceExportMysql(u)
case "mysql":
return resourceExportMysql(u)
case "postgis":
return resourceExportPostgres(u)
case "postgres":
return resourceExportPostgres(u)
default:
return nil, errors.WithStack(fmt.Errorf("export not available for resources of type: %s", r.Type))
}
}
func (p *Provider) ResourceGet(app, name string) (*structs.Resource, error) {
m, _, err := common.AppManifest(p, app)
if err != nil {
return nil, err
}
mr, err := m.Resource(name)
if err != nil {
return nil, err
}
u, err := p.resourceUrl(app, name)
if err != nil {
return nil, err
}
r := &structs.Resource{
Name: name,
Status: "external",
Type: mr.Type,
Url: u,
}
overlay, err := p.resourceOverlay(app, name)
if err != nil {
return nil, err
}
if overlay {
return r, nil
}
d, err := p.Cluster.AppsV1().Deployments(p.AppNamespace(app)).Get(context.TODO(), fmt.Sprintf("resource-%s", nameFilter(name)), am.GetOptions{})
if err != nil {
return nil, err
}
if d.Status.ReadyReplicas < 1 {
r.Status = "pending"
} else {
r.Status = "running"
}
return r, nil
}
func (p *Provider) ResourceImport(app, name string, r io.Reader) error {
rr, err := p.ResourceGet(app, name)
if err != nil {
return errors.WithStack(err)
}
switch rr.Type {
case "mariadb":
return resourceImportMysql(rr, r)
case "mysql":
return resourceImportMysql(rr, r)
case "postgis":
return resourceImportPostgres(rr, r)
case "postgres":
return resourceImportPostgres(rr, r)
default:
return errors.WithStack(fmt.Errorf("import not available for resources of type: %s", rr.Type))
}
}
func (p *Provider) ResourceList(app string) (structs.Resources, error) {
lopts := am.ListOptions{
LabelSelector: fmt.Sprintf("app=%s,type=resource", app),
}
ds, err := p.Cluster.AppsV1().Deployments(p.AppNamespace(app)).List(context.TODO(), lopts)
if err != nil {
return nil, errors.WithStack(err)
}
rs := structs.Resources{}
for _, d := range ds.Items {
r, err := p.ResourceGet(app, d.ObjectMeta.Labels["resource"])
if err != nil {
return nil, errors.WithStack(err)
}
rs = append(rs, *r)
}
return rs, nil
}
func (p *Provider) SystemResourceCreate(kind string, opts structs.ResourceCreateOptions) (*structs.Resource, error) {
switch kind {
case "webhook":
return p.systemResourceCreateWebhook(opts)
default:
return nil, fmt.Errorf("rack resource type unknown: %s", kind)
}
}
func (p *Provider) systemResourceCreateWebhook(opts structs.ResourceCreateOptions) (*structs.Resource, error) {
url, ok := opts.Parameters["Url"]
if !ok {
return nil, fmt.Errorf("parameter required: Url")
}
key := fmt.Sprintf("%s-%d", url, rand.Int63())
name := common.DefaultString(opts.Name, fmt.Sprintf("webhook-%s", fmt.Sprintf("%x", sha256.Sum256([]byte(key)))[0:6]))
if err := p.webhookCreate(name, url); err != nil {
return nil, err
}
return p.SystemResourceGet(name)
}
func (p *Provider) SystemResourceDelete(name string) error {
r, err := p.SystemResourceGet(name)
if err != nil {
return err
}
switch r.Type {
case "webhook":
return p.webhookDelete(r.Name)
default:
return fmt.Errorf("rack resource type unknown: %s", r.Type)
}
}
func (p *Provider) SystemResourceGet(name string) (*structs.Resource, error) {
rs, err := p.SystemResourceList()
if err != nil {
return nil, err
}
for _, r := range rs {
if r.Name == name {
return &r, nil
}
}
return nil, fmt.Errorf("no such resource: %s", name)
}
func (p *Provider) SystemResourceLink(name, app string) (*structs.Resource, error) {
return nil, errors.WithStack(fmt.Errorf("unavailable on v3 racks"))
}
func (p *Provider) SystemResourceList() (structs.Resources, error) {
rs := structs.Resources{}
ws, err := p.webhookList()
if err != nil {
return nil, err
}
for _, w := range ws {
rs = append(rs, structs.Resource{
Name: w.Name,
Parameters: map[string]string{
"Url": w.URL,
},
Status: "running",
Type: "webhook",
})
}
return rs, nil
}
func (p *Provider) SystemResourceTypes() (structs.ResourceTypes, error) {
rst := structs.ResourceTypes{
structs.ResourceType{
Name: "webhook",
Parameters: structs.ResourceParameters{
structs.ResourceParameter{
Default: "",
Description: "url to which to post rack events",
Name: "Url",
},
},
},
}
return rst, nil
}
func (p *Provider) SystemResourceUnlink(name, app string) (*structs.Resource, error) {
return nil, errors.WithStack(fmt.Errorf("unavailable on v3 racks"))
}
func (p *Provider) SystemResourceUpdate(name string, opts structs.ResourceUpdateOptions) (*structs.Resource, error) {
return nil, errors.WithStack(fmt.Errorf("unavailable on v3 racks"))
}
func (p *Provider) resourceOverlay(app, name string) (bool, error) {
m, rel, err := common.AppManifest(p, app)
if err != nil {
return false, err
}
r, err := m.Resource(name)
if err != nil {
return false, err
}
env, err := structs.NewEnvironment([]byte(rel.Env))
if err != nil {
return false, err
}
return env[r.DefaultEnv()] != "", nil
}
func (p *Provider) resourceUrl(app, name string) (string, error) {
cm, err := p.Cluster.CoreV1().ConfigMaps(p.AppNamespace(app)).Get(context.TODO(), fmt.Sprintf("resource-%s", nameFilter(name)), am.GetOptions{})
if err != nil {
return "", errors.WithStack(err)
}
return cm.Data["URL"], nil
}
type resourceConnection struct {
Database string
Host string
Password string
Port string
Username string
}
func parseResourceURL(url_ string) (*resourceConnection, error) {
u, err := url.Parse(url_)
if err != nil {
return nil, errors.WithStack(err)
}
cn := &resourceConnection{
Host: u.Hostname(),
Port: u.Port(),
Username: u.User.Username(),
}
if pw, ok := u.User.Password(); ok {
cn.Password = pw
}
if len(u.Path) > 0 {
cn.Database = u.Path[1:]
}
return cn, nil
}
func resourceConsoleCommand(rw io.ReadWriter, opts structs.ResourceConsoleOptions, command string, args ...string) error {
cmd := exec.Command(command, args...)
size := &pty.Winsize{}
if opts.Height != nil {
size.Rows = uint16(*opts.Height)
}
if opts.Width != nil {
size.Cols = uint16(*opts.Width)
}
fd, err := pty.StartWithSize(cmd, size)
if err != nil {
return errors.WithStack(err)
}
go io.Copy(fd, rw)
io.Copy(rw, fd)
return nil
}
func resourceExportCommand(w io.WriteCloser, command string, args ...string) {
defer w.Close()
cmd := exec.Command(command, args...)
cmd.Stdout = w
cmd.Stderr = w
if err := cmd.Run(); err != nil {
fmt.Fprintf(w, "ERROR: could not export: %v\n", err)
}
}
func resourceExportMysql(url string) (io.ReadCloser, error) {
cn, err := parseResourceURL(url)
if err != nil {
return nil, errors.WithStack(err)
}
rr, ww := io.Pipe()
go resourceExportCommand(ww, "mysqldump", "-h", cn.Host, "-P", cn.Port, "-u", cn.Username, fmt.Sprintf("-p%s", cn.Password), cn.Database)
return rr, nil
}
func resourceExportPostgres(url string) (io.ReadCloser, error) {
rr, ww := io.Pipe()
go resourceExportCommand(ww, "pg_dump", "--no-acl", "--no-owner", url)
return rr, nil
}
func resourceImportMysql(rr *structs.Resource, r io.Reader) error {
cn, err := parseResourceURL(rr.Url)
if err != nil {
return errors.WithStack(err)
}
cmd := exec.Command("mysql", "-h", cn.Host, "-P", cn.Port, "-u", cn.Username, fmt.Sprintf("-p%s", cn.Password), "-D", cn.Database)
cmd.Stdin = r
data, err := cmd.CombinedOutput()
fmt.Printf("string(data): %+v\n", string(data))
if err != nil {
return errors.WithStack(fmt.Errorf("ERROR: import failed"))
}
return nil
}
func resourceImportPostgres(rr *structs.Resource, r io.Reader) error {
cmd := exec.Command("psql", rr.Url)
cmd.Stdin = r
data, err := cmd.CombinedOutput()
fmt.Printf("string(data): %+v\n", string(data))
if err != nil {
return errors.WithStack(fmt.Errorf("ERROR: import failed"))
}
return nil
}