Skip to content

Commit

Permalink
Add the ability to skip pulling images.
Browse files Browse the repository at this point in the history
  • Loading branch information
schmidtw committed Dec 12, 2023
1 parent 0d74975 commit 667cad1
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 18 deletions.
48 changes: 30 additions & 18 deletions idock.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ const (
// CLEANUP_ATTEMPTS_FLAG is the default environment variable that controls
// how many times to retry the cleanup process.
CLEANUP_ATTEMPTS_FLAG = "IDOCK_CLEANUP_ATTEMPTS"

// SKIP_DOCKER_PULL_FLAG is the default environment variable that controls
// whether or not to skip the docker-compose pull step.
SKIP_DOCKER_PULL_FLAG = "IDOCK_SKIP_DOCKER_PULL"
)

var (
Expand All @@ -49,12 +53,14 @@ type IDock struct {
dockerMaxWaitFlag string
programMaxWaitFlag string
cleanupAttemptsFlag string
skipDockerPullFlag string

tcpPortMaxWait time.Duration
dockerComposeFile string
dockerTCPPorts []int
dockerMaxWait time.Duration
dockerMaxPullWait time.Duration
skipDockerPull bool
afterDocker func(context.Context, *IDock)
program func()
programTCPPorts []int
Expand Down Expand Up @@ -91,6 +97,7 @@ func New(opts ...Option) *IDock {
DockerMaxWaitEnvarName(DOCKER_MAX_WAIT_FLAG),
ProgramMaxWaitEnvarName(PROGRAM_MAX_WAIT_FLAG),
DockerPullMaxWaitEnvarName(DOCKER_MAX_PULL_WAIT_FLAG),
SkipDockerPullEnvarName(SKIP_DOCKER_PULL_FLAG),
TCPPortMaxWait(10 * time.Millisecond),
DockerMaxWait(10 * time.Second),
DockerPullMaxWait(60 * time.Second),
Expand All @@ -108,6 +115,7 @@ func New(opts ...Option) *IDock {
cleanupRetries(),
dockerMaxWait(),
programMaxWait(),
skipDockerPull(),
}...)

for _, opt := range opts {
Expand Down Expand Up @@ -179,25 +187,29 @@ func (c *IDock) startDocker(ctx context.Context) error {

verbose := c.verbosity > 1

cmd, err := dockerCompose(ctx, verbose, "-f", c.dockerComposeFile, "pull")
if err != nil {
return fmt.Errorf("error pulling docker images: %w", err)
}
if c.skipDockerPull {
c.logf(1, "Skipping docker-compose pull\n")
} else {
cmd, err := dockerCompose(ctx, verbose, "-f", c.dockerComposeFile, "pull")
if err != nil {
return fmt.Errorf("error pulling docker images: %w", err)
}

cmd.WaitDelay = c.dockerMaxPullWait
dockerPullStart := time.Now()
c.logf(1, "Waiting for docker images to pull...\n")
err = cmd.Start()
if err != nil {
return fmt.Errorf("error pulling docker images: %w", err)
}
cmd.WaitDelay = c.dockerMaxPullWait
dockerPullStart := time.Now()
c.logf(1, "Waiting for docker images to pull...\n")
err = cmd.Start()
if err != nil {
return fmt.Errorf("error pulling docker images: %w", err)
}

err = cmd.Wait()
if err != nil {
c.logf(0, "docker-compose pull failed: %s\n", err)
return err
err = cmd.Wait()
if err != nil {
c.logf(0, "docker-compose pull failed: %s\n", err)
return err
}
c.logf(1, "docker-compose pull took %s\n", time.Since(dockerPullStart))
}
c.logf(1, "docker-compose pull took %s\n", time.Since(dockerPullStart))

args := []string{"-f", c.dockerComposeFile, "up", "-d"}
if verbose {
Expand All @@ -207,7 +219,7 @@ func (c *IDock) startDocker(ctx context.Context) error {
ctx, cancel := context.WithTimeout(ctx, c.dockerMaxWait)
defer cancel()

cmd, err = dockerCompose(ctx, verbose, args...)
cmd, err := dockerCompose(ctx, verbose, args...)
if err != nil {
return err
}
Expand Down Expand Up @@ -247,7 +259,7 @@ func (c *IDock) cleanup() {

if c.cleanupAttempts < 1 {
c.logf(0, "Docker container left intact. To cleanup run:\n")
c.logf(0, "docker-compose down --remove-orphans\n")
c.logf(0, "docker-compose -f %s down --remove-orphans\n", c.dockerComposeFile)
return
}

Expand Down
12 changes: 12 additions & 0 deletions idock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,18 @@ func TestIDock_startDocker(t *testing.T) {
localhost: "localhost",
dockerTCPPorts: []int{7999, 7998, 7997},
},
}, {
description: "success, verbosity",
have: IDock{
verbosity: 99,
skipDockerPull: true,
tcpPortMaxWait: 10 * time.Millisecond,
dockerComposeFile: "docker-compose.yml",
dockerMaxWait: 15 * time.Second,
dockerMaxPullWait: 60 * time.Second,
localhost: "localhost",
dockerTCPPorts: []int{7999},
},
}, {
description: "success, verbosity",
have: IDock{
Expand Down
31 changes: 31 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,16 @@ func DockerPullMaxWaitEnvarName(name string) Option {
})
}

// SkipDockerPullEnvarName sets the environment variable name to use for the
// skip docker pull flag.
//
// The default value is IDOCK_SKIP_DOCKER_PULL.
func SkipDockerPullEnvarName(name string) Option {
return optionFunc(func(c *IDock) {
c.skipDockerPullFlag = name
})
}

// verbosity sets the verbosity level based on the environment variable.
func verbosity() Option {
return optionFunc(func(c *IDock) {
Expand Down Expand Up @@ -235,6 +245,13 @@ func programMaxWait() Option {
})
}

// skipDockerPull sets the skipDockerPull flag based on the environment variable.
func skipDockerPull() Option {
return optionFunc(func(c *IDock) {
c.skipDockerPull = envToBool(c.dockerMaxPullWaitFlag, c.skipDockerPull)
})
}

func envToDuration(name string, def time.Duration) time.Duration {
s := strings.TrimSpace(os.Getenv(name))
if s == "" {
Expand All @@ -260,3 +277,17 @@ func envToInt(name string, def int) int {
}
return n
}

func envToBool(name string, def bool) bool {
s := strings.TrimSpace(os.Getenv(name))
if s == "" {
return def
}

b, err := strconv.ParseBool(s)
if err != nil {
panic(fmt.Sprintf("%s having value '%s' must be a boolean: %s\n",
name, s, err))
}
return b
}

0 comments on commit 667cad1

Please sign in to comment.