diff options
author | Daniel Baumann <daniel@debian.org> | 2025-01-24 09:00:09 +0100 |
---|---|---|
committer | Daniel Baumann <daniel@debian.org> | 2025-01-24 09:00:09 +0100 |
commit | 4bf4dabd48aa70da3699b9a023f22689645d24f4 (patch) | |
tree | ed8175444649e71c0ed0e5f1d30101786ca2473e /pkg/container/docker_network.go | |
parent | Initial commit. (diff) | |
download | forgejo-act-debian.tar.xz forgejo-act-debian.zip |
Adding upstream version 1.24.0.HEADupstream/1.24.0upstreamdebian
Signed-off-by: Daniel Baumann <daniel@debian.org>
Diffstat (limited to 'pkg/container/docker_network.go')
-rw-r--r-- | pkg/container/docker_network.go | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/pkg/container/docker_network.go b/pkg/container/docker_network.go new file mode 100644 index 0000000..e97d8f7 --- /dev/null +++ b/pkg/container/docker_network.go @@ -0,0 +1,75 @@ +//go:build !(WITHOUT_DOCKER || !(linux || darwin || windows || netbsd)) + +package container + +import ( + "context" + + "github.com/docker/docker/api/types" + "github.com/nektos/act/pkg/common" +) + +func NewDockerNetworkCreateExecutor(name string, config *types.NetworkCreate) common.Executor { + return func(ctx context.Context) error { + cli, err := GetDockerClient(ctx) + if err != nil { + return err + } + defer cli.Close() + + // Only create the network if it doesn't exist + networks, err := cli.NetworkList(ctx, types.NetworkListOptions{}) + if err != nil { + return err + } + for _, network := range networks { + if network.Name == name { + common.Logger(ctx).Debugf("Network %v exists", name) + return nil + } + } + + _, err = cli.NetworkCreate(ctx, name, *config) + if err != nil { + return err + } + + return nil + } +} + +func NewDockerNetworkRemoveExecutor(name string) common.Executor { + return func(ctx context.Context) error { + cli, err := GetDockerClient(ctx) + if err != nil { + return err + } + defer cli.Close() + + // Make shure that all network of the specified name are removed + // cli.NetworkRemove refuses to remove a network if there are duplicates + networks, err := cli.NetworkList(ctx, types.NetworkListOptions{}) + if err != nil { + return err + } + common.Logger(ctx).Debugf("%v", networks) + for _, network := range networks { + if network.Name == name { + result, err := cli.NetworkInspect(ctx, network.ID, types.NetworkInspectOptions{}) + if err != nil { + return err + } + + if len(result.Containers) == 0 { + if err = cli.NetworkRemove(ctx, network.ID); err != nil { + common.Logger(ctx).Debugf("%v", err) + } + } else { + common.Logger(ctx).Debugf("Refusing to remove network %v because it still has active endpoints", name) + } + } + } + + return err + } +} |