Skip to content

Commit 60c8f6f

Browse files
committed
fix: reassign port in caddy when container gets new port
1 parent 2b31e8e commit 60c8f6f

File tree

2 files changed

+83
-45
lines changed

2 files changed

+83
-45
lines changed
Lines changed: 64 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,73 @@
11
package tasks
22

33
import (
4-
"context"
4+
"context"
5+
"fmt"
6+
"strconv"
57

6-
shared_types "github.com/raghavyuva/nixopus-api/internal/types"
8+
"github.com/raghavyuva/caddygo"
9+
"github.com/raghavyuva/nixopus-api/internal/config"
10+
shared_types "github.com/raghavyuva/nixopus-api/internal/types"
711
)
812

913
// HandleReDeploy clones source, builds image using redeploy flags, and atomically updates the container
1014
func (s *TaskService) HandleReDeploy(ctx context.Context, TaskPayload shared_types.TaskPayload) error {
11-
taskCtx := s.NewTaskContext(TaskPayload)
12-
13-
taskCtx.LogAndUpdateStatus("Starting redeploy process", shared_types.Cloning)
14-
15-
repoPath, err := s.Clone(CloneConfig{
16-
TaskPayload: TaskPayload,
17-
DeploymentType: string(shared_types.DeploymentTypeReDeploy),
18-
TaskContext: taskCtx,
19-
})
20-
if err != nil {
21-
taskCtx.LogAndUpdateStatus("Failed to clone repository: "+err.Error(), shared_types.Failed)
22-
return err
23-
}
24-
25-
taskCtx.LogAndUpdateStatus("Repository cloned successfully", shared_types.Building)
26-
taskCtx.AddLog("Building image from Dockerfile " + repoPath + " for application " + TaskPayload.Application.Name)
27-
28-
buildImageResult, err := s.BuildImage(BuildConfig{
29-
TaskPayload: TaskPayload,
30-
ContextPath: repoPath,
31-
Force: TaskPayload.UpdateOptions.Force,
32-
ForceWithoutCache: TaskPayload.UpdateOptions.ForceWithoutCache,
33-
TaskContext: taskCtx,
34-
})
35-
if err != nil {
36-
taskCtx.LogAndUpdateStatus("Failed to build image: "+err.Error(), shared_types.Failed)
37-
return err
38-
}
39-
40-
taskCtx.AddLog("Image built successfully: " + buildImageResult + " for application " + TaskPayload.Application.Name)
41-
taskCtx.UpdateStatus(shared_types.Deploying)
42-
43-
containerResult, err := s.AtomicUpdateContainer(TaskPayload, taskCtx)
44-
if err != nil {
45-
taskCtx.LogAndUpdateStatus("Failed to update container: "+err.Error(), shared_types.Failed)
46-
return err
47-
}
48-
49-
taskCtx.AddLog("Container updated successfully for application " + TaskPayload.Application.Name + " with container id " + containerResult.ContainerID)
50-
taskCtx.LogAndUpdateStatus("Redeploy completed successfully", shared_types.Deployed)
51-
52-
return nil
53-
}
15+
taskCtx := s.NewTaskContext(TaskPayload)
16+
17+
taskCtx.LogAndUpdateStatus("Starting redeploy process", shared_types.Cloning)
18+
19+
repoPath, err := s.Clone(CloneConfig{
20+
TaskPayload: TaskPayload,
21+
DeploymentType: string(shared_types.DeploymentTypeReDeploy),
22+
TaskContext: taskCtx,
23+
})
24+
if err != nil {
25+
taskCtx.LogAndUpdateStatus("Failed to clone repository: "+err.Error(), shared_types.Failed)
26+
return err
27+
}
28+
29+
taskCtx.LogAndUpdateStatus("Repository cloned successfully", shared_types.Building)
30+
taskCtx.AddLog("Building image from Dockerfile " + repoPath + " for application " + TaskPayload.Application.Name)
31+
32+
buildImageResult, err := s.BuildImage(BuildConfig{
33+
TaskPayload: TaskPayload,
34+
ContextPath: repoPath,
35+
Force: TaskPayload.UpdateOptions.Force,
36+
ForceWithoutCache: TaskPayload.UpdateOptions.ForceWithoutCache,
37+
TaskContext: taskCtx,
38+
})
39+
if err != nil {
40+
taskCtx.LogAndUpdateStatus("Failed to build image: "+err.Error(), shared_types.Failed)
41+
return err
42+
}
43+
44+
taskCtx.AddLog("Image built successfully: " + buildImageResult + " for application " + TaskPayload.Application.Name)
45+
taskCtx.UpdateStatus(shared_types.Deploying)
5446

47+
containerResult, err := s.AtomicUpdateContainer(TaskPayload, taskCtx)
48+
if err != nil {
49+
taskCtx.LogAndUpdateStatus("Failed to update container: "+err.Error(), shared_types.Failed)
50+
return err
51+
}
52+
53+
taskCtx.AddLog("Container updated successfully for application " + TaskPayload.Application.Name + " with container id " + containerResult.ContainerID)
54+
taskCtx.LogAndUpdateStatus("Redeploy completed successfully", shared_types.Deployed)
55+
56+
client := GetCaddyClient()
57+
port, err := strconv.Atoi(containerResult.AvailablePort)
58+
if err != nil {
59+
taskCtx.LogAndUpdateStatus("Failed to convert port to int: "+err.Error(), shared_types.Failed)
60+
return err
61+
}
62+
upstreamHost := config.AppConfig.SSH.Host
63+
64+
err = client.AddDomainWithAutoTLS(TaskPayload.Application.Domain, upstreamHost, port, caddygo.DomainOptions{})
65+
if err != nil {
66+
fmt.Println("Failed to add domain: ", err)
67+
taskCtx.LogAndUpdateStatus("Failed to add domain: "+err.Error(), shared_types.Failed)
68+
return err
69+
}
70+
client.Reload()
71+
72+
return nil
73+
}

api/internal/features/deploy/tasks/update.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ package tasks
33
import (
44
"context"
55
"fmt"
6+
"strconv"
67

8+
"github.com/raghavyuva/caddygo"
9+
"github.com/raghavyuva/nixopus-api/internal/config"
710
"github.com/google/uuid"
811
"github.com/raghavyuva/nixopus-api/internal/features/deploy/types"
912
shared_types "github.com/raghavyuva/nixopus-api/internal/types"
@@ -81,5 +84,21 @@ func (s *TaskService) HandleUpdateDeployment(ctx context.Context, TaskPayload sh
8184
taskCtx.AddLog("Container updated successfully for application " + TaskPayload.Application.Name + " with container id " + containerResult.ContainerID)
8285
taskCtx.LogAndUpdateStatus("Deployment completed successfully", shared_types.Deployed)
8386

87+
client := GetCaddyClient()
88+
port, err := strconv.Atoi(containerResult.AvailablePort)
89+
if err != nil {
90+
taskCtx.LogAndUpdateStatus("Failed to convert port to int: "+err.Error(), shared_types.Failed)
91+
return err
92+
}
93+
upstreamHost := config.AppConfig.SSH.Host
94+
95+
err = client.AddDomainWithAutoTLS(TaskPayload.Application.Domain, upstreamHost, port, caddygo.DomainOptions{})
96+
if err != nil {
97+
fmt.Println("Failed to add domain: ", err)
98+
taskCtx.LogAndUpdateStatus("Failed to add domain: "+err.Error(), shared_types.Failed)
99+
return err
100+
}
101+
client.Reload()
102+
84103
return nil
85104
}

0 commit comments

Comments
 (0)