mirror of
https://github.com/inspec/inspec
synced 2024-11-14 00:47:10 +00:00
c067798fc5
This change adds the `docker_service` resource for Docker swarm mode services. This branches off some of the common elements (id, exists) into a `DockerObject` module along with a utility function for parsing the image/repo string. That function was implemented separately by `docker_image` and `docker_container`, now with a third resource, it made sense to consolidate that into an included module. I used the most comprehensive implementation. Existing classes had to be slightly modified for the genericization. Signed-off-by: Matt Kulka <mkulka@parchment.com>
92 lines
2.2 KiB
Ruby
92 lines
2.2 KiB
Ruby
# encoding: utf-8
|
|
#
|
|
# Copyright 2017, Christoph Hartmann
|
|
#
|
|
# author: Christoph Hartmann
|
|
# author: Patrick Muench
|
|
# author: Dominik Richter
|
|
|
|
require_relative 'docker_object'
|
|
|
|
module Inspec::Resources
|
|
class DockerContainer < Inspec.resource(1)
|
|
include Inspec::Resources::DockerObject
|
|
|
|
name 'docker_container'
|
|
desc ''
|
|
example "
|
|
describe docker_container('an-echo-server') do
|
|
it { should exist }
|
|
it { should be_running }
|
|
its('id') { should_not eq '' }
|
|
its('image') { should eq 'busybox:latest' }
|
|
its('repo') { should eq 'busybox' }
|
|
its('tag') { should eq 'latest' }
|
|
its('ports') { should eq [] }
|
|
its('command') { should eq 'nc -ll -p 1234 -e /bin/cat' }
|
|
end
|
|
|
|
describe docker_container(id: 'e2c52a183358') do
|
|
it { should exist }
|
|
it { should be_running }
|
|
end
|
|
"
|
|
|
|
def initialize(opts = {})
|
|
# if a string is provided, we expect it is the name
|
|
if opts.is_a?(String)
|
|
@opts = { name: opts }
|
|
else
|
|
@opts = opts
|
|
end
|
|
end
|
|
|
|
def running?
|
|
status.downcase.start_with?('up') if object_info.entries.length == 1
|
|
end
|
|
|
|
def status
|
|
object_info.status[0] if object_info.entries.length == 1
|
|
end
|
|
|
|
def labels
|
|
object_info.labels[0] if object_info.entries.length == 1
|
|
end
|
|
|
|
def ports
|
|
object_info.ports[0] if object_info.entries.length == 1
|
|
end
|
|
|
|
def command
|
|
return unless object_info.entries.length == 1
|
|
|
|
cmd = object_info.commands[0]
|
|
cmd.slice(1, cmd.length - 2)
|
|
end
|
|
|
|
def image
|
|
object_info.images[0] if object_info.entries.length == 1
|
|
end
|
|
|
|
def repo
|
|
parse_components_from_image(image)[:repo] if object_info.entries.size == 1
|
|
end
|
|
|
|
def tag
|
|
parse_components_from_image(image)[:tag] if object_info.entries.size == 1
|
|
end
|
|
|
|
def to_s
|
|
name = @opts[:name] || @opts[:id]
|
|
"Docker Container #{name}"
|
|
end
|
|
|
|
private
|
|
|
|
def object_info
|
|
return @info if defined?(@info)
|
|
opts = @opts
|
|
@info = inspec.docker.containers.where { names == opts[:name] || (!id.nil? && !opts[:id].nil? && (id == opts[:id] || id.start_with?(opts[:id]))) }
|
|
end
|
|
end
|
|
end
|