-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
main.go
211 lines (198 loc) · 5.39 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
package main
import (
"fmt"
"os"
"github.com/rafecolton/docker-builder/conf"
"github.com/rafecolton/docker-builder/server"
"github.com/rafecolton/docker-builder/version"
"github.com/Sirupsen/logrus"
"github.com/codegangsta/cli"
"github.com/kelseyhightower/envconfig"
"github.com/modcloth/kamino"
"github.com/onsi/gocleanup"
)
var ver = version.NewVersion()
//Logger is the logger for the docker-builder main
var Logger *logrus.Logger
func init() {
// parse env config
if err := envconfig.Process("docker_builder", &conf.Config); err != nil {
Logger.WithField("err", err).Fatal("envconfig error")
}
// set default config port
if conf.Config.Port == 0 {
conf.Config.Port = 5000
}
// set logger defaults
Logger = logrus.New()
Logger.Formatter = &logrus.TextFormatter{ForceColors: true}
}
func main() {
app := cli.NewApp()
app.Name = "docker-builder"
app.Author = "Rafe Colton"
app.Email = "rafael.colton@gmail.com"
app.Usage = "docker-builder (a.k.a. \"Bob\") builds Docker images from a friendly config file"
app.Version = ver.Version + " " + app.Compiled.String()
app.Flags = []cli.Flag{
cli.BoolFlag{
Name: "branch",
Usage: "print branch and exit",
},
cli.BoolFlag{
Name: "rev",
Usage: "print revision and exit",
},
cli.BoolFlag{
Name: "version-short",
Usage: "print long version and exit",
},
cli.BoolFlag{Name: "quiet, q",
Usage: "produce no output, only exit codes",
},
cli.StringFlag{
Name: "log-level, l",
Value: conf.Config.LogLevel,
Usage: "log level (options: debug/d, info/i, warn/w, error/e, fatal/f, panic/p)",
},
cli.StringFlag{
Name: "log-format, f",
Value: conf.Config.LogFormat,
Usage: "log output format (options: text/t, json/j)",
},
cli.StringFlag{
Name: "dockercfg-un",
Value: conf.Config.CfgUn,
Usage: "Docker registry username",
},
cli.StringFlag{
Name: "dockercfg-pass",
Value: conf.Config.CfgPass,
Usage: "Docker registry password",
},
cli.StringFlag{
Name: "dockercfg-email",
Value: conf.Config.CfgEmail,
Usage: "Docker registry email",
},
}
app.Action = func(c *cli.Context) {
ver = version.NewVersion()
if c.GlobalBool("branch") {
fmt.Println(ver.Branch)
} else if c.GlobalBool("rev") {
fmt.Println(ver.Rev)
} else if c.GlobalBool("version-short") {
fmt.Println(ver.Version)
} else {
cli.ShowAppHelp(c)
}
}
app.Before = func(c *cli.Context) error {
logLevel := c.String("log-level")
logFormat := c.String("log-format")
setLogger(logLevel, logFormat)
kamino.Logger = Logger
return nil
}
app.Commands = []cli.Command{
{
Name: "init",
Usage: "init [dir] - initialize the given directory (default '.') with a Bobfile",
Description: "Make educated guesses to fill out a Bobfile given a directory with a Dockerfile",
Action: initialize,
},
{
Name: "build",
Usage: "build [file] - build Docker images from the provided Bobfile",
Description: "Build Docker images from the provided Bobfile.",
Action: build,
Flags: []cli.Flag{
cli.BoolFlag{
Name: "skip-push",
Usage: "override Bobfile behavior and do not push any images (useful for testing)",
},
cli.BoolFlag{
Name: "force, f",
Usage: "when Bobfile is not present or is considered unsafe, instead of erring, perform a default build",
},
},
},
{
Name: "enqueue",
Usage: "enqueue [Bobfile] - enqueue a build to the DOCKER_BUILDER_HOST",
Description: "Enqueue a build based on what's in the current repo",
Action: enqueue,
Flags: []cli.Flag{
cli.StringFlag{
Name: "host",
Value: func() string {
if os.Getenv("DOCKER_BUILDER_HOST") != "" {
return os.Getenv("DOCKER_BUILDER_HOST")
}
return "http://localhost:5000"
}(),
Usage: "docker builder server host (can be set in the environment via $DOCKER_BUILDER_HOST)",
},
},
},
{
Name: "lint",
Usage: "lint [file] - validates whether or not your Bobfile is parsable",
Description: "Validate whether or not your Bobfile is parsable.",
Action: lint,
},
{
Name: "serve",
Usage: "serve <options> - start a small HTTP web server for receiving build requests",
Description: server.Description,
Action: func(c *cli.Context) { server.Logger(Logger); server.Serve(c) },
Flags: []cli.Flag{
cli.IntFlag{
Name: "port, p",
Value: conf.Config.Port,
Usage: "port on which to serve",
},
cli.StringFlag{
Name: "api-token, t",
Value: "",
Usage: "GitHub API token",
},
cli.BoolFlag{
Name: "skip-push",
Usage: "override Bobfile behavior and do not push any images (useful for testing)",
},
cli.StringFlag{
Name: "username",
Value: "",
Usage: "username for basic auth",
},
cli.StringFlag{
Name: "password",
Value: "",
Usage: "password for basic auth",
},
cli.StringFlag{
Name: "travis-token",
Value: "",
Usage: "Travis API token for webhooks",
},
cli.StringFlag{
Name: "github-secret",
Value: "",
Usage: "GitHub secret for webhooks",
},
cli.BoolFlag{
Name: "no-travis",
Usage: "do not include route for Travis CI webhook",
},
cli.BoolFlag{
Name: "no-github",
Usage: "do not include route for GitHub webhook",
},
},
},
}
app.Run(os.Args)
gocleanup.Exit(0)
}