mirror of
https://code.forgejo.org/forgejo/runner.git
synced 2025-09-05 18:40:59 +00:00
chore: refactor NewDockerVolumeRemoveExecutor (#854)
- remove the force argument as it is always false - accept more than one volume to be removed <!--start release-notes-assistant--> <!--URL:https://code.forgejo.org/forgejo/runner--> - other - [PR](https://code.forgejo.org/forgejo/runner/pulls/854): <!--number 854 --><!--line 0 --><!--description Y2hvcmU6IHJlZmFjdG9yIE5ld0RvY2tlclZvbHVtZVJlbW92ZUV4ZWN1dG9y-->chore: refactor NewDockerVolumeRemoveExecutor<!--description--> <!--end release-notes-assistant--> Reviewed-on: https://code.forgejo.org/forgejo/runner/pulls/854 Reviewed-by: Mathieu Fenniak <mfenniak@noreply.code.forgejo.org> Co-authored-by: Earl Warren <contact@earl-warren.org> Co-committed-by: Earl Warren <contact@earl-warren.org>
This commit is contained in:
parent
0ff1ec4c04
commit
159c6af0dd
2 changed files with 9 additions and 7 deletions
|
@ -4,13 +4,14 @@ package container
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"slices"
|
||||||
|
|
||||||
"code.forgejo.org/forgejo/runner/v9/act/common"
|
"code.forgejo.org/forgejo/runner/v9/act/common"
|
||||||
"github.com/docker/docker/api/types/filters"
|
"github.com/docker/docker/api/types/filters"
|
||||||
"github.com/docker/docker/api/types/volume"
|
"github.com/docker/docker/api/types/volume"
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewDockerVolumeRemoveExecutor(volumeName string, force bool) common.Executor {
|
func NewDockerVolumesRemoveExecutor(volumeNames []string) common.Executor {
|
||||||
return func(ctx context.Context) error {
|
return func(ctx context.Context) error {
|
||||||
cli, err := GetDockerClient(ctx)
|
cli, err := GetDockerClient(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -24,17 +25,18 @@ func NewDockerVolumeRemoveExecutor(volumeName string, force bool) common.Executo
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, vol := range list.Volumes {
|
for _, vol := range list.Volumes {
|
||||||
if vol.Name == volumeName {
|
if slices.Contains(volumeNames, vol.Name) {
|
||||||
return removeExecutor(volumeName, force)(ctx)
|
if err := removeExecutor(vol.Name)(ctx); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Volume not found - do nothing
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func removeExecutor(volume string, force bool) common.Executor {
|
func removeExecutor(volume string) common.Executor {
|
||||||
return func(ctx context.Context) error {
|
return func(ctx context.Context) error {
|
||||||
logger := common.Logger(ctx)
|
logger := common.Logger(ctx)
|
||||||
logger.Debugf("%sdocker volume rm %s", logPrefix, volume)
|
logger.Debugf("%sdocker volume rm %s", logPrefix, volume)
|
||||||
|
@ -49,6 +51,7 @@ func removeExecutor(volume string, force bool) common.Executor {
|
||||||
}
|
}
|
||||||
defer cli.Close()
|
defer cli.Close()
|
||||||
|
|
||||||
|
force := false
|
||||||
return cli.VolumeRemove(ctx, volume, force)
|
return cli.VolumeRemove(ctx, volume, force)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -521,8 +521,7 @@ func (rc *RunContext) prepareJobContainer(ctx context.Context) error {
|
||||||
|
|
||||||
if rc.JobContainer != nil {
|
if rc.JobContainer != nil {
|
||||||
return rc.JobContainer.Remove().IfNot(reuseJobContainer).
|
return rc.JobContainer.Remove().IfNot(reuseJobContainer).
|
||||||
Then(container.NewDockerVolumeRemoveExecutor(rc.jobContainerName(), false)).IfNot(reuseJobContainer).
|
Then(container.NewDockerVolumesRemoveExecutor([]string{rc.jobContainerName(), rc.jobContainerName() + "-env"})).IfNot(reuseJobContainer).
|
||||||
Then(container.NewDockerVolumeRemoveExecutor(rc.jobContainerName()+"-env", false)).IfNot(reuseJobContainer).
|
|
||||||
Then(func(ctx context.Context) error {
|
Then(func(ctx context.Context) error {
|
||||||
if len(rc.ServiceContainers) > 0 {
|
if len(rc.ServiceContainers) > 0 {
|
||||||
logger.Infof("Cleaning up services for job %s", rc.JobName)
|
logger.Infof("Cleaning up services for job %s", rc.JobName)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue