From ae3b26f180f8e911e7dcc2b8acaf9f86aa6fc6f2 Mon Sep 17 00:00:00 2001 From: Bohdan Shulha Date: Mon, 16 Sep 2024 20:54:02 +0200 Subject: [PATCH] feat: #52 add the launch service task --- internal/app/ptah-agent/parse_task.go | 2 ++ internal/app/ptah-agent/service.go | 31 ++++++++++++++++++++++++ internal/app/ptah-agent/task_executor.go | 2 ++ internal/pkg/ptah-client/task_types.go | 9 +++++++ 4 files changed, 44 insertions(+) diff --git a/internal/app/ptah-agent/parse_task.go b/internal/app/ptah-agent/parse_task.go index 6f283e0..245fc43 100644 --- a/internal/app/ptah-agent/parse_task.go +++ b/internal/app/ptah-agent/parse_task.go @@ -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) } diff --git a/internal/app/ptah-agent/service.go b/internal/app/ptah-agent/service.go index e3fd38a..343eb1e 100644 --- a/internal/app/ptah-agent/service.go +++ b/internal/app/ptah-agent/service.go @@ -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 diff --git a/internal/app/ptah-agent/task_executor.go b/internal/app/ptah-agent/task_executor.go index b1aeefd..f5f1880 100644 --- a/internal/app/ptah-agent/task_executor.go +++ b/internal/app/ptah-agent/task_executor.go @@ -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) } diff --git a/internal/pkg/ptah-client/task_types.go b/internal/pkg/ptah-client/task_types.go index aefdb02..60d652b 100644 --- a/internal/pkg/ptah-client/task_types.go +++ b/internal/pkg/ptah-client/task_types.go @@ -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 +}