Modern Docker Management, Designed for Everyone
Go composecontainer-managementcontainersdockerdocker-composedocker-managementgoself-hostedsveltekittypescriptweb-ui
fix: stack logic (#313)
Kyle Mendellb53caa2e233d
diff --git a/backend/internal/api/stack_handler.go b/backend/internal/api/stack_handler.goindex 8de017ee9..de2895084 100644--- a/backend/internal/api/stack_handler.go+++ b/backend/internal/api/stack_handler.go@@ -1,6 +1,7 @@package apiimport (+ "context""fmt""io""net/http"@@ -111,6 +112,9 @@ func (h *StackHandler) CreateStack(c *gin.Context) {func (h *StackHandler) GetStack(c *gin.Context) {stackID := c.Param("stackId")+ if stackID == "" {+ stackID = c.Param("id")+ }stack, err := h.stackService.GetStackByID(c.Request.Context(), stackID)if err != nil {@@ -171,7 +175,10 @@ func (h *StackHandler) GetStack(c *gin.Context) {}func (h *StackHandler) UpdateStack(c *gin.Context) {- stackID := c.Param("id")+ stackID := c.Param("stackId")+ if stackID == "" {+ stackID = c.Param("id")+ }var req dto.UpdateStackDtoif err := c.ShouldBindJSON(&req); err != nil {@@ -256,7 +263,10 @@ func (h *StackHandler) UpdateStack(c *gin.Context) {}func (h *StackHandler) DeleteStack(c *gin.Context) {- stackID := c.Param("id")+ stackID := c.Param("stackId")+ if stackID == "" {+ stackID = c.Param("id")+ }err := h.stackService.DeleteStack(c.Request.Context(), stackID)if err != nil {@@ -274,13 +284,23 @@ func (h *StackHandler) DeleteStack(c *gin.Context) {}func (h *StackHandler) StartStack(c *gin.Context) {- stackID := c.Param("id")+ stackID := c.Param("stackId")+ if stackID == "" {+ stackID = c.Param("id")+ }- err := h.stackService.UpdateStackStatus(c.Request.Context(), stackID, models.StackStatusRunning)- if err != nil {- c.JSON(http.StatusInternalServerError, gin.H{+ if stackID == "" {+ c.JSON(http.StatusBadRequest, gin.H{"success": false,- "error": "Failed to start stack",+ "error": "Stack ID is required",+ })+ return+ }++ if err := h.stackService.DeployStack(c.Request.Context(), stackID); err != nil {+ c.JSON(http.StatusBadRequest, gin.H{+ "success": false,+ "error": err.Error(),})return}@@ -292,7 +312,10 @@ func (h *StackHandler) StartStack(c *gin.Context) {}func (h *StackHandler) StopStack(c *gin.Context) {- stackID := c.Param("id")+ stackID := c.Param("stackId")+ if stackID == "" {+ stackID = c.Param("id")+ }if err := h.stackService.StopStack(c.Request.Context(), stackID); err != nil {c.JSON(http.StatusInternalServerError, gin.H{@@ -309,17 +332,30 @@ func (h *StackHandler) StopStack(c *gin.Context) {}func (h *StackHandler) RestartStack(c *gin.Context) {- stackID := c.Param("id")+ stackID := c.Param("stackId")+ if stackID == "" {+ stackID = c.Param("id")+ }++ if err := h.stackService.RestartStack(c.Request.Context(), stackID); err != nil {+ c.JSON(http.StatusBadRequest, gin.H{+ "success": false,+ "error": err.Error(),+ })+ return+ }c.JSON(http.StatusOK, gin.H{"success": true,"message": "Stack restarted successfully",- "stackId": stackID,})}func (h *StackHandler) RedeployStack(c *gin.Context) {- stackID := c.Param("id")+ stackID := c.Param("stackId")+ if stackID == "" {+ stackID = c.Param("id")+ }var req dto.RedeployStackDtoif err := c.ShouldBindJSON(&req); err != nil {@@ -344,7 +380,10 @@ func (h *StackHandler) RedeployStack(c *gin.Context) {}func (h *StackHandler) DownStack(c *gin.Context) {- stackID := c.Param("id")+ stackID := c.Param("stackId")+ if stackID == "" {+ stackID = c.Param("id")+ }if err := h.stackService.DownStack(c.Request.Context(), stackID); err != nil {c.JSON(http.StatusInternalServerError, gin.H{@@ -361,7 +400,10 @@ func (h *StackHandler) DownStack(c *gin.Context) {}func (h *StackHandler) DestroyStack(c *gin.Context) {- stackID := c.Param("id")+ stackID := c.Param("stackId")+ if stackID == "" {+ stackID = c.Param("id")+ }var req dto.DestroyStackDtoif err := c.ShouldBindJSON(&req); err != nil {@@ -386,7 +428,7 @@ func (h *StackHandler) DestroyStack(c *gin.Context) {}func (h *StackHandler) PullStack(c *gin.Context) {- stackID := c.Param("id")+ stackID := c.Param("stackId")c.JSON(http.StatusOK, gin.H{"success": true,@@ -396,7 +438,10 @@ func (h *StackHandler) PullStack(c *gin.Context) {}func (h *StackHandler) DeployStack(c *gin.Context) {- stackID := c.Param("id")+ stackID := c.Param("stackId")+ if stackID == "" {+ stackID = c.Param("id")+ }var req struct {Profiles []string `json:"profiles"`@@ -427,7 +472,10 @@ func (h *StackHandler) DeployStack(c *gin.Context) {}func (h *StackHandler) GetStackServices(c *gin.Context) {- stackID := c.Param("id")+ stackID := c.Param("stackId")+ if stackID == "" {+ stackID = c.Param("id")+ }services, err := h.stackService.GetStackServices(c.Request.Context(), stackID)if err != nil {@@ -445,7 +493,10 @@ func (h *StackHandler) GetStackServices(c *gin.Context) {}func (h *StackHandler) PullImages(c *gin.Context) {- stackID := c.Param("id")+ stackID := c.Param("stackId")+ if stackID == "" {+ stackID = c.Param("id")+ }if err := h.stackService.PullStackImages(c.Request.Context(), stackID); err != nil {c.JSON(http.StatusInternalServerError, gin.H{@@ -502,58 +553,46 @@ func (h *StackHandler) ConvertDockerRun(c *gin.Context) {func (h *StackHandler) GetStackLogsStream(c *gin.Context) {stackID := c.Param("id")if stackID == "" {- c.JSON(http.StatusBadRequest, gin.H{- "success": false,- "error": "Stack ID is required",- })- return+ stackID = c.Param("stackId")}- // Get query parameters for log options- follow := c.DefaultQuery("follow", "true") == "true"+ follow := c.Query("follow") == "true"tail := c.DefaultQuery("tail", "100")since := c.Query("since")- timestamps := c.DefaultQuery("timestamps", "true") == "true"+ timestamps := c.Query("timestamps") == "true"- // Set headers for SSEc.Header("Content-Type", "text/event-stream")c.Header("Cache-Control", "no-cache")c.Header("Connection", "keep-alive")c.Header("Access-Control-Allow-Origin", "*")- logsChan := make(chan string, 100) // Larger buffer for stack logs- errChan := make(chan error, 1)+ logsChan := make(chan string, 100)+ ctx, cancel := context.WithCancel(c.Request.Context())+ defer cancel()- // Start streaming logs in a goroutinego func() {defer close(logsChan)- defer close(errChan)-- err := h.stackService.StreamStackLogs(c.Request.Context(), stackID, logsChan, follow, tail, since, timestamps)- if err != nil {- errChan <- err+ if err := h.stackService.StreamStackLogs(ctx, stackID, logsChan, follow, tail, since, timestamps); err != nil {+ select {+ case logsChan <- fmt.Sprintf("Error streaming logs: %v", err):+ case <-ctx.Done():+ }}}()- // Send logs to clientc.Stream(func(w io.Writer) bool {select {- case logLine, ok := <-logsChan:+ case log, ok := <-logsChan:if !ok {return false}-- // Parse the log line to extract service name and format as JSON- logData := h.parseStackLogLine(logLine)- c.SSEvent("log", logData)+ c.SSEvent("log", gin.H{"message": log})return true-- case err := <-errChan:- c.SSEvent("error", gin.H{"error": err.Error()})- return false-- case <-c.Request.Context().Done():+ case <-ctx.Done():return false+ case <-time.After(30 * time.Second):+ c.SSEvent("ping", gin.H{"message": "keepalive"})+ return true}})}diff --git a/backend/internal/bootstrap/jobs_bootstrap.go b/backend/internal/bootstrap/jobs_bootstrap.goindex 459528fe4..496db9a05 100644--- a/backend/internal/bootstrap/jobs_bootstrap.go+++ b/backend/internal/bootstrap/jobs_bootstrap.go@@ -25,4 +25,8 @@ func registerJobs(appCtx context.Context, scheduler *job.Scheduler, appServicesif err := job.RegisterAutoUpdateJob(appCtx, scheduler, appServices.AutoUpdate, appServices.Settings); err != nil {slog.Error("Failed to register auto-update job", slog.Any("error", err))}++ if err := job.RegisterStackSyncJob(appCtx, scheduler, appServices.Stack); err != nil {+ slog.Error("Failed to register stack sync job", slog.Any("error", err))+ }}diff --git a/backend/internal/job/stack_sync_job.go b/backend/internal/job/stack_sync_job.gonew file mode 100644index 000000000..21fdf87ad--- /dev/null+++ b/backend/internal/job/stack_sync_job.go@@ -0,0 +1,68 @@+package job++import (+ "context"+ "fmt"+ "log/slog"+ "time"++ "github.com/go-co-op/gocron/v2"+ "github.com/ofkm/arcane-backend/internal/services"+)++const StackSyncJobName = "StackSync"++func RegisterStackSyncJob(+ ctx context.Context,+ scheduler *Scheduler,+ stackService *services.StackService,+) error {+ slog.Info("Registering stack sync job", "jobName", StackSyncJobName)++ taskFunc := func(jobCtx context.Context) error {+ slog.Info("Running stack sync job", "jobName", StackSyncJobName)++ externals, err := stackService.DiscoverExternalStacks(jobCtx)+ if err != nil {+ slog.Error("Failed to discover external stacks", "jobName", StackSyncJobName, slog.Any("error", err))+ return err+ }++ imported := 0+ for _, external := range externals {+ if external.DirName != nil {+ if _, err := stackService.ImportExternalStack(jobCtx, *external.DirName, external.Name); err != nil {+ slog.Warn("Failed to import external stack",+ "jobName", StackSyncJobName,+ "dirName", *external.DirName,+ slog.Any("error", err))+ } else {+ imported+++ }+ }+ }++ slog.Info("Stack sync job completed",+ "jobName", StackSyncJobName,+ "discovered", len(externals),+ "imported", imported)+ return nil+ }++ jobDefinition := gocron.DurationJob(5 * time.Minute)++ err := scheduler.RegisterJob(+ ctx,+ StackSyncJobName,+ jobDefinition,+ taskFunc,+ true, // Run immediately on startup+ )++ if err != nil {+ return fmt.Errorf("failed to register stack sync job %q: %w", StackSyncJobName, err)+ }++ slog.Info("Stack sync job registered successfully", "jobName", StackSyncJobName)+ return nil+}diff --git a/backend/internal/models/stack.go b/backend/internal/models/stack.goindex b3fea6a17..0efe8c162 100644--- a/backend/internal/models/stack.go+++ b/backend/internal/models/stack.go@@ -38,6 +38,9 @@ const (StackStatusStopped StackStatus = "stopped"StackStatusPartiallyRunning StackStatus = "partially running"StackStatusUnknown StackStatus = "unknown"+ StackStatusDeploying StackStatus = "deploying"+ StackStatusStopping StackStatus = "stopping"+ StackStatusRestarting StackStatus = "restarting")type Stack struct {diff --git a/backend/internal/services/stack_service.go b/backend/internal/services/stack_service.goindex d0653aa7f..4520ee4f3 100644--- a/backend/internal/services/stack_service.go+++ b/backend/internal/services/stack_service.go@@ -97,22 +97,34 @@ func (s *StackService) CreateStack(ctx context.Context, name, composeContent strfunc (s *StackService) DeployStack(ctx context.Context, stackID string) error {stack, err := s.GetStackByID(ctx, stackID)if err != nil {- return err+ return fmt.Errorf("failed to get stack: %w", err)}- cmd := exec.CommandContext(ctx, "docker-compose", "up", "-d")- cmd.Dir = stack.Path+ if _, err := os.Stat(stack.Path); os.IsNotExist(err) {+ return fmt.Errorf("stack directory does not exist: %s", stack.Path)+ }+ composeFileName := s.findComposeFileName(stack.Path)+ if composeFileName == "" {+ return fmt.Errorf("no compose file found in stack directory: %s", stack.Path)+ }++ if err := s.UpdateStackStatus(ctx, stackID, models.StackStatusDeploying); err != nil {+ return fmt.Errorf("failed to update stack status to deploying: %w", err)+ }++ cmd := exec.CommandContext(ctx, "docker-compose", "-f", composeFileName, "up", "-d")+ cmd.Dir = stack.Pathcmd.Env = append(os.Environ(),fmt.Sprintf("COMPOSE_PROJECT_NAME=%s", stack.Name),)output, err := cmd.CombinedOutput()if err != nil {- return fmt.Errorf("failed to deploy stack: %w\nOutput: %s", err, string(output))+ s.UpdateStackStatus(ctx, stackID, models.StackStatusStopped)+ return fmt.Errorf("failed to deploy stack: %w\nCommand output: %s", err, string(output))}- // Update status and countsreturn s.updateStackStatusAndCounts(ctx, stackID, models.StackStatusRunning)}@@ -122,6 +134,16 @@ func (s *StackService) StopStack(ctx context.Context, stackID string) error {return err}+ // Verify stack directory exists+ if _, err := os.Stat(stack.Path); os.IsNotExist(err) {+ return fmt.Errorf("stack directory does not exist: %s", stack.Path)+ }++ // Update status to stopping first+ if err := s.UpdateStackStatus(ctx, stackID, models.StackStatusStopping); err != nil {+ return fmt.Errorf("failed to update stack status to stopping: %w", err)+ }+cmd := exec.CommandContext(ctx, "docker-compose", "stop")cmd.Dir = stack.Pathcmd.Env = append(os.Environ(),@@ -133,7 +155,7 @@ func (s *StackService) StopStack(ctx context.Context, stackID string) error {return fmt.Errorf("failed to stop stack: %w\nOutput: %s", err, string(output))}- // Update status and counts+ // Update status and counts after successful stopreturn s.updateStackStatusAndCounts(ctx, stackID, models.StackStatusStopped)}@@ -143,6 +165,16 @@ func (s *StackService) DownStack(ctx context.Context, stackID string) error {return err}+ // Verify stack directory exists+ if _, err := os.Stat(stack.Path); os.IsNotExist(err) {+ return fmt.Errorf("stack directory does not exist: %s", stack.Path)+ }++ // Update status to stopping first+ if err := s.UpdateStackStatus(ctx, stackID, models.StackStatusStopping); err != nil {+ return fmt.Errorf("failed to update stack status to stopping: %w", err)+ }+cmd := exec.CommandContext(ctx, "docker-compose", "down")cmd.Dir = stack.Pathcmd.Env = append(os.Environ(),@@ -319,6 +351,16 @@ func (s *StackService) RestartStack(ctx context.Context, stackID string) error {return err}+ // Verify stack directory exists+ if _, err := os.Stat(stack.Path); os.IsNotExist(err) {+ return fmt.Errorf("stack directory does not exist: %s", stack.Path)+ }++ // Update status to restarting first+ if err := s.UpdateStackStatus(ctx, stackID, models.StackStatusRestarting); err != nil {+ return fmt.Errorf("failed to update stack status to restarting: %w", err)+ }+cmd := exec.CommandContext(ctx, "docker-compose", "restart")cmd.Dir = stack.Pathcmd.Env = append(os.Environ(),@@ -330,7 +372,8 @@ func (s *StackService) RestartStack(ctx context.Context, stackID string) error {return fmt.Errorf("failed to restart stack: %w\nOutput: %s", err, string(output))}- return nil+ // Update status and counts after restart+ return s.updateStackStatusAndCounts(ctx, stackID, models.StackStatusRunning)}func (s *StackService) PullStackImages(ctx context.Context, stackID string) error {@@ -494,14 +537,74 @@ func (s *StackService) RedeployStack(ctx context.Context, stackID string, profilreturn s.DeployStack(ctx, stackID)}+// DiscoverExternalStacks scans the configured stacks directory and returns any+// folders with a compose file not already tracked in the database.func (s *StackService) DiscoverExternalStacks(ctx context.Context) ([]models.Stack, error) {- // TODO- return []models.Stack{}, nil+ dir, err := s.getStacksDirectory(ctx)+ if err != nil {+ return nil, fmt.Errorf("failed to get stacks directory: %w", err)+ }+ entries, err := os.ReadDir(dir)+ if err != nil {+ return nil, fmt.Errorf("failed to read stacks directory: %w", err)+ }+ var externals []models.Stack+ for _, e := range entries {+ if !e.IsDir() {+ continue+ }+ name := e.Name()+ path := filepath.Join(dir, name)+ if s.findComposeFile(path) == "" {+ continue+ }+ var count int64+ s.db.DB.WithContext(ctx).+ Model(&models.Stack{}).+ Where("path = ?", path).+ Or("dir_name = ?", name).+ Count(&count)+ if count > 0 {+ continue+ }+ externals = append(externals, models.Stack{+ ID: "", // not yet persisted+ Name: name,+ DirName: &name,+ Path: path,+ Status: models.StackStatusUnknown,+ })+ }+ return externals, nil}-func (s *StackService) ImportExternalStack(ctx context.Context, stackID, stackName string) (*models.Stack, error) {- // TODO- return nil, fmt.Errorf("external stack import not implemented yet")+// ImportExternalStack takes a directory name under the stacks dir and a desired+// stack name, then registers it in the database.+func (s *StackService) ImportExternalStack(ctx context.Context, dirName, stackName string) (*models.Stack, error) {+ baseDir, err := s.getStacksDirectory(ctx)+ if err != nil {+ return nil, fmt.Errorf("failed to get stacks directory: %w", err)+ }+ path := filepath.Join(baseDir, dirName)+ if _, err := os.Stat(path); os.IsNotExist(err) {+ return nil, fmt.Errorf("stack directory does not exist: %s", path)+ }+ if s.findComposeFile(path) == "" {+ return nil, fmt.Errorf("no compose file found in %s", path)+ }+ stack := &models.Stack{+ ID: uuid.New().String(),+ Name: stackName,+ DirName: &dirName,+ Path: path,+ Status: models.StackStatusUnknown,+ ServiceCount: 0,+ RunningCount: 0,+ }+ if err := s.db.DB.WithContext(ctx).Create(stack).Error; err != nil {+ return nil, fmt.Errorf("failed to import stack: %w", err)+ }+ return stack, nil}func (s *StackService) ValidateStackCompose(ctx context.Context, composeContent string) error {@@ -673,7 +776,7 @@ func (s *StackService) ConvertDockerRun(ctx context.Context, dockerRunCommand stfunc (s *StackService) GetStackByID(ctx context.Context, id string) (*models.Stack, error) {var stack models.Stack- if err := s.db.WithContext(ctx).Where("id = ?", id).First(&stack).Error; err != nil {+ if err := s.db.DB.WithContext(ctx).Where("id = ?", id).First(&stack).Error; err != nil {if errors.Is(err, gorm.ErrRecordNotFound) {return nil, fmt.Errorf("stack not found")}@@ -770,14 +873,14 @@ func (s *StackService) saveStackFiles(stackPath, composeContent string, envConte}func (s *StackService) findComposeFile(stackDir string) string {- possibleFiles := []string{+ composeFiles := []string{"compose.yaml","compose.yml",- "docker-compose.yml","docker-compose.yaml",+ "docker-compose.yml",}- for _, filename := range possibleFiles {+ for _, filename := range composeFiles {fullPath := filepath.Join(stackDir, filename)if _, err := os.Stat(fullPath); err == nil {return fullPath@@ -787,6 +890,24 @@ func (s *StackService) findComposeFile(stackDir string) string {return ""}+func (s *StackService) findComposeFileName(stackDir string) string {+ composeFiles := []string{+ "compose.yaml",+ "compose.yml",+ "docker-compose.yaml",+ "docker-compose.yml",+ }++ for _, filename := range composeFiles {+ fullPath := filepath.Join(stackDir, filename)+ if _, err := os.Stat(fullPath); err == nil {+ return filename // Return just the filename, not the full path+ }+ }++ return ""+}+func (s *StackService) parseComposePS(output string) ([]StackServiceInfo, error) {if strings.TrimSpace(output) == "" {return []StackServiceInfo{}, nil@@ -891,3 +1012,43 @@ func (s *StackService) getServiceCounts(services []StackServiceInfo) (total int,}return total, running}++// Add a helper method to ensure stack directory integrity+func (s *StackService) validateStackDirectory(ctx context.Context, stackID string) (*models.Stack, error) {+ stack, err := s.GetStackByID(ctx, stackID)+ if err != nil {+ return nil, err+ }++ // Check if stack directory exists+ if _, err := os.Stat(stack.Path); os.IsNotExist(err) {+ // Try to reconstruct path from configured stacks directory+ stacksDir, dirErr := s.getStacksDirectory(ctx)+ if dirErr != nil {+ return nil, fmt.Errorf("stack directory does not exist and cannot get stacks directory: %w", dirErr)+ }++ if stack.DirName != nil {+ reconstructedPath := filepath.Join(stacksDir, *stack.DirName)+ if _, statErr := os.Stat(reconstructedPath); statErr == nil {+ // Update the stack path in database+ stack.Path = reconstructedPath+ if _, updateErr := s.UpdateStack(ctx, stack); updateErr != nil {+ return nil, fmt.Errorf("found stack directory but failed to update path: %w", updateErr)+ }+ } else {+ return nil, fmt.Errorf("stack directory does not exist: %s", stack.Path)+ }+ } else {+ return nil, fmt.Errorf("stack directory does not exist and no directory name stored: %s", stack.Path)+ }+ }++ // Verify compose file exists+ composeFile := s.findComposeFile(stack.Path)+ if composeFile == "" {+ return nil, fmt.Errorf("no compose file found in stack directory: %s", stack.Path)+ }++ return stack, nil+}