refactor: move actions into pkg

This commit is contained in:
Simon Aronsson 2019-07-21 20:15:04 +02:00
parent 74ce92760c
commit 62f603bb25
4 changed files with 7 additions and 8 deletions

View file

@ -0,0 +1,177 @@
package actions_test
import (
"errors"
"testing"
"time"
"github.com/containrrr/watchtower/pkg/container"
"github.com/containrrr/watchtower/pkg/container/mocks"
"github.com/docker/docker/api/types"
t "github.com/containrrr/watchtower/pkg/types"
cli "github.com/docker/docker/client"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
func TestActions(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Actions Suite")
}
var _ = Describe("the actions package", func() {
var dockerClient cli.CommonAPIClient
var client mockClient
BeforeSuite(func() {
server := mocks.NewMockAPIServer()
dockerClient, _ = cli.NewClientWithOpts(
cli.WithHost(server.URL),
cli.WithHTTPClient(server.Client()))
})
BeforeEach(func() {
client = mockClient{
api: dockerClient,
pullImages: false,
removeVolumes: false,
TestData: &TestData{},
}
})
Describe("the check prerequisites method", func() {
When("given an empty array", func() {
It("should not do anything", func() {
client.TestData.Containers = []container.Container{}
err := CheckForMultipleWatchtowerInstances(client, false)
Expect(err).NotTo(HaveOccurred())
})
})
When("given an array of one", func() {
It("should not do anything", func() {
client.TestData.Containers = []container.Container{
createMockContainer(
"test-container",
"test-container",
"watchtower",
time.Now()),
}
err := CheckForMultipleWatchtowerInstances(client, false)
Expect(err).NotTo(HaveOccurred())
})
})
When("given multiple containers", func() {
BeforeEach(func() {
client = mockClient{
api: dockerClient,
pullImages: false,
removeVolumes: false,
TestData: &TestData{
NameOfContainerToKeep: "test-container-02",
Containers: []container.Container{
createMockContainer(
"test-container-01",
"test-container-01",
"watchtower",
time.Now().AddDate(0, 0, -1)),
createMockContainer(
"test-container-02",
"test-container-02",
"watchtower",
time.Now()),
},
},
}
})
It("should stop all but the latest one", func() {
err := CheckForMultipleWatchtowerInstances(client, false)
Expect(err).NotTo(HaveOccurred())
})
})
When("deciding whether to cleanup images", func() {
BeforeEach(func() {
client = mockClient{
api: dockerClient,
pullImages: false,
removeVolumes: false,
TestData: &TestData{
Containers: []container.Container{
createMockContainer(
"test-container-01",
"test-container-01",
"watchtower",
time.Now().AddDate(0, 0, -1)),
createMockContainer(
"test-container-02",
"test-container-02",
"watchtower",
time.Now()),
},
},
}
})
It("should try to delete the image if the cleanup flag is true", func() {
err := CheckForMultipleWatchtowerInstances(client, true)
Expect(err).NotTo(HaveOccurred())
Expect(client.TestData.TriedToRemoveImage).To(BeTrue())
})
It("should not try to delete the image if the cleanup flag is false", func() {
err := CheckForMultipleWatchtowerInstances(client, false)
Expect(err).NotTo(HaveOccurred())
Expect(client.TestData.TriedToRemoveImage).To(BeFalse())
})
})
})
})
func createMockContainer(id string, name string, image string, created time.Time) container.Container {
content := types.ContainerJSON{
ContainerJSONBase: &types.ContainerJSONBase{
ID: id,
Image: image,
Name: name,
Created: created.String(),
},
}
return *container.NewContainer(&content, nil)
}
type mockClient struct {
TestData *TestData
api cli.CommonAPIClient
pullImages bool
removeVolumes bool
}
type TestData struct {
TriedToRemoveImage bool
NameOfContainerToKeep string
Containers []container.Container
}
func (client mockClient) ListContainers(f t.Filter) ([]container.Container, error) {
return client.TestData.Containers, nil
}
func (client mockClient) StopContainer(c container.Container, d time.Duration) error {
if c.Name() == client.TestData.NameOfContainerToKeep {
return errors.New("tried to stop the instance we want to keep")
}
return nil
}
func (client mockClient) StartContainer(c container.Container) error {
panic("Not implemented")
}
func (client mockClient) RenameContainer(c container.Container, s string) error {
panic("Not implemented")
}
func (client mockClient) RemoveImage(c container.Container) error {
client.TestData.TriedToRemoveImage = true
return nil
}
func (client mockClient) IsContainerStale(c container.Container) (bool, error) {
panic("Not implemented")
}

84
pkg/actions/check.go Normal file
View file

@ -0,0 +1,84 @@
package actions
import (
"errors"
"fmt"
"sort"
"strings"
"time"
"github.com/opencontainers/runc/Godeps/_workspace/src/github.com/Sirupsen/logrus"
log "github.com/sirupsen/logrus"
"github.com/containrrr/watchtower/pkg/container"
)
// CheckForMultipleWatchtowerInstances will ensure that there are not multiple instances of the
// watchtower running simultaneously. If multiple watchtower containers are detected, this function
// will stop and remove all but the most recently started container.
func CheckForMultipleWatchtowerInstances(client container.Client, cleanup bool) error {
awaitDockerClient()
containers, err := client.ListContainers(container.WatchtowerContainersFilter)
if err != nil {
log.Fatal(err)
return err
}
if len(containers) <= 1 {
log.Debug("There are no additional watchtower containers")
return nil
}
log.Info("Found multiple running watchtower instances. Cleaning up.")
return cleanupExcessWatchtowers(containers, client, cleanup)
}
func cleanupExcessWatchtowers(containers []container.Container, client container.Client, cleanup bool) error {
var cleanupErrors int
var stopErrors int
sort.Sort(container.ByCreated(containers))
allContainersExceptLast := containers[0 : len(containers)-1]
for _, c := range allContainersExceptLast {
if err := client.StopContainer(c, 60); err != nil {
// logging the original here as we're just returning a count
logrus.Error(err)
stopErrors++
continue
}
if cleanup == true {
if err := client.RemoveImage(c); err != nil {
// logging the original here as we're just returning a count
logrus.Error(err)
cleanupErrors++
}
}
}
return createErrorIfAnyHaveOccurred(stopErrors, cleanupErrors)
}
func createErrorIfAnyHaveOccurred(c int, i int) error {
if c == 0 && i == 0 {
return nil
}
var output strings.Builder
if c > 0 {
output.WriteString(fmt.Sprintf("%d errors while stopping containers", c))
}
if i > 0 {
output.WriteString(fmt.Sprintf("%d errors while cleaning up images", c))
}
return errors.New(output.String())
}
func awaitDockerClient() {
log.Debug("Sleeping for a seconds to ensure the docker api client has been properly initialized.")
time.Sleep(1 * time.Second)
}

130
pkg/actions/update.go Normal file
View file

@ -0,0 +1,130 @@
package actions
import (
"math/rand"
"time"
"github.com/containrrr/watchtower/pkg/container"
t "github.com/containrrr/watchtower/pkg/types"
log "github.com/sirupsen/logrus"
)
var (
letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
)
// UpdateParams contains all different options available to alter the behavior of the Update func
type UpdateParams struct {
Filter t.Filter
Cleanup bool
NoRestart bool
Timeout time.Duration
MonitorOnly bool
}
// Update looks at the running Docker containers to see if any of the images
// used to start those containers have been updated. If a change is detected in
// any of the images, the associated containers are stopped and restarted with
// the new image.
func Update(client container.Client, params UpdateParams) error {
log.Debug("Checking containers for updated images")
containers, err := client.ListContainers(params.Filter)
if err != nil {
return err
}
for i, container := range containers {
stale, err := client.IsContainerStale(container)
if err != nil {
log.Infof("Unable to update container %s. Proceeding to next.", containers[i].Name())
log.Debug(err)
stale = false
}
containers[i].Stale = stale
}
containers, err = container.SortByDependencies(containers)
if err != nil {
return err
}
checkDependencies(containers)
if params.MonitorOnly {
return nil
}
// Stop stale containers in reverse order
for i := len(containers) - 1; i >= 0; i-- {
container := containers[i]
if container.IsWatchtower() {
log.Debugf("This is the watchtower container %s", containers[i].Name())
continue
}
if container.Stale {
if err := client.StopContainer(container, params.Timeout); err != nil {
log.Error(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.RenameContainer(container, randName()); err != nil {
log.Error(err)
continue
}
}
if !params.NoRestart {
if err := client.StartContainer(container); err != nil {
log.Error(err)
}
}
if params.Cleanup {
client.RemoveImage(container)
}
}
}
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)
}