Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(build): cleanup orphan build containers on ctrl-c or gitlab cancel
Signed-off-by: Timofey Kirillov <timofey.kirillov@flant.com>
- Loading branch information
1 parent
5a9bb4a
commit 8702efa
Showing
5 changed files
with
77 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package container_backend | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
|
||
"github.com/docker/docker/api/types" | ||
|
||
"github.com/werf/logboek" | ||
"github.com/werf/werf/pkg/docker" | ||
) | ||
|
||
var ( | ||
runningDockerContainers []*runningDockerContainer | ||
mu sync.Mutex | ||
) | ||
|
||
type runningDockerContainer struct { | ||
Name string | ||
Ctx context.Context | ||
} | ||
|
||
func RegisterRunningContainer(name string, ctx context.Context) { | ||
mu.Lock() | ||
defer mu.Unlock() | ||
|
||
runningDockerContainers = append(runningDockerContainers, &runningDockerContainer{ | ||
Name: name, | ||
Ctx: ctx, | ||
}) | ||
} | ||
|
||
func UnregisterRunningContainer(name string) { | ||
mu.Lock() | ||
defer mu.Unlock() | ||
|
||
var res []*runningDockerContainer | ||
for _, cont := range runningDockerContainers { | ||
if cont.Name != name { | ||
res = append(res, cont) | ||
} | ||
} | ||
runningDockerContainers = res | ||
} | ||
|
||
func TerminateRunningDockerContainers() error { | ||
mu.Lock() | ||
defer mu.Unlock() | ||
|
||
for _, container := range runningDockerContainers { | ||
err := docker.ContainerRemove(container.Ctx, container.Name, types.ContainerRemoveOptions{RemoveVolumes: true, Force: true}) | ||
if err != nil { | ||
logboek.Context(container.Ctx).Error().LogF("WARNING: Unable to remove container %q: %w\n", container.Name, err.Error()) | ||
} | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters