/
build.go
261 lines (225 loc) · 6.57 KB
/
build.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
package client
import (
"bytes"
"errors"
"fmt"
"io"
"mime/multipart"
"net/http"
"net/url"
"os"
"path/filepath"
"strings"
"time"
"github.com/tsuru/gnuflag"
"github.com/tsuru/go-tsuruclient/pkg/config"
tsuruClientApp "github.com/tsuru/tsuru-client/tsuru/app"
tsuruHTTP "github.com/tsuru/tsuru-client/tsuru/http"
"github.com/tsuru/tsuru/cmd"
"github.com/tsuru/tsuru/safe"
)
type AppBuild struct {
tsuruClientApp.AppNameMixIn
tag string
fs *gnuflag.FlagSet
filesOnly bool
}
func (c *AppBuild) Flags() *gnuflag.FlagSet {
if c.fs == nil {
c.fs = c.AppNameMixIn.Flags()
tag := "The image tag"
c.fs.StringVar(&c.tag, "tag", "", tag)
c.fs.StringVar(&c.tag, "t", "", tag)
filesOnly := "Enables single file build into the root of the app's tree"
c.fs.BoolVar(&c.filesOnly, "f", false, filesOnly)
c.fs.BoolVar(&c.filesOnly, "files-only", false, filesOnly)
}
return c.fs
}
func (c *AppBuild) Info() *cmd.Info {
desc := `Build a container image following the app deploy's workflow - but do not change anything on the running application on Tsuru.
You can deploy this container image to the app later.
Files specified in the ".tsuruignore" file are skipped - similar to ".gitignore".
Examples:
To build using app's platform build process (just sending source code or configurations):
Uploading all files within the current directory
$ tsuru app build -a <APP> .
Uploading all files within a specific directory
$ tsuru app build -a <APP> mysite/
Uploading specific files
$ tsuru app build -a <APP> ./myfile.jar ./Procfile
Uploading specific files but ignoring their directory trees
$ tsuru app build -a <APP> --files-only ./my-code/main.go ./tsuru_stuff/Procfile
`
return &cmd.Info{
Name: "app-build",
Usage: "app build [-a/--app <appname>] [-t/--tag <image_tag>] [-f/--files-only] <file-or-dir-1> [file-or-dir-2] ... [file-or-dir-n]",
Desc: desc,
MinArgs: 0,
}
}
func (c *AppBuild) Run(context *cmd.Context) error {
context.RawOutput()
if c.tag == "" {
return errors.New("You should provide one tag to build the image.\n")
}
if len(context.Args) == 0 {
return errors.New("You should provide at least one file to build the image.\n")
}
appName, err := c.AppName()
if err != nil {
return err
}
u, err := config.GetURL("/apps/" + appName)
if err != nil {
return err
}
request, err := http.NewRequest("GET", u, nil)
if err != nil {
return err
}
_, err = tsuruHTTP.AuthenticatedClient.Do(request)
if err != nil {
return err
}
values := url.Values{}
values.Set("tag", c.tag)
u, err = config.GetURLVersion("1.5", fmt.Sprintf("/apps/%s/build", appName))
if err != nil {
return err
}
body := safe.NewBuffer(nil)
request, err = http.NewRequest("POST", u, body)
if err != nil {
return err
}
buf := safe.NewBuffer(nil)
respBody := prepareUploadStreams(context, buf)
var archive bytes.Buffer
err = Archive(&archive, c.filesOnly, context.Args, DefaultArchiveOptions(nil))
if err != nil {
return err
}
if err = uploadFiles(context, request, buf, body, values, &archive); err != nil {
return err
}
resp, err := tsuruHTTP.AuthenticatedClient.Do(request)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return cmd.ErrAbortCommand
}
_, err = io.Copy(respBody, resp.Body)
if err != nil {
return err
}
if strings.HasSuffix(buf.String(), "\nOK\n") {
return nil
}
return cmd.ErrAbortCommand
}
func uploadFiles(context *cmd.Context, request *http.Request, buf *safe.Buffer, body *safe.Buffer, values url.Values, archive io.Reader) error {
if archive == nil {
request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
_, err := body.WriteString(values.Encode())
return err
}
writer := multipart.NewWriter(body)
for k := range values {
writer.WriteField(k, values.Get(k))
}
request.Header.Set("Content-Type", writer.FormDataContentType())
f, err := writer.CreateFormFile("file", "archive.tar.gz")
if err != nil {
return err
}
if _, err = io.Copy(f, archive); err != nil {
return err
}
if err = writer.Close(); err != nil {
return err
}
fullSize := float64(body.Len())
megabyte := 1024.0 * 1024.0
fmt.Fprintf(context.Stdout, "Uploading files (%0.2fMB)... ", fullSize/megabyte)
count := 0
go func() {
t0 := time.Now()
lastTransferred := 0.0
for buf.Len() == 0 {
remaining := body.Len()
transferred := fullSize - float64(remaining)
speed := ((transferred - lastTransferred) / megabyte) / (float64(time.Since(t0)) / float64(time.Second))
t0 = time.Now()
lastTransferred = transferred
percent := (transferred / fullSize) * 100.0
fmt.Fprintf(context.Stdout, "\rUploading files (%0.2fMB)... %0.2f%%", fullSize/megabyte, percent)
if remaining > 0 {
fmt.Fprintf(context.Stdout, " (%0.2fMB/s)", speed)
}
if remaining == 0 && buf.Len() == 0 {
fmt.Fprintf(context.Stdout, " Processing%s", strings.Repeat(".", count))
count++
}
time.Sleep(2 * time.Second)
}
}()
return nil
}
func buildWithContainerFile(appName, path string, filesOnly bool, files []string, stderr io.Writer) (string, io.Reader, error) {
fi, err := os.Stat(path)
if err != nil {
return "", nil, fmt.Errorf("failed to stat the file %s: %w", path, err)
}
var containerfile []byte
switch {
case fi.IsDir():
path, err = guessingContainerFile(appName, path)
if err != nil {
return "", nil, fmt.Errorf("failed to guess the container file (can you specify the container file passing --dockerfile ./path/to/Dockerfile?): %w", err)
}
fallthrough
case fi.Mode().IsRegular():
containerfile, err = os.ReadFile(path)
if err != nil {
return "", nil, fmt.Errorf("failed to read %s: %w", path, err)
}
default:
return "", nil, fmt.Errorf("invalid file type")
}
if len(files) == 0 { // no additional files set, using the dockerfile dir
files = []string{filepath.Dir(path)}
}
var buildContext bytes.Buffer
err = Archive(&buildContext, filesOnly, files, DefaultArchiveOptions(stderr))
if err != nil {
return "", nil, err
}
return string(containerfile), &buildContext, nil
}
func guessingContainerFile(app, dir string) (string, error) {
validNames := []string{
fmt.Sprintf("Dockerfile.%s", app),
fmt.Sprintf("Containerfile.%s", app),
"Dockerfile.tsuru",
"Containerfile.tsuru",
"Dockerfile",
"Containerfile",
}
for _, name := range validNames {
path := filepath.Join(dir, name)
fi, err := os.Stat(path)
if errors.Is(err, os.ErrNotExist) {
continue
}
if err != nil {
return "", err
}
if fi.Mode().IsRegular() {
return path, nil
}
}
return "", errors.New("container file not found")
}