diff --git a/app/services/foreman/renderer/scope/macros/base.rb b/app/services/foreman/renderer/scope/macros/base.rb index 59014ec3a37..b222c54595c 100644 --- a/app/services/foreman/renderer/scope/macros/base.rb +++ b/app/services/foreman/renderer/scope/macros/base.rb @@ -133,19 +133,22 @@ def save_to_file(filename, content, verbatim: false) desc "This is useful when rendering output is a whitespace sensitive format, such as YAML." required :count, Integer, desc: 'The number of spaces' keyword :skip1, [true, false], desc: 'Skips the first line prefixing, defaults to false', default: false + keyword :skip_content, String, desc: 'Skips indentation, if the line equals the given content', default: nil block 'Optional. Does nothing if no block is given', schema: '{ code }' returns String, desc: 'The indented text, that was the result of block of code' example "indent(2) { snippet('epel') } # => ' echo Installing yum repo\n yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm'" example "indent(2) { snippet('epel', skip1: true) } # => 'echo Installing yum repo\n yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm'" example "indent 4 do\n snippet 'subscription_manager_registration'\nend" end - def indent(count, skip1: false) + def indent(count, skip1: false, skip_content: nil) return unless block_given? && (text = yield.to_s) prefix = ' ' * count result = [] text.each_line.with_index do |line, line_no| if line_no == 0 && skip1 result << line + elsif skip_content.present? && line.chomp == skip_content + result << line else result << prefix + line end diff --git a/app/views/unattended/provisioning_templates/host_init_config/host_init_config_default.erb b/app/views/unattended/provisioning_templates/host_init_config/host_init_config_default.erb index ddeedb72e80..b19514ae0d3 100644 --- a/app/views/unattended/provisioning_templates/host_init_config/host_init_config_default.erb +++ b/app/views/unattended/provisioning_templates/host_init_config/host_init_config_default.erb @@ -21,7 +21,6 @@ description: | performs initial steps, such as puppet deployment, remote execution SSH key configuration etc. At the end it informs Foreman that provisioning has finished. -%> -<% built_https = foreman_url('built').start_with?('https') -%> #!/bin/bash set -e @@ -29,21 +28,14 @@ echo "# Running [<%= @host.name %>] host initial configuration" <%= snippet_if_exists('host_init_config_pre') -%> -<% if built_https -%> -SSL_CA_CERT=$(mktemp) -cat << EOF > $SSL_CA_CERT -<%= foreman_server_ca_cert %> -EOF -<% end -%> - -foreman_curl() { - curl --silent --show-error <%= '--cacert $SSL_CA_CERT' if built_https %> -o /dev/null --noproxy \* "$@" -} - exit_and_cancel_build() { echo 'Host [<%= @host.name %>] initial configuration failed' - foreman_curl --request POST '<%= foreman_url('failed') %>' \ - --data 'Host initial configuration failed, please see the registration log for more details.' + + <%= indent(2, skip1: true, skip_content: 'EOF') { snippet('built', :variables => { + :endpoint => 'failed', + :method => 'POST', + :post_data => 'Host initial configuration failed, please see the registration log for more details.' }) } + -%> exit 1 } @@ -92,7 +84,7 @@ fi # Call home to exit build mode trap - ERR -foreman_curl '<%= foreman_url('built') %>' +<%= snippet 'built' %> if [[ $? == 0 ]] ; then echo "Host [<%= @host.name %>] successfully configured." @@ -102,7 +94,6 @@ fi <% if plugin_present?('katello') && @host.operatingsystem.family == 'Redhat' -%> subscription-manager facts --update - <% end -%> exit 0 diff --git a/app/views/unattended/provisioning_templates/provision/kickstart_default.erb b/app/views/unattended/provisioning_templates/provision/kickstart_default.erb index ac1b054d0c8..dfbd587d57d 100644 --- a/app/views/unattended/provisioning_templates/provision/kickstart_default.erb +++ b/app/views/unattended/provisioning_templates/provision/kickstart_default.erb @@ -328,10 +328,10 @@ The last post section halts Anaconda to prevent endless loop in case HTTP reques if test -f /tmp/foreman_built; then echo "calling home: build is done!" - <%= indent(2, skip1: true) { snippet('built', :variables => { :endpoint => 'built', :method => 'POST', :body_file => '/root/install.post.log' }) } -%> + <%= indent(2, skip1: true, skip_content: 'EOF') { snippet('built', :variables => { :endpoint => 'built', :method => 'POST', :body_file => '/root/install.post.log' }) } -%> else echo "calling home: build failed!" - <%= indent(2, skip1: true) { snippet('built', :variables => { :endpoint => 'failed', :method => 'POST', :body_file => '/root/install.post.log' }) } -%> + <%= indent(2, skip1: true, skip_content: 'EOF') { snippet('built', :variables => { :endpoint => 'failed', :method => 'POST', :body_file => '/root/install.post.log' }) } -%> fi sync diff --git a/app/views/unattended/provisioning_templates/snippet/built.erb b/app/views/unattended/provisioning_templates/snippet/built.erb index 6918e19c713..00aa944caee 100644 --- a/app/views/unattended/provisioning_templates/snippet/built.erb +++ b/app/views/unattended/provisioning_templates/snippet/built.erb @@ -13,17 +13,27 @@ description: | endpoint = @endpoint || 'built' method = @method || 'POST' url = foreman_url(endpoint) + built_https = url.start_with?('https') curl_opts = ["-H 'Content-Type: text/plain'"] wget_opts = ["--header 'Content-Type: text/plain'"] - if url.start_with?('https') - curl_opts << "--insecure" - wget_opts << "--no-check-certificate" - end if @body_file curl_opts << "--data @#{@body_file}" wget_opts << "--body-file=#{@body_file}" + elsif @post_data + curl_opts << "--data '#{@post_data}'" + wget_opts << "--body-data='#{@post_data}'" end -%> +<% if built_https -%> +SSL_CA_CERT=$(mktemp) +cat << EOF > $SSL_CA_CERT +<%= foreman_server_ca_cert %> +EOF +<% + curl_opts << "--cacert $SSL_CA_CERT" + wget_opts << "--ca-certificate $SSL_CA_CERT" +-%> +<% end -%> if [ -x /usr/bin/curl ]; then /usr/bin/curl -o /dev/null --noproxy \* <%= curl_opts.join(' ') %> --silent '<%= url %>' elif [ -x /usr/bin/wget ]; then diff --git a/test/unit/foreman/renderer/scope/macros/base_test.rb b/test/unit/foreman/renderer/scope/macros/base_test.rb index 879be4379da..8dee5f6ebdc 100644 --- a/test/unit/foreman/renderer/scope/macros/base_test.rb +++ b/test/unit/foreman/renderer/scope/macros/base_test.rb @@ -36,6 +36,13 @@ class BaseMacrosTest < ActiveSupport::TestCase assert_equal indented, "foo\n bar\n baz" end + test "should indent a string ignoring the line if it starts with the word 'EOF'" do + indented = @scope.indent(4, skip_content: 'EOF') do + "foo\nEOF\nbar\nbaz" + end + assert_equal indented, " foo\nEOF\n bar\n baz" + end + test '#foreman_url can be rendered even outside of controller context' do assert_nothing_raised do assert_match /\/unattended\/built/, @scope.foreman_url('built')