From c2cae80de5237912406d569638ed41834adc86f8 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 8 Jul 2015 10:09:15 -0600 Subject: [PATCH] providers/dock: pull image prior to starting --- CHANGELOG.md | 1 + Vagrantfile | 62 +------------------ plugins/providers/docker/action.rb | 2 + plugins/providers/docker/action/pull.rb | 26 ++++++++ plugins/providers/docker/driver.rb | 4 ++ templates/locales/providers_docker.yml | 2 + .../plugins/providers/docker/driver_test.rb | 7 +++ 7 files changed, 44 insertions(+), 60 deletions(-) create mode 100644 plugins/providers/docker/action/pull.rb diff --git a/CHANGELOG.md b/CHANGELOG.md index a16d67b79..f79218d8b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,6 +26,7 @@ IMPROVEMENTS: - guests/photon: initial support [GH-5612] - guests/solaris,solaris11: support inserting generated key [GH-5182] [GH-5290] + - providers/docker: images are pulled prior to starting [GH-5249] - provisioners/chef: add capability for checking if Chef is installed on Windows [GH-5669] - provisioners/puppet: add support for Puppet 4 and configuration options [GH-5601] - provisioners/puppet: add support for `synced_folder_args` in apply [GH-5359] diff --git a/Vagrantfile b/Vagrantfile index 1e20fca33..f72d43b98 100644 --- a/Vagrantfile +++ b/Vagrantfile @@ -4,65 +4,7 @@ # Ruby, run unit tests, etc. Vagrant.configure("2") do |config| - config.vm.box = "hashicorp/precise64" - config.vm.hostname = "vagrant" - config.ssh.shell = "bash -c 'BASH_ENV=/etc/profile exec bash'" - - ["vmware_fusion", "vmware_workstation", "virtualbox"].each do |provider| - config.vm.provider provider do |v, override| - v.memory = "1024" - end - end - - config.vm.provision "shell", inline: $shell - - config.push.define "www", strategy: "local-exec" do |push| - push.script = "scripts/website_push_www.sh" - end - - config.push.define "docs", strategy: "local-exec" do |push| - push.script = "scripts/website_push_docs.sh" + config.vm.provider "docker" do |d| + d.image = "nginx" end end - -$shell = <<-CONTENTS -export DEBIAN_FRONTEND=noninteractive -MARKER_FILE="/usr/local/etc/vagrant_provision_marker" - -# Only provision once -if [ -f "${MARKER_FILE}" ]; then - exit 0 -fi - -# Update apt -apt-get update - -# Install basic dependencies -apt-get install -y build-essential bsdtar curl - -# Import the mpapis public key to verify downloaded releases -su -l -c 'curl -sSL https://rvm.io/mpapis.asc | gpg -q --import -' vagrant - -# Install RVM -su -l -c 'curl -sL https://get.rvm.io | bash -s stable' vagrant - -# Add the vagrant user to the RVM group -#usermod -a -G rvm vagrant - -# Install some Rubies -su -l -c 'rvm install 2.1.1' vagrant -su -l -c 'rvm --default use 2.1.1' vagrant - -# Output the Ruby version (for sanity) -su -l -c 'ruby --version' vagrant - -# Install Git -apt-get install -y git - -# Automatically move into the shared folder, but only add the command -# if it's not already there. -grep -q 'cd /vagrant' /home/vagrant/.bash_profile || echo 'cd /vagrant' >> /home/vagrant/.bash_profile - -# Touch the marker file so we don't do this again -touch ${MARKER_FILE} -CONTENTS diff --git a/plugins/providers/docker/action.rb b/plugins/providers/docker/action.rb index 7ea2bae17..c498c5b14 100644 --- a/plugins/providers/docker/action.rb +++ b/plugins/providers/docker/action.rb @@ -258,6 +258,7 @@ module VagrantPlugins b3.use HandleForwardedPortCollisions b3.use SyncedFolders b3.use ForwardedPorts + b3.use Pull b3.use Create b3.use WaitForRunning else @@ -302,6 +303,7 @@ module VagrantPlugins autoload :IsBuild, action_root.join("is_build") autoload :IsHostMachineCreated, action_root.join("is_host_machine_created") autoload :Login, action_root.join("login") + autoload :Pull, action_root.join("pull") autoload :PrepareSSH, action_root.join("prepare_ssh") autoload :Stop, action_root.join("stop") autoload :PrepareNFSValidIds, action_root.join("prepare_nfs_valid_ids") diff --git a/plugins/providers/docker/action/pull.rb b/plugins/providers/docker/action/pull.rb new file mode 100644 index 000000000..f8695d2c4 --- /dev/null +++ b/plugins/providers/docker/action/pull.rb @@ -0,0 +1,26 @@ +module VagrantPlugins + module DockerProvider + module Action + class Pull + def initialize(app, env) + @app = app + end + + def call(env) + @env = env + @machine = env[:machine] + @provider_config = @machine.provider_config + @driver = @machine.provider.driver + + image = @env[:create_image] + image ||= @provider_config.image + + env[:ui].output(I18n.t("docker_provider.pull", image: image)) + @driver.pull(image) + + @app.call(env) + end + end + end + end +end diff --git a/plugins/providers/docker/driver.rb b/plugins/providers/docker/driver.rb index 3ce34dc6e..421369a96 100644 --- a/plugins/providers/docker/driver.rb +++ b/plugins/providers/docker/driver.rb @@ -102,6 +102,10 @@ module VagrantPlugins execute(*cmd.flatten) end + def pull(image) + execute('docker', 'pull', image) + end + def start(cid) if !running?(cid) execute('docker', 'start', cid) diff --git a/templates/locales/providers_docker.yml b/templates/locales/providers_docker.yml index c04248a95..c2ddb991c 100644 --- a/templates/locales/providers_docker.yml +++ b/templates/locales/providers_docker.yml @@ -43,6 +43,8 @@ en: Container not created. Skipping. not_docker_provider: |- Not backed by Docker provider. Skipping. + pull: |- + Pulling image '%{image}'... run_command_required: |- `vagrant docker-run` requires a command to execute. This command must be specified after a `--` in the command line. This is used diff --git a/test/unit/plugins/providers/docker/driver_test.rb b/test/unit/plugins/providers/docker/driver_test.rb index 05ac67f88..dbfde3595 100644 --- a/test/unit/plugins/providers/docker/driver_test.rb +++ b/test/unit/plugins/providers/docker/driver_test.rb @@ -84,6 +84,13 @@ describe VagrantPlugins::DockerProvider::Driver do end end + describe '#pull' do + it 'should pull images' do + subject.should_receive(:execute).with('docker', 'pull', 'foo') + subject.pull('foo') + end + end + describe '#running?' do let(:result) { subject.running?(cid) }