Skip to content

Commit

Permalink
Added custom docker fact
Browse files Browse the repository at this point in the history
  - Currently the implementation creates hash of the current docker networks
  - Also adds a docker.network.managed_interfaces hash to correspond a docker
    network with real interface
  • Loading branch information
Bob committed Aug 28, 2019
1 parent 4ca7fe8 commit 3cb2f9b
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions lib/facter/docker.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
require 'facter'
require 'json'

def interfaces
Facter.value(:interfaces).split(',')
end

Facter.add(:docker) do
setcode do
if Facter::Util::Resolution.which('docker')
docker = Hash.new
docker['network'] = Hash.new
docker['network']['managed_interfaces'] = Hash.new
network_list = Facter::Util::Resolution.exec('docker network ls | tail -n +2')
docker_network_names = Array.new
network_list.each_line {|line| docker_network_names.push line.split[1] }
docker_network_ids = Array.new
network_list.each_line {|line| docker_network_ids.push line.split[0] }
docker_network_names.each do |network|
inspect = JSON.parse(Facter::Util::Resolution.exec("docker network inspect #{network}"))
docker['network'][network] = inspect[0]
network_id = docker['network'][network]['Id'][0..11]
interfaces.each do |iface|
docker['network']['managed_interfaces'][iface] = network if iface =~ /#{network_id}/
end
end
docker
end
end
end

0 comments on commit 3cb2f9b

Please sign in to comment.