Skip to content

Commit

Permalink
feat: #52 add the launch service task
Browse files Browse the repository at this point in the history
  • Loading branch information
bohdan-shulha committed Sep 16, 2024
1 parent 419c450 commit ae3b26f
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 0 deletions.
2 changes: 2 additions & 0 deletions internal/app/ptah-agent/parse_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ func parseTask(taskType int, payload string) (interface{}, error) {
return unmarshalTask(payload, &ptahClient.JoinSwarmReq{})
case 20:
return unmarshalTask(payload, &ptahClient.UpdateDirdReq{})
case 21:
return unmarshalTask(payload, &ptahClient.LaunchServiceReq{})
default:
return nil, fmt.Errorf("parse task: unknown task type %d", taskType)
}
Expand Down
31 changes: 31 additions & 0 deletions internal/app/ptah-agent/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,37 @@ import (
t "github.com/ptah-sh/ptah-agent/internal/pkg/ptah-client"
)

func (e *taskExecutor) launchDockerService(ctx context.Context, req *t.LaunchServiceReq) (*t.LaunchServiceRes, error) {
var res t.LaunchServiceRes

existingService, err := e.getServiceByName(ctx, req.SwarmServiceSpec.Name)
if err != nil && err != ErrServiceNotFound {
return nil, fmt.Errorf("launch docker service: %w", err)
}

if existingService == nil {
// Service doesn't exist, create it
createRes, err := e.createDockerService(ctx, (*t.CreateServiceReq)(req))
if err != nil {
return nil, fmt.Errorf("launch docker service (create): %w", err)
}

res.Action = "created"
res.Docker.ID = createRes.Docker.ID
} else {
// Service exists, update it
_, err := e.updateDockerService(ctx, (*t.UpdateServiceReq)(req))
if err != nil {
return nil, fmt.Errorf("launch docker service (update): %w", err)
}

res.Action = "updated"
res.Docker.ID = existingService.ID
}

return &res, nil
}

func (e *taskExecutor) createDockerService(ctx context.Context, req *t.CreateServiceReq) (*t.CreateServiceRes, error) {
var res t.CreateServiceRes

Expand Down
2 changes: 2 additions & 0 deletions internal/app/ptah-agent/task_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ func (e *taskExecutor) executeTask(ctx context.Context, task interface{}) (inter
return e.joinSwarm(ctx, task.(*t.JoinSwarmReq))
case *t.UpdateDirdReq:
return e.updateDird(ctx, task.(*t.UpdateDirdReq))
case *t.LaunchServiceReq:
return e.launchDockerService(ctx, task.(*t.LaunchServiceReq))
default:
return nil, fmt.Errorf("execute task: unknown task type %T", task)
}
Expand Down
9 changes: 9 additions & 0 deletions internal/pkg/ptah-client/task_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,3 +221,12 @@ type UpdateDirdReq struct {

type UpdateDirdRes struct {
}

type LaunchServiceReq struct {
ServicePayload
}

type LaunchServiceRes struct {
Action string `json:"action"` // "created" or "updated"
dockerIdRes
}

0 comments on commit ae3b26f

Please sign in to comment.