Skip to content

Commit

Permalink
fix: container name display and deployment retry after removal in sur…
Browse files Browse the repository at this point in the history
…vey prompt
  • Loading branch information
bnema committed Nov 5, 2024
1 parent b5fdca8 commit 26223e3
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 21 deletions.
25 changes: 13 additions & 12 deletions internal/cli/cmd/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package cmd

import (
"bytes"
"encoding/json"
"errors"
"fmt"
Expand Down Expand Up @@ -115,20 +116,27 @@ func exportDockerImage(imageName string) (io.ReadCloser, int64, error) {
}

func deployImage(a *cli.App, reader io.Reader, imageName, port, targetDomain string) error {
// Convert reader to bytes for reuse
imageData, err := io.ReadAll(reader)
if err != nil {
return fmt.Errorf("failed to read image data: %w", err)
}

log.Info("Attempting to deploy...",
"image", imageName,
"port", port,
"target", targetDomain,
)

for {
// Create new reader from bytes for each attempt
reqPayload := common.RequestPayload{
Type: "deploy",
Payload: common.DeployPayload{
Port: port,
TargetDomain: targetDomain,
ImageName: imageName,
Data: io.NopCloser(reader),
Data: io.NopCloser(bytes.NewReader(imageData)),
},
}

Expand All @@ -138,28 +146,21 @@ func deployImage(a *cli.App, reader io.Reader, imageName, port, targetDomain str
if errors.As(err, &deployErr) {
var deployResponse common.DeployResponse
if jsonErr := json.Unmarshal([]byte(deployErr.RawResponse), &deployResponse); jsonErr == nil {
// Handle existing container case
if deployErr.StatusCode == http.StatusConflict && deployResponse.ContainerID != "" {
if err := handler.HandleExistingContainer(a, &deployResponse); err != nil {
if strings.Contains(err.Error(), "cancelled by user") {
log.Warn("Deployment cancelled by user")
} else {
log.Error("Failed to handle existing container", "error", err)
return nil
}
return nil
log.Error("Failed to handle existing container", "error", err)
return err
}
log.Info("Retrying deployment...")
continue // Retry deployment
}

log.Error("Deployment failed",
"error", deployErr.Message,
"status_code", deployErr.StatusCode,
"container_id", deployResponse.ContainerID,
)
}
}
return nil
return err
}

var deployResponse common.DeployResponse
Expand Down
21 changes: 12 additions & 9 deletions internal/httpserve/handlers/cli_endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,21 +264,24 @@ func PostDeploy(c echo.Context, a *server.App) error {
if err != nil {
if strings.Contains(err.Error(), "is already in use") {
log.Warn("Container already exists", "error", err)
extractedID := extractContainerID(err.Error())
log.Info("Extracted container ID", "containerID", extractedID)
existingContainerID := extractContainerID(err.Error())
existingContainerName, _ := docker.GetContainerName(existingContainerID)
// Remove the / from the container name
existingContainerName = strings.TrimLeft(existingContainerName, "/")

if extractedID == "" {
log.Error("Failed to extract container ID from error message")
if existingContainerID == "" || existingContainerName == "" {
log.Error("Failed to extract container ID or Name from error message")
return sendJSONResponse(c, http.StatusConflict, DeployResponse{
Success: false,
Message: "A container with this name already exists, but we couldn't extract its ID.",
Message: "A container for this deploy already exists but could not be identified.",
})
}

return sendJSONResponse(c, http.StatusConflict, DeployResponse{
Success: false,
Message: "A container for this deploy already exists.",
ContainerID: extractedID,
Success: false,
Message: "A container for this deploy already exists.",
ContainerID: existingContainerID,
ContainerName: existingContainerName,
})
}
log.Error("Failed to create or start container", "error", err)
Expand All @@ -305,7 +308,7 @@ func PostDeploy(c echo.Context, a *server.App) error {
return sendJSONResponse(c, http.StatusOK, response)
}

// is already in use by 08433f639cb1c01254d43f234360bfa793aa8c33b29e8de8ce42b0706c0090ba. example
// extractContainerID extracts the container ID from the error message
func extractContainerID(errorMessage string) string {
re := regexp.MustCompile(`by\s+([0-9a-f]+)\.`)
match := re.FindStringSubmatch(errorMessage)
Expand Down

0 comments on commit 26223e3

Please sign in to comment.