/
deploys.go
257 lines (198 loc) · 5.6 KB
/
deploys.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
package bitballoon
import(
"io/ioutil"
"os"
"time"
"path"
"path/filepath"
"errors"
"strings"
"crypto/sha1"
"encoding/hex"
)
// Deploy represents a specific deploy of a site
type Deploy struct {
Id string `json:"id"`
SiteId string `json:"site_id"`
UserId string `json:"user_id"`
// State of the deploy (uploading/uploaded/processing/ready/error)
State string `json:"state"`
// Cause of error if State is "error"
ErrorMessage string `json:"error_message"`
// Shas of files that needs to be uploaded before the deploy is ready
Required []string `json:"required"`
DeployUrl string `json:"deploy_url"`
ScreenshotUrl string `json:"screenshot_url"`
CreatedAt Timestamp `json:"created_at"`
UpdatedAt Timestamp `json:"updated_at"`
client *Client
}
// DeploysService is used to access all Deploy related API methods
type DeploysService struct {
site *Site
client *Client
}
type deployFiles struct {
Files *map[string]string `json:"files"`
}
func (s *DeploysService) apiPath() string {
if s.site != nil {
return path.Join(s.site.apiPath(), "deploys")
} else {
return "/deploys"
}
}
// Create a new deploy
//
// Example: site.Deploys.Create("/path/to/site-dir")
func (s *DeploysService) Create(dirOrZip string) (*Deploy, *Response, error) {
if s.site == nil {
return nil, nil, errors.New("You can only create a new deploy for an existing site (site.Deploys.Create(dirOrZip)))")
}
if strings.HasSuffix(dirOrZip, ".zip") {
return s.deployZip(dirOrZip)
} else {
return s.deployDir(dirOrZip)
}
}
// List all deploys. Takes ListOptions to control pagination.
func (s *DeploysService) List(options *ListOptions) ([]Deploy, *Response, error) {
deploys := new([]Deploy)
reqOptions := &RequestOptions{QueryParams: options.toQueryParamsMap()}
resp, err := s.client.Request("GET", s.apiPath(), reqOptions, deploys)
for _, deploy := range(*deploys) {
deploy.client = s.client
}
return *deploys, resp, err
}
// Get a specific deploy.
func (d *DeploysService) Get(id string) (*Deploy, *Response, error) {
deploy := &Deploy{Id: id, client: d.client}
resp, err := deploy.Reload()
return deploy, resp, err
}
func (deploy *Deploy) apiPath() string {
return path.Join("/deploys", deploy.Id)
}
// Reload a deploy from the API
func (deploy *Deploy) Reload() (*Response, error) {
if deploy.Id == "" {
return nil, errors.New("Cannot fetch deploy without an ID")
}
return deploy.client.Request("GET", deploy.apiPath(), nil, deploy)
}
// Restore an old deploy. Sets the deploy as the active deploy for a site
func (deploy *Deploy) Restore() (*Response, error) {
return deploy.client.Request("POST", path.Join(deploy.apiPath(), "restore"), nil, deploy)
}
func (s *DeploysService) deployDir(dir string) (*Deploy, *Response, error) {
files := map[string]string{}
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if info.IsDir() == false {
rel, err := filepath.Rel(dir, path)
if err != nil {
return err
}
if strings.HasPrefix(rel, ".") || strings.Contains(rel, "/.") {
return nil
}
sha := sha1.New()
data, err := ioutil.ReadFile(path)
if err != nil {
return err
}
sha.Write(data)
files[rel] = hex.EncodeToString(sha.Sum(nil))
}
return nil
})
options := &RequestOptions{
JsonBody: &deployFiles{
Files: &files,
},
}
deploy := &Deploy{client: s.client}
resp, err := s.client.Request("POST", s.apiPath(), options, deploy)
if err != nil {
return deploy, resp, err
}
lookup := map[string]bool{}
for _, sha := range deploy.Required {
lookup[sha] = true
}
for path, sha := range files {
if lookup[sha] == true {
file, err := os.Open(filepath.Join(dir, path))
defer file.Close()
if err != nil {
return deploy, nil, err
}
info, err := file.Stat()
if err != nil {
return deploy, nil, err
}
options = &RequestOptions{
RawBody: file,
RawBodyLength: info.Size(),
Headers: &map[string]string{"Content-Type": "application/octet-stream"},
}
resp, err = s.client.Request("PUT", filepath.Join(deploy.apiPath(), "files", path), options, nil)
if err != nil {
return deploy, resp, err
}
}
}
return deploy, resp, err
}
func (s *DeploysService) deployZip(zip string) (*Deploy, *Response, error) {
zipPath, err := filepath.Abs(zip)
if err != nil {
return nil, nil, err
}
zipFile, err := os.Open(zipPath)
defer zipFile.Close()
if err != nil {
return nil, nil, err
}
info, err := zipFile.Stat()
if err != nil {
return nil, nil, err
}
options := &RequestOptions{
RawBody: zipFile,
RawBodyLength: info.Size(),
Headers: &map[string]string{"Content-Type": "application/zip"},
}
deploy := &Deploy{client: s.client}
resp, err := s.client.Request("POST", s.apiPath(), options, deploy)
return deploy, resp, err
}
func (deploy *Deploy) WaitForReady(timeout time.Duration) error {
if deploy.State == "ready" {
return nil
}
if timeout == 0 {
timeout = defaultTimeout
}
timedOut := false
time.AfterFunc(timeout*time.Second, func() {
timedOut = true
})
done := make(chan error)
go func() {
for {
time.Sleep(1 * time.Second)
if timedOut {
done <- errors.New("Timeout while waiting for processing")
break
}
_, err := deploy.Reload()
if err != nil || (deploy.State == "ready") {
done <- err
break
}
}
}()
err := <-done
return err
}