grim/convey

Bump the version
v0.14.0-alpha4
2018-02-20, Gary Kramlich
89afa53fab1b
Bump the version
// Convey
// Copyright 2016-2018 Gary Kramlich <grim@reaperworld.com>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package docker
import (
"fmt"
"time"
"github.com/aphistic/gomol"
"bitbucket.org/rw_grim/convey/logging"
"bitbucket.org/rw_grim/convey/state"
"bitbucket.org/rw_grim/convey/util"
)
// Network represents a docker network.
type Network struct {
name string
logger *gomol.LogAdapter
state *state.State
}
// NewNetwork creates a new docker network.
func NewNetwork(st *state.State) (*Network, error) {
network := &Network{
name: util.ID(),
logger: logging.NewAdapter("network"),
state: st,
}
cmdv := []string{"network", "create", network.name}
_, _, err := DockerOutput("create network", cmdv, st)
if err != nil {
return nil, err
}
err = network.logger.Debugf("created network: %#v", network.name)
if err != nil {
fmt.Printf("error reporting debug: %s\n", err)
}
return network, nil
}
// Name returns the name of the network.
func (network *Network) Name() string {
return network.name
}
// Destroy tears down the docker network.
func (network *Network) Destroy() error {
for _, name := range network.state.GetRunning() {
cmdv := []string{
"network",
"disconnect",
"--force",
network.name,
name,
}
err := network.logger.Infof("disconnecting container %s from network %s", name, network.Name())
if err != nil {
return err
}
err = Docker("disconnect container", cmdv, network.state)
if err != nil {
msgFmt := "failed to disconnect container %s from network %s"
if lErr := network.logger.Warningf(msgFmt, name, network.Name()); lErr != nil {
fmt.Printf("error reporting warning: %s\n", lErr)
}
continue
}
msgFmt := "disconnected container %s from network %s"
if lErr := network.logger.Infof(msgFmt, name, network.Name()); lErr != nil {
fmt.Printf("error report info: %s\n", lErr)
}
}
// monkey with the timeout so our cleanup always runs
oldTimeout := network.state.PlanTimeout
defer func() {
network.state.PlanTimeout = oldTimeout
}()
network.state.PlanTimeout = 15 * time.Minute
cmdv := []string{"network", "rm", network.name}
return Docker("remove network", cmdv, network.state)
}