2015-07-14 22:51:12 +02:00
|
|
|
package docker
|
|
|
|
|
|
|
|
import (
|
2015-07-16 22:25:01 +02:00
|
|
|
"fmt"
|
2015-07-14 22:51:12 +02:00
|
|
|
"log"
|
2015-07-16 22:25:01 +02:00
|
|
|
"strings"
|
2015-07-17 20:56:09 +02:00
|
|
|
"time"
|
2015-07-14 22:51:12 +02:00
|
|
|
|
|
|
|
"github.com/samalba/dockerclient"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
pullImages bool
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
pullImages = true
|
|
|
|
}
|
|
|
|
|
2015-07-21 00:54:18 +02:00
|
|
|
type ContainerFilter func(Container) bool
|
|
|
|
|
|
|
|
func AllContainersFilter(Container) bool { return true }
|
|
|
|
func WatchtowerContainersFilter(c Container) bool { return c.IsWatchtower() }
|
|
|
|
|
2015-07-14 22:51:12 +02:00
|
|
|
type Client interface {
|
2015-07-21 00:54:18 +02:00
|
|
|
ListContainers(ContainerFilter) ([]Container, error)
|
|
|
|
RefreshImage(*Container) error
|
|
|
|
Stop(Container, time.Duration) error
|
|
|
|
Start(Container) error
|
|
|
|
Rename(Container, string) error
|
2015-07-14 22:51:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewClient() Client {
|
|
|
|
docker, err := dockerclient.NewDockerClient("unix:///var/run/docker.sock", nil)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Error instantiating Docker client: %s\n", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return DockerClient{api: docker}
|
|
|
|
}
|
|
|
|
|
|
|
|
type DockerClient struct {
|
|
|
|
api dockerclient.Client
|
|
|
|
}
|
|
|
|
|
2015-07-21 00:54:18 +02:00
|
|
|
func (client DockerClient) ListContainers(fn ContainerFilter) ([]Container, error) {
|
2015-07-14 22:51:12 +02:00
|
|
|
cs := []Container{}
|
|
|
|
|
|
|
|
runningContainers, err := client.api.ListContainers(false, false, "")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, runningContainer := range runningContainers {
|
|
|
|
containerInfo, err := client.api.InspectContainer(runningContainer.Id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
imageInfo, err := client.api.InspectImage(containerInfo.Image)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-07-21 00:54:18 +02:00
|
|
|
c := Container{containerInfo: containerInfo, imageInfo: imageInfo}
|
|
|
|
if fn(c) {
|
|
|
|
cs = append(cs, Container{containerInfo: containerInfo, imageInfo: imageInfo})
|
|
|
|
}
|
2015-07-14 22:51:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return cs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (client DockerClient) RefreshImage(c *Container) error {
|
|
|
|
containerInfo := c.containerInfo
|
|
|
|
oldImageInfo := c.imageInfo
|
|
|
|
imageName := containerInfo.Config.Image
|
|
|
|
|
|
|
|
if pullImages {
|
2015-07-16 22:25:01 +02:00
|
|
|
if !strings.Contains(imageName, ":") {
|
|
|
|
imageName = fmt.Sprintf("%s:latest", imageName)
|
|
|
|
}
|
|
|
|
|
2015-07-14 22:51:12 +02:00
|
|
|
log.Printf("Pulling %s for %s\n", imageName, c.Name())
|
|
|
|
if err := client.api.PullImage(imageName, nil); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
newImageInfo, err := client.api.InspectImage(imageName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if newImageInfo.Id != oldImageInfo.Id {
|
|
|
|
log.Printf("Found new %s image (%s)\n", imageName, newImageInfo.Id)
|
|
|
|
c.Stale = true
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-07-21 00:54:18 +02:00
|
|
|
func (client DockerClient) Stop(c Container, timeout time.Duration) error {
|
2015-07-14 22:51:12 +02:00
|
|
|
signal := "SIGTERM"
|
|
|
|
|
|
|
|
if sig, ok := c.containerInfo.Config.Labels["com.centurylinklabs.watchtower.stop-signal"]; ok {
|
|
|
|
signal = sig
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("Stopping: %s\n", c.Name())
|
|
|
|
|
|
|
|
if err := client.api.KillContainer(c.containerInfo.Id, signal); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-07-17 20:56:09 +02:00
|
|
|
// Wait for container to exit, but proceed anyway after 10 seconds
|
2015-07-21 00:54:18 +02:00
|
|
|
client.waitForStop(c, timeout)
|
2015-07-17 20:56:09 +02:00
|
|
|
|
2015-07-14 22:51:12 +02:00
|
|
|
return client.api.RemoveContainer(c.containerInfo.Id, true, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (client DockerClient) Start(c Container) error {
|
|
|
|
config := c.runtimeConfig()
|
|
|
|
hostConfig := c.hostConfig()
|
2015-07-21 00:54:18 +02:00
|
|
|
name := c.Name()
|
2015-07-14 22:51:12 +02:00
|
|
|
|
2015-07-21 00:54:18 +02:00
|
|
|
if name == "" {
|
|
|
|
log.Printf("Starting new container from %s", c.containerInfo.Config.Image)
|
|
|
|
} else {
|
|
|
|
log.Printf("Starting %s\n", name)
|
|
|
|
}
|
2015-07-14 22:51:12 +02:00
|
|
|
|
2015-07-21 00:54:18 +02:00
|
|
|
newContainerId, err := client.api.CreateContainer(config, name)
|
2015-07-14 22:51:12 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return client.api.StartContainer(newContainerId, hostConfig)
|
|
|
|
}
|
2015-07-21 00:54:18 +02:00
|
|
|
|
|
|
|
func (client DockerClient) Rename(c Container, newName string) error {
|
|
|
|
return client.api.RenameContainer(c.containerInfo.Id, newName)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (client DockerClient) waitForStop(c Container, waitTime time.Duration) error {
|
|
|
|
timeout := time.After(waitTime)
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-timeout:
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
if ci, err := client.api.InspectContainer(c.containerInfo.Id); err != nil {
|
|
|
|
return err
|
|
|
|
} else if !ci.State.Running {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|