From e08396c25d778b643c212820e525044db4f106d3 Mon Sep 17 00:00:00 2001 From: Piotr Date: Sat, 1 Jun 2024 13:29:06 +0200 Subject: [PATCH] Add Docker container removal functionality The commit adds functionality to remove Docker containers. It specifically introduces a new action "docker-remove" in the action map, which results in removal of Docker container with the specified container ID. Moreover, a 'RemoveContainer' function has been added to the DockerContainer.go file, allowing the removal of Docker containers from within the application. --- cmd/worker/actions/docker/docker-remove.go | 54 ++++++++++++++++++++++ internal/Container/DockerContainer.go | 29 ++++++++++++ internal/executor/map-executor.go | 19 ++++---- 3 files changed, 93 insertions(+), 9 deletions(-) create mode 100644 cmd/worker/actions/docker/docker-remove.go diff --git a/cmd/worker/actions/docker/docker-remove.go b/cmd/worker/actions/docker/docker-remove.go new file mode 100644 index 0000000..abd374b --- /dev/null +++ b/cmd/worker/actions/docker/docker-remove.go @@ -0,0 +1,54 @@ +package docker + +import ( + "context" + "github.com/PiotrFerenc/mash2/cmd/worker/actions" + "github.com/PiotrFerenc/mash2/internal/Container" + "github.com/PiotrFerenc/mash2/internal/types" +) + +type dockerRemoveContainer struct { + containerId actions.Property +} + +func CreateDockerRemove() actions.Action { + return &dockerRemoveContainer{ + containerId: actions.Property{ + Name: "id", + Type: actions.Text, + Description: "The unique identifier for the Docker container to be removed", + DisplayName: "Container ID", + Validation: "required", + }, + } +} + +func (d *dockerRemoveContainer) GetCategoryName() string { + return "docker" +} + +func (d *dockerRemoveContainer) Inputs() []actions.Property { + return []actions.Property{ + d.containerId, + } +} + +func (d *dockerRemoveContainer) Outputs() []actions.Property { + return []actions.Property{} +} + +func (d *dockerRemoveContainer) Execute(process types.Process) (types.Process, error) { + ctx := context.Background() + + containerId, err := d.containerId.GetStringFrom(&process) + if err != nil { + return process, err + } + + err = Container.RemoveContainer(containerId, ctx) + if err != nil { + return process, err + } + + return process, nil +} diff --git a/internal/Container/DockerContainer.go b/internal/Container/DockerContainer.go index cdd2567..a831ec8 100644 --- a/internal/Container/DockerContainer.go +++ b/internal/Container/DockerContainer.go @@ -91,3 +91,32 @@ func BuildImage(dockerfilePath, imageName string, ctx context.Context) (string, return imgID.ID, nil } + +// RemoveContainer removes a Docker container with the specified container ID and context. +// +// Parameters: +// +// containerId: The ID of the container to be removed. +// ctx: The context to use for the Docker client. +// +// Returns: +// +// error: An error if the container failed to be removed. +func RemoveContainer(containerId string, ctx context.Context) error { + cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation()) + if err != nil { + return err + } + + err = cli.ContainerRemove(ctx, containerId, container.RemoveOptions{ + RemoveVolumes: true, + RemoveLinks: true, + Force: true, + }) + + if err != nil { + return err + } + + return nil +} diff --git a/internal/executor/map-executor.go b/internal/executor/map-executor.go index 28f7f1d..9c80217 100644 --- a/internal/executor/map-executor.go +++ b/internal/executor/map-executor.go @@ -114,15 +114,16 @@ func unmarshal(d amqp.Delivery) (types.Process, error) { // CreateActionMap takes a pointer to a Config struct as input parameter. It creates and returns a map of actions. The keys of the map are the names of the actions, and the values are instances of the corresponding actions. The map is created using the specified configuration. func CreateActionMap(config *configuration.Config) map[string]actions.Action { return map[string]actions.Action{ - "console": others.CreateConsoleAction(), - "add-numbers": math.CreateAddNumbers(), - "git-clone": git.CreateGitClone(config), - "git-commit": git.CreateGitCommit(config), - "git-branch": git.CreateGitCreateBranch(config), - "file-create": file.CreateContentToFile(config), - "docker-run": docker.CreateDockerRun(), - "file-delete": file.CreateDeleteFileAction(config), - "file-append": file.CreateAppendContentToFile(config), + "console": others.CreateConsoleAction(), + "add-numbers": math.CreateAddNumbers(), + "git-clone": git.CreateGitClone(config), + "git-commit": git.CreateGitCommit(config), + "git-branch": git.CreateGitCreateBranch(config), + "file-create": file.CreateContentToFile(config), + "docker-run": docker.CreateDockerRun(), + "docker-remove": docker.CreateDockerRemove(), + "file-delete": file.CreateDeleteFileAction(config), + "file-append": file.CreateAppendContentToFile(config), //"for-each": common.CreateForEachLoop(), } }