Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure docker resource works with docker 1.13+ #1966

Merged
merged 1 commit into from
Jun 26, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 30 additions & 3 deletions lib/resources/docker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,20 @@ def images

def version
return @version if defined?(@version)
data = JSON.parse(inspec.command('docker version --format \'{{ json . }}\'').stdout)
data = {}
cmd = inspec.command('docker version --format \'{{ json . }}\'')
data = JSON.parse(cmd.stdout) if cmd.exit_status == 0
@version = Hashie::Mash.new(data)
rescue JSON::ParserError => _e
return Hashie::Mash.new({})
end

def info
return @info if defined?(@info)
data = JSON.parse(inspec.command('docker info --format \'{{ json . }}\'').stdout)
data = {}
# docke info format is only supported for Docker 17.03+
cmd = inspec.command('docker info --format \'{{ json . }}\'')
data = JSON.parse(cmd.stdout) if cmd.exit_status == 0
@info = Hashie::Mash.new(data)
rescue JSON::ParserError => _e
return Hashie::Mash.new({})
Expand All @@ -138,7 +143,19 @@ def to_s
private

def parse_containers
raw_containers = inspec.command('docker ps -a --no-trunc --format \'{{ json . }}\'').stdout
# @see https://github.com/moby/moby/issues/20625, works for docker 1.13+
# raw_containers = inspec.command('docker ps -a --no-trunc --format \'{{ json . }}\'').stdout
# therefore we stick with older approach
labels = %w{Command CreatedAt ID Image Labels Mounts Names Ports RunningFor Size Status}

# Networks LocalVolumes work with 1.13+ only
if !version.empty? && Gem::Version.new(version['Client']['Version']) >= Gem::Version.new('1.13')
labels.push('Networks')
labels.push('LocalVolumes')
end
# build command
format = labels.map { |label| "\"#{label}\": {{json .#{label}}}" }
raw_containers = inspec.command("docker ps -a --no-trunc --format '{#{format.join(', ')}}'").stdout
ps = []
# since docker is not outputting valid json, we need to parse each row
raw_containers.each_line { |entry|
Expand All @@ -147,6 +164,9 @@ def parse_containers
j = j.map { |k, v|
[k.downcase, v]
}.to_h

# ensure all keys are there
j = ensure_container_keys(j)
ps.push(j)
}
ps
Expand All @@ -155,6 +175,13 @@ def parse_containers
[]
end

def ensure_container_keys(entry)
%w{Command CreatedAt ID Image Labels Mounts Names Ports RunningFor Size Status Networks LocalVolumes}.each { |key|
entry[key.downcase] = nil if !entry.key?(key.downcase)
}
entry
end

def parse_images
# docker does not support the `json .` function here, therefore we need to emulate that behavior.
raw_images = inspec.command('docker images -a --no-trunc --format \'{ "id": {{json .ID}}, "repository": {{json .Repository}}, "tag": {{json .Tag}}, "size": {{json .Size}}, "digest": {{json .Digest}}, "createdat": {{json .CreatedAt}}, "createdsize": {{json .CreatedSince}} }\'').stdout
Expand Down
2 changes: 1 addition & 1 deletion test/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ def md.directory?
# zfs output for pool tank
'/sbin/zpool get -Hp all tank' => cmd.call('zpool-get-all-tank'),
# docker
"docker ps -a --no-trunc --format '{{ json . }}'" => cmd.call('docker-ps-a'),
"4f8e24022ea8b7d3b117041ec32e55d9bf08f11f4065c700e7c1dc606c84fd17" => cmd.call('docker-ps-a'),
"docker version --format '{{ json . }}'" => cmd.call('docker-version'),
"docker info --format '{{ json . }}'" => cmd.call('docker-info'),
"docker inspect 71b5df59442b" => cmd.call('docker-inspec'),
Expand Down