Skip to content

Commit

Permalink
Merge pull request #69 from joeygibson/issue-5
Browse files Browse the repository at this point in the history
Teardown will now remove all networks created by the project
  • Loading branch information
joeygibson authored Apr 14, 2017
2 parents cb59c19 + a996783 commit ac8b3a2
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
8 changes: 4 additions & 4 deletions blackbox-test/features/teardown.feature
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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`
Expand Down
26 changes: 26 additions & 0 deletions command/teardown.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,18 @@ import (
"github.com/codegangsta/cli"
"github.com/fsouza/go-dockerclient"
"github.com/sirupsen/logrus"
"strings"
)

func CmdTeardown(c *cli.Context) error {
if err := helpers.RunCommand(helpers.DockerComposeCommand("kill")); err != nil {
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 {
Expand All @@ -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()
Expand Down

0 comments on commit ac8b3a2

Please sign in to comment.