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 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
9 changes: 9 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,14 @@
order => '10',
content => epp("${module_name}/haproxy-base.cfg.epp", $parameters),
}

if !empty($programs) {
$programs.each |String $program, Hash $attributes| {
Resource['haproxy::program'] { $program:
*=> $attributes,
}
}
}
}

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
51 changes: 51 additions & 0 deletions manifests/program.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# @summary Program definition
#
# @param command
# A command to be executed by haproxy master process
# @param user
# User account used for executing command
# @param group
# Assigned group
# @param options
# By default, the process manager stops and recreates child programs at haproxy reload.
# In order to disable this, set this parameter to `no option start-on-reload`
# @param instance
# haproxy instance
# @param config_file
# Path to haproxy config
# @see https://www.haproxy.com/documentation/haproxy-configuration-tutorials/programs/
# @example
# haproxy::program { 'hello':
# command => 'hello world',
# }
define haproxy::program (
String $command,
Optional[String] $user = undef,
Optional[String] $group = undef,
Optional[String] $options = undef,
String $instance = 'haproxy',
Optional[Stdlib::Absolutepath] $config_file = undef,
) {
# 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,
}]]