-
Notifications
You must be signed in to change notification settings - Fork 0
/
deploy.go
140 lines (119 loc) · 3.27 KB
/
deploy.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
package main
import (
"context"
"fmt"
"github.com/fanatic/waypoint-plugin-heroku/heroku"
"github.com/hashicorp/go-hclog"
"github.com/hashicorp/waypoint-plugin-sdk/component"
"github.com/hashicorp/waypoint-plugin-sdk/terminal"
herokuSDK "github.com/heroku/heroku-go/v5"
)
type DeployConfig struct {
Pipeline string `hcl:"pipeline,optional"`
App string `hcl:"app,optional"`
}
func (d *Deployment) URL() string { return d.Url }
type Platform struct {
config DeployConfig
}
// Implement Configurable
func (p *Platform) Config() (interface{}, error) {
return &p.config, nil
}
// Implement Builder
func (p *Platform) DeployFunc() interface{} {
// return a function which will be called by Waypoint
return p.deploy
}
// DefaultReleaserFunc implements component.PlatformReleaser
func (p *Platform) DefaultReleaserFunc() interface{} {
return func() *Releaser { return &Releaser{} }
}
func (p *Platform) deploy(
ctx context.Context,
ui terminal.UI,
src *component.Source,
job *component.JobInfo,
log hclog.Logger,
artifact *Artifact,
//slug *builder.Slug,
) (*Deployment, error) {
log.Info(
"Start deploy",
"src", src,
"config", p.config,
"artifact", artifact,
)
h, err := heroku.New()
if err != nil {
return nil, err
}
// TODO: support dynamically creating an app in the pipeline per deploy
// if p.config.App == "" {
// p.createHerokuApp()
// }
if artifact.ContainerImageDigest != "" {
if err := p.releaseHerokuContainer(ctx, log, h, p.config.App, artifact.ContainerImageDigest); err != nil {
return nil, err
}
} else if artifact.SlugID != "" {
if err := p.releaseHerokuSlug(ctx, log, h, job, p.config.App, artifact.SlugID); err != nil {
return nil, err
}
} else {
return nil, fmt.Errorf("missing either container or slug artifact")
}
app, err := h.AppInfo(ctx, p.config.App)
if err != nil {
return nil, err
}
return &Deployment{
Url: app.WebURL,
}, nil
}
func (p *Platform) releaseHerokuContainer(ctx context.Context, log hclog.Logger, h *herokuSDK.Service, app, dockerImage string) error {
type Update struct {
DockerImage string `json:"docker_image" url:"docker_image,key"`
Process string `json:"process" url:"process,key"`
}
opts := struct {
Updates []Update `json:"updates" url:"updates,key"`
}{}
opts.Updates = append(opts.Updates, Update{Process: "web", DockerImage: dockerImage})
log.Info(
"About to update formation",
"app", app,
"dockerImage", dockerImage,
"opts", opts,
)
var formation herokuSDK.FormationBatchUpdateResult
if err := h.Patch(ctx, &formation, fmt.Sprintf("/apps/%v/formation", app), opts); err != nil {
return err
}
log.Info(
"Formation updated",
"formation", formation,
)
return nil
}
func (p *Platform) releaseHerokuSlug(ctx context.Context, log hclog.Logger, h *herokuSDK.Service, job *component.JobInfo, app, slugID string) error {
desc := "Deployed " + job.Id
release, err := h.ReleaseCreate(ctx, app, herokuSDK.ReleaseCreateOpts{
Description: &desc,
Slug: slugID,
})
if err != nil {
return err
}
log.Info(
"Release created",
"release", release,
)
return nil
}
var (
_ component.Platform = (*Platform)(nil)
_ component.Configurable = (*Platform)(nil)
_ component.PlatformReleaser = (*Platform)(nil)
_ component.Deployment = (*Deployment)(nil)
)