Skip to content

Commit 69f9d86

Browse files
committed
fix(self-host): port mapping to match with what caddy listens as a proxy service
1 parent 1a40289 commit 69f9d86

File tree

2 files changed

+25
-27
lines changed

2 files changed

+25
-27
lines changed

api/internal/features/deploy/service/dockerfile_deployer.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ package service
22

33
import (
44
"fmt"
5-
"strconv"
6-
75
"github.com/raghavyuva/nixopus-api/internal/features/deploy/proxy"
86
"github.com/raghavyuva/nixopus-api/internal/features/deploy/types"
97
"github.com/raghavyuva/nixopus-api/internal/features/logger"
@@ -12,11 +10,12 @@ import (
1210
func (s *DeployService) handleDockerfileDeployment(d DeployerConfig) error {
1311
s.addLog(d.application.ID, types.LogUsingDockerfileStrategy, d.deployment_config.ID)
1412
s.addLog(d.application.ID, fmt.Sprintf(types.LogBuildContextPath, d.contextPath), d.deployment_config.ID)
15-
if err := s.buildAndRunDockerImage(d); err != nil {
13+
availablePort, err := s.buildAndRunDockerImage(d)
14+
if err != nil {
1615
return err
1716
}
1817

19-
caddyProxy := proxy.NewCaddy(&s.logger, d.contextPath, d.application.Domain, strconv.Itoa(d.application.Port), proxy.ReverseProxy)
18+
caddyProxy := proxy.NewCaddy(&s.logger, d.contextPath, d.application.Domain, availablePort, proxy.ReverseProxy)
2019
if err := caddyProxy.Serve(); err != nil {
2120
s.addLog(d.application.ID, fmt.Sprintf("Failed to start Caddy proxy: %v", err), d.deployment_config.ID)
2221
return err
@@ -26,23 +25,23 @@ func (s *DeployService) handleDockerfileDeployment(d DeployerConfig) error {
2625
return nil
2726
}
2827

29-
func (s *DeployService) buildAndRunDockerImage(d DeployerConfig) error {
28+
func (s *DeployService) buildAndRunDockerImage(d DeployerConfig) (string, error) {
3029
_, err := s.buildImageFromDockerfile(d)
3130
if err != nil {
3231
s.addLog(d.application.ID, fmt.Sprintf(types.LogFailedToBuildDockerImage, err.Error()), d.deployment_config.ID)
33-
return fmt.Errorf("%w: %v", types.ErrBuildDockerImage, err)
32+
return "", fmt.Errorf("%w: %v", types.ErrBuildDockerImage, err)
3433
}
3534

3635
s.logger.Log(logger.Info, types.LogDockerImageBuiltSuccessfully, d.application.Name)
3736
s.addLog(d.application.ID, types.LogDockerImageBuiltSuccessfully, d.deployment_config.ID)
38-
containerID, err := s.AtomicUpdateContainer(d)
37+
containerID, availablePort, err := s.AtomicUpdateContainer(d)
3938
if err != nil {
4039
s.addLog(d.application.ID, fmt.Sprintf(types.LogFailedToRunDockerImage, err.Error()), d.deployment_config.ID)
41-
return fmt.Errorf("%w: %v", types.ErrRunDockerImage, err)
40+
return "", fmt.Errorf("%w: %v", types.ErrRunDockerImage, err)
4241
}
4342

4443
s.addLog(d.application.ID, fmt.Sprintf(types.LogContainerRunning, containerID), d.deployment_config.ID)
4544
s.addLog(d.application.ID, fmt.Sprintf(types.LogApplicationExposed, d.application.Port), d.deployment_config.ID)
4645

47-
return nil
46+
return availablePort, nil
4847
}

api/internal/features/deploy/service/run_image.go

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,7 @@ func (s *DeployService) prepareContainerConfig(
6666
}
6767

6868
// prepareHostConfig creates Docker host configuration with port bindings
69-
func (s *DeployService) prepareHostConfig(port nat.Port) container.HostConfig {
70-
availablePort, err := s.getAvailablePort()
71-
if err != nil {
72-
s.logger.Log(logger.Error, types.ErrFailedToGetAvailablePort.Error(), err.Error())
73-
return container.HostConfig{}
74-
}
75-
69+
func (s *DeployService) prepareHostConfig(port nat.Port, availablePort string) container.HostConfig {
7670
return container.HostConfig{
7771
NetworkMode: "bridge",
7872
PortBindings: map[nat.Port][]nat.PortBinding{
@@ -140,7 +134,7 @@ func (s *DeployService) getRunningContainers(r DeployerConfig) ([]container.Summ
140134
return currentContainers, nil
141135
}
142136

143-
func (s *DeployService) createContainerConfigs(r DeployerConfig) (container.Config, container.HostConfig, network.NetworkingConfig) {
137+
func (s *DeployService) createContainerConfigs(r DeployerConfig) (container.Config, container.HostConfig, network.NetworkingConfig, string) {
144138
port_str := fmt.Sprintf("%d", r.application.Port)
145139
port, _ := nat.NewPort("tcp", port_str)
146140

@@ -159,16 +153,21 @@ func (s *DeployService) createContainerConfigs(r DeployerConfig) (container.Conf
159153
env_vars,
160154
r.application.ID.String(),
161155
)
162-
host_config := s.prepareHostConfig(port)
156+
availablePort, err := s.getAvailablePort()
157+
if err != nil {
158+
s.logger.Log(logger.Error, types.ErrFailedToGetAvailablePort.Error(), err.Error())
159+
return container.Config{}, container.HostConfig{}, network.NetworkingConfig{}, ""
160+
}
161+
host_config := s.prepareHostConfig(port, availablePort)
163162
network_config := s.prepareNetworkConfig()
164163

165-
return container_config, host_config, network_config
164+
return container_config, host_config, network_config, availablePort
166165
}
167166

168167
// AtomicUpdateContainer performs a zero-downtime update of a running container
169-
func (s *DeployService) AtomicUpdateContainer(r DeployerConfig) (string, error) {
168+
func (s *DeployService) AtomicUpdateContainer(r DeployerConfig) (string, string, error) {
170169
if r.application.Name == "" {
171-
return "", types.ErrMissingImageName
170+
return "", "", types.ErrMissingImageName
172171
}
173172

174173
s.logger.Log(logger.Info, types.LogUpdatingContainer, r.application.Name)
@@ -177,16 +176,16 @@ func (s *DeployService) AtomicUpdateContainer(r DeployerConfig) (string, error)
177176

178177
currentContainers, err := s.getRunningContainers(r)
179178
if err != nil {
180-
return "", err
179+
return "", "", err
181180
}
182181

183-
container_config, host_config, network_config := s.createContainerConfigs(r)
182+
container_config, host_config, network_config, availablePort := s.createContainerConfigs(r)
184183

185184
s.formatLog(r.application.ID, r.deployment_config.ID, types.LogCreatingNewContainer)
186185
resp, err := s.dockerRepo.CreateContainer(container_config, host_config, network_config, "")
187186
if err != nil {
188187
fmt.Printf("Failed to create container: %v\n", err)
189-
return "", types.ErrFailedToCreateContainer
188+
return "", "", types.ErrFailedToCreateContainer
190189
}
191190
s.formatLog(r.application.ID, r.deployment_config.ID, types.LogNewContainerCreated+"%s", resp.ID)
192191

@@ -209,7 +208,7 @@ func (s *DeployService) AtomicUpdateContainer(r DeployerConfig) (string, error)
209208
if err != nil {
210209
fmt.Printf("Failed to start container: %v\n", err)
211210
s.dockerRepo.RemoveContainer(resp.ID, container.RemoveOptions{Force: true})
212-
return "", types.ErrFailedToStartNewContainer
211+
return "", "", types.ErrFailedToStartNewContainer
213212
}
214213
s.formatLog(r.application.ID, r.deployment_config.ID, types.LogNewContainerStartedSuccessfully)
215214

@@ -219,7 +218,7 @@ func (s *DeployService) AtomicUpdateContainer(r DeployerConfig) (string, error)
219218
if err != nil || containerInfo.State.Status != "running" {
220219
s.dockerRepo.StopContainer(resp.ID, container.StopOptions{})
221220
s.dockerRepo.RemoveContainer(resp.ID, container.RemoveOptions{Force: true})
222-
return "", types.ErrFailedToUpdateContainer
221+
return "", "", types.ErrFailedToUpdateContainer
223222
}
224223

225224
s.formatLog(r.application.ID, r.deployment_config.ID, types.LogContainerUpdateCompleted)
@@ -242,7 +241,7 @@ func (s *DeployService) AtomicUpdateContainer(r DeployerConfig) (string, error)
242241

243242
go s.collectContainerLogs(log_collection_config)
244243

245-
return resp.ID, nil
244+
return resp.ID, availablePort, nil
246245
}
247246

248247
// Helper function to create a pointer to an integer

0 commit comments

Comments
 (0)