mirror of
https://github.com/containrrr/watchtower.git
synced 2025-09-22 05:40:50 +02:00
Add godoc comments
This commit is contained in:
parent
058d6e5507
commit
d6321bf8dc
6 changed files with 42 additions and 1 deletions
|
@ -1,8 +1,9 @@
|
||||||
# Watchtower
|
# Watchtower
|
||||||

|

|
||||||
|
|
||||||
|
[](https://circleci.com/gh/CenturyLinkLabs/watchtower)
|
||||||
|
[](https://godoc.org/github.com/CenturyLinkLabs/watchtower)
|
||||||
[](https://imagelayers.io/?images=centurylink/watchtower:latest 'Get your own badge on imagelayers.io')
|
[](https://imagelayers.io/?images=centurylink/watchtower:latest 'Get your own badge on imagelayers.io')
|
||||||
[](https://circleci.com/gh/CenturyLinkLabs/watchtower)
|
|
||||||
|
|
||||||
A process for watching your Docker containers and automatically restarting them whenever their base image is refreshed.
|
A process for watching your Docker containers and automatically restarting them whenever their base image is refreshed.
|
||||||
|
|
||||||
|
|
|
@ -8,6 +8,10 @@ import (
|
||||||
|
|
||||||
func watchtowerContainersFilter(c container.Container) bool { return c.IsWatchtower() }
|
func watchtowerContainersFilter(c container.Container) bool { return c.IsWatchtower() }
|
||||||
|
|
||||||
|
// CheckPrereqs 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 CheckPrereqs(client container.Client, cleanup bool) error {
|
func CheckPrereqs(client container.Client, cleanup bool) error {
|
||||||
containers, err := client.ListContainers(watchtowerContainersFilter)
|
containers, err := client.ListContainers(watchtowerContainersFilter)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -15,6 +15,10 @@ var (
|
||||||
|
|
||||||
func allContainersFilter(container.Container) bool { return true }
|
func allContainersFilter(container.Container) bool { return true }
|
||||||
|
|
||||||
|
// 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, cleanup bool) error {
|
func Update(client container.Client, cleanup bool) error {
|
||||||
log.Info("Checking containers for updated images")
|
log.Info("Checking containers for updated images")
|
||||||
|
|
||||||
|
|
|
@ -13,8 +13,12 @@ const (
|
||||||
defaultStopSignal = "SIGTERM"
|
defaultStopSignal = "SIGTERM"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// A Filter is a prototype for a function that can be used to filter the
|
||||||
|
// results from a call to the ListContainers() method on the Client.
|
||||||
type Filter func(Container) bool
|
type Filter func(Container) bool
|
||||||
|
|
||||||
|
// A Client is the interface through which watchtower interacts with the
|
||||||
|
// Docker API.
|
||||||
type Client interface {
|
type Client interface {
|
||||||
ListContainers(Filter) ([]Container, error)
|
ListContainers(Filter) ([]Container, error)
|
||||||
StopContainer(Container, time.Duration) error
|
StopContainer(Container, time.Duration) error
|
||||||
|
@ -24,6 +28,8 @@ type Client interface {
|
||||||
RemoveImage(Container) error
|
RemoveImage(Container) error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewClient returns a new Client instance which can be used to interact with
|
||||||
|
// the Docker API.
|
||||||
func NewClient(dockerHost string, tlsConfig *tls.Config, pullImages bool) Client {
|
func NewClient(dockerHost string, tlsConfig *tls.Config, pullImages bool) Client {
|
||||||
docker, err := dockerclient.NewDockerClient(dockerHost, tlsConfig)
|
docker, err := dockerclient.NewDockerClient(dockerHost, tlsConfig)
|
||||||
|
|
||||||
|
|
|
@ -12,6 +12,8 @@ const (
|
||||||
signalLabel = "com.centurylinklabs.watchtower.stop-signal"
|
signalLabel = "com.centurylinklabs.watchtower.stop-signal"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// NewContainer returns a new Container instance instantiated with the
|
||||||
|
// specified ContainerInfo and ImageInfo structs.
|
||||||
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,
|
||||||
|
@ -19,6 +21,7 @@ func NewContainer(containerInfo *dockerclient.ContainerInfo, imageInfo *dockercl
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Container represents a running Docker container.
|
||||||
type Container struct {
|
type Container struct {
|
||||||
Stale bool
|
Stale bool
|
||||||
|
|
||||||
|
@ -26,18 +29,25 @@ type Container struct {
|
||||||
imageInfo *dockerclient.ImageInfo
|
imageInfo *dockerclient.ImageInfo
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ID returns the Docker container ID.
|
||||||
func (c Container) ID() string {
|
func (c Container) ID() string {
|
||||||
return c.containerInfo.Id
|
return c.containerInfo.Id
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Name returns the Docker container name.
|
||||||
func (c Container) Name() string {
|
func (c Container) Name() string {
|
||||||
return c.containerInfo.Name
|
return c.containerInfo.Name
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ImageID returns the ID of the Docker image that was used to start the
|
||||||
|
// container.
|
||||||
func (c Container) ImageID() string {
|
func (c Container) ImageID() string {
|
||||||
return c.imageInfo.Id
|
return c.imageInfo.Id
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ImageName returns the name of the Docker image that was used to start the
|
||||||
|
// container. If the original image was specified without a particular tag, the
|
||||||
|
// "latest" tag is assumed.
|
||||||
func (c Container) ImageName() string {
|
func (c Container) ImageName() string {
|
||||||
imageName := c.containerInfo.Config.Image
|
imageName := c.containerInfo.Config.Image
|
||||||
|
|
||||||
|
@ -48,6 +58,8 @@ func (c Container) ImageName() string {
|
||||||
return imageName
|
return imageName
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Links returns a list containing the names of all the containers to which
|
||||||
|
// this container is linked.
|
||||||
func (c Container) Links() []string {
|
func (c Container) Links() []string {
|
||||||
var links []string
|
var links []string
|
||||||
|
|
||||||
|
@ -61,11 +73,18 @@ func (c Container) Links() []string {
|
||||||
return links
|
return links
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IsWatchtower returns a boolean flag indicating whether or not the current
|
||||||
|
// container is the watchtower container itself. The watchtower container is
|
||||||
|
// identified by the presence of the "com.centurylinklabs.watchtower" label in
|
||||||
|
// the container metadata.
|
||||||
func (c Container) IsWatchtower() bool {
|
func (c Container) IsWatchtower() bool {
|
||||||
val, ok := c.containerInfo.Config.Labels[watchtowerLabel]
|
val, ok := c.containerInfo.Config.Labels[watchtowerLabel]
|
||||||
return ok && val == "true"
|
return ok && val == "true"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// StopSignal returns the custom stop signal (if any) that is encoded in the
|
||||||
|
// container's metadata. If the container has not specified a custom stop
|
||||||
|
// signal, the empty string "" is returned.
|
||||||
func (c Container) StopSignal() string {
|
func (c Container) StopSignal() string {
|
||||||
if val, ok := c.containerInfo.Config.Labels[signalLabel]; ok {
|
if val, ok := c.containerInfo.Config.Labels[signalLabel]; ok {
|
||||||
return val
|
return val
|
||||||
|
|
|
@ -12,6 +12,8 @@ type ByCreated []Container
|
||||||
func (c ByCreated) Len() int { return len(c) }
|
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) Swap(i, j int) { c[i], c[j] = c[j], c[i] }
|
||||||
|
|
||||||
|
// Less will compare two elements (identified by index) in the Container
|
||||||
|
// list by created-date.
|
||||||
func (c ByCreated) Less(i, j int) bool {
|
func (c ByCreated) Less(i, j int) bool {
|
||||||
t1, err := time.Parse(time.RFC3339Nano, c[i].containerInfo.Created)
|
t1, err := time.Parse(time.RFC3339Nano, c[i].containerInfo.Created)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -26,6 +28,11 @@ func (c ByCreated) Less(i, j int) bool {
|
||||||
return t1.Before(t2)
|
return t1.Before(t2)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SortByDependencies will sort the list of containers taking into account any
|
||||||
|
// links between containers. Container with no outgoing links will be sorted to
|
||||||
|
// the front of the list while containers with links will be sorted after all
|
||||||
|
// of their dependencies. This sort order ensures that linked containers can
|
||||||
|
// be started in the correct order.
|
||||||
func SortByDependencies(containers []Container) ([]Container, error) {
|
func SortByDependencies(containers []Container) ([]Container, error) {
|
||||||
sorter := dependencySorter{}
|
sorter := dependencySorter{}
|
||||||
return sorter.Sort(containers)
|
return sorter.Sort(containers)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue