|
1 | 1 | package tasks |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "context" |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "strconv" |
5 | 7 |
|
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" |
7 | 11 | ) |
8 | 12 |
|
9 | 13 | // HandleReDeploy clones source, builds image using redeploy flags, and atomically updates the container |
10 | 14 | 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) |
54 | 46 |
|
| 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 | +} |
0 commit comments