Refactoring & renaming
parent
3dd06cffb1
commit
00f2875abf
@ -0,0 +1,29 @@
|
||||
package actions
|
||||
|
||||
import (
|
||||
"sort"
|
||||
|
||||
"github.com/CenturyLinkLabs/watchtower/container"
|
||||
)
|
||||
|
||||
func watchtowerContainersFilter(c container.Container) bool { return c.IsWatchtower() }
|
||||
|
||||
func CheckPrereqs() error {
|
||||
client := container.NewClient()
|
||||
|
||||
containers, err := client.ListContainers(watchtowerContainersFilter)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if len(containers) > 1 {
|
||||
sort.Sort(container.ByCreated(containers))
|
||||
|
||||
// Iterate over all containers execept the last one
|
||||
for _, c := range containers[0 : len(containers)-1] {
|
||||
client.Stop(c, 60)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
@ -0,0 +1,38 @@
|
||||
package actions
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
"testing"
|
||||
|
||||
"github.com/CenturyLinkLabs/watchtower/container"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestCheckDependencies(t *testing.T) {
|
||||
cs := []container.Container{
|
||||
container.NewTestContainer("1", []string{}),
|
||||
container.NewTestContainer("2", []string{"1:"}),
|
||||
container.NewTestContainer("3", []string{"2:"}),
|
||||
container.NewTestContainer("4", []string{"3:"}),
|
||||
container.NewTestContainer("5", []string{"4:"}),
|
||||
container.NewTestContainer("6", []string{"5:"}),
|
||||
}
|
||||
cs[3].Stale = true
|
||||
|
||||
checkDependencies(cs)
|
||||
|
||||
assert.False(t, cs[0].Stale)
|
||||
assert.False(t, cs[1].Stale)
|
||||
assert.False(t, cs[2].Stale)
|
||||
assert.True(t, cs[3].Stale)
|
||||
assert.True(t, cs[4].Stale)
|
||||
assert.True(t, cs[5].Stale)
|
||||
}
|
||||
|
||||
func TestRandName(t *testing.T) {
|
||||
validPattern := regexp.MustCompile(`^[a-zA-Z0-9][a-zA-Z0-9_.-]+$`)
|
||||
|
||||
name := randName()
|
||||
|
||||
assert.True(t, validPattern.MatchString(name))
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package docker
|
||||
package container
|
||||
|
||||
import (
|
||||
"errors"
|
@ -0,0 +1,98 @@
|
||||
package container
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Sort containers by Created date
|
||||
type ByCreated []Container
|
||||
|
||||
func (c ByCreated) Len() int { return len(c) }
|
||||
func (c ByCreated) Swap(i, j int) { c[i], c[j] = c[j], c[i] }
|
||||
|
||||
func (c ByCreated) Less(i, j int) bool {
|
||||
t1, err := time.Parse(time.RFC3339Nano, c[i].containerInfo.Created)
|
||||
if err != nil {
|
||||
t1 = time.Now()
|
||||
}
|
||||
|
||||
t2, _ := time.Parse(time.RFC3339Nano, c[j].containerInfo.Created)
|
||||
if err != nil {
|
||||
t1 = time.Now()
|
||||
}
|
||||
|
||||
return t1.Before(t2)
|
||||
}
|
||||
|
||||
func SortByDependencies(containers []Container) ([]Container, error) {
|
||||
sorter := dependencySorter{}
|
||||
return sorter.Sort(containers)
|
||||
}
|
||||
|
||||
type dependencySorter struct {
|
||||
unvisited []Container
|
||||
marked map[string]bool
|
||||
sorted []Container
|
||||
}
|
||||
|
||||
func (ds *dependencySorter) Sort(containers []Container) ([]Container, error) {
|
||||
ds.unvisited = containers
|
||||
ds.marked = map[string]bool{}
|
||||
|
||||
for len(ds.unvisited) > 0 {
|
||||
if err := ds.visit(ds.unvisited[0]); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return ds.sorted, nil
|
||||
}
|
||||
|
||||
func (ds *dependencySorter) visit(c Container) error {
|
||||
|
||||
if _, ok := ds.marked[c.Name()]; ok {
|
||||
return fmt.Errorf("Circular reference to %s", c.Name())
|
||||
}
|
||||
|
||||
// Mark any visited node so that circular references can be detected
|
||||
ds.marked[c.Name()] = true
|
||||
defer delete(ds.marked, c.Name())
|
||||
|
||||
// Recursively visit links
|
||||
for _, linkName := range c.Links() {
|
||||
if linkedContainer := ds.findUnvisited(linkName); linkedContainer != nil {
|
||||
if err := ds.visit(*linkedContainer); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Move container from unvisited to sorted
|
||||
ds.removeUnvisited(c)
|
||||
ds.sorted = append(ds.sorted, c)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ds *dependencySorter) findUnvisited(name string) *Container {
|
||||
for _, c := range ds.unvisited {
|
||||
if c.Name() == name {
|
||||
return &c
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ds *dependencySorter) removeUnvisited(c Container) {
|
||||
var idx int
|
||||
for i := range ds.unvisited {
|
||||
if ds.unvisited[i].Name() == c.Name() {
|
||||
idx = i
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
ds.unvisited = append(ds.unvisited[0:idx], ds.unvisited[idx+1:]...)
|
||||
}
|
@ -0,0 +1,59 @@
|
||||
package container
|
||||
|
||||
import (
|
||||
"sort"
|
||||
"testing"
|
||||
|
||||
"github.com/samalba/dockerclient"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestByCreated(t *testing.T) {
|
||||
c1 := Container{
|
||||
containerInfo: &dockerclient.ContainerInfo{
|
||||
Created: "2015-07-01T12:00:01.000000000Z",
|
||||
},
|
||||
}
|
||||
c2 := Container{
|
||||
containerInfo: &dockerclient.ContainerInfo{
|
||||
Created: "2015-07-01T12:00:02.000000000Z",
|
||||
},
|
||||
}
|
||||
c3 := Container{
|
||||
containerInfo: &dockerclient.ContainerInfo{
|
||||
Created: "2015-07-01T12:00:02.000000001Z",
|
||||
},
|
||||
}
|
||||
cs := []Container{c3, c2, c1}
|
||||
|
||||
sort.Sort(ByCreated(cs))
|
||||
|
||||
assert.Equal(t, []Container{c1, c2, c3}, cs)
|
||||
}
|
||||
|
||||
func TestSortByDependencies_Success(t *testing.T) {
|
||||
c1 := NewTestContainer("1", []string{})
|
||||
c2 := NewTestContainer("2", []string{"1:"})
|
||||
c3 := NewTestContainer("3", []string{"2:"})
|
||||
c4 := NewTestContainer("4", []string{"3:"})
|
||||
c5 := NewTestContainer("5", []string{"4:"})
|
||||
c6 := NewTestContainer("6", []string{"5:", "3:"})
|
||||
containers := []Container{c6, c2, c4, c1, c3, c5}
|
||||
|
||||
result, err := SortByDependencies(containers)
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, []Container{c1, c2, c3, c4, c5, c6}, result)
|
||||
}
|
||||
|
||||
func TestSortByDependencies_Error(t *testing.T) {
|
||||
c1 := NewTestContainer("1", []string{"3:"})
|
||||
c2 := NewTestContainer("2", []string{"1:"})
|
||||
c3 := NewTestContainer("3", []string{"2:"})
|
||||
containers := []Container{c1, c2, c3}
|
||||
|
||||
_, err := SortByDependencies(containers)
|
||||
|
||||
assert.Error(t, err)
|
||||
assert.EqualError(t, err, "Circular reference to 1")
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package docker
|
||||
package container
|
||||
|
||||
func sliceEqual(s1, s2 []string) bool {
|
||||
if len(s1) != len(s2) {
|
@ -1,4 +1,4 @@
|
||||
package docker
|
||||
package container
|
||||
|
||||
import (
|
||||
"testing"
|
@ -1,74 +0,0 @@
|
||||
package updater
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/CenturyLinkLabs/watchtower/docker"
|
||||
)
|
||||
|
||||
type ContainerSorter struct {
|
||||
unvisited []docker.Container
|
||||
marked map[string]bool
|
||||
sorted []docker.Container
|
||||
}
|
||||
|
||||
func (cs *ContainerSorter) Sort(containers []docker.Container) ([]docker.Container, error) {
|
||||
cs.unvisited = containers
|
||||
cs.marked = map[string]bool{}
|
||||
|
||||
for len(cs.unvisited) > 0 {
|
||||
if err := cs.visit(cs.unvisited[0]); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return cs.sorted, nil
|
||||
}
|
||||
|
||||
func (cs *ContainerSorter) visit(c docker.Container) error {
|
||||
|
||||
if _, ok := cs.marked[c.Name()]; ok {
|
||||
return fmt.Errorf("Circular reference to %s", c.Name())
|
||||
}
|
||||
|
||||
// Mark any visited node so that circular references can be detected
|
||||
cs.marked[c.Name()] = true
|
||||
defer delete(cs.marked, c.Name())
|
||||
|
||||
// Recursively visit links
|
||||
for _, linkName := range c.Links() {
|
||||
if linkedContainer := cs.findUnvisited(linkName); linkedContainer != nil {
|
||||
if err := cs.visit(*linkedContainer); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Move container from unvisited to sorted
|
||||
cs.removeUnvisited(c)
|
||||
cs.sorted = append(cs.sorted, c)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (cs *ContainerSorter) findUnvisited(name string) *docker.Container {
|
||||
for _, c := range cs.unvisited {
|
||||
if c.Name() == name {
|
||||
return &c
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (cs *ContainerSorter) removeUnvisited(c docker.Container) {
|
||||
var idx int
|
||||
for i := range cs.unvisited {
|
||||
if cs.unvisited[i].Name() == c.Name() {
|
||||
idx = i
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
cs.unvisited = append(cs.unvisited[0:idx], cs.unvisited[idx+1:]...)
|
||||
}
|
@ -1,37 +0,0 @@
|
||||
package updater
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/CenturyLinkLabs/watchtower/docker"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestContainerSorter_Success(t *testing.T) {
|
||||
c1 := docker.NewTestContainer("1", []string{})
|
||||
c2 := docker.NewTestContainer("2", []string{"1:"})
|
||||
c3 := docker.NewTestContainer("3", []string{"2:"})
|
||||
c4 := docker.NewTestContainer("4", []string{"3:"})
|
||||
c5 := docker.NewTestContainer("5", []string{"4:"})
|
||||
c6 := docker.NewTestContainer("6", []string{"5:", "3:"})
|
||||
containers := []docker.Container{c6, c2, c4, c1, c3, c5}
|
||||
|
||||
cs := ContainerSorter{}
|
||||
result, err := cs.Sort(containers)
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, []docker.Container{c1, c2, c3, c4, c5, c6}, result)
|
||||
}
|
||||
|
||||
func TestContainerSorter_Error(t *testing.T) {
|
||||
c1 := docker.NewTestContainer("1", []string{"3:"})
|
||||
c2 := docker.NewTestContainer("2", []string{"1:"})
|
||||
c3 := docker.NewTestContainer("3", []string{"2:"})
|
||||
containers := []docker.Container{c1, c2, c3}
|
||||
|
||||
cs := ContainerSorter{}
|
||||
_, err := cs.Sort(containers)
|
||||
|
||||
assert.Error(t, err)
|
||||
assert.EqualError(t, err, "Circular reference to 1")
|
||||
}
|
@ -1,29 +0,0 @@
|
||||
package updater
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/CenturyLinkLabs/watchtower/docker"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestCheckDependencies(t *testing.T) {
|
||||
cs := []docker.Container{
|
||||
docker.NewTestContainer("1", []string{}),
|
||||
docker.NewTestContainer("2", []string{"1:"}),
|
||||
docker.NewTestContainer("3", []string{"2:"}),
|
||||
docker.NewTestContainer("4", []string{"3:"}),
|
||||
docker.NewTestContainer("5", []string{"4:"}),
|
||||
docker.NewTestContainer("6", []string{"5:"}),
|
||||
}
|
||||
cs[3].Stale = true
|
||||
|
||||
checkDependencies(cs)
|
||||
|
||||
assert.False(t, cs[0].Stale)
|
||||
assert.False(t, cs[1].Stale)
|
||||
assert.False(t, cs[2].Stale)
|
||||
assert.True(t, cs[3].Stale)
|
||||
assert.True(t, cs[4].Stale)
|
||||
assert.True(t, cs[5].Stale)
|
||||
}
|
Loading…
Reference in New Issue