From 528dc0a7b3fd58f5ad7e7767a6ce9ca9f90cce05 Mon Sep 17 00:00:00 2001 From: Brad Rydzewski Date: Tue, 24 Mar 2020 13:49:10 -0700 Subject: [PATCH] update config.json messaging --- cmd/drone-docker/main.go | 12 +++++------ daemon.go | 4 ++-- docker.go | 43 ++++++++++++++++++++-------------------- 3 files changed, 30 insertions(+), 29 deletions(-) diff --git a/cmd/drone-docker/main.go b/cmd/drone-docker/main.go index 98fc000..875b5b6 100644 --- a/cmd/drone-docker/main.go +++ b/cmd/drone-docker/main.go @@ -7,7 +7,7 @@ import ( "github.com/sirupsen/logrus" "github.com/urfave/cli" - "github.com/drone-plugins/drone-docker" + docker "github.com/drone-plugins/drone-docker" ) var ( @@ -245,11 +245,11 @@ func run(c *cli.Context) error { Dryrun: c.Bool("dry-run"), Cleanup: c.BoolT("docker.purge"), Login: docker.Login{ - Registry: c.String("docker.registry"), - Username: c.String("docker.username"), - Password: c.String("docker.password"), - Email: c.String("docker.email"), - DockerConfig: c.String("docker.config"), + Registry: c.String("docker.registry"), + Username: c.String("docker.username"), + Password: c.String("docker.password"), + Email: c.String("docker.email"), + Config: c.String("docker.config"), }, Build: docker.Build{ Remote: c.String("remote.url"), diff --git a/daemon.go b/daemon.go index 09234ff..5976cd1 100644 --- a/daemon.go +++ b/daemon.go @@ -9,7 +9,7 @@ import ( const dockerExe = "/usr/local/bin/docker" const dockerdExe = "/usr/local/bin/dockerd" -const dockerrootconfdir = "/root/.docker/" +const dockerHome = "/root/.docker/" func (p Plugin) startDaemon() { cmd := commandDaemon(p.Daemon) @@ -24,4 +24,4 @@ func (p Plugin) startDaemon() { trace(cmd) cmd.Run() }() -} \ No newline at end of file +} diff --git a/docker.go b/docker.go index d472f10..e94250b 100644 --- a/docker.go +++ b/docker.go @@ -2,11 +2,12 @@ package docker import ( "fmt" + "io/ioutil" "os" "os/exec" + "path/filepath" "strings" "time" - "io/ioutil" ) type ( @@ -29,11 +30,11 @@ type ( // Login defines Docker login parameters. Login struct { - Registry string // Docker registry address - Username string // Docker registry username - Password string // Docker registry password - Email string // Docker registry email - DockerConfig string // Docker Auth Config + Registry string // Docker registry address + Username string // Docker registry username + Password string // Docker registry password + Email string // Docker registry email + Config string // Docker Auth Config } // Build defines Docker build parameters. @@ -86,18 +87,13 @@ func (p Plugin) Exec() error { } // Create Auth Config File - if p.Login.DockerConfig != "" { - fmt.Println("DockerConfig provided.") - err_mkdir := os.MkdirAll(dockerrootconfdir, 0600) - if err_mkdir != nil { - fmt.Println("Error creating root's docker auth config directory: ") - fmt.Println(err_mkdir) - } - conffile := fmt.Sprintf("%s%s", dockerrootconfdir, "config.json") - err_mkconf := ioutil.WriteFile(conffile, []byte(p.Login.DockerConfig), 0600) - if err_mkconf != nil { - fmt.Println("Error creating root's docker auth configuration: ") - fmt.Println(err_mkconf) + if p.Login.Config != "" { + os.MkdirAll(dockerHome, 0600) + + path := filepath.Join(dockerHome, "config.json") + err := ioutil.WriteFile(path, []byte(p.Login.Config), 0600) + if err != nil { + return fmt.Errorf("Error writeing config.json: %s", err) } } @@ -108,12 +104,17 @@ func (p Plugin) Exec() error { if err != nil { return fmt.Errorf("Error authenticating: %s", err) } - } + } - if p.Login.Password != "" && p.Login.DockerConfig != "" { + switch { + case p.Login.Password != "": + fmt.Println("Detected registry credentials") + case p.Login.Config != "": + fmt.Println("Detected registry credentials file") + default: fmt.Println("Registry credentials or Docker config not provided. Guest mode enabled.") } - + if p.Build.Squash && !p.Daemon.Experimental { fmt.Println("Squash build flag is only available when Docker deamon is started with experimental flag. Ignoring...") p.Build.Squash = false