Skip to content

Commit

Permalink
Merge pull request #747 from inspec/vasundhara/fix-uuid-for-local-tra…
Browse files Browse the repository at this point in the history
…nsport-running-inside-docker-container

Fix cannot find a UUID when connect using train with local transport inside docker container
  • Loading branch information
clintoncwolfe authored Jun 23, 2023
2 parents 4de4740 + 21e7039 commit 0b6dc3c
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions lib/train/platforms/detect/helpers/os_common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ def unix_uuid
(unix_uuid_from_chef ||
unix_uuid_from_machine_file ||
uuid_from_command ||
uuid_from_containerized_system ||
raise(Train::TransportError, "Cannot find a UUID for your node."))
end

Expand Down Expand Up @@ -155,6 +156,28 @@ def uuid_from_command
uuid_from_string(result.stdout.chomp) if result.exit_status == 0 && !result.stdout.empty?
end

# This will run if anyone is running Train with local transport inside docker container
# This is fallback plan, if other ways of getting uuid fails for local transport running inside docker container
# TODO: This needs to be improved to support other container runtime
def uuid_from_containerized_system
uuid = nil

if File.exist?("/proc/self/cgroup")
cmd = @backend.run_command("head -1 /proc/self/cgroup|cut -d/ -f3")
unless cmd.stdout.strip.empty?
uuid = cmd.stdout.strip
end
end

if uuid.nil? && File.exist?("/proc/self/mountinfo")
cmd = @backend.run_command("cat /proc/self/mountinfo | grep -i /docker/containers/ | head -n 1 | awk '{print $4}' | awk NF=NF FS=/ | awk '{print $3}'")
unless cmd.stdout.strip.empty?
uuid = cmd.stdout.strip
end
end
uuid
end

# This hashes the passed string into SHA1.
# Then it downgrades the 160bit SHA1 to a 128bit
# then we format it as a valid UUIDv5.
Expand Down

0 comments on commit 0b6dc3c

Please sign in to comment.