Refactoring & renaming

This commit is contained in:
Brian DeHamer 2015-07-21 16:04:41 +00:00
parent 3dd06cffb1
commit 00f2875abf
15 changed files with 254 additions and 233 deletions

29
actions/check.go Normal file
View file

@ -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
}

99
actions/update.go Normal file
View file

@ -0,0 +1,99 @@
package actions
import (
"math/rand"
"github.com/CenturyLinkLabs/watchtower/container"
)
var (
letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
)
func allContainersFilter(container.Container) bool { return true }
func Update() error {
client := container.NewClient()
containers, err := client.ListContainers(allContainersFilter)
if err != nil {
return err
}
for i := range containers {
if err := client.RefreshImage(&containers[i]); err != nil {
return err
}
}
containers, err = container.SortByDependencies(containers)
if err != nil {
return err
}
checkDependencies(containers)
// Stop stale containers in reverse order
for i := len(containers) - 1; i >= 0; i-- {
container := containers[i]
if container.IsWatchtower() {
break
}
if container.Stale {
if err := client.Stop(container, 10); err != nil {
return err
}
}
}
// Restart stale containers in sorted order
for _, container := range containers {
if container.Stale {
// Since we can't shutdown a watchtower container immediately, we need to
// start the new one while the old one is still running. This prevents us
// from re-using the same container name so we first rename the current
// instance so that the new one can adopt the old name.
if container.IsWatchtower() {
if err := client.Rename(container, randName()); err != nil {
return err
}
}
if err := client.Start(container); err != nil {
return err
}
}
}
return nil
}
func checkDependencies(containers []container.Container) {
for i, parent := range containers {
if parent.Stale {
continue
}
LinkLoop:
for _, linkName := range parent.Links() {
for _, child := range containers {
if child.Name() == linkName && child.Stale {
containers[i].Stale = true
break LinkLoop
}
}
}
}
}
// Generates a random, 32-character, Docker-compatible container name.
func randName() string {
b := make([]rune, 32)
for i := range b {
b[i] = letters[rand.Intn(len(letters))]
}
return string(b)
}

38
actions/update_test.go Normal file
View file

@ -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))
}