Skip to content

Commit

Permalink
More helpful cmd_timeout error messages and catchable exceptions for …
Browse files Browse the repository at this point in the history
…container, fixes #31
  • Loading branch information
bflad committed Dec 30, 2013
1 parent bc9dd19 commit e2944fa
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 10 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ Attribute | Description | Type | Default
----------|-------------|------|--------
attach | Attach container's stdout/stderr and forward all signals to the process | TrueClass, FalseClass | nil
cidfile | File to store container ID | String | nil
cmd_timeout | Timeout for docker commands | Integer | `node['docker']['container_cmd_timeout']`
cmd_timeout | Timeout for docker commands (catchable exception: `Chef::Provider::Container::CommandTimeout`)| Integer | `node['docker']['container_cmd_timeout']`
command | Command to run in container | String | nil
container_name | Name for container/service | String | nil
cookbook | Cookbook to grab any templates | String | docker
Expand Down Expand Up @@ -186,7 +186,7 @@ These attributes are under the `docker_image` LWRP namespace.

Attribute | Description | Type | Default
----------|-------------|------|--------
cmd_timeout | Timeout for docker commands | Integer | `node['docker']['image_cmd_timeout']`
cmd_timeout | Timeout for docker commands (catchable exception: `Chef::Provider::Image::CommandTimeout`) | Integer | `node['docker']['image_cmd_timeout']`
dockerfile | Dockerfile to build image | String | nil
id | Image ID (internally set by LWRP) | String | nil
image_name | Image name | String | LWRP name
Expand Down
25 changes: 21 additions & 4 deletions providers/container.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
include Chef::Mixin::ShellOut
include Helpers::Docker

class CommandTimeout < RuntimeError; end

def load_current_resource
@current_resource = Chef::Resource::DockerContainer.new(new_resource)
dps = shell_out('docker ps -a -notrunc', :timeout => new_resource.cmd_timeout)
Expand Down Expand Up @@ -80,6 +82,21 @@ def container_name
end
end

def docker_cmd(cmd, timeout = new_resource.cmd_timeout)
Chef::Log.debug('Executing: docker ' + cmd)
begin
shell_out('docker ' + cmd, :timeout => timeout)
rescue Mixlib::ShellOut::CommandTimeout
raise CommandTimeout, <<-EOM
Docker command timed out:
docker #{cmd}
Please adjust node container_cmd_timeout attribute or this docker_container cmd_timeout attribute if necessary.
EOM
end
end

def exists?
@current_resource.id
end
Expand Down Expand Up @@ -136,7 +153,7 @@ def run
'volumes-from' => new_resource.volumes_from,
'w' => new_resource.working_directory
)
dr = shell_out("docker run #{run_args} #{new_resource.image} #{new_resource.command}", :timeout => new_resource.cmd_timeout)
dr = docker_cmd("run #{run_args} #{new_resource.image} #{new_resource.command}")
dr.error!
new_resource.id(dr.stdout.chomp)
service_create if service?
Expand Down Expand Up @@ -301,7 +318,7 @@ def start
if service?
service_create
else
shell_out("docker start #{start_args} #{current_resource.id}", :timeout => new_resource.cmd_timeout)
docker_cmd("start #{start_args} #{current_resource.id}")
end
end

Expand All @@ -312,10 +329,10 @@ def stop
if service?
service_stop
else
shell_out("docker stop #{stop_args} #{current_resource.id}", :timeout => (new_resource.cmd_timeout + 15))
docker_cmd("stop #{stop_args} #{current_resource.id}", (new_resource.cmd_timeout + 15))
end
end

def wait
shell_out("docker wait #{current_resource.id}", :timeout => new_resource.cmd_timeout)
docker_cmd("wait #{current_resource.id}")
end
25 changes: 21 additions & 4 deletions providers/image.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
include Chef::Mixin::ShellOut
include Helpers::Docker

class CommandTimeout < RuntimeError; end

def load_current_resource
@current_resource = Chef::Resource::DockerImage.new(new_resource)
di = shell_out('docker images -a', :timeout => new_resource.cmd_timeout)
Expand Down Expand Up @@ -59,7 +61,22 @@ def build
command = new_resource.image_url
end

shell_out("docker build -t #{full_image_name} #{command}", :timeout => new_resource.cmd_timeout)
docker_cmd("build -t #{full_image_name} #{command}")
end

def docker_cmd(cmd, timeout = new_resource.cmd_timeout)
Chef::Log.debug('Executing: docker ' + cmd)
begin
shell_out('docker ' + cmd, :timeout => timeout)
rescue Mixlib::ShellOut::CommandTimeout
raise CommandTimeout, <<-EOM
Docker command timed out:
docker #{cmd}
Please adjust node image_cmd_timeout attribute or this docker_image cmd_timeout attribute if necessary.
EOM
end
end

def import
Expand All @@ -72,7 +89,7 @@ def import
import_args += " #{new_resource.tag}" if new_resource.tag
end

shell_out("docker import #{import_args}", :timeout => new_resource.cmd_timeout)
docker_cmd("import #{import_args}")
end

def installed?
Expand All @@ -84,11 +101,11 @@ def pull
'registry' => new_resource.registry,
't' => new_resource.tag
)
shell_out("docker pull #{new_resource.image_name} #{pull_args}", :timeout => new_resource.cmd_timeout)
docker_cmd("pull #{new_resource.image_name} #{pull_args}")
end

def remove
shell_out("docker rmi #{new_resource.image_name}", :timeout => new_resource.cmd_timeout)
docker_cmd("rmi #{new_resource.image_name}")
end

def tag_match
Expand Down

0 comments on commit e2944fa

Please sign in to comment.