Skip to content

Commit 226a718

Browse files
committed
Add support for running programs
1 parent 1b1c22f commit 226a718

File tree

8 files changed

+142
-0
lines changed

8 files changed

+142
-0
lines changed

manifests/config.pp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
Hash $global_options,
1010
Hash $defaults_options,
1111
Boolean $chroot_dir_manage,
12+
Haproxy::Programs $programs = {},
1213
Stdlib::Absolutepath $config_dir = undef,
1314
Optional[String] $custom_fragment = undef,
1415
Boolean $merge_options = $haproxy::merge_options,
@@ -81,6 +82,10 @@
8182
order => '10',
8283
content => epp("${module_name}/haproxy-base.cfg.epp", $parameters),
8384
}
85+
86+
if !empty($programs) {
87+
create_resources(haproxy::program, $programs)
88+
}
8489
}
8590

8691
if $chroot_dir_manage {

manifests/init.pp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@
6363
# Passed directly as the <code>'restart'</code> parameter to the service resource.
6464
# Defaults to undef i.e. whatever the service default is.
6565
#
66+
# @param programs
67+
# A program section contains a set of directives that define the program to be run,
68+
# its command-line options and flags, as well as user, group, and restart options.
69+
# Note, `master-worker` configuration (in `global_options`) is needed.
70+
#
6671
# @param custom_fragment
6772
# Allows arbitrary HAProxy configuration to be passed through to support
6873
# additional configuration not available via parameters, or to short-circute
@@ -131,6 +136,7 @@
131136
Hash $global_options = $haproxy::params::global_options,
132137
Hash $defaults_options = $haproxy::params::defaults_options,
133138
Boolean $merge_options = $haproxy::params::merge_options,
139+
Haproxy::Programs $programs = {},
134140
Optional[String] $restart_command = undef,
135141
Optional[String] $custom_fragment = undef,
136142
Stdlib::Absolutepath $config_dir = $haproxy::params::config_dir,
@@ -187,5 +193,6 @@
187193
service_options => $service_options,
188194
sysconfig_options => $sysconfig_options,
189195
config_validate_cmd => $config_validate_cmd,
196+
programs => $programs,
190197
}
191198
}

manifests/instance.pp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@
8585
#
8686
# @param sysconfig_options
8787
#
88+
# @param programs
89+
# A program section contains a set of directives that define the program to be run,
90+
# its command-line options and flags, as well as user, group, and restart options.
91+
#
8892
# @example
8993
# A single instance of haproxy with all defaults
9094
# i.e. emulate Class['haproxy']
@@ -175,6 +179,7 @@
175179
Boolean $merge_options = $haproxy::params::merge_options,
176180
String $service_options = $haproxy::params::service_options,
177181
String $sysconfig_options = $haproxy::params::sysconfig_options,
182+
Haproxy::Programs $programs = {},
178183
) {
179184
# Since this is a 'define', we can not use 'inherts haproxy::params'.
180185
# Therefore, we "include haproxy::params" for any parameters we need.
@@ -221,6 +226,7 @@
221226
package_ensure => $package_ensure,
222227
chroot_dir_manage => $chroot_dir_manage,
223228
config_validate_cmd => $config_validate_cmd,
229+
programs => $programs,
224230
}
225231
haproxy::install { $title:
226232
package_name => $package_name,

manifests/program.pp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# @summary Program definition
2+
#
3+
# A command to be executed by haproxy master process
4+
#
5+
# @see https://www.haproxy.com/documentation/haproxy-configuration-tutorials/programs/
6+
# @example
7+
# haproxy::program { 'hello':
8+
# command => 'hello world',
9+
# }
10+
define haproxy::program (
11+
String $command,
12+
Optional[String] $user = undef,
13+
Optional[String] $group = undef,
14+
Optional[String] $options = undef,
15+
String $instance = 'haproxy',
16+
Optional[Stdlib::Absolutepath] $config_file = undef,
17+
) {
18+
# We derive these settings so that the caller only has to specify $instance.
19+
include haproxy::params
20+
21+
if $instance == 'haproxy' {
22+
$instance_name = 'haproxy'
23+
$_config_file = pick($config_file, $haproxy::config_file)
24+
} else {
25+
$instance_name = "haproxy-${instance}"
26+
$_config_file = pick($config_file, inline_template($haproxy::params::config_file_tmpl))
27+
}
28+
29+
concat::fragment { "${instance_name}-${name}_program":
30+
order => "40-program-${name}",
31+
target => $_config_file,
32+
content => epp('haproxy/haproxy_program.epp', {
33+
'name' => $name,
34+
'command' => $command,
35+
'user' => $user,
36+
'group' => $group,
37+
'options' => $options,
38+
}),
39+
}
40+
}

spec/classes/haproxy_spec.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -668,4 +668,31 @@
668668
}.to raise_error(Puppet::Error, %r{operating system is not supported with the haproxy module})
669669
end
670670
end
671+
672+
describe 'when programs is specified' do
673+
['Debian'].each do |osfamily|
674+
context "when on #{osfamily} family operatingsystems" do
675+
let(:facts) do
676+
{ os: { family: osfamily } }.merge default_facts
677+
end
678+
let(:contents) { param_value(catalogue, 'concat::fragment', 'haproxy-haproxy-base', 'content').split("\n") }
679+
let(:params) do
680+
{
681+
'programs' => {
682+
'foo' => {
683+
'command' => '/usr/bin/foo',
684+
},
685+
'bar' => {
686+
'command' => '/usr/bin/bar',
687+
},
688+
},
689+
}
690+
end
691+
692+
it { is_expected.to compile }
693+
it { is_expected.to contain_concat__fragment('haproxy-foo_program').with_content(%r{command /usr/bin/foo}) }
694+
it { is_expected.to contain_concat__fragment('haproxy-bar_program').with_content(%r{command /usr/bin/bar}) }
695+
end
696+
end
697+
end
671698
end

spec/defines/program_spec.rb

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
5+
describe 'haproxy::program' do
6+
let :pre_condition do
7+
'class{"haproxy":
8+
config_file => "/tmp/haproxy.cfg"
9+
}
10+
'
11+
end
12+
let(:facts) do
13+
{
14+
concat_basedir: '/foo',
15+
os: {
16+
family: 'Debian'
17+
}
18+
}
19+
end
20+
21+
context 'simple program' do
22+
let(:title) { 'hello' }
23+
let(:params) do
24+
{
25+
command: 'echo "hello world"',
26+
}
27+
end
28+
29+
it {
30+
is_expected.to contain_concat__fragment('haproxy-hello_program').with(
31+
'order' => '40-program-hello',
32+
'target' => '/tmp/haproxy.cfg',
33+
'content' => %r{command echo "hello world"},
34+
)
35+
}
36+
end
37+
end

templates/haproxy_program.epp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
program <%= $name %>
2+
command <%= $command %>
3+
<% if $user { -%>
4+
user <%= $user %>
5+
<% } -%>
6+
<% if $group { -%>
7+
group $group
8+
<% } -%>
9+
<% if $options { -%>
10+
<%= $options %>
11+
<% } -%>

types/programs.pp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
type Haproxy::Programs = Hash[String, Struct[{
2+
command => String[1],
3+
Optional[user] => String[1],
4+
Optional[group] => String[1],
5+
Optional[options] => String[1],
6+
Optional[instance] => String[1],
7+
Optional[config_file] => Stdlib::Absolutepath,
8+
}]]
9+

0 commit comments

Comments
 (0)