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

(CONT-1144) - Conversion of ERB to EPP templates #564

Merged
merged 1 commit into from
Jul 25, 2023
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
2 changes: 1 addition & 1 deletion REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -1247,7 +1247,7 @@ Data type: `String`

The template that will be used to create an unit file.

Default value: `'haproxy/instance_service_unit.erb'`
Default value: `'haproxy/instance_service_unit.epp'`

### <a name="haproxy--listen"></a>`haproxy::listen`

Expand Down
14 changes: 14 additions & 0 deletions lib/puppet/functions/haproxy/generate_error_message.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true
praj1001 marked this conversation as resolved.
Show resolved Hide resolved

# Function created to generate error message. Any string as error message can be passed and the function can
# be called in epp templates.

Puppet::Functions.create_function(:'haproxy::generate_error_message') do
dispatch :generate_error_message do
param 'String', :error_message
end

def generate_error_message(error_message)
raise(error_message)
end
end
15 changes: 15 additions & 0 deletions lib/puppet/functions/haproxy/sort_bind.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

Puppet::Functions.create_function(:'haproxy::sort_bind') do
dispatch :sort_bind do
param 'Hash', :bind
return_type 'Array'
end

def sort_bind(bind)
bind.sort_by do |address_port|
md = %r{^((\d+)\.(\d+)\.(\d+)\.(\d+))?(.*)}.match(address_port[0])
[(md[1] ? md[2..5].inject(0) { |addr, octet| (addr << 8) + octet.to_i } : -1), md[6]]
end
end
end
17 changes: 17 additions & 0 deletions lib/puppet/functions/haproxy/validate_ip_addr.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

require 'ipaddr'

Puppet::Functions.create_function(:'haproxy::validate_ip_addr') do
dispatch :validate_ip_addr do
param 'String', :virtual_ip
return_type 'Boolean'
end

def validate_ip_addr(virtual_ip)
IPAddr.new(virtual_ip)
true
rescue StandardError
false
end
end
10 changes: 9 additions & 1 deletion manifests/backend.pp
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,19 @@
$order = "25-${defaults}-${section_name}-01"
}

$parameters = {
'section_name' => $section_name,
'mode' => $mode,
'description' => $description,
'_sort_options_alphabetic' => $_sort_options_alphabetic,
'options' => $options,
}

# Template uses: $section_name, $ipaddress, $ports, $options
concat::fragment { "${instance_name}-${section_name}_backend_block":
order => $order,
target => $_config_file,
content => template('haproxy/haproxy_backend_block.erb'),
content => epp('haproxy/haproxy_backend_block.epp', $parameters),
}

if $collect_exported {
Expand Down
17 changes: 16 additions & 1 deletion manifests/balancermember.pp
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,25 @@
} else {
$order = "25-${defaults}-${listening_service}-02-${name}"
}

$parameters = {
'type' => $type,
'ipaddresses' => $ipaddresses,
'server_names' => $server_names,
'ports' => $ports,
'define_cookies' => $define_cookies,
'options' => $options,
'verifyhost' => $verifyhost,
'weight' => $weight,
'prefix' => $prefix,
'amount' => $amount,
'fqdn' => $fqdn,
'port' => $port,
}
# Template uses $ipaddresses, $server_name, $ports, $option
concat::fragment { "${instance_name}-${listening_service}_balancermember_${name}":
order => $order,
target => $_config_file,
content => template('haproxy/haproxy_balancermember.erb'),
content => epp('haproxy/haproxy_balancermember.epp', $parameters),
}
}
8 changes: 7 additions & 1 deletion manifests/config.pp
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,17 @@
content => "# This file is managed by Puppet\n",
}

$parameters = {
'_global_options' => $_global_options,
'_defaults_options' => $_defaults_options,
'custom_fragment' => $custom_fragment,
}

# Template uses $_global_options, $_defaults_options, $custom_fragment
concat::fragment { "${instance_name}-haproxy-base":
target => $_config_file,
order => '10',
content => template("${module_name}/haproxy-base.cfg.erb"),
content => epp("${module_name}/haproxy-base.cfg.epp", $parameters),
}
}

Expand Down
8 changes: 7 additions & 1 deletion manifests/defaults.pp
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,15 @@
include haproxy::globals
$_sort_options_alphabetic = pick($sort_options_alphabetic, $haproxy::globals::sort_options_alphabetic)

$parameters = {
'_sort_options_alphabetic' => $_sort_options_alphabetic,
'options' => $options,
'name' => $name,
}

concat::fragment { "${instance_name}-${name}_defaults_block":
order => "25-${name}",
target => $config_file,
content => template('haproxy/haproxy_defaults_block.erb'),
content => epp('haproxy/haproxy_defaults_block.epp', $parameters),
}
}
15 changes: 14 additions & 1 deletion manifests/frontend.pp
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,23 @@
$order = "25-${defaults}-${section_name}-00"
}
}

$parameters = {
'section_name' => $section_name,
'bind' => $bind,
'ipaddress' => $ipaddress,
'ports' => $ports,
'bind_options' => $bind_options,
'mode' => $mode,
'description' => $description,
'options' => $options,
'_sort_options_alphabetic' => $_sort_options_alphabetic,
}

# Template uses: $section_name, $ipaddress, $ports, $options
concat::fragment { "${instance_name}-${section_name}_frontend_block":
order => $order,
target => $_config_file,
content => template('haproxy/haproxy_frontend_block.erb'),
content => epp('haproxy/haproxy_frontend_block.epp', $parameters),
}
}
8 changes: 6 additions & 2 deletions manifests/instance_service.pp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
#
define haproxy::instance_service (
Optional[String] $haproxy_init_source = undef,
String $haproxy_unit_template = 'haproxy/instance_service_unit.erb',
String $haproxy_unit_template = 'haproxy/instance_service_unit.epp',
String $haproxy_package = 'haproxy',
Stdlib::Absolutepath $bindir = '/opt/haproxy/bin',
) {
Expand Down Expand Up @@ -103,12 +103,16 @@
$unitfile = "/lib/systemd/system/haproxy-${title}.service"
}

$parameters = {
'title' => $title,
'wrapper' => $wrapper,
}
file { $unitfile:
ensure => file,
mode => '0644',
owner => 'root',
group => 'root',
content => template($haproxy_unit_template),
content => epp($haproxy_unit_template, $parameters),
notify => Exec['systemctl daemon-reload'],
}
if (!defined(Exec['systemctl daemon-reload'])) {
Expand Down
14 changes: 13 additions & 1 deletion manifests/listen.pp
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,23 @@
$order = "25-${defaults}-${section_name}-00"
}

$parameters = {
'section_name' => $section_name,
'bind' => $bind,
'ipaddress' => $ipaddress,
'ports' => $ports,
'bind_options' => $bind_options,
'mode' => $mode,
'description' => $description,
'options' => $options,
'_sort_options_alphabetic' => $_sort_options_alphabetic,
}

# Template uses: $section_name, $ipaddress, $ports, $options
concat::fragment { "${instance_name}-${section_name}_listen_block":
order => $order,
target => $_config_file,
content => template('haproxy/haproxy_listen_block.erb'),
content => epp('haproxy/haproxy_listen_block.epp', $parameters),
}

if $collect_exported {
Expand Down
8 changes: 7 additions & 1 deletion manifests/mailer.pp
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,16 @@
$config_file = inline_template($haproxy::params::config_file_tmpl)
}

$parameters = {
'ipaddresses' => $ipaddresses,
'server_names' => $server_names,
'port' => $port,
}

# Templates uses $ipaddresses, $server_name, $ports, $option
concat::fragment { "${instance_name}-mailers-${mailers_name}-${name}":
order => "40-mailers-01-${mailers_name}-${name}",
target => $config_file,
content => template('haproxy/haproxy_mailer.erb'),
content => epp('haproxy/haproxy_mailer.epp', $parameters),
}
}
6 changes: 5 additions & 1 deletion manifests/mailers.pp
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,15 @@
$config_file = inline_template($haproxy::params::config_file_tmpl)
}

$parameters = {
'name' => $name,
}

# Template uses: $name
concat::fragment { "${instance_name}-${name}_mailers_block":
order => "40-mailers-00-${name}",
target => $config_file,
content => template('haproxy/haproxy_mailers_block.erb'),
content => epp('haproxy/haproxy_mailers_block.epp', $parameters),
}

if $collect_exported {
Expand Down
7 changes: 6 additions & 1 deletion manifests/mapfile.pp
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,14 @@
notify => Haproxy::Service[$_instances],
}

$parameters = {
'mappings' => $mappings,
'mapfile_name' => $mapfile_name,
}

concat::fragment { "haproxy_mapfile_${mapfile_name}-top":
target => $_mapfile_name,
content => template('haproxy/haproxy_mapfile.erb'),
content => epp('haproxy/haproxy_mapfile.epp', $parameters),
order => '00',
}
}
7 changes: 6 additions & 1 deletion manifests/mapfile/entry.pp
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,14 @@
) {
$_mapfile_name = "${haproxy::config_dir}/${mapfile}.map"

$parameters = {
'mappings' => $mappings,
'mapfile_name' => $title,
}

concat::fragment { "haproxy_mapfile_${mapfile}-${title}":
target => $_mapfile_name,
content => template('haproxy/haproxy_mapfile.erb'),
content => epp('haproxy/haproxy_mapfile.epp', $parameters),
order => $order,
}
}
8 changes: 7 additions & 1 deletion manifests/peer.pp
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,16 @@
$_config_file = pick($config_file, inline_template($haproxy::params::config_file_tmpl))
}

$parameters = {
'ipaddresses' => $ipaddresses,
'server_names' => $server_names,
'port' => $port,
}

# Templates uses $ipaddresses, $server_name, $ports, $option
concat::fragment { "${instance_name}-peers-${peers_name}-${name}":
order => "30-peers-01-${peers_name}-${name}",
target => $_config_file,
content => template('haproxy/haproxy_peer.erb'),
content => epp('haproxy/haproxy_peer.epp', $parameters),
}
}
2 changes: 1 addition & 1 deletion manifests/peers.pp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
concat::fragment { "${instance_name}-${name}_peers_block":
order => "30-peers-00-${name}",
target => $_config_file,
content => template('haproxy/haproxy_peers_block.erb'),
content => epp('haproxy/haproxy_peers_block.epp', { 'name' => $name }),
}

if $collect_exported {
Expand Down
11 changes: 10 additions & 1 deletion manifests/resolver.pp
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,20 @@
$order = "25-${defaults}-${section_name}-02"
}

$parameters = {
'section_name' => $section_name,
'nameservers' => $nameservers,
'parse_resolv_conf' => $parse_resolv_conf,
'resolve_retries' => $resolve_retries,
'timeout' => $timeout,
'hold' => $hold,
'accepted_payload_size' => $accepted_payload_size,
}
# Template uses: $section_name
concat::fragment { "${instance_name}-${section_name}_resolver_block":
order => $order,
target => $_config_file,
content => template('haproxy/haproxy_resolver_block.erb'),
content => epp('haproxy/haproxy_resolver_block.epp', $parameters),
}

if $collect_exported {
Expand Down
9 changes: 9 additions & 0 deletions spec/functions/generate_error_message_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

require 'spec_helper'

describe 'haproxy::generate_error_message' do
it { is_expected.to run.with_params('Invalid IP address or hostname 2323.23.23').and_raise_error('Invalid IP address or hostname 2323.23.23') }
it { is_expected.to run.with_params('Port 181400 is outside of range 1-65535').and_raise_error('Port 181400 is outside of range 1-65535') }
it { is_expected.to run.with_params(nil).and_raise_error(StandardError) }
end
9 changes: 9 additions & 0 deletions spec/functions/sort_bind_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

require 'spec_helper'

describe 'haproxy::sort_bind' do
it { is_expected.to run.with_params({ '0.0.0.0:48001-48003' => [] }).and_return([['0.0.0.0:48001-48003', []]]) }
it { is_expected.to run.with_params({ '192.168.0.1:80' => ['ssl'] }).and_return([['192.168.0.1:80', ['ssl']]]) }
it { is_expected.to run.with_params(nil).and_raise_error(StandardError) }
end
9 changes: 9 additions & 0 deletions spec/functions/validate_ip_addr_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

require 'spec_helper'

describe 'haproxy::validate_ip_addr' do
it { is_expected.to run.with_params('10.0.0.10').and_return(true) }
it { is_expected.to run.with_params('256.168.0.1').and_return(false) }
it { is_expected.to run.with_params(nil).and_raise_error(StandardError) }
end
Empty file removed templates/empty.erb
Empty file.
25 changes: 25 additions & 0 deletions templates/fragments/_bind.epp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<% if $bind { -%>
<%- haproxy::sort_bind($bind).map |$address_port_bind_params| { -%>
bind <%= $address_port_bind_params[0] %> <%= Array($address_port_bind_params[1].flatten).join(" ") %>
<%- } -%>
<% } else { -%>
<%- Array($ipaddress.flatten).unique.each |$virtual_ip| { -%>
<%- if String(type($ports, 'generalized')).index('Array') == 0 { -%>
<%- $ports_as_array = $ports -%>
<%- } elsif String(type($ports, 'generalized')).index('String') == 0 { -%>
<%- $ports_as_array = Array($ports.split(",")) -%>
<%- } else { -%>
<%- $ports_as_array = [] -%>
<%- } -%>
<%- $ports_as_array.each |$port| { -%>
<%- $valid_ip = haproxy::validate_ip_addr($virtual_ip) -%>
<%- if !$valid_ip and !String($virtual_ip).match(/^[A-Za-z][A-Za-z0-9\.-]+$/) and $virtual_ip != '*' and $virtual_ip != "::" { -%>
<%- haproxy::generate_error_message("Invalid IP address or hostname [${virtual_ip}]") -%>
<%- } -%>
<%- if $port.convert_to(Integer) < 1 or $port.convert_to(Integer) > 65535 { -%>
<%- haproxy::generate_error_message("Port [${port}] is outside of range 1-65535") -%>
<%- } -%>
bind <%= $virtual_ip -%>:<%= $port -%> <% if $bind_options { %><%= " ${Array($bind_options.flatten).join(' ')}" %><%} else { %><%= " " %><% } %>
<%- } -%>
<%- } -%>
<%- } -%>
Loading