diff --git a/lib/puppet/functions/pulpcore/generate_header_content.rb b/lib/puppet/functions/pulpcore/generate_header_content.rb new file mode 100644 index 00000000..59d9ac32 --- /dev/null +++ b/lib/puppet/functions/pulpcore/generate_header_content.rb @@ -0,0 +1,20 @@ +Puppet::Functions.create_function(:'pulpcore::generate_header_content') do + dispatch :generate_header_content do + param 'Variant[String, Array[String]]', :raw_content + param 'Integer', :line_width + param 'String', :comment_glyph + return_type 'Array[String]' + end + + def generate_header_content(raw_content, line_width, comment_glyph) + raw_content = [raw_content] unless raw_content.is_a?(Array) + buffer = comment_glyph * (line_width / comment_glyph.length.to_f.ceil) + reduced_width = line_width - (2*(1+comment_glyph.length)) + + raw_content.map do |text| + text.scan(/\S.{0,#{reduced_width}}\S(?=\s|$)|\S+/).map do |line| + "#{comment_glyph} " + line + (" " * (reduced_width - line.length)) + " #{comment_glyph}" + end.append(buffer) + end.flatten.prepend(buffer) + end +end diff --git a/manifests/config.pp b/manifests/config.pp index 8cdaba84..35b90e02 100644 --- a/manifests/config.pp +++ b/manifests/config.pp @@ -17,6 +17,8 @@ ensure_newline => true, } + include pulpcore::header_strings + concat::fragment { 'base': target => 'pulpcore settings', content => template('pulpcore/settings.py.erb'), diff --git a/manifests/header_strings.pp b/manifests/header_strings.pp new file mode 100644 index 00000000..f92a669d --- /dev/null +++ b/manifests/header_strings.pp @@ -0,0 +1,41 @@ +# @summary Re-usable, overridable strings for file headers +# +# @param custom_installer_name +# Override installer_name in headers from "Puppet" to e.g. "foreman-installer" +# +# @param custom_settings_explanation +# Custom string providing further instructions to the user in settings.py header +class pulpcore::header_strings( + Optional[String] $custom_installer_name = undef, + Optional[String] $custom_settings_explanation = undef, +) { + if $custom_installer_name { + $installer_name = $custom_installer_name + } else { + $installer_name = 'Puppet' + } + + $installer_header = "File managed by ${installer_name}.\nModule: ${module_name}" + $warning_header = '!!! WARNING: DO NOT EDIT THIS FILE !!!' + + if $custom_settings_explanation { + $explanation = $custom_settings_explanation + } else { + $explanation = @("EXPLANATION"/L) + Not only are your edits likely to be overwritten, there is also a strong possibility \ + of breaking your system if you change configuration here without making required \ + changes elsewhere. Refer to the documentation you used to install Pulpcore to \ + determine the safe and persistent way to modify the configuration. + | - EXPLANATION + } + + $settings_header_content = pulpcore::generate_header_content( + [ + $warning_header, + $installer_header, + $explanation, + ], + 80, + '#', + ) +} diff --git a/spec/classes/settings_header_spec.rb b/spec/classes/settings_header_spec.rb new file mode 100644 index 00000000..b7423111 --- /dev/null +++ b/spec/classes/settings_header_spec.rb @@ -0,0 +1,66 @@ +require 'spec_helper' + +describe 'pulpcore' do + on_supported_os.each do |os, os_facts| + context "on #{os}" do + let(:facts) { os_facts } + + context 'default params' do + it { is_expected.to compile.with_all_deps } + + it 'adds default header to settings.py' do + is_expected.to contain_concat__fragment('base') + .with_content( + Regexp.new(<<~HEADER.chomp), + ################################################################################ + # !!! WARNING: DO NOT EDIT THIS FILE !!! # + ################################################################################ + # File managed by Puppet. # + # Module: pulpcore # + ################################################################################ + # Not only are your edits likely to be overwritten, there is also a strong # + # possibility of breaking your system if you change configuration here without # + # making required changes elsewhere. Refer to the documentation you used to # + # install Pulpcore to determine the safe and persistent way to modify the # + # configuration. # + ################################################################################ + + CONTENT_HOST = + HEADER + ) + end + end + + context 'with custom config header strings' do + let :pre_condition do + <<~PUPPET + class { pulpcore::header_strings: + custom_installer_name => 'my_pulpcore_installer', + custom_settings_explanation => "Don't even think about it." + } + PUPPET + end + + it { is_expected.to compile.with_all_deps } + + it 'adds customized header to settings.py' do + is_expected.to contain_concat__fragment('base') + .with_content( + Regexp.new(<<~HEADER.chomp), + ################################################################################ + # !!! WARNING: DO NOT EDIT THIS FILE !!! # + ################################################################################ + # File managed by my_pulpcore_installer. # + # Module: pulpcore # + ################################################################################ + # Don't even think about it. # + ################################################################################ + + CONTENT_HOST = + HEADER + ) + end + end + end + end +end diff --git a/templates/settings.py.erb b/templates/settings.py.erb index db79d87e..c96f7585 100644 --- a/templates/settings.py.erb +++ b/templates/settings.py.erb @@ -1,3 +1,5 @@ +<%= scope['pulpcore::header_strings::settings_header_content'].join("\n") %> + CONTENT_HOST = "<%= scope['pulpcore::servername'] %>" CONTENT_ORIGIN = "https://<%= scope['pulpcore::servername'] %>" SECRET_KEY = "<%= scope['pulpcore::django_secret_key'] %>"