Skip to content

Modern Docker Management, Designed for Everyone

fix: projects not being deleted on remote agents (#1047)

Kyle Mendella612ab3fde65
diff --git a/backend/internal/huma/handlers/environments.go b/backend/internal/huma/handlers/environments.go
index 646336ce2..defac375f 100644
--- a/backend/internal/huma/handlers/environments.go
+++ b/backend/internal/huma/handlers/environments.go
@@ -80,8 +80,8 @@ type DeleteEnvironmentOutput struct {
}
type TestConnectionInput struct {
- ID string `path:"id" doc:"Environment ID"`
- Body environment.TestConnectionRequest
+ ID string `path:"id" doc:"Environment ID"`
+ Body *environment.TestConnectionRequest `json:"body,omitempty"`
}
type TestConnectionOutput struct {
@@ -97,8 +97,8 @@ type UpdateHeartbeatOutput struct {
}
type PairAgentInput struct {
- ID string `path:"id" doc:"Environment ID (must be 0 for local)"`
- Body environment.AgentPairRequest
+ ID string `path:"id" doc:"Environment ID (must be 0 for local)"`
+ Body *environment.AgentPairRequest `json:"body,omitempty"`
}
type PairAgentOutput struct {
@@ -420,7 +420,12 @@ func (h *EnvironmentHandler) TestConnection(ctx context.Context, input *TestConn
return nil, huma.Error500InternalServerError("service not available")
}
- status, err := h.environmentService.TestConnection(ctx, input.ID, input.Body.ApiUrl)
+ var apiUrl *string
+ if input.Body != nil {
+ apiUrl = input.Body.ApiUrl
+ }
+
+ status, err := h.environmentService.TestConnection(ctx, input.ID, apiUrl)
resp := environment.Test{Status: status}
if err != nil {
msg := err.Error()
@@ -471,7 +476,8 @@ func (h *EnvironmentHandler) PairAgent(ctx context.Context, input *PairAgentInpu
return nil, huma.Error404NotFound("Not found")
}
- if h.cfg.AgentToken == "" || (input.Body.Rotate != nil && *input.Body.Rotate) {
+ shouldRotate := input.Body != nil && input.Body.Rotate != nil && *input.Body.Rotate
+ if h.cfg.AgentToken == "" || shouldRotate {
h.cfg.AgentToken = utils.GenerateRandomString(48)
}
diff --git a/backend/internal/huma/handlers/projects.go b/backend/internal/huma/handlers/projects.go
index b63ba3dfd..1fd151e0e 100644
--- a/backend/internal/huma/handlers/projects.go
+++ b/backend/internal/huma/handlers/projects.go
@@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
+ "log/slog"
"net/http"
"time"
@@ -544,6 +545,13 @@ func (h *ProjectHandler) DestroyProject(ctx context.Context, input *DestroyProje
if input.Body != nil {
removeFiles = input.Body.RemoveFiles
removeVolumes = input.Body.RemoveVolumes
+ slog.DebugContext(ctx, "DestroyProject handler received body",
+ "removeFiles", removeFiles,
+ "removeVolumes", removeVolumes,
+ "projectID", input.ProjectID)
+ } else {
+ slog.DebugContext(ctx, "DestroyProject handler received nil body",
+ "projectID", input.ProjectID)
}
if err := h.projectService.DestroyProject(ctx, input.ProjectID, removeFiles, removeVolumes, *user); err != nil {
diff --git a/backend/internal/middleware/environment_middleware.go b/backend/internal/middleware/environment_middleware.go
index 1a573ffea..03e158ee7 100644
--- a/backend/internal/middleware/environment_middleware.go
+++ b/backend/internal/middleware/environment_middleware.go
@@ -1,6 +1,7 @@
package middleware
import (
+ "bytes"
"context"
"fmt"
"io"
@@ -182,7 +183,27 @@ func (m *EnvironmentMiddleware) proxyHTTP(c *gin.Context, target string, accessT
// createProxyRequest builds the HTTP request to forward to the remote environment.
func (m *EnvironmentMiddleware) createProxyRequest(c *gin.Context, target string, accessToken *string) (*http.Request, error) {
- req, err := http.NewRequestWithContext(c.Request.Context(), c.Request.Method, target, c.Request.Body)
+ // Read the body to log it and then restore it for forwarding
+ var bodyBytes []byte
+ var err error
+ if c.Request.Body != nil {
+ bodyBytes, err = io.ReadAll(c.Request.Body)
+ if err != nil {
+ return nil, fmt.Errorf("failed to read request body: %w", err)
+ }
+ // Restore the body for forwarding
+ c.Request.Body = io.NopCloser(bytes.NewBuffer(bodyBytes))
+ }
+
+ slog.DebugContext(c.Request.Context(), "Creating proxy request",
+ "method", c.Request.Method,
+ "target", target,
+ "contentLength", c.Request.ContentLength,
+ "contentType", c.GetHeader("Content-Type"),
+ "bodyLength", len(bodyBytes),
+ "body", string(bodyBytes))
+
+ req, err := http.NewRequestWithContext(c.Request.Context(), c.Request.Method, target, bytes.NewBuffer(bodyBytes))
if err != nil {
return nil, err
}
@@ -193,6 +214,11 @@ func (m *EnvironmentMiddleware) createProxyRequest(c *gin.Context, target string
remenv.SetAgentToken(req, accessToken)
remenv.SetForwardedHeaders(req, c.ClientIP(), c.Request.Host)
+ // Set Content-Length based on actual body size
+ if len(bodyBytes) > 0 {
+ req.ContentLength = int64(len(bodyBytes))
+ }
+
return req, nil
}
diff --git a/backend/internal/services/project_service.go b/backend/internal/services/project_service.go
index 4a0eded22..45bf12bdf 100644
--- a/backend/internal/services/project_service.go
+++ b/backend/internal/services/project_service.go
@@ -722,11 +722,22 @@ func (s *ProjectService) CreateProject(ctx context.Context, name, composeContent
}
func (s *ProjectService) DestroyProject(ctx context.Context, projectID string, removeFiles, removeVolumes bool, user models.User) error {
+ slog.DebugContext(ctx, "DestroyProject service called",
+ "projectID", projectID,
+ "removeFiles", removeFiles,
+ "removeVolumes", removeVolumes,
+ "userID", user.ID,
+ "username", user.Username)
+
proj, err := s.GetProjectFromDatabaseByID(ctx, projectID)
if err != nil {
return err
}
+ slog.DebugContext(ctx, "Found project to destroy",
+ "projectName", proj.Name,
+ "projectPath", proj.Path)
+
if err := s.DownProject(ctx, projectID, systemUser); err != nil {
slog.WarnContext(ctx, "failed to bring down project", "error", err)
}
@@ -750,9 +761,14 @@ func (s *ProjectService) DestroyProject(ctx context.Context, projectID string, r
}
if removeFiles {
+ slog.DebugContext(ctx, "Removing project files", "path", proj.Path)
if err := os.RemoveAll(proj.Path); err != nil {
+ slog.ErrorContext(ctx, "Failed to remove project files", "path", proj.Path, "error", err)
return fmt.Errorf("failed to remove project files: %w", err)
}
+ slog.InfoContext(ctx, "Project files removed successfully", "path", proj.Path)
+ } else {
+ slog.DebugContext(ctx, "Skipping file removal (removeFiles=false)", "path", proj.Path)
}
if err := s.db.WithContext(ctx).Delete(proj).Error; err != nil {
diff --git a/backend/internal/utils/fs/fs_writer.go b/backend/internal/utils/fs/fs_writer.go
index 079519f70..307d01f5b 100644
--- a/backend/internal/utils/fs/fs_writer.go
+++ b/backend/internal/utils/fs/fs_writer.go
@@ -103,17 +103,22 @@ func WriteEnvFile(projectsRoot, dirPath, content string) error {
return nil
}
-// WriteProjectFiles writes both compose and optional env files to a project directory
+// WriteProjectFiles writes both compose and env files to a project directory.
+// An empty .env file is always created to prevent compose-go from failing when
+// the compose file references env_file: .env
// projectsRoot is the allowed root directory to prevent path traversal attacks
func WriteProjectFiles(projectsRoot, dirPath, composeContent string, envContent *string) error {
if err := WriteComposeFile(projectsRoot, dirPath, composeContent); err != nil {
return err
}
- if envContent != nil && *envContent != "" {
- if err := WriteEnvFile(projectsRoot, dirPath, *envContent); err != nil {
- return err
- }
+ // Always create a .env file, even if empty, to satisfy compose files that reference env_file: .env
+ envValue := ""
+ if envContent != nil {
+ envValue = *envContent
+ }
+ if err := WriteEnvFile(projectsRoot, dirPath, envValue); err != nil {
+ return err
}
return nil