Add --no-pull support

pull/1/head
Brian DeHamer 9 years ago
parent a8dec129f5
commit 3d0c853e42

@ -14,14 +14,6 @@ const (
signalLabel = "com.centurylinklabs.watchtower.stop-signal"
)
var (
pullImages bool
)
func init() {
pullImages = true
}
type Filter func(Container) bool
type Client interface {
@ -32,18 +24,19 @@ type Client interface {
IsContainerStale(Container) (bool, error)
}
func NewClient(dockerHost string) Client {
func NewClient(dockerHost string, pullImages bool) Client {
docker, err := dockerclient.NewDockerClient(dockerHost, nil)
if err != nil {
log.Fatalf("Error instantiating Docker client: %s\n", err)
}
return DockerClient{api: docker}
return DockerClient{api: docker, pullImages: pullImages}
}
type DockerClient struct {
api dockerclient.Client
api dockerclient.Client
pullImages bool
}
func (client DockerClient) ListContainers(fn Filter) ([]Container, error) {
@ -121,7 +114,7 @@ func (client DockerClient) IsContainerStale(c Container) (bool, error) {
oldImageInfo := c.imageInfo
imageName := containerInfo.Config.Image
if pullImages {
if client.pullImages {
if !strings.Contains(imageName, ":") {
imageName = fmt.Sprintf("%s:latest", imageName)
}

@ -305,7 +305,7 @@ func TestIsContainerStale_NotStaleSuccess(t *testing.T) {
api.On("PullImage", "bar:latest", mock.Anything).Return(nil)
api.On("InspectImage", "bar:latest").Return(newImageInfo, nil)
client := DockerClient{api: api}
client := DockerClient{api: api, pullImages: true}
stale, err := client.IsContainerStale(c)
assert.NoError(t, err)
@ -327,7 +327,28 @@ func TestIsContainerStale_StaleSuccess(t *testing.T) {
api.On("PullImage", "bar:1.0", mock.Anything).Return(nil)
api.On("InspectImage", "bar:1.0").Return(newImageInfo, nil)
client := DockerClient{api: api}
client := DockerClient{api: api, pullImages: true}
stale, err := client.IsContainerStale(c)
assert.NoError(t, err)
assert.True(t, stale)
api.AssertExpectations(t)
}
func TestIsContainerStale_NoPullSuccess(t *testing.T) {
c := Container{
containerInfo: &dockerclient.ContainerInfo{
Name: "foo",
Config: &dockerclient.ContainerConfig{Image: "bar:1.0"},
},
imageInfo: &dockerclient.ImageInfo{Id: "abc123"},
}
newImageInfo := &dockerclient.ImageInfo{Id: "xyz789"}
api := mockclient.NewMockClient()
api.On("InspectImage", "bar:1.0").Return(newImageInfo, nil)
client := DockerClient{api: api, pullImages: false}
stale, err := client.IsContainerStale(c)
assert.NoError(t, err)
@ -347,7 +368,7 @@ func TestIsContainerStale_PullImageError(t *testing.T) {
api := mockclient.NewMockClient()
api.On("PullImage", "bar:latest", mock.Anything).Return(errors.New("oops"))
client := DockerClient{api: api}
client := DockerClient{api: api, pullImages: true}
_, err := client.IsContainerStale(c)
assert.Error(t, err)
@ -369,7 +390,7 @@ func TestIsContainerStale_InspectImageError(t *testing.T) {
api.On("PullImage", "bar:latest", mock.Anything).Return(nil)
api.On("InspectImage", "bar:latest").Return(newImageInfo, errors.New("uh-oh"))
client := DockerClient{api: api}
client := DockerClient{api: api, pullImages: true}
_, err := client.IsContainerStale(c)
assert.Error(t, err)

@ -35,6 +35,10 @@ func main() {
Value: 300,
Usage: "poll interval (in seconds)",
},
cli.BoolFlag{
Name: "no-pull",
Usage: "do not pull new images",
},
}
handleSignals()
@ -76,5 +80,6 @@ func start(c *cli.Context) {
func newContainerClient(c *cli.Context) container.Client {
dockerHost := c.GlobalString("host")
return container.NewClient(dockerHost)
noPull := c.GlobalBool("no-pull")
return container.NewClient(dockerHost, !noPull)
}

Loading…
Cancel
Save