Skip to content

Commit

Permalink
fix docker box delete
Browse files Browse the repository at this point in the history
  • Loading branch information
niqdev committed Oct 17, 2023
1 parent b8fe7bb commit 384504a
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 41 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,8 @@ Please, feel free to contribute to the companion [repository](https://github.com

<!--

fix distroless kube

solve the machine and add how to after docker https://github.com/juice-shop/juice-shop#docker-container

* test all catalog
Expand Down
90 changes: 53 additions & 37 deletions pkg/box/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package docker

import (
"fmt"
"os"
"os/signal"
"syscall"

"github.com/pkg/errors"
"golang.org/x/exp/maps"
Expand Down Expand Up @@ -168,7 +171,7 @@ func (box *DockerBoxClient) connectBox(opts *boxModel.ConnectOptions) error {
if opts.DisableExec || opts.DisableTunnel {
box.eventBus.Publish(newContainerExecIgnoreDockerEvent(info.Id))
}
return box.execBox(opts.Template, info, opts.StreamOpts, opts.DeleteOnExit)
return box.execBox(opts.Template, *info, opts.StreamOpts, opts.DeleteOnExit)
}
}

Expand All @@ -185,7 +188,7 @@ func (box *DockerBoxClient) searchBox(name string) (*boxModel.BoxInfo, error) {
return nil, errors.New("box not found")
}

func (box *DockerBoxClient) execBox(template *boxModel.BoxV1, info *boxModel.BoxInfo, streamOpts *commonModel.StreamOptions, deleteOnExit bool) error {
func (box *DockerBoxClient) execBox(template *boxModel.BoxV1, info boxModel.BoxInfo, streamOpts *commonModel.StreamOptions, deleteOnExit bool) error {
box.eventBus.Publish(newContainerExecDockerEvent(info.Id, info.Name, template.Shell))

// attempt to restart all associated sidecars
Expand Down Expand Up @@ -217,11 +220,10 @@ func (box *DockerBoxClient) execBox(template *boxModel.BoxV1, info *boxModel.Box
}

if template.Shell == boxModel.BoxShellNone {
if deleteOnExit {
// stop loader
box.eventBus.Publish(newContainerExecDockerLoaderEvent())
}
return box.logsBox(info.Id, streamOpts)
// stop loader
box.eventBus.Publish(newContainerExecDockerLoaderEvent())

return box.logsBox(info, streamOpts, deleteOnExit)
}

// already printed for temporary box
Expand Down Expand Up @@ -268,21 +270,17 @@ func (box *DockerBoxClient) execBox(template *boxModel.BoxV1, info *boxModel.Box
},
OnStreamCloseCallback: func() {
box.eventBus.Publish(newContainerExecExitDockerEvent(info.Id))

if deleteOnExit {
for _, sidecar := range sidecars {
if err := box.client.ContainerRemove(sidecar.Id); err != nil {
box.eventBus.Publish(newContainerExecErrorDockerEvent(sidecar.Id, errors.Wrap(err, "error sidecar exec remove")))
}
}

if err := box.client.ContainerRemove(info.Id); err != nil {
box.eventBus.Publish(newContainerExecErrorDockerEvent(info.Id, errors.Wrap(err, "error container exec remove")))
}
// ignore error
box.deleteBox(info)
}
},
OnStreamErrorCallback: func(err error) {
box.eventBus.Publish(newContainerExecErrorDockerEvent(info.Id, err))
if deleteOnExit {
// ignore error
box.deleteBox(info)
}
},
}

Expand All @@ -300,16 +298,27 @@ func (box *DockerBoxClient) publishPortInfo(networkMap map[string]boxModel.BoxPo
box.eventBus.Publish(newContainerCreatePortBindDockerConsoleEvent(containerName, networkPort, portPadding))
}

func (box *DockerBoxClient) logsBox(containerId string, streamOpts *commonModel.StreamOptions) error {
func (box *DockerBoxClient) logsBox(info boxModel.BoxInfo, streamOpts *commonModel.StreamOptions, deleteOnExit bool) error {

if deleteOnExit {
// captures CTRL+C
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, os.Interrupt, syscall.SIGTERM)
go func() {
<-signalChan
box.deleteBox(info)
}()
}

opts := &docker.ContainerLogsOpts{
ContainerId: containerId,
ContainerId: info.Id,
OutStream: streamOpts.Out,
ErrStream: streamOpts.Err,
OnStreamCloseCallback: func() {
box.eventBus.Publish(newContainerExecExitDockerEvent(containerId))
box.eventBus.Publish(newContainerExecExitDockerEvent(info.Id))
},
OnStreamErrorCallback: func(err error) {
box.eventBus.Publish(newContainerExecErrorDockerEvent(containerId, err))
box.eventBus.Publish(newContainerExecErrorDockerEvent(info.Id, err))
},
}
return box.client.ContainerLogs(opts)
Expand Down Expand Up @@ -417,25 +426,32 @@ func (box *DockerBoxClient) deleteBoxes(names []string) ([]string, error) {
// all or filter
if len(names) == 0 || slices.Contains(names, boxInfo.Name) {

if err := box.client.ContainerRemove(boxInfo.Id); err == nil {
if err := box.deleteBox(boxInfo); err == nil {
deleted = append(deleted, boxInfo.Name)
box.eventBus.Publish(newContainerRemoveDockerEvent(boxInfo.Id))
} else {
// silently ignore
box.eventBus.Publish(newContainerRemoveIgnoreDockerEvent(boxInfo.Id))
}

// delete all sidecars
sidecars, _ := box.dockerCommon.SidecarList(boxInfo.Name)
for _, sidecar := range sidecars {
if err := box.client.ContainerRemove(sidecar.Id); err != nil {
box.eventBus.Publish(newContainerRemoveDockerEvent(sidecar.Id))
} else {
// silently ignore
box.eventBus.Publish(newContainerRemoveIgnoreDockerEvent(sidecar.Id))
}
}
}
}
return deleted, nil
}

func (box *DockerBoxClient) deleteBox(boxInfo boxModel.BoxInfo) error {

// delete all sidecars
sidecars, _ := box.dockerCommon.SidecarList(boxInfo.Name)
for _, sidecar := range sidecars {
if err := box.client.ContainerRemove(sidecar.Id); err != nil {
// silently ignore
box.eventBus.Publish(newContainerRemoveIgnoreDockerEvent(sidecar.Name, sidecar.Id, err))
} else {
box.eventBus.Publish(newContainerRemoveDockerEvent(sidecar.Name, sidecar.Id))
}
}

if err := box.client.ContainerRemove(boxInfo.Id); err != nil {
box.eventBus.Publish(newContainerRemoveIgnoreDockerEvent(boxInfo.Name, boxInfo.Id, err))
return err
}

box.eventBus.Publish(newContainerRemoveDockerEvent(boxInfo.Name, boxInfo.Id))
return nil
}
8 changes: 4 additions & 4 deletions pkg/box/docker/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,12 @@ func newContainerListDockerEvent(index int, containerName string, containerId st
return &dockerBoxEvent{kind: event.LogDebug, value: fmt.Sprintf("container list: (%d) containerName=%s containerId=%s healthy=%v", index, containerName, containerId, healthy)}
}

func newContainerRemoveDockerEvent(containerId string) *dockerBoxEvent {
return &dockerBoxEvent{kind: event.LogInfo, value: fmt.Sprintf("container remove: containerId=%s", containerId)}
func newContainerRemoveDockerEvent(containerName string, containerId string) *dockerBoxEvent {
return &dockerBoxEvent{kind: event.LogInfo, value: fmt.Sprintf("container remove: containerName=%s containerId=%s", containerName, containerId)}
}

func newContainerRemoveIgnoreDockerEvent(containerId string) *dockerBoxEvent {
return &dockerBoxEvent{kind: event.LogWarning, value: fmt.Sprintf("container remove ignored: containerId=%s", containerId)}
func newContainerRemoveIgnoreDockerEvent(containerName string, containerId string, err error) *dockerBoxEvent {
return &dockerBoxEvent{kind: event.LogWarning, value: fmt.Sprintf("container remove ignored: containerName=%s containerId=%s error=%v", containerName, containerId, err)}
}

func newContainerInspectDockerEvent(containerId string) *dockerBoxEvent {
Expand Down

0 comments on commit 384504a

Please sign in to comment.