Skip to content

Commit

Permalink
cirrus run: introduce --container-backend argument
Browse files Browse the repository at this point in the history
  • Loading branch information
edigaryev committed Nov 9, 2020
1 parent a96d30e commit b3b9add
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 57 deletions.
46 changes: 5 additions & 41 deletions internal/commands/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ var environment []string
var verbose bool

// Container-related flags.
var containerBackend string
var containerNoPull bool

// Flags useful for debugging.
Expand Down Expand Up @@ -93,51 +94,11 @@ func readStarlarkConfig(ctx context.Context, env map[string]string) (string, err
return lrk.Main(ctx, string(starlarkSource))
}

func getContainerBackend() (containerbackend.ContainerBackend, error) {
availableBackends := []struct {
Name string
Instantiate func() (containerbackend.ContainerBackend, error)
}{
{"docker", containerbackend.NewDocker},
{"podman", containerbackend.NewPodman},
}

// Specific backend was requested
desiredBackendName := os.Getenv("CIRRUS_CONTAINER_BACKEND")
for _, availableBackend := range availableBackends {
if availableBackend.Name != desiredBackendName {
continue
}

backend, err := availableBackend.Instantiate()
if err != nil {
return nil, fmt.Errorf("%w: cannot connect to %s daemon: %v, make sure it is installed",
ErrRun, strings.Title(availableBackend.Name), err)
}

return backend, nil
}

var backendNames []string

// No preferences, try all backends in the order of our own preference
for _, availableBackend := range availableBackends {
backendNames = append(backendNames, strings.Title(availableBackend.Name))

if backend, err := availableBackend.Instantiate(); err == nil {
return backend, nil
}
}

return nil, fmt.Errorf("%w: cannot connect to %s daemons, make sure the one of them is installed",
ErrRun, strings.Join(backendNames, " or "))
}

func run(cmd *cobra.Command, args []string) error {
// https://github.com/spf13/cobra/issues/340#issuecomment-374617413
cmd.SilenceUsage = true

backend, err := getContainerBackend()
backend, err := containerbackend.New(containerBackend)
if err != nil {
return err
}
Expand Down Expand Up @@ -252,6 +213,9 @@ func newRunCmd() *cobra.Command {
"supported values: %s", strings.Join(logs.Formats(), ", ")))

// Container-related flags
cmd.PersistentFlags().StringVar(&containerBackend, "container-backend", containerbackend.BackendAuto,
fmt.Sprintf("container engine backend to use, either \"%s\", \"%s\" or \"%s\"",
containerbackend.BackendDocker, containerbackend.BackendPodman, containerbackend.BackendAuto))
cmd.PersistentFlags().BoolVar(&containerNoPull, "container-no-pull", false,
"don't attempt to pull the images before starting containers")

Expand Down
42 changes: 40 additions & 2 deletions internal/executor/instance/containerbackend/containerbackend.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ package containerbackend
import (
"context"
"errors"
"fmt"
"io"
"os"
)

var (
ErrNotFound = errors.New("not found")
ErrDone = errors.New("done")
ErrNotFound = errors.New("not found")
ErrDone = errors.New("done")
ErrNewFailed = errors.New("failed to create backend")
)

type ContainerBackend interface {
Expand Down Expand Up @@ -79,3 +82,38 @@ type SystemInfo struct {
TotalCPUs int64
TotalMemoryBytes int64
}

const (
BackendAuto = "auto"
BackendDocker = "docker"
BackendPodman = "podman"
)

func New(name string) (ContainerBackend, error) {
if name == BackendAuto {
if nameFromEnv, ok := os.LookupEnv("CIRRUS_CONTAINER_BACKEND"); ok {
name = nameFromEnv
}
}

switch name {
case BackendDocker:
return NewDocker()
case BackendPodman:
return NewPodman()
case BackendAuto:
if backend, err := NewDocker(); err != nil {
return backend, nil
}

if backend, err := NewPodman(); err != nil {
return backend, nil
}

return nil, fmt.Errorf("%w: failed to instantiate all supported container backends"+
" (tried %q and %q, are these actually installed on the system?)",
ErrNewFailed, BackendDocker, BackendPodman)
default:
return nil, fmt.Errorf("%w: unknown container backend name %q", ErrNewFailed, name)
}
}
15 changes: 1 addition & 14 deletions internal/testutil/containerbackend.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,11 @@ package testutil

import (
"github.com/cirruslabs/cirrus-cli/internal/executor/instance/containerbackend"
"os"
"testing"
)

func ContainerBackendFromEnv(t *testing.T) containerbackend.ContainerBackend {
backendName := os.Getenv("CIRRUS_CONTAINER_BACKEND")

if backendName == "podman" {
backend, err := containerbackend.NewPodman()
if err != nil {
t.Fatal(err)
}

return backend
}

// Default to Docker
backend, err := containerbackend.NewDocker()
backend, err := containerbackend.New(containerbackend.BackendAuto)
if err != nil {
t.Fatal(err)
}
Expand Down

0 comments on commit b3b9add

Please sign in to comment.