/
deployments.go
202 lines (169 loc) · 5.11 KB
/
deployments.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
package scalingo
import (
"encoding/json"
"net/http"
"net/url"
"time"
httpclient "github.com/Scalingo/go-scalingo/http"
"golang.org/x/net/websocket"
"gopkg.in/errgo.v1"
)
type DeploymentsService interface {
DeploymentList(app string) ([]*Deployment, error)
Deployment(app string, deploy string) (*Deployment, error)
DeploymentLogs(deployURL string) (*http.Response, error)
DeploymentStream(deployURL string) (*websocket.Conn, error)
DeploymentsCreate(app string, params *DeploymentsCreateParams) (*Deployment, error)
}
var _ DeploymentsService = (*Client)(nil)
type DeploymentStatus string
const (
StatusSuccess DeploymentStatus = "success"
StatusQueued DeploymentStatus = "queued"
StatusBuilding DeploymentStatus = "building"
StatusStarting DeploymentStatus = "starting"
StatusPushing DeploymentStatus = "pushing"
StatusAborted DeploymentStatus = "aborted"
StatusBuildError DeploymentStatus = "build-error"
StatusCrashedError DeploymentStatus = "crashed-error"
StatusTimeoutError DeploymentStatus = "timeout-error"
StatusHookError DeploymentStatus = "hook-error"
)
type Deployment struct {
ID string `json:"id"`
AppID string `json:"app_id"`
CreatedAt *time.Time `json:"created_at"`
Status DeploymentStatus `json:"status"`
GitRef string `json:"git_ref"`
Image string `json:"image"`
Registry string `json:"registry"`
Duration int `json:"duration"`
PostdeployHook string `json:"postdeploy_hook"`
User *User `json:"pusher"`
Links *DeploymentLinks `json:"links"`
}
type DeploymentsCreateParams struct {
GitRef *string `json:"git_ref"`
SourceURL string `json:"source_url"`
}
type DeploymentsCreateRes struct {
Deployment *Deployment `json:"deployment"`
}
func (d *Deployment) HasFailed() bool {
return HasFailedString(d.Status)
}
func HasFailedString(status DeploymentStatus) bool {
if !IsFinishedString(status) {
return false
}
if status == StatusSuccess {
return false
}
return true
}
func (d *Deployment) IsFinished() bool {
return IsFinishedString(d.Status)
}
func IsFinishedString(status DeploymentStatus) bool {
return status != StatusBuilding && status != StatusStarting &&
status != StatusPushing && status != StatusQueued
}
type DeploymentList struct {
Deployments []*Deployment `json:"deployments"`
}
type DeploymentLinks struct {
Output string `json:"output"`
}
type AuthenticationData struct {
Token string `json:"token"`
}
type AuthStruct struct {
Type string `json:"type"`
Data AuthenticationData `json:"data"`
}
func (c *Client) DeploymentList(app string) ([]*Deployment, error) {
var deployments DeploymentList
req := &httpclient.APIRequest{
Endpoint: "/apps/" + app + "/deployments",
}
err := c.ScalingoAPI().DoRequest(req, &deployments)
if err != nil {
return []*Deployment{}, errgo.Mask(err, errgo.Any)
}
return deployments.Deployments, nil
}
func (c *Client) Deployment(app string, deploy string) (*Deployment, error) {
var deploymentMap map[string]*Deployment
req := &httpclient.APIRequest{
Endpoint: "/apps/" + app + "/deployments/" + deploy,
}
err := c.ScalingoAPI().DoRequest(req, &deploymentMap)
if err != nil {
return nil, errgo.Mask(err, errgo.Any)
}
return deploymentMap["deployment"], nil
}
func (c *Client) DeploymentLogs(deployURL string) (*http.Response, error) {
u, err := url.Parse(deployURL)
if err != nil {
return nil, errgo.Mask(err, errgo.Any)
}
req := &httpclient.APIRequest{
Expected: httpclient.Statuses{200, 404},
Endpoint: u.Path,
URL: u.Scheme + "://" + u.Host,
}
return c.ScalingoAPI().Do(req)
}
func (c *Client) DeploymentStream(deployURL string) (*websocket.Conn, error) {
token, err := c.ScalingoAPI().TokenGenerator().GetAccessToken()
if err != nil {
return nil, errgo.Notef(err, "fail to generate token")
}
authString, err := json.Marshal(&AuthStruct{
Type: "auth",
Data: AuthenticationData{
Token: token,
},
})
if err != nil {
return nil, errgo.Mask(err, errgo.Any)
}
conn, err := websocket.Dial(deployURL, "", "http://scalingo-cli.local/1")
if err != nil {
return nil, errgo.Mask(err, errgo.Any)
}
_, err = conn.Write(authString)
if err != nil {
return nil, errgo.Mask(err, errgo.Any)
}
return conn, nil
}
func (c *Client) DeploymentsCreate(app string, params *DeploymentsCreateParams) (*Deployment, error) {
var response *DeploymentsCreateRes
req := &httpclient.APIRequest{
Method: "POST",
Endpoint: "/apps/" + app + "/deployments",
Expected: httpclient.Statuses{201},
Params: map[string]interface{}{
"deployment": params,
},
}
err := c.ScalingoAPI().DoRequest(req, &response)
if err != nil {
return nil, errgo.Mask(err, errgo.Any)
}
return response.Deployment, nil
}
func (c *Client) DeploymentCacheReset(app string) error {
req := &httpclient.APIRequest{
Endpoint: "/apps/" + app + "/caches/deployment",
Method: "DELETE",
Expected: httpclient.Statuses{204},
}
err := c.ScalingoAPI().DoRequest(req, nil)
if err != nil {
return errgo.Mask(err)
}
return nil
}