This commit is contained in:
Markus Pöschl 2016-12-27 18:29:55 +00:00 committed by GitHub
commit 4d44fc8072
4 changed files with 45 additions and 18 deletions

View file

@ -2,12 +2,13 @@ package container
import (
"fmt"
"time"
"io/ioutil"
"time"
log "github.com/Sirupsen/logrus"
dockerclient "github.com/docker/docker/client"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/network"
dockerclient "github.com/docker/docker/client"
"golang.org/x/net/context"
)
@ -76,7 +77,7 @@ func (client dockerClient) ListContainers(fn Filter) ([]Container, error) {
}
c := Container{containerInfo: &containerInfo, imageInfo: &imageInfo}
if (fn(c)) {
if fn(c) {
cs = append(cs, c)
}
}
@ -115,20 +116,52 @@ func (client dockerClient) StopContainer(c Container, timeout time.Duration) err
}
func (client dockerClient) StartContainer(c Container) error {
bg := context.Background();
bg := context.Background()
config := c.runtimeConfig()
hostConfig := c.hostConfig()
networkConfig := &network.NetworkingConfig{EndpointsConfig: c.containerInfo.NetworkSettings.Networks}
// simpleNetworkConfig is a networkConfig with only 1 network.
// see: https://github.com/docker/docker/issues/29265
simpleNetworkConfig := func() *network.NetworkingConfig {
oneEndpoint := make(map[string]*network.EndpointSettings)
for k, v := range networkConfig.EndpointsConfig {
oneEndpoint[k] = v
// we only need 1
break
}
return &network.NetworkingConfig{EndpointsConfig: oneEndpoint}
}()
name := c.Name()
log.Infof("Starting %s", name)
creation, err := client.api.ContainerCreate(bg, config, hostConfig, nil, name)
creation, err := client.api.ContainerCreate(bg, config, hostConfig, simpleNetworkConfig, name)
if err != nil {
return err
}
log.Debugf("Starting container %s (%s)", name, creation.ID)
return client.api.ContainerStart(bg, creation.ID, types.ContainerStartOptions{})
err = client.api.ContainerStart(bg, creation.ID, types.ContainerStartOptions{})
if err != nil {
return err
}
for k, _ := range simpleNetworkConfig.EndpointsConfig {
err = client.api.NetworkDisconnect(bg, k, creation.ID, true)
if err != nil {
return err
}
}
for k, v := range networkConfig.EndpointsConfig {
err = client.api.NetworkConnect(bg, k, creation.ID, v)
if err != nil {
return err
}
}
return nil
}
func (client dockerClient) RenameContainer(c Container, newName string) error {
@ -145,7 +178,7 @@ func (client dockerClient) IsContainerStale(c Container) (bool, error) {
if client.pullImages {
log.Debugf("Pulling %s for %s", imageName, c.Name())
var opts types.ImagePullOptions // ImagePullOptions can take a RegistryAuth arg to authenticate against a private registry
auth, err := EncodedAuth(imageName)
if err != nil {
@ -164,7 +197,7 @@ func (client dockerClient) IsContainerStale(c Container) (bool, error) {
return false, err
}
defer response.Close()
// the pull request will be aborted prematurely unless the response is read
_, err = ioutil.ReadAll(response)
}
@ -180,7 +213,6 @@ func (client dockerClient) IsContainerStale(c Container) (bool, error) {
} else {
log.Debugf("No new images found for %s", c.Name())
}
return false, nil
}

View file

@ -2,8 +2,6 @@ package container
import (
"errors"
"os"
"strings"
log "github.com/Sirupsen/logrus"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/reference"
@ -11,6 +9,8 @@ import (
"github.com/docker/docker/cliconfig"
"github.com/docker/docker/cliconfig/configfile"
"github.com/docker/docker/cliconfig/credentials"
"os"
"strings"
)
/**
@ -35,7 +35,7 @@ func EncodedEnvAuth(ref string) (string, error) {
username := os.Getenv("REPO_USER")
password := os.Getenv("REPO_PASS")
if username != "" && password != "" {
auth := types.AuthConfig {
auth := types.AuthConfig{
Username: username,
Password: password,
}
@ -80,7 +80,7 @@ func ParseServerAddress(ref string) (string, error) {
}
parts := strings.Split(repository, "/")
return parts[0], nil
}
// CredentialsStore returns a new credentials store based

View file

@ -1,8 +1,5 @@
package container
import (
)
func sliceEqual(s1, s2 []string) bool {
if len(s1) != len(s2) {
return false
@ -65,4 +62,3 @@ func structMapSubtract(m1, m2 map[string]struct{}) map[string]struct{} {
return m
}

View file

@ -63,4 +63,3 @@ func TestStructMapSubtract(t *testing.T) {
assert.Equal(t, map[string]struct{}{"a": x, "b": x, "c": x}, m1)
assert.Equal(t, map[string]struct{}{"a": x, "c": x}, m2)
}