Skip to content

Commit

Permalink
feat: check for Docker connectivity on install (#38)
Browse files Browse the repository at this point in the history
Fixes #30
  • Loading branch information
agaffney authored Feb 24, 2024
1 parent 48fd211 commit e313fa7
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
24 changes: 24 additions & 0 deletions pkgmgr/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package pkgmgr

import (
"context"
"errors"
"fmt"
"io"
"log/slog"
Expand All @@ -28,6 +29,20 @@ import (
"github.com/docker/go-connections/nat"
)

const (
dockerInstallError = `could not contact Docker daemon
Docker is required to be already installed and running. Please refer to the following pages for more information
about how to install Docker.
* https://docs.docker.com/get-docker/
* https://docs.docker.com/engine/install/
If Docker is already installed but the socket is not in a standard location, you can use the DOCKER_HOST environment
variable to point to it.
`
)

type DockerService struct {
client *client.Client
logger *slog.Logger
Expand Down Expand Up @@ -287,6 +302,7 @@ func (d *DockerService) getClient() (*client.Client, error) {
if d.client == nil {
tmpClient, err := client.NewClientWithOpts(
client.WithAPIVersionNegotiation(),
client.WithHostFromEnv(),
)
if err != nil {
return nil, err
Expand All @@ -295,3 +311,11 @@ func (d *DockerService) getClient() (*client.Client, error) {
}
return d.client, nil
}

func CheckDockerConnectivity() error {
tmpDockerService := &DockerService{}
if _, err := tmpDockerService.getClient(); err != nil {
return errors.New(dockerInstallError)
}
return nil
}
3 changes: 3 additions & 0 deletions pkgmgr/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,6 @@ var ErrContextNotExist = errors.New("context does not exist")

// ErrContextAlreadyExists is returned when creating a context with a name that is already in use
var ErrContextAlreadyExists = errors.New("specified context already exists")

// ErrContainerAlreadyExists is returned when creating a new container with a name that is already in use
var ErrContainerAlreadyExists = errors.New("specified container already exists")
24 changes: 24 additions & 0 deletions pkgmgr/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,21 @@ type Package struct {

func (p Package) install(cfg Config, context string) error {
pkgName := fmt.Sprintf("%s-%s-%s", p.Name, p.Version, context)
// Run pre-flight checks
for _, installStep := range p.InstallSteps {
// Make sure only one install method is specified per install step
if installStep.Docker != nil &&
installStep.File != nil {
return ErrMultipleInstallMethods
}
if installStep.Docker != nil {
if err := installStep.Docker.preflight(cfg, pkgName); err != nil {
return fmt.Errorf("pre-flight check failed: %s", err)
}
}
}
// Perform install
for _, installStep := range p.InstallSteps {
if installStep.Docker != nil {
if err := installStep.Docker.install(cfg, pkgName); err != nil {
return err
Expand Down Expand Up @@ -91,6 +100,21 @@ type PackageInstallStepDocker struct {
Ports []string `yaml:"ports,omitempty"`
}

func (p *PackageInstallStepDocker) preflight(cfg Config, pkgName string) error {
if err := CheckDockerConnectivity(); err != nil {
return err
}
containerName := fmt.Sprintf("%s-%s", pkgName, p.ContainerName)
svc, err := NewDockerServiceFromContainerName(containerName, cfg.Logger)
if err != nil {
return err
}
if svc != nil {
return ErrContainerAlreadyExists
}
return nil
}

func (p *PackageInstallStepDocker) install(cfg Config, pkgName string) error {
containerName := fmt.Sprintf("%s-%s", pkgName, p.ContainerName)
svc := DockerService{
Expand Down

0 comments on commit e313fa7

Please sign in to comment.