Skip to content

Modern Docker Management, Designed for Everyone

5.9 KB
Raw Blame
package volumehelper

import (
	"context"
	"errors"
	"fmt"
	"io"
	"os"
	"runtime"
	"strings"

	ref "github.com/distribution/reference"
	"github.com/moby/moby/api/types/container"
	"github.com/moby/moby/api/types/mount"
	"github.com/moby/moby/client"
	"github.com/samber/mo"
	"go.getarcane.app/docker/compat"
	kit "go.getarcane.app/kit/pkg"

	"github.com/getarcaneapp/arcane/backend/v2/pkg/libarcane"
)

// RuntimeImage describes the Arcane runtime image that can run internal helper
// commands from inside temporary containers.
type RuntimeImage struct {
	Image      string
	Entrypoint []string
	Command    []string
	Source     string
}

const (
	toolsRepoPathInternal = "getarcaneapp/tools"
	ContainerLabel        = "com.getarcaneapp.volume-helper"
)

// ToolsImage returns the shared Arcane tools image reference hosted on the given registry setting.
func ToolsImage(registry string) string {
	return libarcane.ArcaneRegistryHost(registry) + "/" + toolsRepoPathInternal + ":latest"
}

// Labels returns the labels used for temporary internal volume helper containers.
func Labels() map[string]string {
	return map[string]string{
		libarcane.InternalResourceLabel: "true",
		ContainerLabel:                  "true",
	}
}

// RemoveOptions returns the container remove options used for volume helpers.
func RemoveOptions() client.ContainerRemoveOptions {
	return client.ContainerRemoveOptions{Force: true, RemoveVolumes: true}
}

// HostConfig builds the host config shared by volume helper containers.
func HostConfig(helperImage string, binds []string, mounts []mount.Mount) *container.HostConfig {
	hostConfig := &container.HostConfig{
		Binds:      binds,
		Mounts:     mounts,
		AutoRemove: true,
	}

	if runtime.GOOS == "linux" && isArcaneFallbackImageInternal(helperImage) {
		hostConfig.Tmpfs = map[string]string{
			"/app/data": "rw,noexec,nosuid,nodev",
		}
	}

	return hostConfig
}

// ResolveHelperImage returns an image suitable for temporary volume helper
// containers, pulling the tools image when it is not already present.
func ResolveHelperImage(ctx context.Context, dockerClient *client.Client, toolsImage string) (string, error) {
	if dockerClient == nil {
		return "", errors.New("docker service unavailable")
	}

	if _, err := dockerClient.ImageInspect(ctx, toolsImage); err == nil {
		return toolsImage, nil
	}

	pullReader, pullErr := dockerClient.ImagePull(ctx, toolsImage, client.ImagePullOptions{})
	if pullErr == nil {
		if pullReader == nil {
			pullErr = errors.New("helper image pull returned no response body")
		} else {
			defer func() { _ = pullReader.Close() }()
			if _, err := io.Copy(io.Discard, pullReader); err != nil {
				pullErr = fmt.Errorf("read helper image pull response: %w", err)
			} else {
				return toolsImage, nil
			}
		}
	}

	if fallback, ok := ResolveArcaneRuntimeImage(ctx, dockerClient).Get(); ok && strings.TrimSpace(fallback.Image) != "" {
		return fallback.Image, nil
	}

	return "", fmt.Errorf("failed to resolve helper image: tools image unavailable and arcane fallback not found: %w", pullErr)
}

// ResolveArcaneRuntimeImage resolves the current Arcane or Arcane agent image
// so internal helper commands can run without pulling an external helper image.
func ResolveArcaneRuntimeImage(ctx context.Context, dockerClient *client.Client) mo.Option[RuntimeImage] {
	hostname, _ := os.Hostname()
	if hostname != "" {
		if inspect,
			err := compat.ContainerInspectWithCompatibility(ctx,
			dockerClient,
			hostname,
			client.ContainerInspectOptions{}); err == nil && inspect.Container.Config != nil && strings.TrimSpace(inspect.Container.Config.Image) != "" {
			return mo.Some(buildRuntimeImageInternal(inspect.Container.Config.Image, inspect.Container.Config.Entrypoint, inspect.Container.Config.Cmd, "hostname"))
		}
	}

	for _, label := range []string{"com.getarcaneapp.arcane=true", "com.getarcaneapp.arcane.agent=true"} {
		filter := make(client.Filters)
		filter = filter.Add("label", label)
		containers, err := dockerClient.ContainerList(ctx, client.ContainerListOptions{Filters: filter, All: true})
		if err != nil || len(containers.Items) == 0 {
			continue
		}

		if resolved, ok := resolveRuntimeImageFromContainersInternal(ctx, dockerClient, containers.Items, label, true).Get(); ok {
			return mo.Some(resolved)
		}
		if resolved, ok := resolveRuntimeImageFromContainersInternal(ctx, dockerClient, containers.Items, label, false).Get(); ok {
			return mo.Some(resolved)
		}
	}

	return mo.None[RuntimeImage]()
}

func resolveRuntimeImageFromContainersInternal(ctx context.Context, dockerClient *client.Client, containers []container.Summary, label string, runningOnly bool) mo.Option[RuntimeImage] {
	source := kit.Ternary(strings.Contains(label, ".agent="), "arcane-agent-label", "arcane-label")

	for _, c := range containers {
		if runningOnly && c.State != container.StateRunning {
			continue
		}
		if !runningOnly && c.State == container.StateRunning {
			continue
		}
		inspect, err := compat.ContainerInspectWithCompatibility(ctx, dockerClient, c.ID, client.ContainerInspectOptions{})
		if err == nil && inspect.Container.Config != nil && strings.TrimSpace(inspect.Container.Config.Image) != "" {
			return mo.Some(buildRuntimeImageInternal(inspect.Container.Config.Image, inspect.Container.Config.Entrypoint, inspect.Container.Config.Cmd, source))
		}
		if strings.TrimSpace(c.Image) != "" {
			return mo.Some(buildRuntimeImageInternal(c.Image, nil, nil, source))
		}
	}

	return mo.None[RuntimeImage]()
}

func buildRuntimeImageInternal(image string, entrypoint, command []string, source string) RuntimeImage {
	return RuntimeImage{
		Image:      strings.TrimSpace(image),
		Entrypoint: append([]string(nil), entrypoint...),
		Command:    append([]string(nil), command...),
		Source:     source,
	}
}

func isArcaneFallbackImageInternal(helperImage string) bool {
	named, err := ref.ParseNormalizedNamed(strings.TrimSpace(helperImage))
	return err != nil || ref.Path(named) != toolsRepoPathInternal
}