mirror of
https://github.com/containrrr/watchtower.git
synced 2025-01-17 18:26:19 +02:00
Add more accessors to Container struct
This commit is contained in:
parent
bfed95ecaf
commit
4275d1cd3d
@ -1,8 +1,6 @@
|
|||||||
package container
|
package container
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"strings"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
log "github.com/Sirupsen/logrus"
|
log "github.com/Sirupsen/logrus"
|
||||||
@ -11,7 +9,6 @@ import (
|
|||||||
|
|
||||||
const (
|
const (
|
||||||
defaultStopSignal = "SIGTERM"
|
defaultStopSignal = "SIGTERM"
|
||||||
signalLabel = "com.centurylinklabs.watchtower.stop-signal"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Filter func(Container) bool
|
type Filter func(Container) bool
|
||||||
@ -74,24 +71,23 @@ func (client DockerClient) ListContainers(fn Filter) ([]Container, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (client DockerClient) StopContainer(c Container, timeout time.Duration) error {
|
func (client DockerClient) StopContainer(c Container, timeout time.Duration) error {
|
||||||
signal := defaultStopSignal
|
signal := c.StopSignal()
|
||||||
|
if signal == "" {
|
||||||
if sig, ok := c.containerInfo.Config.Labels[signalLabel]; ok {
|
signal = defaultStopSignal
|
||||||
signal = sig
|
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Infof("Stopping %s (%s) with %s", c.Name(), c.containerInfo.Id, signal)
|
log.Infof("Stopping %s (%s) with %s", c.Name(), c.ID(), signal)
|
||||||
|
|
||||||
if err := client.api.KillContainer(c.containerInfo.Id, signal); err != nil {
|
if err := client.api.KillContainer(c.ID(), signal); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Wait for container to exit, but proceed anyway after the timeout elapses
|
// Wait for container to exit, but proceed anyway after the timeout elapses
|
||||||
client.waitForStop(c, timeout)
|
client.waitForStop(c, timeout)
|
||||||
|
|
||||||
log.Debugf("Removing container %s", c.containerInfo.Id)
|
log.Debugf("Removing container %s", c.ID())
|
||||||
|
|
||||||
return client.api.RemoveContainer(c.containerInfo.Id, true, false)
|
return client.api.RemoveContainer(c.ID(), true, false)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (client DockerClient) StartContainer(c Container) error {
|
func (client DockerClient) StartContainer(c Container) error {
|
||||||
@ -112,20 +108,15 @@ func (client DockerClient) StartContainer(c Container) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (client DockerClient) RenameContainer(c Container, newName string) error {
|
func (client DockerClient) RenameContainer(c Container, newName string) error {
|
||||||
log.Debugf("Renaming container %s (%s) to %s", c.Name(), c.containerInfo.Id, newName)
|
log.Debugf("Renaming container %s (%s) to %s", c.Name(), c.ID(), newName)
|
||||||
return client.api.RenameContainer(c.containerInfo.Id, newName)
|
return client.api.RenameContainer(c.ID(), newName)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (client DockerClient) IsContainerStale(c Container) (bool, error) {
|
func (client DockerClient) IsContainerStale(c Container) (bool, error) {
|
||||||
containerInfo := c.containerInfo
|
|
||||||
oldImageInfo := c.imageInfo
|
oldImageInfo := c.imageInfo
|
||||||
imageName := containerInfo.Config.Image
|
imageName := c.ImageName()
|
||||||
|
|
||||||
if client.pullImages {
|
if client.pullImages {
|
||||||
if !strings.Contains(imageName, ":") {
|
|
||||||
imageName = fmt.Sprintf("%s:latest", imageName)
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Debugf("Pulling %s for %s", imageName, c.Name())
|
log.Debugf("Pulling %s for %s", imageName, c.Name())
|
||||||
if err := client.api.PullImage(imageName, nil); err != nil {
|
if err := client.api.PullImage(imageName, nil); err != nil {
|
||||||
return false, err
|
return false, err
|
||||||
@ -153,7 +144,7 @@ func (client DockerClient) waitForStop(c Container, waitTime time.Duration) erro
|
|||||||
case <-timeout:
|
case <-timeout:
|
||||||
return nil
|
return nil
|
||||||
default:
|
default:
|
||||||
if ci, err := client.api.InspectContainer(c.containerInfo.Id); err != nil {
|
if ci, err := client.api.InspectContainer(c.ID()); err != nil {
|
||||||
return err
|
return err
|
||||||
} else if !ci.State.Running {
|
} else if !ci.State.Running {
|
||||||
return nil
|
return nil
|
||||||
|
@ -7,6 +7,11 @@ import (
|
|||||||
"github.com/samalba/dockerclient"
|
"github.com/samalba/dockerclient"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
watchtowerLabel = "com.centurylinklabs.watchtower"
|
||||||
|
signalLabel = "com.centurylinklabs.watchtower.stop-signal"
|
||||||
|
)
|
||||||
|
|
||||||
func NewContainer(containerInfo *dockerclient.ContainerInfo, imageInfo *dockerclient.ImageInfo) *Container {
|
func NewContainer(containerInfo *dockerclient.ContainerInfo, imageInfo *dockerclient.ImageInfo) *Container {
|
||||||
return &Container{
|
return &Container{
|
||||||
containerInfo: containerInfo,
|
containerInfo: containerInfo,
|
||||||
@ -21,10 +26,24 @@ type Container struct {
|
|||||||
imageInfo *dockerclient.ImageInfo
|
imageInfo *dockerclient.ImageInfo
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c Container) ID() string {
|
||||||
|
return c.containerInfo.Id
|
||||||
|
}
|
||||||
|
|
||||||
func (c Container) Name() string {
|
func (c Container) Name() string {
|
||||||
return c.containerInfo.Name
|
return c.containerInfo.Name
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c Container) ImageName() string {
|
||||||
|
imageName := c.containerInfo.Config.Image
|
||||||
|
|
||||||
|
if !strings.Contains(imageName, ":") {
|
||||||
|
imageName = fmt.Sprintf("%s:latest", imageName)
|
||||||
|
}
|
||||||
|
|
||||||
|
return imageName
|
||||||
|
}
|
||||||
|
|
||||||
func (c Container) Links() []string {
|
func (c Container) Links() []string {
|
||||||
var links []string
|
var links []string
|
||||||
|
|
||||||
@ -39,10 +58,18 @@ func (c Container) Links() []string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c Container) IsWatchtower() bool {
|
func (c Container) IsWatchtower() bool {
|
||||||
val, ok := c.containerInfo.Config.Labels["com.centurylinklabs.watchtower"]
|
val, ok := c.containerInfo.Config.Labels[watchtowerLabel]
|
||||||
return ok && val == "true"
|
return ok && val == "true"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c Container) StopSignal() string {
|
||||||
|
if val, ok := c.containerInfo.Config.Labels[signalLabel]; ok {
|
||||||
|
return val
|
||||||
|
}
|
||||||
|
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
// Ideally, we'd just be able to take the ContainerConfig from the old container
|
// Ideally, we'd just be able to take the ContainerConfig from the old container
|
||||||
// and use it as the starting point for creating the new container; however,
|
// and use it as the starting point for creating the new container; however,
|
||||||
// the ContainerConfig that comes back from the Inspect call merges the default
|
// the ContainerConfig that comes back from the Inspect call merges the default
|
||||||
|
@ -7,14 +7,44 @@ import (
|
|||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func TestID(t *testing.T) {
|
||||||
|
c := Container{
|
||||||
|
containerInfo: &dockerclient.ContainerInfo{Id: "foo"},
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.Equal(t, "foo", c.ID())
|
||||||
|
}
|
||||||
|
|
||||||
func TestName(t *testing.T) {
|
func TestName(t *testing.T) {
|
||||||
c := Container{
|
c := Container{
|
||||||
containerInfo: &dockerclient.ContainerInfo{Name: "foo"},
|
containerInfo: &dockerclient.ContainerInfo{Name: "foo"},
|
||||||
}
|
}
|
||||||
|
|
||||||
name := c.Name()
|
assert.Equal(t, "foo", c.Name())
|
||||||
|
}
|
||||||
|
|
||||||
assert.Equal(t, "foo", name)
|
func TestImageName_Tagged(t *testing.T) {
|
||||||
|
c := Container{
|
||||||
|
containerInfo: &dockerclient.ContainerInfo{
|
||||||
|
Config: &dockerclient.ContainerConfig{
|
||||||
|
Image: "foo:latest",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.Equal(t, "foo:latest", c.ImageName())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestImageName_Untagged(t *testing.T) {
|
||||||
|
c := Container{
|
||||||
|
containerInfo: &dockerclient.ContainerInfo{
|
||||||
|
Config: &dockerclient.ContainerConfig{
|
||||||
|
Image: "foo",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.Equal(t, "foo:latest", c.ImageName())
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestLinks(t *testing.T) {
|
func TestLinks(t *testing.T) {
|
||||||
@ -66,3 +96,29 @@ func TestIsWatchtower_NoLabel(t *testing.T) {
|
|||||||
|
|
||||||
assert.False(t, c.IsWatchtower())
|
assert.False(t, c.IsWatchtower())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestStopSignal_Present(t *testing.T) {
|
||||||
|
c := Container{
|
||||||
|
containerInfo: &dockerclient.ContainerInfo{
|
||||||
|
Config: &dockerclient.ContainerConfig{
|
||||||
|
Labels: map[string]string{
|
||||||
|
"com.centurylinklabs.watchtower.stop-signal": "SIGQUIT",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.Equal(t, "SIGQUIT", c.StopSignal())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestStopSignal_NoLabel(t *testing.T) {
|
||||||
|
c := Container{
|
||||||
|
containerInfo: &dockerclient.ContainerInfo{
|
||||||
|
Config: &dockerclient.ContainerConfig{
|
||||||
|
Labels: map[string]string{},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.Equal(t, "", c.StopSignal())
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user