Skip to content

Commit

Permalink
Added option to force pull docker image.
Browse files Browse the repository at this point in the history
  • Loading branch information
tnachen committed Nov 20, 2014
1 parent 2eaa7d3 commit 8682569
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 17 deletions.
5 changes: 5 additions & 0 deletions include/mesos/mesos.proto
Original file line number Diff line number Diff line change
Expand Up @@ -956,6 +956,11 @@ message ContainerInfo {
// to be supported moving forward, as we might move away from
// the docker CLI.
repeated Parameter parameters = 5;

// With this flag set to true, the docker containerizer will
// pull the docker image from the registry even if the image
// is already downloaded on the slave.
optional bool force_pull_image = 6;
}

required Type type = 1;
Expand Down
26 changes: 21 additions & 5 deletions src/docker/docker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -776,7 +776,8 @@ Future<list<Docker::Container> > Docker::__ps(

Future<Docker::Image> Docker::pull(
const string& directory,
const string& image) const
const string& image,
bool force) const
{
vector<string> argv;

Expand All @@ -793,6 +794,11 @@ Future<Docker::Image> Docker::pull(
dockerImage += ":latest";
}

if (force) {
// Skip inspect and docker pull the image.
return Docker::__pull(*this, directory, image, path);
}

argv.push_back(path);
argv.push_back("inspect");
argv.push_back(dockerImage);
Expand Down Expand Up @@ -836,9 +842,19 @@ Future<Docker::Image> Docker::_pull(
Option<int> status = s.status().get();
if (status.isSome() && status.get() == 0) {
return io::read(s.out().get())
.then(lambda::bind(&Docker::___pull, lambda::_1));
.then(lambda::bind(&Docker::____pull, lambda::_1));
}

return Docker::__pull(docker, directory, image, path);
}


Future<Docker::Image> Docker::__pull(
const Docker& docker,
const string& directory,
const string& image,
const string& path)
{
vector<string> argv;
argv.push_back(path);
argv.push_back("pull");
Expand Down Expand Up @@ -871,7 +887,7 @@ Future<Docker::Image> Docker::_pull(
// process.
return s_.get().status()
.then(lambda::bind(
&Docker::__pull,
&Docker::___pull,
docker,
s_.get(),
cmd,
Expand All @@ -888,7 +904,7 @@ void Docker::pullDiscarded(const Subprocess& s, const string& cmd)
}


Future<Docker::Image> Docker::__pull(
Future<Docker::Image> Docker::___pull(
const Docker& docker,
const Subprocess& s,
const string& cmd,
Expand All @@ -912,7 +928,7 @@ Future<Docker::Image> Docker::__pull(
}


Future<Docker::Image> Docker::___pull(
Future<Docker::Image> Docker::____pull(
const string& output)
{
Try<JSON::Array> parse = JSON::parse<JSON::Array>(output);
Expand Down
11 changes: 9 additions & 2 deletions src/docker/docker.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,8 @@ class Docker

virtual process::Future<Image> pull(
const std::string& directory,
const std::string& image) const;
const std::string& image,
bool force = false) const;

protected:
// Uses the specified path to the Docker CLI tool.
Expand Down Expand Up @@ -168,13 +169,19 @@ class Docker
const std::string& path);

static process::Future<Image> __pull(
const Docker& docker,
const std::string& directory,
const std::string& image,
const std::string& path);

static process::Future<Image> ___pull(
const Docker& docker,
const process::Subprocess& s,
const std::string& cmd,
const std::string& directory,
const std::string& image);

static process::Future<Image> ___pull(
static process::Future<Image> ____pull(
const std::string& output);

static void pullDiscarded(
Expand Down
11 changes: 8 additions & 3 deletions src/slave/containerizer/docker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -250,9 +250,10 @@ Future<Nothing> DockerContainerizerProcess::fetch(
Future<Nothing> DockerContainerizerProcess::pull(
const ContainerID& containerId,
const string& directory,
const string& image)
const string& image,
bool forcePullImage)
{
Future<Docker::Image> future = docker->pull(directory, image);
Future<Docker::Image> future = docker->pull(directory, image, forcePullImage);
containers_[containerId]->pull = future;
return future.then(defer(self(), &Self::_pull, image));
}
Expand Down Expand Up @@ -615,7 +616,11 @@ Future<Nothing> DockerContainerizerProcess::_launch(

container->state = Container::PULLING;

return pull(containerId, container->directory, container->image());
return pull(
containerId,
container->directory,
container->image(),
container->forcePullImage());
}


Expand Down
12 changes: 11 additions & 1 deletion src/slave/containerizer/docker.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,8 @@ class DockerContainerizerProcess
virtual process::Future<Nothing> pull(
const ContainerID& containerId,
const std::string& directory,
const std::string& image);
const std::string& image,
bool forcePullImage);

virtual process::Future<hashset<ContainerID>> containers();

Expand Down Expand Up @@ -311,6 +312,15 @@ class DockerContainerizerProcess
return executor.container().docker().image();
}

bool forcePullImage() const
{
if (task.isSome()) {
return task.get().container().docker().force_pull_image();
}

return executor.container().docker().force_pull_image();
}

ContainerInfo container() const
{
if (task.isSome()) {
Expand Down
15 changes: 9 additions & 6 deletions src/tests/docker_containerizer_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -291,20 +291,21 @@ class MockDockerContainerizerProcess : public DockerContainerizerProcess
EXPECT_CALL(*this, fetch(_))
.WillRepeatedly(Invoke(this, &MockDockerContainerizerProcess::_fetch));

EXPECT_CALL(*this, pull(_, _, _))
EXPECT_CALL(*this, pull(_, _, _, _))
.WillRepeatedly(Invoke(this, &MockDockerContainerizerProcess::_pull));
}

MOCK_METHOD1(
fetch,
process::Future<Nothing>(const ContainerID& containerId));

MOCK_METHOD3(
MOCK_METHOD4(
pull,
process::Future<Nothing>(
const ContainerID& containerId,
const std::string& directory,
const std::string& image));
const std::string& image,
bool forcePullImage));

process::Future<Nothing> _fetch(const ContainerID& containerId)
{
Expand All @@ -314,12 +315,14 @@ class MockDockerContainerizerProcess : public DockerContainerizerProcess
process::Future<Nothing> _pull(
const ContainerID& containerId,
const std::string& directory,
const std::string& image)
const std::string& image,
bool forcePullImage)
{
return DockerContainerizerProcess::pull(
containerId,
directory,
image);
image,
forcePullImage);
}
};

Expand Down Expand Up @@ -2474,7 +2477,7 @@ TEST_F(DockerContainerizerTest, ROOT_DOCKER_DestroyWhilePulling)
Promise<Nothing> promise;

// We want to pause the fetch call to simulate a long fetch time.
EXPECT_CALL(*process, pull(_, _, _))
EXPECT_CALL(*process, pull(_, _, _, _))
.WillOnce(Return(promise.future()));

Try<PID<Slave> > slave = StartSlave(&dockerContainerizer);
Expand Down

0 comments on commit 8682569

Please sign in to comment.