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

Add support for running programs #604

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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: 5 additions & 0 deletions manifests/config.pp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
Hash $global_options,
Hash $defaults_options,
Boolean $chroot_dir_manage,
Haproxy::Programs $programs = {},
Stdlib::Absolutepath $config_dir = undef,
Optional[String] $custom_fragment = undef,
Boolean $merge_options = $haproxy::merge_options,
Expand Down Expand Up @@ -81,6 +82,10 @@
order => '10',
content => epp("${module_name}/haproxy-base.cfg.epp", $parameters),
}

if !empty($programs) {
create_resources(haproxy::program, $programs)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't use create_resources(), it's deprecated and we want to get rid of it. please iterate on $programs and call the defined resource for each element.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bastelfreak Doesn't seem to be deprecated in stdlib v9.4.1 where can I find such deprecation note? Generally I'd try to avoid code repetition, this feels like a code smell. I've tried to replace it, but I'm not sure what you mean.

}
}

if $chroot_dir_manage {
Expand Down
7 changes: 7 additions & 0 deletions manifests/init.pp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@
# Passed directly as the <code>'restart'</code> parameter to the service resource.
# Defaults to undef i.e. whatever the service default is.
#
# @param programs
# A program section contains a set of directives that define the program to be run,
# its command-line options and flags, as well as user, group, and restart options.
# Note, `master-worker` configuration (in `global_options`) is needed.
#
# @param custom_fragment
# Allows arbitrary HAProxy configuration to be passed through to support
# additional configuration not available via parameters, or to short-circute
Expand Down Expand Up @@ -131,6 +136,7 @@
Hash $global_options = $haproxy::params::global_options,
Hash $defaults_options = $haproxy::params::defaults_options,
Boolean $merge_options = $haproxy::params::merge_options,
Haproxy::Programs $programs = {},
Optional[String] $restart_command = undef,
Optional[String] $custom_fragment = undef,
Stdlib::Absolutepath $config_dir = $haproxy::params::config_dir,
Expand Down Expand Up @@ -187,5 +193,6 @@
service_options => $service_options,
sysconfig_options => $sysconfig_options,
config_validate_cmd => $config_validate_cmd,
programs => $programs,
}
}
6 changes: 6 additions & 0 deletions manifests/instance.pp
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@
#
# @param sysconfig_options
#
# @param programs
# A program section contains a set of directives that define the program to be run,
# its command-line options and flags, as well as user, group, and restart options.
#
# @example
# A single instance of haproxy with all defaults
# i.e. emulate Class['haproxy']
Expand Down Expand Up @@ -175,6 +179,7 @@
Boolean $merge_options = $haproxy::params::merge_options,
String $service_options = $haproxy::params::service_options,
String $sysconfig_options = $haproxy::params::sysconfig_options,
Haproxy::Programs $programs = {},
) {
# Since this is a 'define', we can not use 'inherts haproxy::params'.
# Therefore, we "include haproxy::params" for any parameters we need.
Expand Down Expand Up @@ -221,6 +226,7 @@
package_ensure => $package_ensure,
chroot_dir_manage => $chroot_dir_manage,
config_validate_cmd => $config_validate_cmd,
programs => $programs,
}
haproxy::install { $title:
package_name => $package_name,
Expand Down
40 changes: 40 additions & 0 deletions manifests/program.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# @summary Program definition
#
# A command to be executed by haproxy master process
#
# @see https://www.haproxy.com/documentation/haproxy-configuration-tutorials/programs/
# @example
# haproxy::program { 'hello':
# command => 'hello world',
# }
define haproxy::program (
String $command,

Check warning on line 11 in manifests/program.pp

View workflow job for this annotation

GitHub Actions / Spec / Spec tests (Puppet: ~> 7.24, Ruby Ver: 2.7)

missing documentation for defined type parameter haproxy::program::command (check: parameter_documentation)

Check warning on line 11 in manifests/program.pp

View workflow job for this annotation

GitHub Actions / Spec / Spec tests (Puppet: ~> 7.24, Ruby Ver: 2.7)

missing documentation for defined type parameter haproxy::program::command (check: parameter_documentation)

Check warning on line 11 in manifests/program.pp

View workflow job for this annotation

GitHub Actions / Spec / Spec tests (Puppet: ~> 8.0, Ruby Ver: 3.2)

missing documentation for defined type parameter haproxy::program::command (check: parameter_documentation)

Check warning on line 11 in manifests/program.pp

View workflow job for this annotation

GitHub Actions / Spec / Spec tests (Puppet: ~> 8.0, Ruby Ver: 3.2)

missing documentation for defined type parameter haproxy::program::command (check: parameter_documentation)
Optional[String] $user = undef,

Check warning on line 12 in manifests/program.pp

View workflow job for this annotation

GitHub Actions / Spec / Spec tests (Puppet: ~> 7.24, Ruby Ver: 2.7)

missing documentation for defined type parameter haproxy::program::user (check: parameter_documentation)

Check warning on line 12 in manifests/program.pp

View workflow job for this annotation

GitHub Actions / Spec / Spec tests (Puppet: ~> 7.24, Ruby Ver: 2.7)

missing documentation for defined type parameter haproxy::program::user (check: parameter_documentation)

Check warning on line 12 in manifests/program.pp

View workflow job for this annotation

GitHub Actions / Spec / Spec tests (Puppet: ~> 8.0, Ruby Ver: 3.2)

missing documentation for defined type parameter haproxy::program::user (check: parameter_documentation)

Check warning on line 12 in manifests/program.pp

View workflow job for this annotation

GitHub Actions / Spec / Spec tests (Puppet: ~> 8.0, Ruby Ver: 3.2)

missing documentation for defined type parameter haproxy::program::user (check: parameter_documentation)
Optional[String] $group = undef,

Check warning on line 13 in manifests/program.pp

View workflow job for this annotation

GitHub Actions / Spec / Spec tests (Puppet: ~> 7.24, Ruby Ver: 2.7)

missing documentation for defined type parameter haproxy::program::group (check: parameter_documentation)

Check warning on line 13 in manifests/program.pp

View workflow job for this annotation

GitHub Actions / Spec / Spec tests (Puppet: ~> 7.24, Ruby Ver: 2.7)

missing documentation for defined type parameter haproxy::program::group (check: parameter_documentation)

Check warning on line 13 in manifests/program.pp

View workflow job for this annotation

GitHub Actions / Spec / Spec tests (Puppet: ~> 8.0, Ruby Ver: 3.2)

missing documentation for defined type parameter haproxy::program::group (check: parameter_documentation)

Check warning on line 13 in manifests/program.pp

View workflow job for this annotation

GitHub Actions / Spec / Spec tests (Puppet: ~> 8.0, Ruby Ver: 3.2)

missing documentation for defined type parameter haproxy::program::group (check: parameter_documentation)
Optional[String] $options = undef,

Check warning on line 14 in manifests/program.pp

View workflow job for this annotation

GitHub Actions / Spec / Spec tests (Puppet: ~> 7.24, Ruby Ver: 2.7)

missing documentation for defined type parameter haproxy::program::options (check: parameter_documentation)

Check warning on line 14 in manifests/program.pp

View workflow job for this annotation

GitHub Actions / Spec / Spec tests (Puppet: ~> 7.24, Ruby Ver: 2.7)

missing documentation for defined type parameter haproxy::program::options (check: parameter_documentation)

Check warning on line 14 in manifests/program.pp

View workflow job for this annotation

GitHub Actions / Spec / Spec tests (Puppet: ~> 8.0, Ruby Ver: 3.2)

missing documentation for defined type parameter haproxy::program::options (check: parameter_documentation)

Check warning on line 14 in manifests/program.pp

View workflow job for this annotation

GitHub Actions / Spec / Spec tests (Puppet: ~> 8.0, Ruby Ver: 3.2)

missing documentation for defined type parameter haproxy::program::options (check: parameter_documentation)
String $instance = 'haproxy',

Check warning on line 15 in manifests/program.pp

View workflow job for this annotation

GitHub Actions / Spec / Spec tests (Puppet: ~> 7.24, Ruby Ver: 2.7)

missing documentation for defined type parameter haproxy::program::instance (check: parameter_documentation)

Check warning on line 15 in manifests/program.pp

View workflow job for this annotation

GitHub Actions / Spec / Spec tests (Puppet: ~> 8.0, Ruby Ver: 3.2)

missing documentation for defined type parameter haproxy::program::instance (check: parameter_documentation)
Optional[Stdlib::Absolutepath] $config_file = undef,

Check warning on line 16 in manifests/program.pp

View workflow job for this annotation

GitHub Actions / Spec / Spec tests (Puppet: ~> 7.24, Ruby Ver: 2.7)

missing documentation for defined type parameter haproxy::program::config_file (check: parameter_documentation)

Check warning on line 16 in manifests/program.pp

View workflow job for this annotation

GitHub Actions / Spec / Spec tests (Puppet: ~> 8.0, Ruby Ver: 3.2)

missing documentation for defined type parameter haproxy::program::config_file (check: parameter_documentation)
) {
# We derive these settings so that the caller only has to specify $instance.
include haproxy::params

if $instance == 'haproxy' {
$instance_name = 'haproxy'
$_config_file = pick($config_file, $haproxy::config_file)
} else {
$instance_name = "haproxy-${instance}"
$_config_file = pick($config_file, inline_template($haproxy::params::config_file_tmpl))
}

concat::fragment { "${instance_name}-${name}_program":
order => "40-program-${name}",
target => $_config_file,
content => epp('haproxy/haproxy_program.epp', {
'name' => $name,
'command' => $command,
'user' => $user,
'group' => $group,
'options' => $options,
}),
}
}
27 changes: 27 additions & 0 deletions spec/classes/haproxy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -668,4 +668,31 @@
}.to raise_error(Puppet::Error, %r{operating system is not supported with the haproxy module})
end
end

describe 'when programs is specified' do
['Debian'].each do |osfamily|
context "when on #{osfamily} family operatingsystems" do
let(:facts) do
{ os: { family: osfamily } }.merge default_facts
end
let(:contents) { param_value(catalogue, 'concat::fragment', 'haproxy-haproxy-base', 'content').split("\n") }
let(:params) do
{
'programs' => {
'foo' => {
'command' => '/usr/bin/foo',
},
'bar' => {
'command' => '/usr/bin/bar',
},
},
}
end

it { is_expected.to compile }
it { is_expected.to contain_concat__fragment('haproxy-foo_program').with_content(%r{command /usr/bin/foo}) }
it { is_expected.to contain_concat__fragment('haproxy-bar_program').with_content(%r{command /usr/bin/bar}) }
end
end
end
end
37 changes: 37 additions & 0 deletions spec/defines/program_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# frozen_string_literal: true

require 'spec_helper'

describe 'haproxy::program' do
let :pre_condition do
'class{"haproxy":
config_file => "/tmp/haproxy.cfg"
}
'
end
let(:facts) do
{
concat_basedir: '/foo',
os: {
family: 'Debian'
}
}
end

context 'simple program' do
let(:title) { 'hello' }
let(:params) do
{
command: 'echo "hello world"',
}
end

it {
is_expected.to contain_concat__fragment('haproxy-hello_program').with(
'order' => '40-program-hello',
'target' => '/tmp/haproxy.cfg',
'content' => %r{command echo "hello world"},
)
}
end
end
11 changes: 11 additions & 0 deletions templates/haproxy_program.epp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
program <%= $name %>
command <%= $command %>
<% if $user { -%>
user <%= $user %>
<% } -%>
<% if $group { -%>
group $group
<% } -%>
<% if $options { -%>
<%= $options %>
<% } -%>
9 changes: 9 additions & 0 deletions types/programs.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
type Haproxy::Programs = Hash[String, Struct[{
command => String[1],
Optional[user] => String[1],
Optional[group] => String[1],
Optional[options] => String[1],
Optional[instance] => String[1],
Optional[config_file] => Stdlib::Absolutepath,
}]]

Loading