Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

destroy topo by --prefix #34

Merged
merged 1 commit into from
Sep 6, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 29 additions & 2 deletions cmd/destroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ package cmd

import (
"context"
"fmt"

"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/srl-wim/container-lab/clab"
Expand All @@ -19,7 +22,31 @@ var destroyCmd = &cobra.Command{
if err != nil {
log.Fatal(err)
}

ctx, cancel := context.WithCancel(context.Background())
defer cancel()
if prefix != "" {
filter := filters.NewArgs()
filter.Add("label", fmt.Sprintf("containerlab=lab-%s", prefix))
containers, err := c.DockerClient.ContainerList(ctx, types.ContainerListOptions{
Filters: filter,
})
if err != nil {
log.Fatalf("could not list containers: %v", err)
}
var name string
for _, cont := range containers {
name = cont.ID
if len(cont.Names) > 0 {
name = cont.Names[0]
}
log.Infof("Removing container: %s", name)
err = c.DockerClient.ContainerRemove(ctx, cont.ID, types.ContainerRemoveOptions{})
if err != nil {
log.Errorf("could not remove container '%s': %v", name, err)
}
}
return
}
log.Info("Getting topology information ...")
if err = c.GetTopology(&topo); err != nil {
log.Fatal(err)
Expand All @@ -31,7 +58,6 @@ var destroyCmd = &cobra.Command{
log.Fatal(err)
}

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

log.Info("Destroying container lab: ... ", topo)
Expand All @@ -58,4 +84,5 @@ var destroyCmd = &cobra.Command{
func init() {
rootCmd.AddCommand(destroyCmd)
destroyCmd.Flags().StringVarP(&topo, "topo", "t", "/etc/containerlab/lab-examples/wan-topo.yml", "path to the file with topology information")
destroyCmd.Flags().StringVarP(&prefix, "prefix", "p", "", "lab name prefix")
}