func lint(c *cli.Context) { _, err := unitconfig.ReadFromFile("./"+c.Args().First(), unitconfig.TOML) if err != nil { Logger.Error(err) gocleanup.Exit(1) } gocleanup.Exit(0) }
func build(c *cli.Context) { builderfile := c.Args().First() if builderfile == "" { builderfile = "Bobfile" } unitConfig, err := unitconfig.ReadFromFile("./"+builderfile, unitconfig.TOML, unitconfig.YAML) if err != nil { if c.Bool("force") { if err := forceBuild(); err != nil { Logger.Warn(err.Error()) } } gocleanup.Exit(0) } globals := unitconfig.ConfigGlobals{ SkipPush: c.Bool("skip-push") || conf.Config.SkipPush, CfgUn: conf.Config.CfgUn, CfgPass: conf.Config.CfgPass, CfgEmail: conf.Config.CfgEmail, } unitConfig.SetGlobals(globals) if err := runner.RunBuildSynchronously(runner.Options{ UnitConfig: unitConfig, ContextDir: os.Getenv("PWD"), LogLevel: Logger.Level, }); err != nil { exitErr(1, "unable to build", err) } }
func init() { go func() { c := make(chan os.Signal, 1) signal.Notify(c, syscall.SIGUSR1) <-c gocleanup.Exit(166) }() }
func enqueue(c *cli.Context) { var top = os.Getenv("PWD") var bobfile = c.Args().First() if bobfile == "" { bobfile = "Bobfile" } if !git.IsClean(top) { Logger.Error("cannot enqueue, working directory is dirty") gocleanup.Exit(1) } upToDate := git.UpToDate(top) if upToDate != git.StatusUpToDate { switch upToDate { case git.StatusNeedToPull: Logger.Warn("CAUTION: need to pull") case git.StatusNeedToPush: Logger.Warn("CAUTION: need to push") case git.StatusDiverged: Logger.Error("cannot enqueue, status has diverged from remote") gocleanup.Exit(1) } } var host = c.String("host") opts := EnqueueOptions{ Host: host, Bobfile: bobfile, Top: top, } enqueuer := NewEnqueuer(opts) result, err := enqueuer.Enqueue() if err != nil { Logger.Errorf("error enqueueing build: %q", err.Error()) gocleanup.Exit(1) } Logger.Debugln(result) gocleanup.Exit(0) }
func exitErr(exitCode int, message string, args interface{}) { var fields logrus.Fields switch args.(type) { case error: fields = logrus.Fields{ "error": args, } case map[string]interface{}: fields = args.(map[string]interface{}) } Logger.WithFields(fields).Error(message) gocleanup.Exit(exitCode) }
func main() { app := cli.NewApp() app.Name = "docker-builder" app.Author = "Rafe Colton" app.Email = "*****@*****.**" 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) }