/
deploy.go
347 lines (301 loc) · 10.2 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
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
package commands
import (
"archive/tar"
"bytes"
"compress/gzip"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"mime/multipart"
"net/http"
"net/url"
"os"
"path/filepath"
"runtime"
"strconv"
"strings"
"time"
"github.com/rs/zerolog/log"
"github.com/alecthomas/kong"
"github.com/section/sectionctl/api"
)
// MaxFileSize is the tarball file size allowed to be uploaded in bytes.
const MaxFileSize = 1073741824 // 1GB
// DeployCmd handles deploying an app to Section.
type DeployCmd struct {
AccountID int `short:"a" help:"AccountID to deploy application to."`
AppID int `short:"i" help:"AppID to deploy application to."`
Environment string `short:"e" default:"Production" help:"Environment to deploy application to. (name of git branch ie: Production, staging, development)"`
Directory string `short:"C" default:"." help:"Directory which contains the application to deploy."`
ServerURL *url.URL `default:"https://aperture.section.io/new/code_upload/v1/upload" help:"URL to upload application to"`
Timeout time.Duration `default:"600s" help:"Timeout of individual HTTP requests."`
SkipDelete bool `help:"Skip delete of temporary tarball created to upload app."`
SkipValidation bool `help:"Skip validation of the workload before pushing into Section. Use with caution."`
AppPath string `default:"nodejs" help:"Path of NodeJS application in environment repository."`
}
// UploadResponse represents the response from a request to the upload service.
type UploadResponse struct {
PayloadID string `json:"payloadID"`
}
// PayloadValue represents the value of a trigger update payload.
type PayloadValue struct {
ID string `json:"section_payload_id"`
}
// Run deploys an app to Section's edge
func (c *DeployCmd) Run(ctx *kong.Context, logWriters *LogWriters) (err error) {
dir := c.Directory
if dir == "." {
abs, err := filepath.Abs(dir)
if err == nil {
dir = abs
}
}
log.Info().Msg(Green("Deploying your node.js package to Account ID: %d, App ID: %d, Environment %s", c.AccountID, c.AppID, c.Environment))
if !c.SkipValidation {
errs := IsValidNodeApp(dir)
if len(errs) > 0 {
var se []string
for _, err := range errs {
se = append(se, fmt.Sprintf("- %s", err))
}
errstr := strings.Join(se, "\n")
return fmt.Errorf("not a valid Node.js app: \n\n%s", errstr)
}
}
s := NewSpinner(fmt.Sprintf("Packaging app in: %s", dir), logWriters)
s.Start()
ignores := []string{".lint", ".git"}
files, err := BuildFilelist(dir, ignores)
if err != nil {
s.Stop()
return fmt.Errorf("unable to build file list: %s", err)
}
s.Stop()
log.Debug().Msg("Archiving files:")
for _, file := range files {
log.Debug().Str("file", file)
}
tempFile, err := ioutil.TempFile("", "sectionctl-deploy.*.tar.gz")
if err != nil {
s.Stop()
return fmt.Errorf("couldn't create a temp file: %v", err)
}
if c.SkipDelete {
s.Stop()
log.Debug().Str("Temporar upload tarball location", tempFile.Name())
s.Start()
} else {
defer os.Remove(tempFile.Name())
}
err = CreateTarball(tempFile, files)
if err != nil {
s.Stop()
return fmt.Errorf("failed to pack files: %v", err)
}
s.Stop()
log.Debug().Str("Temporar file location", tempFile.Name())
stat, err := tempFile.Stat()
if err != nil {
return fmt.Errorf("%s: could not stat, got error: %s", tempFile.Name(), err)
}
if stat.Size() > MaxFileSize {
return fmt.Errorf("failed to upload tarball: file size (%d) is greater than (%d)", stat.Size(), MaxFileSize)
}
_, err = tempFile.Seek(0, 0)
if err != nil {
return fmt.Errorf("unable to seek to beginning of tarball: %s", err)
}
req, err := newFileUploadRequest(c, tempFile)
if err != nil {
return fmt.Errorf("unable to build file upload: %s", err)
}
req.Header.Add("section-token", api.Token)
log.Debug().Str("URL", req.URL.String())
artifactSizeMB := stat.Size() / 1024 / 1024
log.Debug().Msg(fmt.Sprintf("Upload artifact is %dMB (%d bytes) large", artifactSizeMB, stat.Size()))
s = NewSpinner(fmt.Sprintf("Uploading app (%dMB)...", artifactSizeMB), logWriters)
s.Start()
client := &http.Client{
Timeout: c.Timeout,
}
resp, err := client.Do(req)
if err != nil {
return fmt.Errorf("upload request failed: %v", err)
}
defer resp.Body.Close()
s.Stop()
if resp.StatusCode != 200 && resp.StatusCode != 204 {
return fmt.Errorf("upload failed with status: %s and transaction ID %s", resp.Status, resp.Header["Aperture-Tx-Id"][0])
}
var response UploadResponse
err = json.NewDecoder(resp.Body).Decode(&response)
if err != nil {
return fmt.Errorf("failed to decode response %v", err)
}
err = globalGitService.UpdateGitViaGit(ctx, c, response, logWriters)
if err != nil {
if err.Error() == "file not found" {
return fmt.Errorf("this application is not configured to host a node.js app on Section, or, possibly, you didn't specify the proper --AppPath")
}
return fmt.Errorf("failed to trigger app update: %v", err)
}
log.Info().Msg("Done!")
return nil
}
// IsValidNodeApp detects if a Node.js app is present in a given directory
func IsValidNodeApp(dir string) (errs []error) {
packageJSONPath := filepath.Join(dir, "package.json")
if packageJSONContents, err := ioutil.ReadFile(packageJSONPath); err != nil {
if os.IsNotExist(err) {
log.Debug().Msg(fmt.Sprintf("[WARN] %s is not a file", packageJSONPath))
} else {
log.Info().Err(err).Msg("Error reading your package.json")
}
} else {
packageJSON, err := ParsePackageJSON(string(packageJSONContents))
if err != nil {
log.Info().Err(err).Msg("Error parsing your package.json")
}
if len(packageJSON.Section.StartScript) == 0 && packageJSON.Scripts["start"] == "" {
errs = append(errs, fmt.Errorf("package.json does not include a start script. please add one"))
} else if len(packageJSON.Section.StartScript) > 0 && len(packageJSON.Scripts[packageJSON.Section.StartScript]) == 0 {
errs = append(errs, fmt.Errorf("package.json does not include the script: %s", packageJSON.Section.StartScript))
}
}
nodeModulesPath := filepath.Join(dir, "node_modules")
fi, err := os.Stat(nodeModulesPath)
if os.IsNotExist(err) {
errs = append(errs, fmt.Errorf("%s is not a directory", nodeModulesPath))
} else {
if !fi.IsDir() {
errs = append(errs, fmt.Errorf("%s is not a directory", nodeModulesPath))
}
}
return errs
}
// Split helps differentiate between different directory delimiters. / or \
func Split(r rune) bool {
return r == '\\' || r == '/'
}
// BuildFilelist builds a list of files to be tarballed, with optional ignores.
func BuildFilelist(dir string, ignores []string) (files []string, err error) {
var fi os.FileInfo
if fi, err = os.Stat(dir); os.IsNotExist(err) {
return files, err
}
if !fi.IsDir() {
return files, fmt.Errorf("specified path is not a directory: %s", dir)
}
err = filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
for _, i := range ignores {
location := strings.FieldsFunc(path, Split) // split by subdirectory or filename
for _, loc := range location {
if strings.Contains(loc, i) {
return nil
}
}
}
files = append(files, path)
return nil
})
if err != nil {
return files, fmt.Errorf("failed to walk path: %v", err)
}
return files, err
}
// CreateTarball creates a tarball containing all the files in filePaths and writes it to w.
func CreateTarball(w io.Writer, filePaths []string) error {
gzipWriter := gzip.NewWriter(w)
defer gzipWriter.Close()
tarWriter := tar.NewWriter(gzipWriter)
defer tarWriter.Close()
prefix := filePaths[0]
for _, filePath := range filePaths {
err := addFileToTarWriter(filePath, tarWriter, prefix)
if err != nil {
return fmt.Errorf(fmt.Sprintf("Could not add file '%s', to tarball, got error '%s'", filePath, err.Error()))
}
}
return nil
}
func addFileToTarWriter(filePath string, tarWriter *tar.Writer, prefix string) error {
file, err := os.Open(filePath)
if err != nil {
return fmt.Errorf(fmt.Sprintf("Could not open file '%s', got error '%s'", filePath, err.Error()))
}
defer file.Close()
stat, err := os.Lstat(filePath)
if err != nil {
return fmt.Errorf(fmt.Sprintf("Could not get stat for file '%s', got error '%s'", filePath, err.Error()))
}
baseFilePath := strings.TrimPrefix(filePath, prefix)
header, err := tar.FileInfoHeader(stat, baseFilePath)
if err != nil {
return err
}
if stat.Mode()&os.ModeSymlink == os.ModeSymlink {
link, err := os.Readlink(filePath)
if err != nil {
return err
}
header.Linkname = link
}
// must provide real name
// (see https://golang.org/src/archive/tar/common.go?#L626)
header.Name = filepath.ToSlash(baseFilePath)
// ensure windows provides filemodes for binaries in node_modules/.bin
if runtime.GOOS == "windows" {
match := strings.Contains(baseFilePath, "node_modules\\.bin")
if match {
header.Mode = 0o755
}
}
err = tarWriter.WriteHeader(header)
if err != nil {
return fmt.Errorf(fmt.Sprintf("Could not write header for file '%s', got error '%s'", baseFilePath, err.Error()))
}
if !stat.IsDir() && stat.Mode()&os.ModeSymlink != os.ModeSymlink {
_, err = io.Copy(tarWriter, file)
if err != nil {
return fmt.Errorf(fmt.Sprintf("Could not copy the file '%s' data to the tarball, got error '%s'", baseFilePath, err.Error()))
}
}
return nil
}
// newFileUploadRequest builds a HTTP request for uploading an app and the account + app it belongs to
func newFileUploadRequest(c *DeployCmd, f *os.File) (r *http.Request, err error) {
contents, err := ioutil.ReadAll(f)
if err != nil {
return nil, err
}
defer f.Close()
var body bytes.Buffer
writer := multipart.NewWriter(&body)
part, err := writer.CreateFormFile("file", filepath.Base(f.Name()))
if err != nil {
return nil, err
}
_, err = part.Write(contents)
if err != nil {
return nil, err
}
err = writer.WriteField("account_id", strconv.Itoa(c.AccountID))
if err != nil {
return nil, err
}
err = writer.WriteField("app_id", strconv.Itoa(c.AppID))
if err != nil {
return nil, err
}
err = writer.Close()
if err != nil {
return nil, err
}
req, err := http.NewRequest(http.MethodPost, c.ServerURL.String(), &body)
if err != nil {
return nil, fmt.Errorf("failed to create upload URL: %v", err)
}
req.Header.Add("Content-Type", writer.FormDataContentType())
return req, err
}