From 21e7039dbcb327d6123aa2ccf9c5abb7d3b2ba70 Mon Sep 17 00:00:00 2001 From: Vasu1105 Date: Fri, 23 Jun 2023 12:54:57 +0530 Subject: [PATCH] This includes one more way to find the uuid in local transport running inside docker container Signed-off-by: Vasu1105 --- .../platforms/detect/helpers/os_common.rb | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/lib/train/platforms/detect/helpers/os_common.rb b/lib/train/platforms/detect/helpers/os_common.rb index dc747b38..2cb4c20b 100644 --- a/lib/train/platforms/detect/helpers/os_common.rb +++ b/lib/train/platforms/detect/helpers/os_common.rb @@ -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 @@ -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.