mirror of
https://github.com/drone-plugins/drone-docker.git
synced 2026-06-04 18:24:24 +08:00
Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| e70d271e93 | |||
| f32aa46ea8 | |||
| 5810bf8a5a | |||
| 23887402c3 |
@@ -112,6 +112,12 @@ func main() {
|
||||
Usage: "don't start the docker daemon",
|
||||
EnvVar: "PLUGIN_DAEMON_OFF",
|
||||
},
|
||||
cli.IntFlag{
|
||||
Name: "daemon.retry-count",
|
||||
Usage: "number of retry attempts to reach docker daemon",
|
||||
Value: 15,
|
||||
EnvVar: "PLUGIN_DAEMON_RETRY_COUNT",
|
||||
},
|
||||
cli.StringFlag{
|
||||
Name: "dockerfile",
|
||||
Usage: "build dockerfile",
|
||||
@@ -419,6 +425,7 @@ func run(c *cli.Context) error {
|
||||
DNSSearch: c.StringSlice("daemon.dns-search"),
|
||||
MTU: c.String("daemon.mtu"),
|
||||
Experimental: c.Bool("daemon.experimental"),
|
||||
RetryCount: c.Int("daemon.retry-count"),
|
||||
RegistryType: registryType,
|
||||
},
|
||||
BaseImageRegistry: c.String("docker.baseimageregistry"),
|
||||
|
||||
@@ -30,6 +30,7 @@ type (
|
||||
MTU string // Docker daemon mtu setting
|
||||
IPv6 bool // Docker daemon IPv6 networking
|
||||
Experimental bool // Docker daemon enable experimental mode
|
||||
RetryCount int // Number of retry attempts to reach Docker daemon
|
||||
RegistryType drone.RegistryType // Docker registry type
|
||||
}
|
||||
|
||||
@@ -137,14 +138,18 @@ func (p Plugin) Exec() error {
|
||||
|
||||
// poll the docker daemon until it is started. This ensures the daemon is
|
||||
// ready to accept connections before we proceed.
|
||||
maxRetries := p.Daemon.RetryCount
|
||||
if maxRetries <= 0 {
|
||||
maxRetries = 15 // default value
|
||||
}
|
||||
for i := 0; ; i++ {
|
||||
cmd := commandInfo()
|
||||
err := cmd.Run()
|
||||
if err == nil {
|
||||
break
|
||||
}
|
||||
if i == 15 {
|
||||
fmt.Println("Unable to reach Docker Daemon after 15 attempts.")
|
||||
if i == maxRetries {
|
||||
fmt.Printf("Unable to reach Docker Daemon after %d attempts.\n", maxRetries)
|
||||
break
|
||||
}
|
||||
time.Sleep(time.Second * 1)
|
||||
@@ -592,7 +597,8 @@ func addProxyValue(build *Build, key string) {
|
||||
|
||||
// helper function to get a proxy value from the environment.
|
||||
//
|
||||
// assumes that the upper and lower case versions of are the same.
|
||||
// Checks in order: lowercase key, uppercase key, then HARNESS_<UPPERCASE_KEY>.
|
||||
// Assumes that the upper and lower case versions are the same value.
|
||||
func getProxyValue(key string) string {
|
||||
value := os.Getenv(key)
|
||||
|
||||
@@ -600,15 +606,26 @@ func getProxyValue(key string) string {
|
||||
return value
|
||||
}
|
||||
|
||||
return os.Getenv(strings.ToUpper(key))
|
||||
value = os.Getenv(strings.ToUpper(key))
|
||||
|
||||
if len(value) > 0 {
|
||||
return value
|
||||
}
|
||||
|
||||
harnessValue := os.Getenv("HARNESS_" + strings.ToUpper(key))
|
||||
if len(harnessValue) > 0 {
|
||||
fmt.Printf("Using HARNESS_%s as proxy value for %s\n", strings.ToUpper(key), key)
|
||||
}
|
||||
return harnessValue
|
||||
}
|
||||
|
||||
// helper function that looks to see if a proxy value was set in the build args.
|
||||
func hasProxyBuildArg(build *Build, key string) bool {
|
||||
keyUpper := strings.ToUpper(key)
|
||||
harnessKey := "HARNESS_" + keyUpper
|
||||
|
||||
for _, s := range build.Args {
|
||||
if strings.HasPrefix(s, key) || strings.HasPrefix(s, keyUpper) {
|
||||
if strings.HasPrefix(s, key) || strings.HasPrefix(s, keyUpper) || strings.HasPrefix(s, harnessKey) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
@@ -617,9 +634,10 @@ func hasProxyBuildArg(build *Build, key string) bool {
|
||||
}
|
||||
func hasProxyBuildArgNew(build *Build, key string) bool {
|
||||
keyUpper := strings.ToUpper(key)
|
||||
harnessKey := "HARNESS_" + keyUpper
|
||||
|
||||
for _, s := range build.ArgsNew {
|
||||
if strings.HasPrefix(s, key) || strings.HasPrefix(s, keyUpper) {
|
||||
if strings.HasPrefix(s, key) || strings.HasPrefix(s, keyUpper) || strings.HasPrefix(s, harnessKey) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package docker
|
||||
|
||||
import (
|
||||
"os"
|
||||
"os/exec"
|
||||
"reflect"
|
||||
"strings"
|
||||
@@ -179,3 +180,90 @@ func TestCommandBuild(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetProxyValue(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
key string
|
||||
envVars map[string]string
|
||||
expected string
|
||||
}{
|
||||
{
|
||||
name: "lowercase env var set",
|
||||
key: "http_proxy",
|
||||
envVars: map[string]string{"http_proxy": "http://proxy:8080"},
|
||||
expected: "http://proxy:8080",
|
||||
},
|
||||
{
|
||||
name: "uppercase env var set",
|
||||
key: "http_proxy",
|
||||
envVars: map[string]string{"HTTP_PROXY": "http://proxy:8080"},
|
||||
expected: "http://proxy:8080",
|
||||
},
|
||||
{
|
||||
name: "HARNESS prefixed env var set",
|
||||
key: "http_proxy",
|
||||
envVars: map[string]string{"HARNESS_HTTP_PROXY": "http://harness-proxy:8080"},
|
||||
expected: "http://harness-proxy:8080",
|
||||
},
|
||||
{
|
||||
name: "standard takes precedence over HARNESS",
|
||||
key: "http_proxy",
|
||||
envVars: map[string]string{
|
||||
"HTTP_PROXY": "http://standard:8080",
|
||||
"HARNESS_HTTP_PROXY": "http://harness:8080",
|
||||
},
|
||||
expected: "http://standard:8080",
|
||||
},
|
||||
{
|
||||
name: "lowercase takes precedence over uppercase",
|
||||
key: "no_proxy",
|
||||
envVars: map[string]string{
|
||||
"no_proxy": "localhost,127.0.0.1",
|
||||
"NO_PROXY": "*.example.com",
|
||||
"HARNESS_NO_PROXY": "*.local",
|
||||
},
|
||||
expected: "localhost,127.0.0.1",
|
||||
},
|
||||
{
|
||||
name: "lowercase takes precedence over HARNESS",
|
||||
key: "https_proxy",
|
||||
envVars: map[string]string{
|
||||
"https_proxy": "https://standard:8080",
|
||||
"HARNESS_HTTPS_PROXY": "https://harness:8080",
|
||||
},
|
||||
expected: "https://standard:8080",
|
||||
},
|
||||
{
|
||||
name: "no env var set",
|
||||
key: "http_proxy",
|
||||
envVars: map[string]string{},
|
||||
expected: "",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
// Clean env
|
||||
lowercaseKey := tt.key
|
||||
uppercaseKey := strings.ToUpper(tt.key)
|
||||
harnessKey := "HARNESS_" + strings.ToUpper(tt.key)
|
||||
|
||||
os.Unsetenv(lowercaseKey)
|
||||
os.Unsetenv(uppercaseKey)
|
||||
os.Unsetenv(harnessKey)
|
||||
|
||||
// Set test environment variables
|
||||
for k, v := range tt.envVars {
|
||||
os.Setenv(k, v)
|
||||
defer os.Unsetenv(k)
|
||||
}
|
||||
|
||||
// Execute and verify
|
||||
result := getProxyValue(tt.key)
|
||||
if result != tt.expected {
|
||||
t.Errorf("getProxyValue(%q) = %q, want %q", tt.key, result, tt.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user