From f464a5b4099e98485f3ffca3b6b42f37ff336d79 Mon Sep 17 00:00:00 2001 From: Shim Shtein Date: Tue, 20 Aug 2024 06:08:27 -0400 Subject: [PATCH] Fix save_to_file to be indentation independent --- app/services/foreman/renderer/scope/macros/base.rb | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/app/services/foreman/renderer/scope/macros/base.rb b/app/services/foreman/renderer/scope/macros/base.rb index b222c54595c..05bbafe3dfd 100644 --- a/app/services/foreman/renderer/scope/macros/base.rb +++ b/app/services/foreman/renderer/scope/macros/base.rb @@ -117,15 +117,21 @@ def pxe_kernel_options example "save_to_file(shell_escape('/tmp/a file with spaces'), nil) # => 'cp /dev/null /tmp/a\ file\ with\ spaces'" end def save_to_file(filename, content, verbatim: false) - delimiter = 'EOF-' + Digest::SHA512.hexdigest(filename)[0..7] if content.empty? "cp /dev/null #{filename}" elsif verbatim content = Base64.encode64(content) - "cat << #{delimiter} | base64 -d > #{filename}\n#{content}#{delimiter}" + # since content is a base64 string we don't need to escape it + "echo #{content} | base64 -d > #{filename}" else - content += "\n" unless content.end_with?("\n") - "cat << #{delimiter} > #{filename}\n#{content}#{delimiter}" + content_echos = content.split("\n").map do |content_line| + # to make sure there the substitutions are working, have the bash script + # handle all special characters except double quotes. + "echo \"#{content_line.gsub('"', '\"')}\" >> #{filename}" + end.join("\n") + + # prefix the append commands with a cleanup command + "> #{filename}\n#{content_echos}" end end