Skip to content

Commit 49e3782

Browse files
authored
Add playground session id (#113)
1 parent 26259ff commit 49e3782

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

internal/local_runner.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"github.com/docker/docker/client"
2222
"github.com/docker/docker/pkg/stdcopy"
2323
"github.com/ethereum/go-ethereum/log"
24+
"github.com/google/uuid"
2425
"gopkg.in/yaml.v2"
2526
)
2627

@@ -62,6 +63,10 @@ type LocalRunner struct {
6263

6364
// wether to bind the ports to the local interface
6465
bindHostPortsLocally bool
66+
67+
// sessionID is a random sequence that is used to identify the session
68+
// it is used to identify the containers in the cleanup process
69+
sessionID string
6570
}
6671

6772
type task struct {
@@ -97,8 +102,6 @@ func NewLocalRunner(out *output, manifest *Manifest, overrides map[string]string
97102
return nil, fmt.Errorf("failed to create docker client: %w", err)
98103
}
99104

100-
fmt.Println(bindHostPortsLocally)
101-
102105
// merge the overrides with the manifest overrides
103106
if overrides == nil {
104107
overrides = make(map[string]string)
@@ -149,6 +152,7 @@ func NewLocalRunner(out *output, manifest *Manifest, overrides map[string]string
149152
taskUpdateCh: make(chan struct{}),
150153
exitErr: make(chan error, 2),
151154
bindHostPortsLocally: bindHostPortsLocally,
155+
sessionID: uuid.New().String(),
152156
}
153157

154158
if interactive {
@@ -295,8 +299,9 @@ func (d *LocalRunner) ExitErr() <-chan error {
295299
}
296300

297301
func (d *LocalRunner) Stop() error {
302+
// only stop the containers that belong to this session
298303
containers, err := d.client.ContainerList(context.Background(), container.ListOptions{
299-
Filters: filters.NewArgs(filters.Arg("label", "playground=true")),
304+
Filters: filters.NewArgs(filters.Arg("label", fmt.Sprintf("playground.session=%s", d.sessionID))),
300305
})
301306
if err != nil {
302307
return fmt.Errorf("error getting container list: %w", err)
@@ -518,8 +523,9 @@ func (d *LocalRunner) toDockerComposeService(s *service) (map[string]interface{}
518523
labels := map[string]string{
519524
// It is important to use the playground label to identify the containers
520525
// during the cleanup process
521-
"playground": "true",
522-
"service": s.Name,
526+
"playground": "true",
527+
"playground.session": d.sessionID,
528+
"service": s.Name,
523529
}
524530

525531
// add the local ports exposed by the service as labels
@@ -722,7 +728,7 @@ func (d *LocalRunner) trackLogs(serviceName string, containerID string) error {
722728

723729
func (d *LocalRunner) trackContainerStatusAndLogs() {
724730
eventCh, errCh := d.client.Events(context.Background(), events.ListOptions{
725-
Filters: filters.NewArgs(filters.Arg("label", "playground=true")),
731+
Filters: filters.NewArgs(filters.Arg("label", fmt.Sprintf("playground.session=%s", d.sessionID))),
726732
})
727733

728734
for {

0 commit comments

Comments
 (0)