Skip to content

Commit

Permalink
Merge pull request #759 from carabasdaniel/timoutfix
Browse files Browse the repository at this point in the history
Timeout for hangs of the docker_client in the facts generation
  • Loading branch information
adrianiurca authored Jul 5, 2021
2 parents eb96d64 + 9974359 commit 4a4e29a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
18 changes: 9 additions & 9 deletions lib/facter/docker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ def interfaces

Facter.add(:docker_version) do
setcode do
if Facter::Util::Resolution.which('docker')
value = Facter::Util::Resolution.exec(
"#{docker_command} version --format '{{json .}}'",
if Facter::Core::Execution.which('docker')
value = Facter::Core::Execution.exec(
"#{docker_command} version --format '{{json .}}'", timeout: 90
)
val = JSON.parse(value)
end
Expand Down Expand Up @@ -102,23 +102,23 @@ def interfaces
Facter.add(:docker) do
setcode do
docker_version = Facter.value(:docker_client_version)
unless %r{1[.][0-9][0-2]?[.]\w+}.match?(docker_version)
if Facter::Util::Resolution.which('docker')
docker_json_str = Facter::Util::Resolution.exec(
"#{docker_command} info --format '{{json .}}'",
if docker_version.match?(%r{1[0-9][0-2]?[.]\w+})
if Facter::Core::Execution.which('docker')
docker_json_str = Facter::Core::Execution.exec(
"#{docker_command} info --format '{{json .}}'", timeout: 90
)
begin
docker = JSON.parse(docker_json_str)
docker['network'] = {}

docker['network']['managed_interfaces'] = {}
network_list = Facter::Util::Resolution.exec("#{docker_command} network ls | tail -n +2")
network_list = Facter::Core::Execution.exec("#{docker_command} network ls | tail -n +2", timeout: 90)
docker_network_names = []
network_list.each_line { |line| docker_network_names.push line.split[1] }
docker_network_ids = []
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_command} network inspect #{network}"))
inspect = JSON.parse(Facter::Core::Execution.exec("#{docker_command} network inspect #{network}", timeout: 90))
docker['network'][network] = inspect[0]
network_id = docker['network'][network]['Id'][0..11]
interfaces.each do |iface|
Expand Down
16 changes: 10 additions & 6 deletions spec/unit/lib/facter/docker_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,28 @@
Facter.clear
if Facter.value(:kernel) == 'windows'
docker_command = 'powershell -NoProfile -NonInteractive -NoLogo -ExecutionPolicy Bypass -c docker'
Facter::Util::Resolution.stubs(:which).with('docker').returns('C:\Program Files\Docker\docker.exe')
Facter::Core::Execution.stubs(:which).with('dhcpcd').returns('C:\Windows\dhcpd.exe')
Facter::Core::Execution.stubs(:which).with('route').returns('C:\Windows\System32\ROUTE.EXE')
Facter::Core::Execution.stubs(:which).with('docker').returns('C:\Program Files\Docker\docker.exe')
else
docker_command = 'docker'
Facter::Util::Resolution.stubs(:which).with('docker').returns('/usr/bin/docker')
Facter::Core::Execution.stubs(:which).with('route').returns('/usr/bin/route')
Facter::Core::Execution.stubs(:which).with('dhcpcd').returns('/usr/bin/dhcpd')
Facter::Core::Execution.stubs(:which).with('docker').returns('/usr/bin/docker')
end
docker_info = File.read(fixtures('facts', 'docker_info'))
Facter::Util::Resolution.stubs(:exec).with("#{docker_command} info --format '{{json .}}'").returns(docker_info)
Facter::Core::Execution.stubs(:exec).with("#{docker_command} info --format '{{json .}}'", timeout: 90).returns(docker_info)
processors = File.read(fixtures('facts', 'processors'))
Facter.fact(:processors).stubs(:value).returns(JSON.parse(processors))
docker_version = File.read(fixtures('facts', 'docker_version'))
Facter::Util::Resolution.stubs(:exec).with("#{docker_command} version --format '{{json .}}'").returns(docker_version)
Facter::Core::Execution.stubs(:exec).with("#{docker_command} version --format '{{json .}}'", timeout: 90).returns(docker_version)
docker_network_list = File.read(fixtures('facts', 'docker_network_list'))
Facter::Util::Resolution.stubs(:exec).with("#{docker_command} network ls | tail -n +2").returns(docker_network_list)
Facter::Core::Execution.stubs(:exec).with("#{docker_command} network ls | tail -n +2", timeout: 90).returns(docker_network_list)
docker_network_names = []
docker_network_list.each_line { |line| docker_network_names.push line.split[1] }
docker_network_names.each do |network|
inspect = File.read(fixtures('facts', "docker_network_inspect_#{network}"))
Facter::Util::Resolution.stubs(:exec).with("#{docker_command} network inspect #{network}").returns(inspect)
Facter::Core::Execution.stubs(:exec).with("#{docker_command} network inspect #{network}", timeout: 90).returns(inspect)
end
docker_worker_token = File.read(fixtures('facts', 'docker_swarm_worker_token'))
Facter::Util::Resolution.stubs(:exec).with("#{docker_command} swarm join-token worker -q").returns(docker_worker_token.chomp)
Expand Down

0 comments on commit 4a4e29a

Please sign in to comment.