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

Docker host detection and information #1125

Merged
merged 3 commits into from
Feb 16, 2018
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
10 changes: 10 additions & 0 deletions lib/ohai/plugins/darwin/virtualization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,20 @@ def fusion_exists?
::File.exist?("/Applications/VMware\ Fusion.app/")
end

def docker_exists?
which("docker")
end

collect_data(:darwin) do
virtualization Mash.new unless virtualization
virtualization[:systems] = Mash.new unless virtualization[:systems]

if docker_exists?
virtualization[:system] = "docker"
virtualization[:role] = "host"
virtualization[:systems][:docker] = "host"
end

if vboxmanage_exists?
virtualization[:system] = "vbox"
virtualization[:role] = "host"
Expand Down
57 changes: 57 additions & 0 deletions lib/ohai/plugins/docker.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#
# Copyright:: 2018 Chef Software, Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

Ohai.plugin(:Docker) do
require "json"

provides "docker"
depends "virtualization"

def docker_info_json
so = shell_out("docker info --format '{{json .}}'")
if so.exitstatus == 0
return JSON.parse(so.stdout)
end
rescue Ohai::Exceptions::Exec
Ohai::Log.debug('Plugin Docker: Could not shell_out "docker info --format \'{{json .}}\'". Skipping plugin')
end

def docker_ohai_data(shellout_data)
docker Mash.new
docker[:version_string] = shellout_data["ServerVersion"]
docker[:version] = shellout_data["ServerVersion"].split("-")[0] if shellout_data["ServerVersion"] # guard this so missing data doesn't fail the run
docker[:runtimes] = shellout_data["Runtimes"]
docker[:root_dir] = shellout_data["DockerRootDir"]
docker[:containers] = {}
docker[:containers][:total] = shellout_data["Containers"]
docker[:containers][:running] = shellout_data["ContainersRunning"]
docker[:containers][:paused] = shellout_data["ContainersPaused"]
docker[:containers][:stopped] = shellout_data["ContainersStopped"]
docker[:plugins] = shellout_data["Plugins"]
docker[:networking] = {}
docker[:networking][:ipv4_forwarding] = shellout_data["IPv4Forwarding"]
docker[:networking][:bridge_nf_iptables] = shellout_data["BridgeNfIptables"]
docker[:networking][:bridge_nf_ipv6_iptables] = shellout_data["BridgeNfIp6tables"]
docker[:swarm] = shellout_data["Swarm"]
end

collect_data do
if virtualization[:systems][:docker]
docker_ohai_data(docker_info_json)
end
end
end
25 changes: 17 additions & 8 deletions lib/ohai/plugins/linux/virtualization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,27 @@ def nova_exists?
which("nova")
end

def docker_exists?
which("docker")
end

collect_data(:linux) do
virtualization Mash.new unless virtualization
virtualization[:systems] = Mash.new unless virtualization[:systems]

## Xen
# /proc/xen is an empty dir for EL6 + Linode Guests + Paravirt EC2 instances
# Docker hosts
if docker_exists?
virtualization[:system] = "docker"
virtualization[:role] = "host"
virtualization[:systems][:docker] = "host"
end

# Xen Notes:
# - /proc/xen is an empty dir for EL6 + Linode Guests + Paravirt EC2 instances
# - cpuid of guests, if we could get it, would also be a clue
# - may be able to determine if under paravirt from /dev/xen/evtchn (See OHAI-253)
# - Additional edge cases likely should not change the above assumptions
# but rather be additive - btm
if File.exist?("/proc/xen")
virtualization[:system] = "xen"
# Assume guest
Expand All @@ -51,12 +66,6 @@ def nova_exists?
end
end

# Xen Notes:
# - cpuid of guests, if we could get it, would also be a clue
# - may be able to determine if under paravirt from /dev/xen/evtchn (See OHAI-253)
# - Additional edge cases likely should not change the above assumptions
# but rather be additive - btm

# Detect Virtualbox from kernel module
if File.exist?("/proc/modules")
modules = File.read("/proc/modules")
Expand Down
9 changes: 9 additions & 0 deletions spec/unit/plugins/darwin/virtualization_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
allow(plugin).to receive(:ioreg_exists?).and_return(false)
allow(plugin).to receive(:vboxmanage_exists?).and_return(false)
allow(plugin).to receive(:fusion_exists?).and_return(false)
allow(plugin).to receive(:docker_exists?).and_return(false)
end

describe "when detecting OS X virtualization" do
Expand All @@ -37,6 +38,14 @@
expect(plugin[:virtualization]).to eq({ "systems" => {} })
end

it "should set docker host if docker exists" do
allow(plugin).to receive(:docker_exists?).and_return(true)
plugin.run
expect(plugin[:virtualization][:system]).to eq("docker")
expect(plugin[:virtualization][:role]).to eq("host")
expect(plugin[:virtualization][:systems][:docker]).to eq("host")
end

it "should set vmware host if /Applications/VMware\ Fusion.app exists" do
allow(plugin).to receive(:fusion_exists?).and_return(true)
plugin.run
Expand Down
100 changes: 100 additions & 0 deletions spec/unit/plugins/docker_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
#
# Copyright:: 2018 Chef Software, Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

require_relative "../../spec_helper.rb"

docker_output = <<EOF
{"ID":"KZET:VDFN:2V2G:JS5Z:HAKO:SOGI:AFSZ:HDMT:GVEM:V2NT:DUSW:J3Z6","Containers":11,"ContainersRunning":0,"ContainersPaused":0,"ContainersStopped":11,"Images":30,"Driver":"overlay2","DriverStatus":[["Backing Filesystem","extfs"],["Supports d_type","true"],["Native Overlay Diff","true"]],"SystemStatus":null,"Plugins":{"Volume":["local"],"Network":["bridge","host","ipvlan","macvlan","null","overlay"],"Authorization":null,"Log":["awslogs","fluentd","gcplogs","gelf","journald","json-file","logentries","splunk","syslog"]},"MemoryLimit":true,"SwapLimit":true,"KernelMemory":true,"CpuCfsPeriod":true,"CpuCfsQuota":true,"CPUShares":true,"CPUSet":true,"IPv4Forwarding":true,"BridgeNfIptables":true,"BridgeNfIp6tables":true,"Debug":true,"NFd":21,"OomKillDisable":true,"NGoroutines":39,"SystemTime":"2018-02-15T19:12:40.214106068Z","LoggingDriver":"json-file","CgroupDriver":"cgroupfs","NEventsListener":2,"KernelVersion":"4.9.60-linuxkit-aufs","OperatingSystem":"Docker for Mac","OSType":"linux","Architecture":"x86_64","IndexServerAddress":"https://index.docker.io/v1/","RegistryConfig":{"AllowNondistributableArtifactsCIDRs":[],"AllowNondistributableArtifactsHostnames":[],"InsecureRegistryCIDRs":["127.0.0.0/8"],"IndexConfigs":{"docker.io":{"Name":"docker.io","Mirrors":[],"Secure":true,"Official":true}},"Mirrors":[]},"NCPU":4,"MemTotal":2095816704,"GenericResources":null,"DockerRootDir":"/var/lib/docker","HttpProxy":"docker.for.mac.http.internal:3128","HttpsProxy":"docker.for.mac.http.internal:3129","NoProxy":"","Name":"linuxkit-025000000001","Labels":[],"ExperimentalBuild":true,"ServerVersion":"17.12.0-ce","ClusterStore":"","ClusterAdvertise":"","Runtimes":{"runc":{"path":"docker-runc"}},"DefaultRuntime":"runc","Swarm":{"NodeID":"","NodeAddr":"","LocalNodeState":"inactive","ControlAvailable":false,"Error":"","RemoteManagers":null},"LiveRestoreEnabled":false,"Isolation":"","InitBinary":"docker-init","ContainerdCommit":{"ID":"89623f28b87a6004d4b785663257362d1658a729","Expected":"89623f28b87a6004d4b785663257362d1658a729"},"RuncCommit":{"ID":"b2567b37d7b75eb4cf325b77297b140ea686ce8f","Expected":"b2567b37d7b75eb4cf325b77297b140ea686ce8f"},"InitCommit":{"ID":"949e6fa","Expected":"949e6fa"},"SecurityOptions":["name=seccomp,profile=default"]}
EOF

expected_output = {
"version_string" => "17.12.0-ce",
"version" => "17.12.0",
"runtimes" => {
"runc" => {
"path" => "docker-runc",
},
},
"root_dir" => "/var/lib/docker",
"containers" => {
"total" => 11,
"running" => 0,
"paused" => 0,
"stopped" => 11,
},
"plugins" => {
"Volume" => [
"local",
],
"Network" => %w{
bridge
host
ipvlan
macvlan
null
overlay},
"Authorization" => nil,
"Log" => [
"awslogs",
"fluentd",
"gcplogs",
"gelf",
"journald",
"json-file",
"logentries",
"splunk",
"syslog",
],
},
"networking" => {
"ipv4_forwarding" => true,
"bridge_nf_iptables" => true,
"bridge_nf_ipv6_iptables" => true,
},
"swarm" => {
"NodeID" => "",
"NodeAddr" => "",
"LocalNodeState" => "inactive",
"ControlAvailable" => false,
"Error" => "",
"RemoteManagers" => nil,
},
}

describe Ohai::System, "plugin docker" do
let(:plugin) { get_plugin("docker") }

context "if the machine does not have docker installed" do
it "should not create a docker attribute" do
plugin[:virtualization] = Mash.new
plugin[:virtualization][:systems] = Mash.new
plugin.run
expect(plugin).not_to have_key(:docker)
end
end

context "if the machine has docker installed" do
it "should create a docker attribute with correct data" do
plugin[:virtualization] = Mash.new
plugin[:virtualization][:systems] = Mash.new
plugin[:virtualization][:systems][:docker] = "host"
allow(plugin).to receive(:shell_out).with("docker info --format '{{json .}}'").and_return(mock_shell_out(0, docker_output, ""))
plugin.run
expect(plugin).to have_key(:docker)
expect(plugin[:docker]).to eq(expected_output)
end
end
end
11 changes: 11 additions & 0 deletions spec/unit/plugins/linux/virtualization_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
# default the which wrappers to nil
allow(plugin).to receive(:which).with("lxc-version").and_return(nil)
allow(plugin).to receive(:which).with("lxc-start").and_return(nil)
allow(plugin).to receive(:which).with("docker").and_return(nil)
allow(plugin).to receive(:nova_exists?).and_return(false)
end

Expand Down Expand Up @@ -85,6 +86,16 @@
end
end

describe "when we are checking for docker" do
it "sets docker host if docker binary exists" do
allow(plugin).to receive(:which).with("docker").and_return(true)
plugin.run
expect(plugin[:virtualization][:system]).to eq("docker")
expect(plugin[:virtualization][:role]).to eq("host")
expect(plugin[:virtualization][:systems][:docker]).to eq("host")
end
end

describe "when we are checking for openstack" do
it "sets openstack host if nova binary exists" do
allow(plugin).to receive(:nova_exists?).and_return("/usr/bin/nova")
Expand Down