Skip to content

Commit

Permalink
feat: #7 support service deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
bohdan-shulha committed Jun 26, 2024
1 parent 07b04b1 commit 6d37ab7
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 1 deletion.
2 changes: 1 addition & 1 deletion cmd/ptah-agent/ptah-agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ var version string = "dev"

func main() {
baseUrl := "http://localhost:8000/api/_nodes/v1"
ptahToken := "GqUL37nDBpGc34I29u6o23X0dlFC5OEKkjUNPGxysi"
ptahToken := "8JKoOtkGpe6E7zRgGBtKXyROcJ52nmfOqd93MVanvs"

agent := ptahAgent.New(version, baseUrl, ptahToken)

Expand Down
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 @@ -25,6 +25,8 @@ func parseTask(taskType int, payload string) (interface{}, error) {
return unmarshalTask(payload, &ptahClient.UpdateServiceReq{})
case 7:
return unmarshalTask(payload, &ptahClient.UpdateCurrentNodeReq{})
case 8:
return unmarshalTask(payload, &ptahClient.DeleteServiceReq{})
default:
return nil, fmt.Errorf("parse task: unknown task type %d", taskType)
}
Expand Down
27 changes: 27 additions & 0 deletions internal/app/ptah-agent/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,30 @@ func (e *taskExecutor) prepareServicePayload(ctx context.Context, spec swarm.Ser

return &spec, nil
}

func (e *taskExecutor) deleteDockerService(ctx context.Context, req *t.DeleteServiceReq) (*t.DeleteServiceRes, error) {
var res t.DeleteServiceRes

services, err := e.docker.ServiceList(ctx, types.ServiceListOptions{
Filters: filters.NewArgs(
filters.Arg("name", req.ServiceName),
),
})

if err != nil {
return nil, errors.Wrapf(err, "delete docker service")
}

if len(services) == 0 {
// TODO: return warnings if the service has not been found
//return nil, fmt.Errorf("service with name %s not found", req.ServiceName)
return nil, nil
}

err = e.docker.ServiceRemove(ctx, services[0].ID)
if err != nil {
return nil, errors.Wrapf(err, "delete docker service")
}

return &res, nil
}
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 @@ -33,6 +33,8 @@ func (e *taskExecutor) executeTask(ctx context.Context, task interface{}) (inter
return e.updateDockerService(ctx, task.(*t.UpdateServiceReq))
case *t.UpdateCurrentNodeReq:
return e.updateCurrentNode(ctx, task.(*t.UpdateCurrentNodeReq))
case *t.DeleteServiceReq:
return e.deleteDockerService(ctx, task.(*t.DeleteServiceReq))
default:
return nil, fmt.Errorf("execute task: unknown task type %T", task)
}
Expand Down
7 changes: 7 additions & 0 deletions internal/pkg/ptah-client/task_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,10 @@ type UpdateCurrentNodeReq struct {

type UpdateCurrentNodeRes struct {
}

type DeleteServiceReq struct {
ServiceName string
}

type DeleteServiceRes struct {
}

0 comments on commit 6d37ab7

Please sign in to comment.