Skip to content

Commit

Permalink
Update all LWRPs to check that docker is alive.
Browse files Browse the repository at this point in the history
Before issuing any commands that talk to the local docker daemon,
make sure it is running (since its service starts in the background).
  • Loading branch information
Joe Crobak committed Feb 3, 2014
1 parent c94d799 commit 85d3f25
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 0 deletions.
1 change: 1 addition & 0 deletions attributes/default.rb
Expand Up @@ -14,6 +14,7 @@
default['docker']['http_proxy'] = nil
default['docker']['image_cmd_timeout'] = 300
default['docker']['registry_cmd_timeout'] = 60
default['docker']['docker_daemon_timeout'] = 10

default['docker']['init_type'] = value_for_platform(
%w{ centos debian oracle redhat } => {
Expand Down
49 changes: 49 additions & 0 deletions libraries/helpers.rb
@@ -1,7 +1,25 @@
require 'chef/mixin/shell_out'
include Chef::Mixin::ShellOut

# Helpers module
module Helpers
# Helpers::Docker module
module Docker
class DockerNotReady < StandardError
def initialize(endpoint, timeout)
super <<-EOH
The Docker daemon did not become ready within #{timeout} seconds.
This most likely means that Docker failed to start.
Docker can fail to start if:
- a configuration file is invalid
- permissions are incorrect for the root directory of the docker runtime.

This comment has been minimized.

Copy link
@bflad

bflad Feb 4, 2014

Please see: sous-chefs#49 -- the socket may not be available because docker has not fully started yet (due to slow node performance, etc).

If this problem persists, check your service log files.
EOH
end
end

def cli_args(spec)
cli_line = ''
spec.each_pair do |arg, value|
Expand All @@ -18,5 +36,36 @@ def cli_args(spec)
end
cli_line
end

def timeout
node['docker']['docker_daemon_timeout']
end

# This is based upon wait_until_ready! from the opscode jenkins cookbook.
#
# Since the docker service returns immediately and the actual docker
# process is started as a daemon, we block the Chef Client run until the
# daemon is actually ready.
#
# This method will effectively "block" the current thread until the docker
# daemon is ready
#
# @raise [DockerNotReady]
# if the Docker master does not respond within (+timeout+) seconds
#
def wait_until_ready!
Timeout.timeout(timeout) do
while true do
result = shell_out('docker info')
if not !Array(result.valid_exit_codes).include?(result.exitstatus)
break
end
Chef::Log.debug("Docker daemon is not running - #{result.stdout}\n#{result.stderr}")
sleep(0.5)
end
end
rescue Timeout::Error
raise DockerNotReady.new(timeout)
end
end
end
1 change: 1 addition & 0 deletions providers/container.rb
Expand Up @@ -6,6 +6,7 @@ class CommandTimeout < RuntimeError; end

def load_current_resource
@current_resource = Chef::Resource::DockerContainer.new(new_resource)
wait_until_ready!
dps = docker_cmd('ps -a -notrunc')
dps.stdout.each_line do |dps_line|
next unless dps_line.include?(new_resource.image)
Expand Down
1 change: 1 addition & 0 deletions providers/image.rb
Expand Up @@ -5,6 +5,7 @@
class CommandTimeout < RuntimeError; end

def load_current_resource
wait_until_ready!
@current_resource = Chef::Resource::DockerImage.new(new_resource)
di = docker_cmd('images -a')
if di.stdout.include?(new_resource.image_name)
Expand Down
1 change: 1 addition & 0 deletions providers/registry.rb
Expand Up @@ -6,6 +6,7 @@ class CommandTimeout < RuntimeError; end

def load_current_resource
@current_resource = Chef::Resource::DockerRegistry.new(new_resource)
wait_until_ready!
# TODO: load current resource?
@current_resource
end
Expand Down

1 comment on commit 85d3f25

@bflad
Copy link

@bflad bflad commented on 85d3f25 Feb 3, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is awesome functionality.

Please sign in to comment.