|
1 | 1 | package docker |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "errors" |
5 | 4 | "fmt" |
| 5 | + "log" |
6 | 6 | "os" |
7 | 7 | "os/exec" |
8 | 8 | "path/filepath" |
9 | 9 | "runtime" |
10 | 10 | "strings" |
11 | 11 | "time" |
12 | 12 |
|
| 13 | + "github.com/drone-plugins/drone-docker/internal/docker" |
13 | 14 | "github.com/drone-plugins/drone-plugin-lib/drone" |
| 15 | + "github.com/pkg/errors" |
| 16 | + |
14 | 17 | ) |
15 | 18 |
|
16 | 19 | type ( |
@@ -75,13 +78,16 @@ type ( |
75 | 78 |
|
76 | 79 | // Plugin defines the Docker plugin parameters. |
77 | 80 | Plugin struct { |
78 | | - Login Login // Docker login configuration |
79 | | - Build Build // Docker build configuration |
80 | | - Daemon Daemon // Docker daemon configuration |
81 | | - Dryrun bool // Docker push is skipped |
82 | | - Cleanup bool // Docker purge is enabled |
83 | | - CardPath string // Card path to write file to |
84 | | - ArtifactFile string // Artifact path to write file to |
| 81 | + Login Login // Docker login configuration |
| 82 | + Build Build // Docker build configuration |
| 83 | + Daemon Daemon // Docker daemon configuration |
| 84 | + Dryrun bool // Docker push is skipped |
| 85 | + Cleanup bool // Docker purge is enabled |
| 86 | + CardPath string // Card path to write file to |
| 87 | + ArtifactFile string // Artifact path to write file to |
| 88 | + BaseImageRegistry string // Docker registry to pull base image |
| 89 | + BaseImageUsername string // Docker registry username to pull base image |
| 90 | + BaseImagePassword string // Docker registry password to pull base image |
85 | 91 | } |
86 | 92 |
|
87 | 93 | Card []struct { |
@@ -154,11 +160,42 @@ func (p Plugin) Exec() error { |
154 | 160 | os.MkdirAll(dockerHome, 0600) |
155 | 161 |
|
156 | 162 | path := filepath.Join(dockerHome, "config.json") |
157 | | - err := os.WriteFile(path, []byte(p.Login.Config), 0600) |
| 163 | + file, err := os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600) |
| 164 | + if err != nil { |
| 165 | + return fmt.Errorf("Error writing config.json: %s", err) |
| 166 | + } |
| 167 | + _, err = file.Write([]byte(p.Login.Config)) |
| 168 | + fmt.Println("Writing p.Login.Config: %s", p.Login.Config) |
| 169 | + |
158 | 170 | if err != nil { |
159 | 171 | return fmt.Errorf("Error writing config.json: %s", err) |
160 | 172 | } |
| 173 | + defer file.Close() |
| 174 | + } |
| 175 | + log.Printf("p.Login.Config .... %s", p.Login.Config) |
| 176 | + // add docker credentials to the existing config file, else create new |
| 177 | + if p.Login.Password != "" && p.BaseImagePassword != "" { |
| 178 | + json, err := setDockerAuth(p.Login.Username, p.Login.Password, p.Login.Registry, |
| 179 | + p.BaseImageUsername, p.BaseImagePassword, p.BaseImageRegistry) |
| 180 | + fmt.Println("json after set Auth: %s", json) |
| 181 | + if err != nil { |
| 182 | + return errors.Wrap(err, "Failed to set authentication in docker config") |
| 183 | + } |
| 184 | + if json != nil { |
| 185 | + path := filepath.Join(dockerHome, "config.json") |
| 186 | + file, err := os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600) |
| 187 | + if err != nil { |
| 188 | + return fmt.Errorf("Error opening config.json: %s", err) |
| 189 | + } |
| 190 | + defer file.Close() |
| 191 | + _, err = file.Write(json) |
| 192 | + if err != nil { |
| 193 | + return fmt.Errorf("Error writing config.json: %s", err) |
| 194 | + } |
| 195 | + } |
161 | 196 | } |
| 197 | + fmt.Println("json after set Auth: %s", json) |
| 198 | + |
162 | 199 |
|
163 | 200 | // login to the Docker registry |
164 | 201 | if p.Login.Password != "" { |
@@ -270,6 +307,32 @@ func (p Plugin) Exec() error { |
270 | 307 | return nil |
271 | 308 | } |
272 | 309 |
|
| 310 | +// helper function to set the credentials |
| 311 | +func setDockerAuth(username, password, registry, baseImageUsername, |
| 312 | + baseImagePassword, baseImageRegistry string) ([]byte, error) { |
| 313 | + dockerConfig := docker.NewConfig() |
| 314 | + pushToRegistryCreds := docker.RegistryCredentials{ |
| 315 | + Registry: registry, |
| 316 | + Username: username, |
| 317 | + Password: password, |
| 318 | + } |
| 319 | + // push registry auth |
| 320 | + //credentials := []docker.RegistryCredentials{pushToRegistryCreds} |
| 321 | + credentials := []docker.RegistryCredentials{} |
| 322 | + |
| 323 | + if baseImageRegistry != "" { |
| 324 | + pullFromRegistryCreds := docker.RegistryCredentials{ |
| 325 | + Registry: baseImageRegistry, |
| 326 | + Username: baseImageUsername, |
| 327 | + Password: baseImagePassword, |
| 328 | + } |
| 329 | + // base image registry auth |
| 330 | + credentials = append(credentials, pullFromRegistryCreds) |
| 331 | + } |
| 332 | + // Creates docker config for both the registries used for authentication |
| 333 | + return dockerConfig.CreateDockerConfigJson(credentials) |
| 334 | +} |
| 335 | + |
273 | 336 | // helper function to create the docker login command. |
274 | 337 | func commandLogin(login Login) *exec.Cmd { |
275 | 338 | if login.Email != "" { |
@@ -504,6 +567,7 @@ func commandPush(build Build, tag string) *exec.Cmd { |
504 | 567 |
|
505 | 568 | // helper function to create the docker daemon command. |
506 | 569 | func commandDaemon(daemon Daemon) *exec.Cmd { |
| 570 | + fmt.Println(" Aishwarya config.json is 5.." ) |
507 | 571 | args := []string{ |
508 | 572 | "--data-root", daemon.StoragePath, |
509 | 573 | "--host=unix:///var/run/docker.sock", |
@@ -585,6 +649,8 @@ func trace(cmd *exec.Cmd) { |
585 | 649 | } |
586 | 650 |
|
587 | 651 | func GetDroneDockerExecCmd() string { |
| 652 | + fmt.Println(" Aishwarya config.json is 3.." ) |
| 653 | + |
588 | 654 | if runtime.GOOS == "windows" { |
589 | 655 | return "C:/bin/drone-docker.exe" |
590 | 656 | } |
|
0 commit comments