This repository has been archived by the owner on Apr 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 88
/
main.go
514 lines (443 loc) · 12.6 KB
/
main.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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
/*-
* Copyright 2015 Grammarly, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package main
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/grammarly/rocker/src/build"
"github.com/grammarly/rocker/src/debugtrap"
"github.com/grammarly/rocker/src/dockerclient"
"github.com/grammarly/rocker/src/storage/s3"
"github.com/grammarly/rocker/src/template"
"github.com/grammarly/rocker/src/textformatter"
"github.com/grammarly/rocker/src/util"
"github.com/codegangsta/cli"
"github.com/docker/docker/pkg/units"
"github.com/fatih/color"
"github.com/fsouza/go-dockerclient"
log "github.com/Sirupsen/logrus"
runconfigopts "github.com/docker/docker/runconfig/opts"
)
var (
// Version that is passed on compile time through -ldflags
Version = "built locally"
// GitCommit that is passed on compile time through -ldflags
GitCommit = "none"
// GitBranch that is passed on compile time through -ldflags
GitBranch = "none"
// BuildTime that is passed on compile time through -ldflags
BuildTime = "none"
// HumanVersion is a human readable app version
HumanVersion = fmt.Sprintf("%s - %.7s (%s) %s", Version, GitCommit, GitBranch, BuildTime)
)
func init() {
log.SetOutput(os.Stdout)
log.SetLevel(log.InfoLevel)
debugtrap.SetupDumpStackTrap()
}
func main() {
app := cli.NewApp()
app.Name = "rocker"
app.Version = HumanVersion
app.Usage = "Docker based build tool\n\n Run 'rocker COMMAND --help' for more information on a command."
app.Author = ""
app.Email = ""
app.Authors = []cli.Author{
{"Yura Bogdanov", "[email protected]"},
{"Stas Levental", "[email protected]"},
{"Roman Khlystik", "[email protected]"},
}
app.Flags = append([]cli.Flag{
cli.BoolFlag{
Name: "verbose, vv, D",
Usage: "Be verbose",
},
cli.BoolFlag{
Name: "json",
Usage: "Print output in json",
},
cli.BoolTFlag{
Name: "colors",
Usage: "Make output colored",
},
cli.BoolFlag{
Name: "cmd, C",
EnvVar: "ROCKER_PRINT_COMMAND",
Usage: "Print command-line that was used to exec",
},
}, dockerclient.GlobalCliParams()...)
buildFlags := []cli.Flag{
cli.StringFlag{
Name: "file, f",
Value: "Rockerfile",
Usage: "rocker build file to execute",
},
cli.StringFlag{
Name: "auth, a",
Value: "",
Usage: "Username and password in user:password format",
},
cli.StringSliceFlag{
Name: "build-arg",
Value: &cli.StringSlice{},
Usage: "Set build-time variables, can pass multiple of those, format is key=value (default [])",
},
cli.StringSliceFlag{
Name: "var",
Value: &cli.StringSlice{},
Usage: "set variables to pass to build tasks, value is like \"key=value\"",
},
cli.StringSliceFlag{
Name: "vars",
Value: &cli.StringSlice{},
Usage: "Load variables form a file, either JSON or YAML. Can pass multiple of this.",
},
cli.BoolFlag{
Name: "no-cache",
Usage: "supresses cache for docker builds",
},
cli.BoolFlag{
Name: "reload-cache",
Usage: "removes any cache that hit and save the new one",
},
cli.StringFlag{
Name: "cache-dir",
Value: "~/.rocker_cache",
Usage: "Set the directory where the cache will be stored",
},
cli.BoolFlag{
Name: "no-reuse",
Usage: "suppresses reuse for all the volumes in the build",
},
cli.BoolFlag{
Name: "push",
Usage: "pushes all the images marked with push to docker hub",
},
cli.BoolFlag{
Name: "pull",
Usage: "always attempt to pull a newer version of the FROM images",
},
cli.BoolFlag{
Name: "attach",
Usage: "attach to a container in place of ATTACH command",
},
cli.BoolFlag{
Name: "meta",
Usage: "add metadata to the tagged images, such as user, Rockerfile source, variables and git branch/sha",
},
cli.BoolFlag{
Name: "print",
Usage: "just print the Rockerfile after template processing and stop",
},
cli.BoolFlag{
Name: "demand-artifacts",
Usage: "fail if artifacts not found for {{ image }} helpers",
},
cli.StringFlag{
Name: "id",
Usage: "override the default id generation strategy for current build",
},
cli.StringFlag{
Name: "artifacts-path",
Usage: "put artifacts (files with pushed images description) to the directory",
},
cli.BoolFlag{
Name: "no-garbage",
Usage: "remove the images from the tail if not tagged",
},
cli.IntFlag{
Name: "push-retry",
Usage: "number of retries for failed image pushes",
},
}
app.Commands = []cli.Command{
{
Name: "build",
Usage: "launches a build for the specified Rockerfile",
Action: buildCommand,
Flags: buildFlags,
},
{
Name: "pull",
Usage: "launches a pull of image (supports s3 storage driver)",
Action: pullCommand,
Flags: []cli.Flag{
cli.StringFlag{
Name: "file, f",
Value: "Rockerfile",
Usage: "rocker build file to execute",
},
cli.StringFlag{
Name: "auth, a",
Value: "",
Usage: "Username and password in user:password format",
},
cli.StringFlag{
Name: "cache-dir",
Value: "~/.rocker_cache",
Usage: "Set the directory where the cache will be stored",
},
},
},
dockerclient.InfoCommandSpec(),
}
app.Before = func(c *cli.Context) error {
initLogs(c)
if c.GlobalBool("cmd") {
log.Infof("rocker %s | Cmd: %s\n", HumanVersion, strings.Join(os.Args, " "))
}
return nil
}
app.CommandNotFound = func(ctx *cli.Context, command string) {
fmt.Printf("Command not found: %v\n", command)
os.Exit(1)
}
if err := app.Run(os.Args); err != nil {
fmt.Printf(err.Error())
os.Exit(1)
}
}
func buildCommand(c *cli.Context) {
var (
rockerfile *build.Rockerfile
err error
)
// We don't want info level for 'print' mode
// So log only errors unless 'debug' is on
if c.Bool("print") && log.StandardLogger().Level != log.DebugLevel {
log.StandardLogger().Level = log.ErrorLevel
}
vars, err := template.VarsFromFileMulti(c.StringSlice("vars"))
if err != nil {
log.Fatal(err)
os.Exit(1)
}
cliVars, err := template.VarsFromStrings(c.StringSlice("var"))
if err != nil {
log.Fatal(err)
}
vars = vars.Merge(cliVars)
if c.Bool("demand-artifacts") {
vars["DemandArtifacts"] = true
}
wd, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
configFilename := c.String("file")
contextDir := wd
if configFilename == "-" {
rockerfile, err = build.NewRockerfile(filepath.Base(wd), os.Stdin, vars, template.Funs{})
if err != nil {
log.Fatal(err)
}
} else {
if !filepath.IsAbs(configFilename) {
configFilename = filepath.Join(wd, configFilename)
}
rockerfile, err = build.NewRockerfileFromFile(configFilename, vars, template.Funs{})
if err != nil {
log.Fatal(err)
}
// Initialize context dir
contextDir = filepath.Dir(configFilename)
}
args := c.Args()
if len(args) > 0 {
contextDir = args[0]
if !filepath.IsAbs(contextDir) {
contextDir = filepath.Join(wd, args[0])
}
} else if contextDir != wd {
log.Warningf("Implicit context directory used: %s. You can override context directory using the last argument.", contextDir)
}
dir, err := os.Stat(contextDir)
if err != nil {
log.Errorf("Problem with opening directory %s, error: %s", contextDir, err)
os.Exit(2)
}
if !dir.IsDir() {
log.Errorf("Context directory %s is not a directory.", contextDir)
os.Exit(2)
}
log.Debugf("Context directory: %s", contextDir)
if c.Bool("print") {
fmt.Print(rockerfile.Content)
os.Exit(0)
}
dockerignore := []string{}
dockerignoreFilename := filepath.Join(contextDir, ".dockerignore")
if _, err := os.Stat(dockerignoreFilename); err == nil {
if dockerignore, err = build.ReadDockerignoreFile(dockerignoreFilename); err != nil {
log.Fatal(err)
}
}
var config *dockerclient.Config
config = dockerclient.NewConfigFromCli(c)
dockerClient, err := dockerclient.NewFromConfig(config)
if err != nil {
log.Fatal(err)
}
cacheDir, err := util.MakeAbsolute(c.String("cache-dir"))
if err != nil {
log.Fatal(err)
}
var cache build.Cache
if !c.Bool("no-cache") {
cache = build.NewCacheFS(cacheDir)
}
var (
stdoutContainerFormatter log.Formatter = &log.JSONFormatter{}
stderrContainerFormatter log.Formatter = &log.JSONFormatter{}
)
if !c.GlobalBool("json") {
stdoutContainerFormatter = build.NewMonochromeContainerFormatter()
stderrContainerFormatter = build.NewColoredContainerFormatter()
}
options := build.DockerClientOptions{
Client: dockerClient,
Auth: initAuth(c),
Log: log.StandardLogger(),
S3storage: s3.New(dockerClient, cacheDir),
StdoutContainerFormatter: stdoutContainerFormatter,
StderrContainerFormatter: stderrContainerFormatter,
PushRetryCount: c.Int("push-retry"),
Host: config.Host,
LogExactSizes: c.GlobalBool("json"),
}
client := build.NewDockerClient(options)
builder := build.New(client, rockerfile, cache, build.Config{
InStream: os.Stdin,
OutStream: os.Stdout,
ContextDir: contextDir,
Dockerignore: dockerignore,
ArtifactsPath: c.String("artifacts-path"),
Pull: c.Bool("pull"),
NoGarbage: c.Bool("no-garbage"),
Attach: c.Bool("attach"),
Verbose: c.GlobalBool("verbose"),
ID: c.String("id"),
NoCache: c.Bool("no-cache"),
ReloadCache: c.Bool("reload-cache"),
Push: c.Bool("push"),
CacheDir: cacheDir,
LogJSON: c.GlobalBool("json"),
BuildArgs: runconfigopts.ConvertKVStringsToMap(c.StringSlice("build-arg")),
})
plan, err := build.NewPlan(rockerfile.Commands(), true)
if err != nil {
log.Fatal(err)
}
// Check the docker connection before we actually run
if err := dockerclient.Ping(dockerClient, 5000); err != nil {
log.Fatal(err)
}
if err := builder.Run(plan); err != nil {
log.Fatal(err)
}
fields := log.Fields{}
if c.GlobalBool("json") {
fields["size"] = builder.VirtualSize
fields["delta"] = builder.ProducedSize
}
size := fmt.Sprintf("final size %s (+%s from the base image)",
units.HumanSize(float64(builder.VirtualSize)),
units.HumanSize(float64(builder.ProducedSize)),
)
log.WithFields(fields).Infof("Successfully built %.12s | %s", builder.GetImageID(), size)
}
func pullCommand(c *cli.Context) {
args := c.Args()
if len(args) < 1 {
log.Fatal("rocker pull <image>")
}
dockerClient, err := dockerclient.NewFromCli(c)
if err != nil {
log.Fatal(err)
}
cacheDir, err := util.MakeAbsolute(c.String("cache-dir"))
if err != nil {
log.Fatal(err)
}
options := build.DockerClientOptions{
Client: dockerClient,
Auth: initAuth(c),
Log: log.StandardLogger(),
S3storage: s3.New(dockerClient, cacheDir),
StdoutContainerFormatter: log.StandardLogger().Formatter,
StderrContainerFormatter: log.StandardLogger().Formatter,
}
client := build.NewDockerClient(options)
if err := client.PullImage(args[0]); err != nil {
log.Fatal(err)
}
}
func initAuth(c *cli.Context) (auth *docker.AuthConfigurations) {
var err error
if c.IsSet("auth") {
// Obtain auth configuration from cli params
authParam := c.String("auth")
if strings.Contains(authParam, ":") {
userPass := strings.Split(authParam, ":")
auth = &docker.AuthConfigurations{
Configs: map[string]docker.AuthConfiguration{
"*": docker.AuthConfiguration{
Username: userPass[0],
Password: userPass[1],
},
},
}
}
return
}
// Obtain auth configuration from .docker/config.json
if auth, err = docker.NewAuthConfigurationsFromDockerCfg(); err != nil && !os.IsNotExist(err) {
log.Fatal(err)
}
return
}
func initLogs(ctx *cli.Context) {
logger := log.StandardLogger()
if ctx.GlobalBool("verbose") {
logger.Level = log.DebugLevel
}
var (
isTerm = log.IsTerminal()
json = ctx.GlobalBool("json")
useColors = isTerm && !json
)
if ctx.GlobalIsSet("colors") {
useColors = ctx.GlobalBool("colors")
}
color.NoColor = !useColors
if json {
logger.Formatter = &log.JSONFormatter{}
} else {
formatter := &textformatter.TextFormatter{}
formatter.DisableColors = !useColors
logger.Formatter = formatter
}
}
func stringOr(args ...string) string {
for _, str := range args {
if str != "" {
return str
}
}
return ""
}