From a996783a555cc13ad1b23f2331f756234e0d17e1 Mon Sep 17 00:00:00 2001 From: Joey Gibson Date: Thu, 13 Apr 2017 18:29:55 -0400 Subject: [PATCH] fixes issue #5 --- blackbox-test/features/teardown.feature | 8 ++++---- command/teardown.go | 26 +++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/blackbox-test/features/teardown.feature b/blackbox-test/features/teardown.feature index 3a83c14..bc68934 100644 --- a/blackbox-test/features/teardown.feature +++ b/blackbox-test/features/teardown.feature @@ -1,11 +1,11 @@ # Copyright 2016 Cisco Systems, Inc. -# +# # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at -# +# # http://www.apache.org/licenses/LICENSE-2.0 -# +# # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -90,7 +90,7 @@ Feature: teardown task When I run `lc teardown` Then it should succeed When I run `docker network ls` - Then the output should contain 'elsy_teardown_lc_bbt_network_test' + Then the output should not contain 'elsy_teardown_lc_bbt_network_test' When I run `lc teardown -f` Then it should succeed When I run `docker network ls` diff --git a/command/teardown.go b/command/teardown.go index 511af44..85a8076 100644 --- a/command/teardown.go +++ b/command/teardown.go @@ -24,6 +24,7 @@ import ( "github.com/codegangsta/cli" "github.com/fsouza/go-dockerclient" "github.com/sirupsen/logrus" + "strings" ) func CmdTeardown(c *cli.Context) error { @@ -31,6 +32,10 @@ func CmdTeardown(c *cli.Context) error { return err } + if err := removeNetworks(); err != nil { + return err + } + if c.Bool("force") { logrus.Debugf("found -f flag on teardown, removing all containers") if err := helpers.RunCommand(helpers.DockerComposeCommand("down", "--remove-orphans", "-v")); err != nil { @@ -42,6 +47,27 @@ func CmdTeardown(c *cli.Context) error { } } +func removeNetworks() error { + projectName := os.Getenv("COMPOSE_PROJECT_NAME") + + client := helpers.GetDockerClient() + + networks, err := client.ListNetworks() + if err != nil { + return err + } + + for _, network := range networks { + if strings.HasPrefix(network.Name, projectName) { + if err := client.RemoveNetwork(network.ID); err != nil { + logrus.Errorf("error removing network: %v", err) + } + } + } + + return nil +} + func removeContainersWithoutGcLabel() error { // only remove containers that don't have the com.lancope.docker-gc.keep set client := helpers.GetDockerClient()