|
|
@ -1,13 +1,14 @@
|
|
|
|
package container
|
|
|
|
package container
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
|
|
|
|
|
|
|
"fmt"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
|
|
log "github.com/Sirupsen/logrus"
|
|
|
|
log "github.com/Sirupsen/logrus"
|
|
|
|
"github.com/samalba/dockerclient"
|
|
|
|
dockerclient "github.com/docker/docker/client"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
const (
|
|
|
@ -17,6 +18,7 @@ const (
|
|
|
|
var username = os.Getenv("REPO_USER")
|
|
|
|
var username = os.Getenv("REPO_USER")
|
|
|
|
var password = os.Getenv("REPO_PASS")
|
|
|
|
var password = os.Getenv("REPO_PASS")
|
|
|
|
var email = os.Getenv("REPO_EMAIL")
|
|
|
|
var email = os.Getenv("REPO_EMAIL")
|
|
|
|
|
|
|
|
var api_version = "1.24"
|
|
|
|
|
|
|
|
|
|
|
|
// A Filter is a prototype for a function that can be used to filter the
|
|
|
|
// 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.
|
|
|
|
// results from a call to the ListContainers() method on the Client.
|
|
|
@ -31,48 +33,57 @@ type Client interface {
|
|
|
|
RenameContainer(Container, string) error
|
|
|
|
RenameContainer(Container, string) error
|
|
|
|
IsContainerStale(Container) (bool, error)
|
|
|
|
IsContainerStale(Container) (bool, error)
|
|
|
|
RemoveImage(Container) error
|
|
|
|
RemoveImage(Container) error
|
|
|
|
|
|
|
|
RegistryLogin(string) error
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// NewClient returns a new Client instance which can be used to interact with
|
|
|
|
// NewClient returns a new Client instance which can be used to interact with
|
|
|
|
// the Docker API.
|
|
|
|
// the Docker API.
|
|
|
|
func NewClient(dockerHost string, tlsConfig *tls.Config, pullImages bool) Client {
|
|
|
|
func NewClient(dockerHost string, pullImages bool) Client {
|
|
|
|
docker, err := dockerclient.NewDockerClient(dockerHost, tlsConfig)
|
|
|
|
cli, err := dockerclient.NewClient(dockerHost, api_version, nil, nil)
|
|
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Error instantiating Docker client: %s", err)
|
|
|
|
log.Fatalf("Error instantiating Docker client: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return dockerClient{api: docker, pullImages: pullImages}
|
|
|
|
return dockerClient{api: cli, pullImages: pullImages}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type dockerClient struct {
|
|
|
|
type dockerClient struct {
|
|
|
|
api dockerclient.Client
|
|
|
|
api *dockerclient.Client
|
|
|
|
pullImages bool
|
|
|
|
pullImages bool
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (client dockerClient) RegistryLogin(registryURL string) error {
|
|
|
|
|
|
|
|
log.Debug("Login not implemented")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (client dockerClient) ListContainers(fn Filter) ([]Container, error) {
|
|
|
|
func (client dockerClient) ListContainers(fn Filter) ([]Container, error) {
|
|
|
|
cs := []Container{}
|
|
|
|
cs := []Container{}
|
|
|
|
|
|
|
|
bg := context.Background()
|
|
|
|
|
|
|
|
|
|
|
|
log.Debug("Retrieving running containers")
|
|
|
|
log.Debug("Retrieving running containers")
|
|
|
|
|
|
|
|
|
|
|
|
runningContainers, err := client.api.ListContainers(false, false, "")
|
|
|
|
runningContainers, err := client.api.ContainerList(
|
|
|
|
|
|
|
|
bg,
|
|
|
|
|
|
|
|
types.ContainerListOptions{})
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
for _, runningContainer := range runningContainers {
|
|
|
|
for _, runningContainer := range runningContainers {
|
|
|
|
containerInfo, err := client.api.InspectContainer(runningContainer.Id)
|
|
|
|
containerInfo, err := client.api.ContainerInspect(bg, runningContainer.ID)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
imageInfo, err := client.api.InspectImage(containerInfo.Image)
|
|
|
|
imageInfo, _, err := client.api.ImageInspectWithRaw(bg, containerInfo.Image)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
c := Container{containerInfo: containerInfo, imageInfo: imageInfo}
|
|
|
|
c := Container{containerInfo: &containerInfo, imageInfo: &imageInfo}
|
|
|
|
if fn(c) {
|
|
|
|
if (fn(c)) {
|
|
|
|
cs = append(cs, c)
|
|
|
|
cs = append(cs, c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -81,6 +92,7 @@ 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 {
|
|
|
|
|
|
|
|
bg := context.Background()
|
|
|
|
signal := c.StopSignal()
|
|
|
|
signal := c.StopSignal()
|
|
|
|
if signal == "" {
|
|
|
|
if signal == "" {
|
|
|
|
signal = defaultStopSignal
|
|
|
|
signal = defaultStopSignal
|
|
|
@ -88,7 +100,7 @@ func (client dockerClient) StopContainer(c Container, timeout time.Duration) err
|
|
|
|
|
|
|
|
|
|
|
|
log.Infof("Stopping %s (%s) with %s", c.Name(), c.ID(), signal)
|
|
|
|
log.Infof("Stopping %s (%s) with %s", c.Name(), c.ID(), signal)
|
|
|
|
|
|
|
|
|
|
|
|
if err := client.api.KillContainer(c.ID(), signal); err != nil {
|
|
|
|
if err := client.api.ContainerKill(bg, c.ID(), signal); err != nil {
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -97,7 +109,7 @@ func (client dockerClient) StopContainer(c Container, timeout time.Duration) err
|
|
|
|
|
|
|
|
|
|
|
|
log.Debugf("Removing container %s", c.ID())
|
|
|
|
log.Debugf("Removing container %s", c.ID())
|
|
|
|
|
|
|
|
|
|
|
|
if err := client.api.RemoveContainer(c.ID(), true, false); err != nil {
|
|
|
|
if err := client.api.ContainerRemove(bg, c.ID(), types.ContainerRemoveOptions{Force: true, RemoveVolumes: false}); err != nil {
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -110,72 +122,55 @@ func (client dockerClient) StopContainer(c Container, timeout time.Duration) err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (client dockerClient) StartContainer(c Container) error {
|
|
|
|
func (client dockerClient) StartContainer(c Container) error {
|
|
|
|
|
|
|
|
bg := context.Background();
|
|
|
|
config := c.runtimeConfig()
|
|
|
|
config := c.runtimeConfig()
|
|
|
|
hostConfig := c.hostConfig()
|
|
|
|
hostConfig := c.hostConfig()
|
|
|
|
name := c.Name()
|
|
|
|
name := c.Name()
|
|
|
|
|
|
|
|
|
|
|
|
log.Infof("Starting %s", name)
|
|
|
|
log.Infof("Starting %s", name)
|
|
|
|
|
|
|
|
creation, err := client.api.ContainerCreate(bg, config, hostConfig, nil, name)
|
|
|
|
var err error
|
|
|
|
|
|
|
|
var newContainerID string
|
|
|
|
|
|
|
|
if username != "" && password != "" && email != "" {
|
|
|
|
|
|
|
|
auth := dockerclient.AuthConfig{
|
|
|
|
|
|
|
|
Username: username,
|
|
|
|
|
|
|
|
Password: password,
|
|
|
|
|
|
|
|
Email: email,
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
newContainerID, err = client.api.CreateContainer(config, name, &auth)
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
newContainerID, err = client.api.CreateContainer(config, name, nil)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
log.Debugf("Starting container %s (%s)", name, newContainerID)
|
|
|
|
log.Debugf("Starting container %s (%s)", name, creation.ID)
|
|
|
|
|
|
|
|
|
|
|
|
return client.api.StartContainer(newContainerID, hostConfig)
|
|
|
|
return client.api.ContainerStart(bg, creation.ID, types.ContainerStartOptions{})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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.ID(), newName)
|
|
|
|
log.Debugf("Renaming container %s (%s) to %s", c.Name(), c.ID(), newName)
|
|
|
|
return client.api.RenameContainer(c.ID(), newName)
|
|
|
|
//return client.api.ContainerRename(c.ID(), newName)
|
|
|
|
|
|
|
|
// no op
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (client dockerClient) IsContainerStale(c Container) (bool, error) {
|
|
|
|
func (client dockerClient) IsContainerStale(c Container) (bool, error) {
|
|
|
|
|
|
|
|
bg := context.Background()
|
|
|
|
oldImageInfo := c.imageInfo
|
|
|
|
oldImageInfo := c.imageInfo
|
|
|
|
imageName := c.ImageName()
|
|
|
|
imageName := c.ImageName()
|
|
|
|
|
|
|
|
|
|
|
|
if client.pullImages {
|
|
|
|
if client.pullImages {
|
|
|
|
log.Debugf("Pulling %s for %s", imageName, c.Name())
|
|
|
|
log.Debugf("Pulling %s for %s", imageName, c.Name())
|
|
|
|
|
|
|
|
|
|
|
|
if username != "" && password != "" && email != "" {
|
|
|
|
// Note: ImagePullOptions below can take a RegistryAuth arg if 401 on private registry
|
|
|
|
auth := dockerclient.AuthConfig{
|
|
|
|
closer, err := client.api.ImagePull(bg, imageName, types.ImagePullOptions{})
|
|
|
|
Username: username,
|
|
|
|
if (err != nil) {
|
|
|
|
Password: password,
|
|
|
|
|
|
|
|
Email: email,
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := client.api.PullImage(imageName, &auth); err != nil {
|
|
|
|
|
|
|
|
log.Debugf("Error pulling image %s with authentication, %s", imageName, err)
|
|
|
|
|
|
|
|
return false, err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
if err := client.api.PullImage(imageName, nil); err != nil {
|
|
|
|
|
|
|
|
log.Debugf("Error pulling image %s, %s", imageName, err)
|
|
|
|
log.Debugf("Error pulling image %s, %s", imageName, err)
|
|
|
|
return false, err
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
|
|
|
|
closer.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
newImageInfo, err := client.api.InspectImage(imageName)
|
|
|
|
newImageInfo, _, err := client.api.ImageInspectWithRaw(bg, imageName)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if newImageInfo.Id != oldImageInfo.Id {
|
|
|
|
if newImageInfo.ID != oldImageInfo.ID {
|
|
|
|
log.Infof("Found new %s image (%s)", imageName, newImageInfo.Id)
|
|
|
|
log.Infof("Found new %s image (%s)", imageName, newImageInfo.ID)
|
|
|
|
return true, nil
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -185,11 +180,12 @@ func (client dockerClient) IsContainerStale(c Container) (bool, error) {
|
|
|
|
func (client dockerClient) RemoveImage(c Container) error {
|
|
|
|
func (client dockerClient) RemoveImage(c Container) error {
|
|
|
|
imageID := c.ImageID()
|
|
|
|
imageID := c.ImageID()
|
|
|
|
log.Infof("Removing image %s", imageID)
|
|
|
|
log.Infof("Removing image %s", imageID)
|
|
|
|
_, err := client.api.RemoveImage(imageID, true)
|
|
|
|
_, err := client.api.ImageRemove(context.Background(), imageID, types.ImageRemoveOptions{Force: true})
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (client dockerClient) waitForStop(c Container, waitTime time.Duration) error {
|
|
|
|
func (client dockerClient) waitForStop(c Container, waitTime time.Duration) error {
|
|
|
|
|
|
|
|
bg := context.Background()
|
|
|
|
timeout := time.After(waitTime)
|
|
|
|
timeout := time.After(waitTime)
|
|
|
|
|
|
|
|
|
|
|
|
for {
|
|
|
|
for {
|
|
|
@ -197,7 +193,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.ID()); err != nil {
|
|
|
|
if ci, err := client.api.ContainerInspect(bg, c.ID()); err != nil {
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
} else if !ci.State.Running {
|
|
|
|
} else if !ci.State.Running {
|
|
|
|
return nil
|
|
|
|
return nil
|
|
|
|