Skip to content

Commit 827db1c

Browse files
committed
chore: add pre run and post run command executor
1 parent 18221d1 commit 827db1c

File tree

3 files changed

+60
-1
lines changed

3 files changed

+60
-1
lines changed

api/api/versions.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
{
44
"version": "v1",
55
"status": "active",
6-
"release_date": "2025-08-20T23:05:32.812174289+05:30",
6+
"release_date": "2025-08-21T22:58:05.772340699+05:30",
77
"end_of_life": "0001-01-01T00:00:00Z",
88
"changes": [
99
"Initial API version"
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package tasks
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/google/uuid"
7+
"github.com/raghavyuva/nixopus-api/internal/features/ssh"
8+
shared_types "github.com/raghavyuva/nixopus-api/internal/types"
9+
)
10+
11+
func (s *TaskService) runCommands(applicationID uuid.UUID, deploymentConfigID uuid.UUID, command string, commandType string) error {
12+
taskCtx := s.NewTaskContext(shared_types.TaskPayload{
13+
Application: shared_types.Application{
14+
ID: applicationID,
15+
},
16+
ApplicationDeployment: shared_types.ApplicationDeployment{
17+
ID: deploymentConfigID,
18+
},
19+
})
20+
taskCtx.AddLog(fmt.Sprintf("Running %s commands %v", commandType, command))
21+
22+
if command == "" {
23+
return nil
24+
}
25+
26+
client := ssh.NewSSH()
27+
output, err := client.RunCommand(command)
28+
if err != nil {
29+
taskCtx.AddLog(fmt.Sprintf("Error while running %s command %v", commandType, output))
30+
return err
31+
}
32+
33+
if output != "" {
34+
taskCtx.AddLog(fmt.Sprintf("%s command resulted in %v", commandType, output))
35+
}
36+
37+
return nil
38+
}
39+
40+
func (s *TaskService) PrerunCommands(d shared_types.TaskPayload) error {
41+
return s.runCommands(d.Application.ID, d.ApplicationDeployment.ID,
42+
d.Application.PreRunCommand, "pre run")
43+
}
44+
45+
func (s *TaskService) PostRunCommands(d shared_types.TaskPayload) error {
46+
return s.runCommands(d.Application.ID, d.ApplicationDeployment.ID,
47+
d.Application.PostRunCommand, "post run")
48+
}

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,18 @@ func (t *TaskService) BuildPack(ctx context.Context, d shared_types.TaskPayload)
8484
var err error
8585
switch d.Application.BuildPack {
8686
case shared_types.DockerFile:
87+
err = t.PrerunCommands(d)
88+
if err != nil {
89+
return err
90+
}
8791
err = t.HandleCreateDockerfileDeployment(ctx, d)
92+
if err != nil {
93+
return err
94+
}
95+
err = t.PostRunCommands(d)
96+
if err != nil {
97+
return err
98+
}
8899
case shared_types.DockerCompose:
89100
err = t.HandleCreateDockerComposeDeployment(ctx, d)
90101
case shared_types.Static:

0 commit comments

Comments
 (0)