Modern Docker Management, Designed for Everyone
Go composecontainer-managementcontainersdockerdocker-composedocker-managementgoself-hostedsveltekittypescriptweb-ui
refactor: rework auto update service, and auto search registries (#296)
Kyle Mendellc1a7eeecd258
diff --git a/backend/internal/api/auto_update_handler.go b/backend/internal/api/auto_update_handler.gonew file mode 100644index 000000000..5899ce0c9--- /dev/null+++ b/backend/internal/api/auto_update_handler.go@@ -0,0 +1,43 @@+package api++import (+ "net/http"++ "github.com/gin-gonic/gin"+ "github.com/ofkm/arcane-backend/internal/services"+)++type AutoUpdateHandler struct {+ autoUpdateService *services.AutoUpdateService+}++func NewAutoUpdateHandler(autoUpdateService *services.AutoUpdateService) *AutoUpdateHandler {+ return &AutoUpdateHandler{+ autoUpdateService: autoUpdateService,+ }+}++func (h *AutoUpdateHandler) CheckContainersForUpdates(c *gin.Context) {+ result, err := h.autoUpdateService.CheckAndUpdateContainers(c.Request.Context())+ if err != nil {+ c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})+ return+ }++ c.JSON(http.StatusOK, result)+}++func (h *AutoUpdateHandler) CheckStacksForUpdates(c *gin.Context) {+ result, err := h.autoUpdateService.CheckAndUpdateStacks(c.Request.Context())+ if err != nil {+ c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})+ return+ }++ c.JSON(http.StatusOK, result)+}++func (h *AutoUpdateHandler) GetUpdateStatus(c *gin.Context) {+ status := h.autoUpdateService.GetUpdateStatus()+ c.JSON(http.StatusOK, status)+}diff --git a/backend/internal/api/routes.go b/backend/internal/api/routes.goindex d50f958cb..fe6366376 100644--- a/backend/internal/api/routes.go+++ b/backend/internal/api/routes.go@@ -25,6 +25,7 @@ type Services struct {Template *services.TemplateServiceContainerRegistry *services.ContainerRegistryServiceSystem *services.SystemService+ AutoUpdate *services.AutoUpdateService}func SetupRoutes(r *gin.Engine, services *Services, appConfig *config.Config) {@@ -44,6 +45,7 @@ func SetupRoutes(r *gin.Engine, services *Services, appConfig *config.Config) {setupNetworkRoutes(api, services)setupTemplateRoutes(api, services)setupContainerRegistryRoutes(api, services)+ setupAutoUpdateRoutes(api, services.AutoUpdate)}func setupContainerRegistryRoutes(api *gin.RouterGroup, services *Services) {@@ -200,6 +202,16 @@ func setupSystemRoutes(api *gin.RouterGroup, dockerService *services.DockerCliensystem.POST("/containers/stop-all", systemHandler.StopAllContainers)}+func setupAutoUpdateRoutes(api *gin.RouterGroup, autoUpdateService *services.AutoUpdateService) {+ autoUpdate := api.Group("/updates")++ autoUpdateHandler := NewAutoUpdateHandler(autoUpdateService)++ autoUpdate.POST("/check/containers", autoUpdateHandler.CheckContainersForUpdates)+ autoUpdate.POST("/check/compose", autoUpdateHandler.CheckStacksForUpdates)+ autoUpdate.GET("/status", autoUpdateHandler.GetUpdateStatus)+}+func setupContainerRoutes(api *gin.RouterGroup, services *Services) {containers := api.Group("/containers")containers.Use(middleware.AuthMiddleware(services.Auth))diff --git a/backend/internal/bootstrap/jobs_bootstrap.go b/backend/internal/bootstrap/jobs_bootstrap.goindex de425c344..459528fe4 100644--- a/backend/internal/bootstrap/jobs_bootstrap.go+++ b/backend/internal/bootstrap/jobs_bootstrap.go@@ -21,5 +21,8 @@ func registerJobs(appCtx context.Context, scheduler *job.Scheduler, appServicesif err := job.RegisterImageMaturityJob(appCtx, scheduler, appServices.Settings, appServices.ImageMaturity, appServices.Image); err != nil {slog.Error("Failed to register image maturity job", slog.Any("error", err))}- // Register other jobs here++ if err := job.RegisterAutoUpdateJob(appCtx, scheduler, appServices.AutoUpdate, appServices.Settings); err != nil {+ slog.Error("Failed to register auto-update job", slog.Any("error", err))+ }}diff --git a/backend/internal/bootstrap/services_bootstrap.go b/backend/internal/bootstrap/services_bootstrap.goindex 91cd76218..705cd39b9 100644--- a/backend/internal/bootstrap/services_bootstrap.go+++ b/backend/internal/bootstrap/services_bootstrap.go@@ -27,6 +27,7 @@ func initializeServices(db *database.DB, cfg *config.Config) (*api.Services, *seauthService := services.NewAuthService(userService, settingsService, cfg.JWTSecret, cfg)oidcService := services.NewOidcService(authService)containerRegistry := services.NewContainerRegistryService(db)+ autoUpdate := services.NewAutoUpdateService(dockerClientService, settingsService, containerService, stackService, imageService, containerRegistry)systemService := services.NewSystemService(db, dockerClientService, containerService, imageService, volumeService, networkService, settingsService)appServices := &api.Services{@@ -47,6 +48,7 @@ func initializeServices(db *database.DB, cfg *config.Config) (*api.Services, *seTemplate: templateService,ContainerRegistry: containerRegistry,System: systemService,+ AutoUpdate: autoUpdate,}return appServices, dockerClientService, nil}diff --git a/backend/internal/job/auto_update_job.go b/backend/internal/job/auto_update_job.gonew file mode 100644index 000000000..2a6ea2338--- /dev/null+++ b/backend/internal/job/auto_update_job.go@@ -0,0 +1,113 @@+package job++import (+ "context"+ "log"+ "time"++ "github.com/go-co-op/gocron/v2"+ "github.com/ofkm/arcane-backend/internal/services"+)++// AutoUpdateJob handles the scheduled auto-update functionality+type AutoUpdateJob struct {+ autoUpdateService *services.AutoUpdateService+ settingsService *services.SettingsService+}++// NewAutoUpdateJob creates a new auto-update job+func NewAutoUpdateJob(autoUpdateService *services.AutoUpdateService, settingsService *services.SettingsService) *AutoUpdateJob {+ return &AutoUpdateJob{+ autoUpdateService: autoUpdateService,+ settingsService: settingsService,+ }+}++// Execute runs the auto-update check+func (j *AutoUpdateJob) Execute(ctx context.Context) error {+ log.Println("=== Starting scheduled auto-update check ===")++ // Verify auto-update is still enabled+ settings, err := j.settingsService.GetSettings(ctx)+ if err != nil {+ log.Printf("Failed to get settings during scheduled update: %v", err)+ return err+ }++ if !settings.AutoUpdate {+ log.Println("Auto-update disabled, skipping scheduled check")+ return nil+ }++ // Run container updates+ containerResults, err := j.autoUpdateService.CheckAndUpdateContainers(ctx)+ if err != nil {+ log.Printf("Error during container auto-update: %v", err)+ } else {+ log.Printf("Container auto-update completed: %d checked, %d updated, %d errors",+ containerResults.Checked, containerResults.Updated, len(containerResults.Errors))+ }++ // Run stack updates+ stackResults, err := j.autoUpdateService.CheckAndUpdateStacks(ctx)+ if err != nil {+ log.Printf("Error during stack auto-update: %v", err)+ } else {+ log.Printf("Stack auto-update completed: %d checked, %d updated, %d errors",+ stackResults.Checked, stackResults.Updated, len(stackResults.Errors))+ }++ log.Println("=== Scheduled auto-update check completed ===")+ return nil+}++// RegisterAutoUpdateJob registers the auto-update job with the scheduler+func RegisterAutoUpdateJob(ctx context.Context, scheduler *Scheduler, autoUpdateService *services.AutoUpdateService, settingsService *services.SettingsService) error {+ // Get current settings to determine if auto-update is enabled and get interval+ settings, err := settingsService.GetSettings(ctx)+ if err != nil {+ return err+ }++ if !settings.AutoUpdate {+ log.Println("Auto-update is disabled, not registering auto-update job")+ return nil+ }++ interval := time.Duration(settings.AutoUpdateInterval) * time.Minute+ if interval < 5*time.Minute {+ interval = 60 * time.Minute+ log.Printf("Auto-update interval too low, using default 60 minutes")+ }++ log.Printf("Registering auto-update job with %v interval", interval)++ job := NewAutoUpdateJob(autoUpdateService, settingsService)++ jobDefinition := gocron.DurationJob(interval)++ return scheduler.RegisterJob(+ ctx,+ "auto-update",+ jobDefinition,+ job.Execute,+ false, // Don't run immediately on startup+ )+}++func UpdateAutoUpdateJobSchedule(ctx context.Context, scheduler *Scheduler, autoUpdateService *services.AutoUpdateService, settingsService *services.SettingsService) error {++ settings, err := settingsService.GetSettings(ctx)+ if err != nil {+ return err+ }++ if !settings.AutoUpdate {+ log.Println("Auto-update disabled, job will be skipped")+ return nil+ }+ interval := time.Duration(settings.AutoUpdateInterval) * time.Minute+ log.Printf("Auto-update settings changed - new interval: %v", interval)++ return nil+}diff --git a/backend/internal/services/auto_update_service.go b/backend/internal/services/auto_update_service.gonew file mode 100644index 000000000..ad1a851c1--- /dev/null+++ b/backend/internal/services/auto_update_service.go@@ -0,0 +1,879 @@+package services++import (+ "bufio"+ "context"+ "encoding/base64"+ "encoding/json"+ "fmt"+ "io"+ "log"+ "regexp"+ "strings"+ "sync"+ "time"++ "github.com/docker/docker/api/types"+ "github.com/docker/docker/api/types/container"+ "github.com/docker/docker/api/types/image"+ "github.com/docker/docker/api/types/network"+ "github.com/docker/docker/api/types/registry"+ "gopkg.in/yaml.v3"++ "github.com/ofkm/arcane-backend/internal/models"+)++type AutoUpdateService struct {+ dockerService *DockerClientService+ settingsService *SettingsService+ containerService *ContainerService+ stackService *StackService+ imageService *ImageService+ registryService *ContainerRegistryService+ updatingContainers map[string]bool+ updatingStacks map[string]bool+ mutex sync.RWMutex+}++type UpdateResult struct {+ Checked int `json:"checked"`+ Updated int `json:"updated"`+ Skipped int `json:"skipped"`+ Errors []UpdateError `json:"errors"`+ StartTime time.Time `json:"start_time"`+ EndTime time.Time `json:"end_time"`+ Duration string `json:"duration"`+}++type UpdateError struct {+ ID string `json:"id"`+ Name string `json:"name"`+ Type string `json:"type"` // "container" or "stack"+ Error string `json:"error"`+ ImageRef string `json:"image_ref,omitempty"`+}++type RegistryCredentials struct {+ URL string `json:"url"`+ Username string `json:"username"`+ Password string `json:"password"`+}++func NewAutoUpdateService(dockerService *DockerClientService, settingsService *SettingsService,+ containerService *ContainerService, stackService *StackService, imageService *ImageService,+ registryService *ContainerRegistryService) *AutoUpdateService {+ return &AutoUpdateService{+ dockerService: dockerService,+ settingsService: settingsService,+ containerService: containerService,+ stackService: stackService,+ imageService: imageService,+ registryService: registryService,+ updatingContainers: make(map[string]bool),+ updatingStacks: make(map[string]bool),+ }+}++// CheckAndUpdateContainers checks and updates eligible containers+func (s *AutoUpdateService) CheckAndUpdateContainers(ctx context.Context) (*UpdateResult, error) {+ startTime := time.Now()+ result := &UpdateResult{+ StartTime: startTime,+ Errors: []UpdateError{},+ }++ settings, err := s.settingsService.GetSettings(ctx)+ if err != nil {+ return nil, fmt.Errorf("failed to get settings: %w", err)+ }++ if !settings.AutoUpdate {+ result.EndTime = time.Now()+ result.Duration = result.EndTime.Sub(result.StartTime).String()+ return result, nil+ }++ log.Println("Starting container auto-update check...")++ dockerClient, err := s.dockerService.CreateConnection(ctx)+ if err != nil {+ return nil, fmt.Errorf("failed to connect to Docker: %w", err)+ }+ defer dockerClient.Close()++ containers, err := dockerClient.ContainerList(ctx, container.ListOptions{All: true})+ if err != nil {+ return nil, fmt.Errorf("failed to list containers: %w", err)+ }++ var eligibleContainers []container.Summary+ for _, container := range containers {+ containerName := s.getContainerName(container)++ if container.State != "running" {+ continue+ }++ if s.hasAutoUpdateLabel(container.Labels) {+ if s.isPartOfStack(container.Labels) {+ log.Printf("Skipping container %s - part of a stack, will be updated via stack update", containerName)+ continue+ }++ eligibleContainers = append(eligibleContainers, container)+ }+ }++ result.Checked = len(eligibleContainers)+ log.Printf("Found %d standalone containers eligible for auto-update", len(eligibleContainers))++ for _, container := range eligibleContainers {+ containerID := container.ID+ containerName := s.getContainerName(container)++ s.mutex.RLock()+ isUpdating := s.updatingContainers[containerID]+ s.mutex.RUnlock()++ if isUpdating {+ log.Printf("Container %s is already being updated, skipping", containerName)+ result.Skipped+++ continue+ }++ s.mutex.Lock()+ s.updatingContainers[containerID] = true+ s.mutex.Unlock()++ func() {+ defer func() {+ s.mutex.Lock()+ delete(s.updatingContainers, containerID)+ s.mutex.Unlock()+ }()++ log.Printf("Checking container for updates: %s", containerName)++ updateAvailable, err := s.checkContainerForUpdate(ctx, container, settings)+ if err != nil {+ log.Printf("Error checking container %s: %v", containerName, err)+ result.Errors = append(result.Errors, UpdateError{+ ID: containerID,+ Name: containerName,+ Type: "container",+ Error: err.Error(),+ ImageRef: container.Image,+ })+ return+ }++ if updateAvailable {+ log.Printf("Update available for container %s, recreating...", containerName)++ if err := s.recreateStandaloneContainer(ctx, container); err != nil {+ log.Printf("Error recreating container %s: %v", containerName, err)+ result.Errors = append(result.Errors, UpdateError{+ ID: containerID,+ Name: containerName,+ Type: "container",+ Error: fmt.Sprintf("Failed to recreate: %v", err),+ ImageRef: container.Image,+ })+ return+ }++ log.Printf("Successfully updated container %s", containerName)+ result.Updated+++ } else {+ log.Printf("Container %s is up-to-date", containerName)+ }+ }()+ }++ result.EndTime = time.Now()+ result.Duration = result.EndTime.Sub(result.StartTime).String()++ log.Printf("Container auto-update completed: %d checked, %d updated, %d skipped, %d errors",+ result.Checked, result.Updated, result.Skipped, len(result.Errors))++ return result, nil+}++// recreateStandaloneContainer recreates a standalone container preserving its configuration+func (s *AutoUpdateService) recreateStandaloneContainer(ctx context.Context, container types.Container) error {+ containerName := s.getContainerName(container)++ dockerClient, err := s.dockerService.CreateConnection(ctx)+ if err != nil {+ return fmt.Errorf("failed to connect to Docker: %w", err)+ }+ defer dockerClient.Close()++ containerJSON, err := dockerClient.ContainerInspect(ctx, container.ID)+ if err != nil {+ return fmt.Errorf("failed to inspect container: %w", err)+ }++ log.Printf("Stopping container %s...", containerName)+ if err := s.containerService.StopContainer(ctx, container.ID); err != nil {+ return fmt.Errorf("failed to stop container: %w", err)+ }++ log.Printf("Removing container %s...", containerName)+ if err := s.containerService.DeleteContainer(ctx, container.ID, false, false); err != nil {+ return fmt.Errorf("failed to remove container: %w", err)+ }++ config := containerJSON.Config+ hostConfig := containerJSON.HostConfig+ networkingConfig := &network.NetworkingConfig{+ EndpointsConfig: containerJSON.NetworkSettings.Networks,+ }++ log.Printf("Creating new container %s with updated image...", containerName)+ _, err = s.containerService.CreateContainer(ctx, config, hostConfig, networkingConfig, containerName)+ if err != nil {+ return fmt.Errorf("failed to create new container: %w", err)+ }++ return nil+}++// isPartOfStack checks if a container is part of a Docker Compose stack+func (s *AutoUpdateService) isPartOfStack(labels map[string]string) bool {+ if labels == nil {+ return false+ }++ _, hasComposeProject := labels["com.docker.compose.project"]+ _, hasComposeService := labels["com.docker.compose.service"]++ return hasComposeProject || hasComposeService+}++// CheckAndUpdateStacks checks and updates eligible stacks+func (s *AutoUpdateService) CheckAndUpdateStacks(ctx context.Context) (*UpdateResult, error) {+ startTime := time.Now()+ result := &UpdateResult{+ StartTime: startTime,+ Errors: []UpdateError{},+ }++ settings, err := s.settingsService.GetSettings(ctx)+ if err != nil {+ return nil, fmt.Errorf("failed to get settings: %w", err)+ }++ if !settings.AutoUpdate {+ result.EndTime = time.Now()+ result.Duration = result.EndTime.Sub(result.StartTime).String()+ return result, nil+ }++ log.Println("Starting stack auto-update check...")++ stacks, err := s.stackService.ListStacks(ctx)+ if err != nil {+ return nil, fmt.Errorf("failed to list stacks: %w", err)+ }++ var eligibleStacks []models.Stack+ for _, stack := range stacks {+ if stack.Status != "running" && stack.Status != "partially running" {+ continue+ }++ eligible, err := s.isStackEligibleForAutoUpdate(ctx, stack)+ if err != nil {+ log.Printf("Error checking stack %s eligibility: %v", stack.Name, err)+ continue+ }++ if eligible {+ eligibleStacks = append(eligibleStacks, stack)+ }+ }++ result.Checked = len(eligibleStacks)+ log.Printf("Found %d stacks eligible for auto-update", len(eligibleStacks))++ for _, stack := range eligibleStacks {+ stackID := stack.ID+ stackName := stack.Name++ s.mutex.RLock()+ isUpdating := s.updatingStacks[stackID]+ s.mutex.RUnlock()++ if isUpdating {+ log.Printf("Stack %s is already being updated, skipping", stackName)+ result.Skipped+++ continue+ }++ s.mutex.Lock()+ s.updatingStacks[stackID] = true+ s.mutex.Unlock()++ func() {+ defer func() {+ s.mutex.Lock()+ delete(s.updatingStacks, stackID)+ s.mutex.Unlock()+ }()++ log.Printf("Checking stack for updates: %s", stackName)++ updateAvailable, err := s.checkStackForUpdate(ctx, stack, settings)+ if err != nil {+ log.Printf("Error checking stack %s: %v", stackName, err)+ result.Errors = append(result.Errors, UpdateError{+ ID: stackID,+ Name: stackName,+ Type: "stack",+ Error: err.Error(),+ })+ return+ }++ if updateAvailable {+ log.Printf("Updates available for stack %s, stopping, pulling images and redeploying...", stackName)++ log.Printf("Stopping stack %s...", stackName)+ if err := s.stackService.StopStack(ctx, stackID); err != nil {+ log.Printf("Error stopping stack %s: %v", stackName, err)+ result.Errors = append(result.Errors, UpdateError{+ ID: stackID,+ Name: stackName,+ Type: "stack",+ Error: fmt.Sprintf("Failed to stop stack: %v", err),+ })+ return+ }++ log.Printf("Pulling updated images for stack %s...", stackName)+ if err := s.pullStackImages(ctx, stack, settings); err != nil {+ log.Printf("Error pulling images for stack %s: %v", stackName, err)+ result.Errors = append(result.Errors, UpdateError{+ ID: stackID,+ Name: stackName,+ Type: "stack",+ Error: fmt.Sprintf("Failed to pull images: %v", err),+ })+ return+ }++ log.Printf("Deploying stack %s with updated images...", stackName)+ if err := s.stackService.DeployStack(ctx, stackID); err != nil {+ log.Printf("Error redeploying stack %s: %v", stackName, err)+ result.Errors = append(result.Errors, UpdateError{+ ID: stackID,+ Name: stackName,+ Type: "stack",+ Error: fmt.Sprintf("Failed to redeploy: %v", err),+ })+ return+ }++ log.Printf("Successfully updated stack %s", stackName)+ result.Updated+++ } else {+ log.Printf("Stack %s is up-to-date", stackName)+ }+ }()+ }++ result.EndTime = time.Now()+ result.Duration = result.EndTime.Sub(result.StartTime).String()++ log.Printf("Stack auto-update completed: %d checked, %d updated, %d skipped, %d errors",+ result.Checked, result.Updated, result.Skipped, len(result.Errors))++ return result, nil+}++// pullStackImages pulls all images for a stack before redeployment+func (s *AutoUpdateService) pullStackImages(ctx context.Context, stack models.Stack, settings *models.Settings) error {+ composeContent, _, err := s.stackService.GetStackContent(ctx, stack.ID)+ if err != nil {+ return fmt.Errorf("failed to get stack content: %w", err)+ }++ imageRefs := s.extractImageReferences(composeContent)+ if len(imageRefs) == 0 {+ return nil+ }++ log.Printf("Pulling %d images for stack %s", len(imageRefs), stack.Name)++ for _, imageRef := range imageRefs {+ if s.isDigestBasedImage(imageRef) {+ log.Printf("Skipping digest-based image: %s", imageRef)+ continue+ }++ log.Printf("Pulling image: %s", imageRef)+ if err := s.pullImageWithAuth(ctx, imageRef, settings); err != nil {+ log.Printf("Warning: Failed to pull image %s: %v", imageRef, err)+ }+ }++ return nil+}++// pullImageWithAuth pulls an image using appropriate registry credentials+func (s *AutoUpdateService) pullImageWithAuth(ctx context.Context, imageRef string, settings *models.Settings) error {+ registryHost := s.extractRegistryHost(imageRef)+ var authConfig *registry.AuthConfig++ registries, err := s.getEnabledRegistries(ctx)+ if err != nil {+ log.Printf("Warning: Failed to get registry credentials: %v", err)+ } else if len(registries) > 0 {+ for _, reg := range registries {+ if s.isRegistryMatch(reg.URL, registryHost) {+ decryptedToken, err := s.registryService.GetDecryptedToken(ctx, reg.ID)+ if err != nil {+ log.Printf("ERROR: Failed to decrypt token for registry %s: %v", reg.URL, err)+ continue+ }++ authConfig = ®istry.AuthConfig{+ Username: reg.Username,+ Password: decryptedToken,+ ServerAddress: s.normalizeRegistryURL(reg.URL),+ }+ break+ }+ }+ }++ dockerClient, err := s.dockerService.CreateConnection(ctx)+ if err != nil {+ return fmt.Errorf("failed to connect to Docker: %w", err)+ }+ defer dockerClient.Close()++ pullOptions := image.PullOptions{}+ if authConfig != nil {+ authBytes, err := json.Marshal(authConfig)+ if err != nil {+ return fmt.Errorf("failed to marshal auth config: %w", err)+ }+ pullOptions.RegistryAuth = base64.URLEncoding.EncodeToString(authBytes)+ }++ reader, err := dockerClient.ImagePull(ctx, imageRef, pullOptions)+ if err != nil {+ if strings.Contains(err.Error(), "not found") {+ return fmt.Errorf("image not found: %s - please verify the image exists and you have access", imageRef)+ } else if strings.Contains(err.Error(), "unauthorized") {+ return fmt.Errorf("authentication failed for %s - please verify your credentials", imageRef)+ } else if strings.Contains(err.Error(), "denied") {+ return fmt.Errorf("access denied for %s - please verify your permissions", imageRef)+ }++ return fmt.Errorf("failed to pull image %s: %w", imageRef, err)+ }+ defer reader.Close()++ if err := s.consumeDockerResponse(reader); err != nil {+ return fmt.Errorf("error during image pull: %w", err)+ }++ return nil+}++// getEnabledRegistries gets enabled container registries from the database+func (s *AutoUpdateService) getEnabledRegistries(ctx context.Context) ([]models.ContainerRegistry, error) {+ registries, err := s.registryService.GetEnabledRegistries(ctx)+ if err != nil {+ log.Printf("Failed to get registry credentials from database: %v", err)+ return []models.ContainerRegistry{}, nil+ }++ return registries, nil+}++// consumeDockerResponse consumes the Docker API response stream+func (s *AutoUpdateService) consumeDockerResponse(reader io.ReadCloser) error {+ scanner := bufio.NewScanner(reader)+ for scanner.Scan() {+ var response map[string]interface{}+ if err := json.Unmarshal(scanner.Bytes(), &response); err != nil {+ continue+ }++ if errorMsg, exists := response["error"]; exists {+ return fmt.Errorf("docker operation failed: %v", errorMsg)+ }+ }++ return scanner.Err()+}++// checkContainerForUpdate checks if a container has an image update available+func (s *AutoUpdateService) checkContainerForUpdate(ctx context.Context, container types.Container, settings *models.Settings) (bool, error) {+ containerName := s.getContainerName(container)++ dockerClient, err := s.dockerService.CreateConnection(ctx)+ if err != nil {+ return false, fmt.Errorf("failed to connect to Docker: %w", err)+ }+ defer dockerClient.Close()++ containerJSON, err := dockerClient.ContainerInspect(ctx, container.ID)+ if err != nil {+ return false, fmt.Errorf("failed to inspect container: %w", err)+ }++ imageRef := containerJSON.Config.Image+ log.Printf("Container %s is using image: %s", containerName, imageRef)++ if s.isDigestBasedImage(imageRef) {+ log.Printf("Skipping digest-based image for container %s: %s", containerName, imageRef)+ return false, nil+ }++ currentImageID := container.ImageID+ log.Printf("Container %s current image ID: %s", containerName, currentImageID)++ log.Printf("Pulling same tag for container %s: %s", containerName, imageRef)+ if err := s.pullImageWithAuth(ctx, imageRef, settings); err != nil {+ return false, fmt.Errorf("failed to pull image %s: %w", imageRef, err)+ }++ newImageID, err := s.getImageID(ctx, imageRef)+ if err != nil {+ return false, fmt.Errorf("failed to get image ID after pull: %w", err)+ }++ log.Printf("Container %s after pull image ID: %s", containerName, newImageID)++ hasUpdate := newImageID != currentImageID+ if hasUpdate {+ log.Printf("Update detected for container %s: %s -> %s (same tag: %s)", containerName, currentImageID, newImageID, imageRef)+ } else {+ log.Printf("Container %s is up-to-date (tag: %s)", containerName, imageRef)+ }++ return hasUpdate, nil+}++// checkStackForUpdate checks if a stack has any image updates available+func (s *AutoUpdateService) checkStackForUpdate(ctx context.Context, stack models.Stack, settings *models.Settings) (bool, error) {+ composeContent, _, err := s.stackService.GetStackContent(ctx, stack.ID)+ if err != nil {+ return false, fmt.Errorf("failed to get stack content: %w", err)+ }++ if composeContent == "" {+ return false, fmt.Errorf("stack has no compose content")+ }++ imageRefs := s.extractImageReferences(composeContent)+ if len(imageRefs) == 0 {+ log.Printf("No images found in stack %s", stack.Name)+ return false, nil+ }++ log.Printf("Checking %d images for stack %s", len(imageRefs), stack.Name)++ for _, imageRef := range imageRefs {+ if s.isDigestBasedImage(imageRef) {+ log.Printf("Skipping digest-based image in stack %s: %s", stack.Name, imageRef)+ continue+ }++ hasUpdate, err := s.checkImageForUpdate(ctx, imageRef, settings)+ if err != nil {+ log.Printf("Error checking image %s in stack %s: %v", imageRef, stack.Name, err)+ continue+ }++ if hasUpdate {+ log.Printf("Update found for image %s in stack %s", imageRef, stack.Name)+ return true, nil+ }+ }++ return false, nil+}++// checkImageForUpdate checks if a single image has an update available+func (s *AutoUpdateService) checkImageForUpdate(ctx context.Context, imageRef string, settings *models.Settings) (bool, error) {+ currentImageID, err := s.getImageID(ctx, imageRef)+ if err != nil {+ log.Printf("Image %s not found locally, considering as new", imageRef)+ currentImageID = ""+ }++ log.Printf("Pulling image: %s", imageRef)+ if err := s.pullImageWithAuth(ctx, imageRef, settings); err != nil {+ return false, fmt.Errorf("failed to pull image %s: %w", imageRef, err)+ }++ newImageID, err := s.getImageID(ctx, imageRef)+ if err != nil {+ return false, fmt.Errorf("failed to get image ID after pull: %w", err)+ }++ if currentImageID == "" {+ log.Printf("New image pulled: %s", imageRef)+ return true, nil+ }++ hasUpdate := newImageID != currentImageID+ if hasUpdate {+ log.Printf("Image updated: %s (%s -> %s)", imageRef, currentImageID, newImageID)+ }++ return hasUpdate, nil+}++// hasAutoUpdateLabel checks if labels contain auto-update flag+func (s *AutoUpdateService) hasAutoUpdateLabel(labels map[string]string) bool {+ if labels == nil {+ return false+ }++ value, exists := labels["arcane.auto-update"]+ return exists && value == "true"+}++// isStackEligibleForAutoUpdate checks if a stack is eligible for auto-update+func (s *AutoUpdateService) isStackEligibleForAutoUpdate(ctx context.Context, stack models.Stack) (bool, error) {+ composeContent, _, err := s.stackService.GetStackContent(ctx, stack.ID)+ if err != nil {+ return false, fmt.Errorf("failed to get stack content: %w", err)+ }++ if composeContent == "" {+ return false, nil+ }++ var composeData map[string]interface{}+ if err := yaml.Unmarshal([]byte(composeContent), &composeData); err != nil {+ return false, fmt.Errorf("failed to parse compose file: %w", err)+ }++ services, ok := composeData["services"].(map[string]interface{})+ if !ok {+ return false, nil+ }++ for serviceName, service := range services {+ if s.serviceHasAutoUpdateLabel(service) {+ log.Printf("Found auto-update label in service %s of stack %s", serviceName, stack.Name)+ return true, nil+ }+ }++ return false, nil+}++// serviceHasAutoUpdateLabel checks if a service has auto-update label+func (s *AutoUpdateService) serviceHasAutoUpdateLabel(service interface{}) bool {+ serviceMap, ok := service.(map[string]interface{})+ if !ok {+ return false+ }++ labels, ok := serviceMap["labels"]+ if !ok {+ return false+ }++ switch labelsType := labels.(type) {+ case []interface{}:+ for _, label := range labelsType {+ if labelStr, ok := label.(string); ok {+ if labelStr == "arcane.stack.auto-update=true" {+ return true+ }+ }+ }+ case map[string]interface{}:+ if value, exists := labelsType["arcane.stack.auto-update"]; exists {+ if valueStr, ok := value.(string); ok && valueStr == "true" {+ return true+ }+ if valueBool, ok := value.(bool); ok && valueBool {+ return true+ }+ }+ }++ return false+}++// extractImageReferences extracts all image references from compose content+func (s *AutoUpdateService) extractImageReferences(composeContent string) []string {+ var composeData map[string]interface{}+ if err := yaml.Unmarshal([]byte(composeContent), &composeData); err != nil {+ log.Printf("Error parsing compose content: %v", err)+ return []string{}+ }++ var images []string+ services, ok := composeData["services"].(map[string]interface{})+ if !ok {+ return images+ }++ for _, service := range services {+ serviceMap, ok := service.(map[string]interface{})+ if !ok {+ continue+ }++ if imageVal, exists := serviceMap["image"]; exists {+ if imageStr, ok := imageVal.(string); ok {+ images = append(images, strings.TrimSpace(imageStr))+ }+ }+ }++ return s.removeDuplicates(images)+}++// extractRegistryHost extracts the registry hostname from an image reference+func (s *AutoUpdateService) extractRegistryHost(imageRef string) string {+ parts := strings.Split(imageRef, "@")+ if len(parts) > 1 {+ imageRef = parts[0]+ }++ parts = strings.Split(imageRef, ":")+ if len(parts) > 1 {+ imageRef = parts[0]+ }++ parts = strings.Split(imageRef, "/")++ if len(parts) == 1 {+ return "docker.io"+ }++ firstPart := parts[0]+ if strings.Contains(firstPart, ".") || strings.Contains(firstPart, ":") {+ return firstPart+ }++ return "docker.io"+}++// isRegistryMatch checks if a credential URL matches a registry host+func (s *AutoUpdateService) isRegistryMatch(credURL, registryHost string) bool {+ normalizedCred := s.normalizeRegistryForComparison(credURL)+ normalizedHost := s.normalizeRegistryForComparison(registryHost)++ return normalizedCred == normalizedHost+}++// normalizeRegistryForComparison normalizes registry URLs for comparison+func (s *AutoUpdateService) normalizeRegistryForComparison(url string) string {+ url = strings.TrimPrefix(url, "https://")+ url = strings.TrimPrefix(url, "http://")+ url = strings.TrimSuffix(url, "/")++ if url == "docker.io" || url == "registry-1.docker.io" || url == "index.docker.io" {+ return "docker.io"+ }++ return url+}++// normalizeRegistryURL normalizes registry URL for Docker client+func (s *AutoUpdateService) normalizeRegistryURL(url string) string {+ normalized := s.normalizeRegistryForComparison(url)+ if normalized == "docker.io" {+ return "https://index.docker.io/v1/"+ }++ // For other registries, return raw hostname without protocol+ result := strings.TrimPrefix(url, "https://")+ result = strings.TrimPrefix(result, "http://")+ result = strings.TrimSuffix(result, "/")++ return result+}++// isDigestBasedImage checks if an image reference uses a digest+func (s *AutoUpdateService) isDigestBasedImage(imageRef string) bool {+ matched, _ := regexp.MatchString(`@sha256:[a-f0-9]{64}`, imageRef)+ return matched+}++// getImageID gets the ID of a local image+func (s *AutoUpdateService) getImageID(ctx context.Context, imageRef string) (string, error) {+ dockerClient, err := s.dockerService.CreateConnection(ctx)+ if err != nil {+ return "", fmt.Errorf("failed to connect to Docker: %w", err)+ }+ defer dockerClient.Close()++ images, err := dockerClient.ImageList(ctx, image.ListOptions{})+ if err != nil {+ return "", fmt.Errorf("failed to list images: %w", err)+ }++ for _, img := range images {+ for _, tag := range img.RepoTags {+ if tag == imageRef {+ return img.ID, nil+ }+ }+ }++ return "", fmt.Errorf("image not found: %s", imageRef)+}++// getContainerName gets a friendly name for a container+func (s *AutoUpdateService) getContainerName(container types.Container) string {+ if len(container.Names) > 0 {+ name := container.Names[0]+ if strings.HasPrefix(name, "/") {+ return name[1:]+ }+ return name+ }+ return container.ID[:12]+}++// removeDuplicates removes duplicate strings from a slice+func (s *AutoUpdateService) removeDuplicates(items []string) []string {+ seen := make(map[string]bool)+ var result []string++ for _, item := range items {+ if !seen[item] {+ seen[item] = true+ result = append(result, item)+ }+ }++ return result+}++// GetUpdateStatus returns the current update status+func (s *AutoUpdateService) GetUpdateStatus() map[string]interface{} {+ s.mutex.RLock()+ defer s.mutex.RUnlock()++ return map[string]interface{}{+ "updating_containers": len(s.updatingContainers),+ "updating_stacks": len(s.updatingStacks),+ "container_ids": s.getKeys(s.updatingContainers),+ "stack_ids": s.getKeys(s.updatingStacks),+ }+}++// getKeys returns the keys of a map+func (s *AutoUpdateService) getKeys(m map[string]bool) []string {+ keys := make([]string, 0, len(m))+ for k := range m {+ keys = append(keys, k)+ }+ return keys+}diff --git a/frontend/src/lib/components/ui/alert/alert.svelte b/frontend/src/lib/components/ui/alert/alert.svelteindex 8da77dbe1..85d6631a4 100644--- a/frontend/src/lib/components/ui/alert/alert.svelte+++ b/frontend/src/lib/components/ui/alert/alert.svelte@@ -6,6 +6,7 @@variants: {variant: {default: 'bg-background text-foreground',+ warning: 'border-amber-600/50 text-amber-300 dark:border-amber-600 [&>svg]:text-amber-300',destructive:'border-destructive/50 text-destructive dark:border-destructive [&>svg]:text-destructive'}diff --git a/frontend/src/lib/services/api/autoupdate-api-service.ts b/frontend/src/lib/services/api/autoupdate-api-service.tsnew file mode 100644index 000000000..1c6fa414d--- /dev/null+++ b/frontend/src/lib/services/api/autoupdate-api-service.ts@@ -0,0 +1,17 @@+import BaseAPIService from './api-service';++export default class AutoUpdateAPIService extends BaseAPIService {+ async checkContainers(): Promise<boolean> {+ const response = await this.api.post('/updates/check/containers');+ return response.data;+ }++ async checkStack(): Promise<boolean> {+ const response = await this.api.post('/updates/check/compose');+ return response.data;+ }++ async getStatus(): Promise<void> {+ await this.api.get('/updates/status');+ }+}diff --git a/frontend/src/lib/services/api/index.ts b/frontend/src/lib/services/api/index.tsindex 5bde181b7..de5cda632 100644--- a/frontend/src/lib/services/api/index.ts+++ b/frontend/src/lib/services/api/index.ts@@ -17,6 +17,7 @@ import ValidationAPIService from './validation-api-service';import AppConfigAPIService from './appconfig-api-service';import AgentAPIService from './agent-api-service';import AgentStackAPIService from './agent-stack-api-service';+import AutoUpdateAPIService from './autoupdate-api-service';import ConverterAPIService from './converter-api-service';import ContainerRegistryAPIService from './container-registry-api-service';@@ -41,6 +42,7 @@ export const agentAPI = new AgentAPIService();export const agentStackAPI = new AgentStackAPIService();export const converterAPI = new ConverterAPIService();export const containerRegistryAPI = new ContainerRegistryAPIService();+export const autoUpdateAPI = new AutoUpdateAPIService();interface APIServices {container: ContainerAPIService;@@ -63,6 +65,7 @@ interface APIServices {agent: AgentAPIService;converter: ConverterAPIService;containerRegistry: ContainerRegistryAPIService;+ autoUpdate: AutoUpdateAPIService;}const apiServices: APIServices = {@@ -85,7 +88,8 @@ const apiServices: APIServices = {appConfig: appConfigAPI,agent: agentAPI,converter: converterAPI,- containerRegistry: containerRegistryAPI+ containerRegistry: containerRegistryAPI,+ autoUpdate: autoUpdateAPI};export default apiServices;diff --git a/frontend/src/lib/types/actions.type.ts b/frontend/src/lib/types/actions.type.tsindex 90fc24970..c5ee4c2b9 100644--- a/frontend/src/lib/types/actions.type.ts+++ b/frontend/src/lib/types/actions.type.ts@@ -6,6 +6,7 @@ export type StackActions =| 'import'| 'destroy'| 'pull'- | 'migrate';+ | 'migrate'+ | 'update';export type ContainerActions = 'start' | 'stop' | 'restart' | 'pull' | 'remove';export type PruneType = 'containers' | 'images' | 'networks' | 'volumes';diff --git a/frontend/src/routes/compose/+page.svelte b/frontend/src/routes/compose/+page.svelteindex 4f9217fb3..63f0b60d8 100644--- a/frontend/src/routes/compose/+page.svelte+++ b/frontend/src/routes/compose/+page.svelte@@ -31,26 +31,27 @@import ArcaneButton from '$lib/components/arcane-button.svelte';import { tablePersistence } from '$lib/stores/table-store';import { formatFriendlyDate } from '$lib/utils/date.utils';+ import { autoUpdateAPI } from '$lib/services/api';let { data }: { data: PageData } = $props();let stacks = $derived(Array.isArray(data.stacks) ? data.stacks : []);const isLoading = $state<- Record<'start' | 'stop' | 'restart' | 'remove' | 'destroy' | 'pull', boolean>+ Record<'start' | 'stop' | 'restart' | 'remove' | 'destroy' | 'pull' | 'update', boolean>>({start: false,stop: false,restart: false,remove: false,destroy: false,- pull: false+ pull: false,+ update: false});const isAnyLoading = $derived(Object.values(isLoading).some((loading) => loading));const stackApi = new StackAPIService();- // Simplified derived values - only for managed stacksconst totalStacks = $derived(stacks.length);const runningStacks = $derived(stacks.filter((s) => s.status === 'running').length);const stoppedStacks = $derived(stacks.filter((s) => s.status === 'stopped').length);@@ -126,6 +127,19 @@isLoading[action as keyof typeof isLoading] = false;}}++ async function handleCheckForUpdates() {+ isLoading.update = true;+ handleApiResultWithCallbacks({+ result: await tryCatch(autoUpdateAPI.checkStack()),+ message: 'Failed to Check Compose Projects for Updates',+ setLoadingState: (value) => (isLoading.update = value),+ async onSuccess() {+ toast.success('Compose Projects Updated Successfully.');+ await invalidateAll();+ }+ });+ }</script><div class="space-y-6">@@ -189,11 +203,21 @@<Card.Title>Compose Projects List</Card.Title></div><div class="flex items-center gap-2">- <ArcaneButton- action="create"- customLabel="Create Compose Project"- onClick={() => goto(`/compose/new`)}- />+ {#if stacks.length > 0}+ <ArcaneButton+ action="inspect"+ label="Update Compose Projects"+ onClick={() => handleCheckForUpdates()}+ loading={isLoading.update}+ loadingLabel="Updating..."+ disabled={isLoading.update}+ />+ <ArcaneButton+ action="create"+ customLabel="Create Compose Project"+ onClick={() => goto(`/compose/new`)}+ />+ {/if}</div></div></Card.Header>@@ -315,7 +339,7 @@<DropdownMenu.Separator /><DropdownMenu.Item- class="text-red-500 focus:text-red-700!"+ class="focus:text-red-700! text-red-500"onclick={() => performStackAction('destroy', item.id)}disabled={isLoading.remove || isAnyLoading}>@@ -335,14 +359,14 @@{:else if !data.error}<div class="flex flex-col items-center justify-center px-6 py-12 text-center"><FileStack class="text-muted-foreground mb-4 size-12 opacity-40" />- <p class="text-lg font-medium">No stacks found</p>+ <p class="text-lg font-medium">No Projects found</p><p class="text-muted-foreground mt-1 max-w-md text-sm">- Create a new stack using the "Create Stack" button above+ Create a new stack using the "Create Project" button above</p><div class="mt-4 flex gap-3"><ArcaneButtonaction="create"- customLabel="Create Stack"+ customLabel="Create Project"onClick={() => goto(`/compose/new`)}size="sm"/>diff --git a/frontend/src/routes/compose/new/+page.svelte b/frontend/src/routes/compose/new/+page.svelteindex e5e61dc94..8dfccc840 100644--- a/frontend/src/routes/compose/new/+page.svelte+++ b/frontend/src/routes/compose/new/+page.svelte@@ -73,7 +73,7 @@toast.success(`Stack "${name}" created successfully.`);await invalidateAll();// Use the returned stack ID instead of name for navigation- goto(`/compose/${data.stack?.id || name}`);+ goto(`/compose/${data.stack?.name}`);}});}diff --git a/frontend/src/routes/containers/+page.svelte b/frontend/src/routes/containers/+page.svelteindex ce214d41c..1737e3023 100644--- a/frontend/src/routes/containers/+page.svelte+++ b/frontend/src/routes/containers/+page.svelte@@ -32,6 +32,7 @@import StatCard from '$lib/components/stat-card.svelte';import { parseStatusTime } from '$lib/utils/string.utils';import type { ContainerInfo } from 'dockerode';+ import { autoUpdateAPI } from '$lib/services/api';let { data }: { data: PageData & { containers: ContainerInfo[] } } = $props();let containers = $state(data.containers);@@ -42,7 +43,8 @@start: false,stop: false,restart: false,- remove: false+ remove: false,+ checking: false});const isAnyLoading = $derived(Object.values(isLoading).some((loading) => loading));const runningContainers = $derived(@@ -144,6 +146,19 @@toast.error('An Unknown Error Occurred');}}++ async function handleCheckForUpdates() {+ isLoading.checking = true;+ handleApiResultWithCallbacks({+ result: await tryCatch(autoUpdateAPI.checkContainers()),+ message: 'Failed to Check Containers for Updates',+ setLoadingState: (value) => (isLoading.checking = value),+ async onSuccess() {+ toast.success('Containers Updated Successfully.');+ await invalidateAll();+ }+ });+ }</script><div class="space-y-6">@@ -203,6 +218,14 @@<Card.Title>Container List</Card.Title></div><div class="flex items-center gap-2">+ <ArcaneButton+ action="inspect"+ label="Update Containers"+ onClick={() => handleCheckForUpdates()}+ loading={isLoading.checking}+ loadingLabel="Updating..."+ disabled={isLoading.checking}+ /><ArcaneButton action="create" label="Create Container" onClick={openCreateDialog} /></div></div>@@ -253,14 +276,10 @@><Table.Cell>{shortId(item.Id)}</Table.Cell><Table.Cell>{item.Image}</Table.Cell>- <Table.Cell- ><StatusBadge- variant={stateVariant}- text={capitalizeFirstLetter(item.State)}- /></Table.Cell- >+ <Table.Cell>+ <StatusBadge variant={stateVariant} text={capitalizeFirstLetter(item.State)} />+ </Table.Cell><Table.Cell>{item.Status}</Table.Cell>- <!-- Still displays the original status text --><Table.Cell><DropdownMenu.Root><DropdownMenu.Trigger>@@ -322,7 +341,7 @@<DropdownMenu.Separator /><DropdownMenu.Item- class="text-red-500 focus:text-red-700!"+ class="focus:text-red-700! text-red-500"onclick={() => handleRemoveContainer(item.Id)}disabled={isLoading.remove || isAnyLoading}>diff --git a/frontend/src/routes/images/+page.svelte b/frontend/src/routes/images/+page.svelteindex 1836ab175..95ccdf4de 100644--- a/frontend/src/routes/images/+page.svelte+++ b/frontend/src/routes/images/+page.svelte@@ -39,6 +39,7 @@loadImageMaturityBatch} from '$lib/stores/maturity-store';+let { data }: { data: PageData } = $props();let images = $derived(data.images || []);let error = $state(data.error);@@ -411,7 +412,7 @@/><ArcaneButtonaction="inspect"- label="Check Updates"+ label="Recheck Maturitys"onClick={() => handleTriggerBulkMaturityCheck()}loading={isLoading.checking}loadingLabel="Checking..."diff --git a/frontend/src/routes/settings/docker/+page.svelte b/frontend/src/routes/settings/docker/+page.svelteindex 71fdfcebf..7220b6bd6 100644--- a/frontend/src/routes/settings/docker/+page.svelte+++ b/frontend/src/routes/settings/docker/+page.svelte@@ -300,6 +300,19 @@description="How often to check for new images (5-1440 minutes)"/>+ <!-- Rate Limiting Warning -->+ {#if pollingIntervalInput.value < 30}+ <Alert.Root variant="warning">+ <Zap class="size-4" />+ <Alert.Title>Rate Limiting Warning</Alert.Title>+ <Alert.Description>+ Polling intervals below 30 minutes may trigger rate limits on Docker registries,+ potentially blocking your account temporarily. Consider using longer intervals for+ production environments.+ </Alert.Description>+ </Alert.Root>+ {/if}+<!-- Auto Update Toggle --><divclass="from-background flex items-center justify-between rounded-lg border bg-gradient-to-r to-amber-50/50 p-4"