Modern Docker Management, Designed for Everyone
Go composecontainer-managementcontainersdockerdocker-composedocker-managementgoself-hostedsveltekittypescriptweb-ui
fix: system upgrade doesnt support non unix socket docker hosts (#2651)
Kyle Mendell2aeb552b8436
diff --git a/backend/api/handlers/system.go b/backend/api/handlers/system.goindex e1e4eb63d..90bac028f 100644--- a/backend/api/handlers/system.go+++ b/backend/api/handlers/system.go@@ -2,7 +2,6 @@ package handlersimport ("context"- "errors""log/slog""net/http""strings"@@ -544,7 +543,7 @@ func (h *SystemHandler) TriggerUpgrade(ctx context.Context, input *TriggerUpgradif err != nil {slog.Error("System upgrade failed", "error", err, "user", user.Username)- if errors.Is(err, services.ErrUpgradeInProgress) {+ if common.IsUpgradeInProgressError(err) {return nil, huma.Error409Conflict((&common.UpgradeTriggerError{Err: err}).Error())}diff --git a/backend/internal/common/errors.go b/backend/internal/common/errors.goindex 5fb38d154..1cc554184 100644--- a/backend/internal/common/errors.go+++ b/backend/internal/common/errors.go@@ -1070,6 +1070,34 @@ func (e *UpgradeTriggerError) Error() string {return fmt.Sprintf("Failed to initiate upgrade: %v", e.Err)}+type NotRunningInDockerError struct{}++func (e *NotRunningInDockerError) Error() string {+ return "arcane is not running in a Docker container"+}++type ArcaneContainerNotFoundError struct{}++func (e *ArcaneContainerNotFoundError) Error() string {+ return "could not find Arcane container"+}++type UpgradeInProgressError struct{}++func (e *UpgradeInProgressError) Error() string {+ return "an upgrade is already in progress"+}++func IsUpgradeInProgressError(err error) bool {+ return isErrorTypeInternal[*UpgradeInProgressError](err)+}++type DockerSocketAccessError struct{}++func (e *DockerSocketAccessError) Error() string {+ return "docker socket is not accessible"+}+type TemplateListError struct {Err error}diff --git a/backend/internal/services/system_upgrade_service.go b/backend/internal/services/system_upgrade_service.goindex 6193a5687..5ef649001 100644--- a/backend/internal/services/system_upgrade_service.go+++ b/backend/internal/services/system_upgrade_service.go@@ -10,6 +10,7 @@ import ("sync/atomic""time"+ "github.com/getarcaneapp/arcane/backend/internal/common""github.com/getarcaneapp/arcane/backend/internal/models"dockerutils "github.com/getarcaneapp/arcane/backend/pkg/dockerutil""github.com/getarcaneapp/arcane/backend/pkg/libarcane"@@ -20,13 +21,7 @@ import ("github.com/moby/moby/client")-var (- ErrNotRunningInDocker = errors.New("arcane is not running in a Docker container")- ErrContainerNotFound = errors.New("could not find Arcane container")- ErrUpgradeInProgress = errors.New("an upgrade is already in progress")- ErrDockerSocketAccess = errors.New("docker socket is not accessible")- ArcaneUpgraderImage = "ghcr.io/getarcaneapp/arcane:latest"-)+var ArcaneUpgraderImage = "ghcr.io/getarcaneapp/arcane:latest"type SystemUpgradeService struct {upgrading atomic.Bool@@ -36,6 +31,12 @@ type SystemUpgradeService struct {settingsService *SettingsService}+type upgraderRuntimeOptionsInternal struct {+ ContainerEnv []string+ Mounts []mounttypes.Mount+ NetworkMode containertypes.NetworkMode+}+func NewSystemUpgradeService(dockerService *DockerClientService,versionService *VersionService,@@ -53,19 +54,19 @@ func NewSystemUpgradeService(// CanUpgrade checks if self-upgrade is possiblefunc (s *SystemUpgradeService) CanUpgrade(ctx context.Context) (bool, error) {// Check if running in Docker- containerId, err := s.getCurrentContainerID()+ containerId, err := s.getCurrentContainerIDInternal()if err != nil {- return false, ErrNotRunningInDocker+ return false, err}// Verify we can access Docker_, err = s.dockerService.GetClient(ctx)if err != nil {- return false, ErrDockerSocketAccess+ return false, &common.DockerSocketAccessError{}}// Verify we can find our container- _, err = s.findArcaneContainer(ctx, containerId)+ _, err = s.findArcaneContainerInternal(ctx, containerId)if err != nil {return false, err}@@ -77,17 +78,17 @@ func (s *SystemUpgradeService) CanUpgrade(ctx context.Context) (bool, error) {// This avoids self-termination issues by running the upgrade from outsidefunc (s *SystemUpgradeService) TriggerUpgradeViaCLI(ctx context.Context, user models.User) error {if !s.upgrading.CompareAndSwap(false, true) {- return ErrUpgradeInProgress+ return &common.UpgradeInProgressError{}}defer s.upgrading.Store(false)// Get current container name- containerId, err := s.getCurrentContainerID()+ containerId, err := s.getCurrentContainerIDInternal()if err != nil {return fmt.Errorf("get current container: %w", err)}- currentContainer, err := s.findArcaneContainer(ctx, containerId)+ currentContainer, err := s.findArcaneContainerInternal(ctx, containerId)if err != nil {return fmt.Errorf("inspect container: %w", err)}@@ -162,18 +163,33 @@ func (s *SystemUpgradeService) TriggerUpgradeViaCLI(ctx context.Context, user mo}// Create the upgrader container config+ runtimeOptions, err := resolveSystemUpgraderRuntimeOptionsInternal(+ ctx,+ s.dockerService.DockerHost(),+ ¤tContainer,+ func(ctx context.Context, containerPath string) (string, error) {+ return dockerutils.GetHostPathForContainerPath(ctx, dockerClient, containerPath)+ },+ func() bool {+ _, err := dockerutils.GetCurrentContainerID()+ return err == nil+ },+ )+ if err != nil {+ return fmt.Errorf("resolve upgrader docker runtime: %w", err)+ }+config := &containertypes.Config{Image: ArcaneUpgraderImage,Cmd: []string{binaryPath, "upgrade", "--container", containerName},+ Env: runtimeOptions.ContainerEnv,Labels: map[string]string{"com.getarcaneapp.arcane.upgrader": "true","com.getarcaneapp.arcane": "true",},}- mounts := []mounttypes.Mount{- {Type: mounttypes.TypeBind, Source: "/var/run/docker.sock", Target: "/var/run/docker.sock"},- }+ mounts := append([]mounttypes.Mount{}, runtimeOptions.Mounts...)if appDataMount != nil {mounts = append(mounts, *appDataMount)}@@ -184,8 +200,9 @@ func (s *SystemUpgradeService) TriggerUpgradeViaCLI(ctx context.Context, user mo}hostConfig := &containertypes.HostConfig{- AutoRemove: !keepUpgraderContainer, // default: clean up after completion- Mounts: mounts,+ AutoRemove: !keepUpgraderContainer, // default: clean up after completion+ Mounts: mounts,+ NetworkMode: runtimeOptions.NetworkMode,}containerName = fmt.Sprintf("%s-upgrader-%d", containerName, time.Now().Unix())@@ -218,17 +235,57 @@ func determineUpgradeBinaryPathInternal(labels map[string]string) string {return "/app/arcane"}+func resolveSystemUpgraderRuntimeOptionsInternal(+ ctx context.Context,+ dockerHost string,+ currentContainer *containertypes.InspectResponse,+ discoverHostPath func(context.Context, string) (string, error),+ isRunningInDocker func() bool,+) (upgraderRuntimeOptionsInternal, error) {+ options := upgraderRuntimeOptionsInternal{+ ContainerEnv: buildTrivyDockerHostEnvInternal(dockerHost),+ }++ scheme, socketPath, err := parseTrivyDockerHostInternal(dockerHost)+ if err != nil {+ return upgraderRuntimeOptionsInternal{}, fmt.Errorf("resolve docker host %q: %w", dockerHost, err)+ }++ if scheme != "unix" {+ options.NetworkMode = containertypes.NetworkMode(selectTrivyAutoNetworkModeInternal(currentContainer))+ return options, nil+ }++ socketSource, err := resolveTrivyUnixSocketSourceInternal(+ ctx,+ socketPath,+ discoverHostPath,+ isRunningInDocker,+ )+ if err != nil {+ return upgraderRuntimeOptionsInternal{}, fmt.Errorf("resolve unix socket source: %w", err)+ }++ options.Mounts = append(options.Mounts, mounttypes.Mount{+ Type: mounttypes.TypeBind,+ Source: socketSource,+ Target: socketPath,+ })++ return options, nil+}+// getCurrentContainerID detects if we're running in Docker and returns container ID-func (s *SystemUpgradeService) getCurrentContainerID() (string, error) {+func (s *SystemUpgradeService) getCurrentContainerIDInternal() (string, error) {id, err := dockerutils.GetCurrentContainerID()if err != nil {- return "", ErrNotRunningInDocker+ return "", &common.NotRunningInDockerError{}}return id, nil}// findArcaneContainer finds the container using the ID-func (s *SystemUpgradeService) findArcaneContainer(ctx context.Context, containerId string) (containertypes.InspectResponse, error) {+func (s *SystemUpgradeService) findArcaneContainerInternal(ctx context.Context, containerId string) (containertypes.InspectResponse, error) {dockerClient, err := s.dockerService.GetClient(ctx)if err != nil {return containertypes.InspectResponse{}, err@@ -278,5 +335,5 @@ func (s *SystemUpgradeService) findArcaneContainer(ctx context.Context, containe}}- return containertypes.InspectResponse{}, ErrContainerNotFound+ return containertypes.InspectResponse{}, &common.ArcaneContainerNotFoundError{}}diff --git a/backend/internal/services/system_upgrade_service_test.go b/backend/internal/services/system_upgrade_service_test.goindex 7de4424fc..f76ace096 100644--- a/backend/internal/services/system_upgrade_service_test.go+++ b/backend/internal/services/system_upgrade_service_test.go@@ -1,9 +1,15 @@package servicesimport (+ "context"+ "errors""testing"+ "github.com/getarcaneapp/arcane/backend/internal/common"libupdater "github.com/getarcaneapp/arcane/backend/pkg/libarcane/imageupdate"+ containertypes "github.com/moby/moby/api/types/container"+ mounttypes "github.com/moby/moby/api/types/mount"+ networktypes "github.com/moby/moby/api/types/network""github.com/stretchr/testify/require")@@ -35,16 +41,16 @@ func TestSystemUpgradeService_Initialization(t *testing.T) {// TestSystemUpgradeService_ErrorVariables tests that error variables are properly definedfunc TestSystemUpgradeService_ErrorVariables(t *testing.T) {// Test that all expected errors exist and are not nil- require.Error(t, ErrNotRunningInDocker)- require.Error(t, ErrContainerNotFound)- require.Error(t, ErrUpgradeInProgress)- require.Error(t, ErrDockerSocketAccess)+ require.Error(t, &common.NotRunningInDockerError{})+ require.Error(t, &common.ArcaneContainerNotFoundError{})+ require.Error(t, &common.UpgradeInProgressError{})+ require.Error(t, &common.DockerSocketAccessError{})// Test error messages- require.Equal(t, "arcane is not running in a Docker container", ErrNotRunningInDocker.Error())- require.Equal(t, "could not find Arcane container", ErrContainerNotFound.Error())- require.Equal(t, "an upgrade is already in progress", ErrUpgradeInProgress.Error())- require.Equal(t, "docker socket is not accessible", ErrDockerSocketAccess.Error())+ require.Equal(t, "arcane is not running in a Docker container", (&common.NotRunningInDockerError{}).Error())+ require.Equal(t, "could not find Arcane container", (&common.ArcaneContainerNotFoundError{}).Error())+ require.Equal(t, "an upgrade is already in progress", (&common.UpgradeInProgressError{}).Error())+ require.Equal(t, "docker socket is not accessible", (&common.DockerSocketAccessError{}).Error())}// TestSystemUpgradeService_UpgradingFlag_ConcurrentAccess tests upgrading flag@@ -116,12 +122,12 @@ func TestSystemUpgradeService_ConcurrentUpgradeAttempts(t *testing.T) {// TestSystemUpgradeService_UpgradeInProgressError tests the upgrade in progress sentinel errorfunc TestSystemUpgradeService_UpgradeInProgressError(t *testing.T) {// This tests the specific error that the handler checks for- // The handler uses: if errors.Is(err, services.ErrUpgradeInProgress)+ // The handler uses common.IsUpgradeInProgressError for conflict detection.- require.Equal(t, "an upgrade is already in progress", ErrUpgradeInProgress.Error())+ err := &common.UpgradeInProgressError{}+ require.Equal(t, "an upgrade is already in progress", err.Error())- // Test that the error is not nil- require.Error(t, ErrUpgradeInProgress)+ require.True(t, common.IsUpgradeInProgressError(err))}// TestSystemUpgradeService_AtomicOperations tests atomic.Bool operations@@ -184,3 +190,84 @@ func TestDetermineUpgradeBinaryPath(t *testing.T) {})}}++func TestResolveSystemUpgraderRuntimeOptionsInternal_TCPDockerHost(t *testing.T) {+ currentContainer := &containertypes.InspectResponse{+ HostConfig: &containertypes.HostConfig{NetworkMode: "bridge"},+ NetworkSettings: &containertypes.NetworkSettings{+ Networks: map[string]*networktypes.EndpointSettings{+ "bridge": {},+ "arcane-test": {},+ },+ },+ }++ options, err := resolveSystemUpgraderRuntimeOptionsInternal(+ context.Background(),+ "tcp://docker-socket-proxy:2375",+ currentContainer,+ nil,+ func() bool { return true },+ )+ require.NoError(t, err)+ require.Equal(t, []string{"DOCKER_HOST=tcp://docker-socket-proxy:2375"}, options.ContainerEnv)+ require.Empty(t, options.Mounts)+ require.Equal(t, containertypes.NetworkMode("arcane-test"), options.NetworkMode)+}++func TestResolveSystemUpgraderRuntimeOptionsInternal_UnixDockerHost(t *testing.T) {+ options, err := resolveSystemUpgraderRuntimeOptionsInternal(+ context.Background(),+ "unix:///var/run/docker.sock",+ nil,+ func(context.Context, string) (string, error) {+ return "/host/run/docker.sock", nil+ },+ func() bool { return true },+ )+ require.NoError(t, err)+ require.Equal(t, []string{"DOCKER_HOST=unix:///var/run/docker.sock"}, options.ContainerEnv)+ require.Equal(t, containertypes.NetworkMode(""), options.NetworkMode)+ require.Equal(t, []mounttypes.Mount{+ {+ Type: mounttypes.TypeBind,+ Source: "/host/run/docker.sock",+ Target: "/var/run/docker.sock",+ },+ }, options.Mounts)+}++func TestResolveSystemUpgraderRuntimeOptionsInternal_DefaultDockerHost(t *testing.T) {+ options, err := resolveSystemUpgraderRuntimeOptionsInternal(+ context.Background(),+ "",+ nil,+ func(context.Context, string) (string, error) {+ return "/var/run/docker.sock", nil+ },+ func() bool { return true },+ )+ require.NoError(t, err)+ require.Nil(t, options.ContainerEnv)+ require.Equal(t, []mounttypes.Mount{+ {+ Type: mounttypes.TypeBind,+ Source: "/var/run/docker.sock",+ Target: "/var/run/docker.sock",+ },+ }, options.Mounts)+}++func TestResolveSystemUpgraderRuntimeOptionsInternal_UnixDockerHostResolutionError(t *testing.T) {+ _, err := resolveSystemUpgraderRuntimeOptionsInternal(+ context.Background(),+ "unix:///var/run/docker.sock",+ nil,+ func(context.Context, string) (string, error) {+ return "", errors.New("inspect failed")+ },+ func() bool { return true },+ )+ require.Error(t, err)+ require.Contains(t, err.Error(), "resolve unix socket source")+}