Skip to content

Commit

Permalink
Add Docker container removal functionality
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
PiotrFerenc committed Jun 1, 2024
1 parent efe9f23 commit e08396c
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 9 deletions.
54 changes: 54 additions & 0 deletions cmd/worker/actions/docker/docker-remove.go
Original file line number Diff line number Diff line change
@@ -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
}
29 changes: 29 additions & 0 deletions internal/Container/DockerContainer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
19 changes: 10 additions & 9 deletions internal/executor/map-executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}
}

0 comments on commit e08396c

Please sign in to comment.