Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #37495 - unify sending the built state #10179

Merged
merged 1 commit into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion app/services/foreman/renderer/scope/macros/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,21 @@ 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

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
}

Expand Down Expand Up @@ -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."
Expand All @@ -102,7 +94,6 @@ fi

<% if plugin_present?('katello') && @host.operatingsystem.family == 'Redhat' -%>
subscription-manager facts --update

<% end -%>

exit 0
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 14 additions & 4 deletions app/views/unattended/provisioning_templates/snippet/built.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions test/unit/foreman/renderer/scope/macros/base_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
Loading