Debian Codegraph @

manifests/instance_service.pp

raw
1 # @summary
2 # Set up the environment for an haproxy service.
3 #
4 # @note
5 # * Associate an haproxy instance with the haproxy package it should use.
6 # * Create the start/restart/stop functions needed by Service[].
7 # In other words: sets things up so that Service[$instance_name] will work.
8 #
9 # In particular:
10 # * Create a link to the binary an instance will be using. This
11 # way each instance can link to a different binary.
12 # If you have an instance called "foo", you know "haproxy-foo"
13 # is a link to the binary it should be using.
14 # * Create an init.d file named after the instance. This way
15 # Service[$instance] can start/restart the service.
16 #
17 # @note
18 # This manifest is just one example of how to set up Service[$instance].
19 # Other sites may choose to do it very differently. In that case, do
20 # not call haproxy::instance_service; write your own module. The only
21 # requirement is that before haproxy::instance{$instance:} is called,
22 # Service[$instance] must be defined.
23 #
24 # FIXME: This hasn't been tested on FreeBSD.
25 # FIXME: This should take advantage of systemd when available.
26 #
27 #
28 # @param haproxy_package
29 # The name of the package to be installed. This is useful if
30 # you package your own custom version of haproxy.
31 # Defaults to 'haproxy'
32 #
33 # @param bindir
34 # Where to put symlinks to the binary used for each instance.
35 # Defaults to '/opt/haproxy'
36 #
37 # @param haproxy_init_source
38 # The init.d script that will start/restart/reload this instance.
39 #
40 # @param haproxy_unit_template
41 # The template that will be used to create an unit file.
42 #
43 define haproxy::instance_service (
44 Optional[String] $haproxy_init_source = undef,
45 String $haproxy_unit_template = 'haproxy/instance_service_unit.epp',
46 String $haproxy_package = 'haproxy',
47 Stdlib::Absolutepath $bindir = '/opt/haproxy/bin',
48 ) {
49 ensure_resource('package', $haproxy_package, {
50 'ensure' => 'present',
51 })
52
53 # Manage the parent directory.
54 ensure_resource('file', $bindir, {
55 ensure => directory,
56 owner => 'root',
57 group => 'root',
58 mode => '0744',
59 })
60
61 # Create a link named after the instance. This just makes it easier
62 # to manage difference instances using different versions of haproxy.
63 # If you have an instance called "foo", you know "haproxy-foo"
64 # is the binary.
65 $haproxy_link = "${bindir}/haproxy-${title}"
66 if $haproxy_package == 'haproxy' {
67 $haproxy_target = '/usr/sbin/haproxy'
68 } else {
69 $haproxy_target = "/opt/${haproxy_package}/sbin/haproxy"
70 }
71 file { $haproxy_link:
72 ensure => link,
73 target => $haproxy_target,
74 }
75
76 # Create init.d or systemd files so that "service haproxy-$instance start"
77 # or "systemd start haproxy-$instance" works.
78 # This is not required if the standard instance is being used.
79 if ($title == 'haproxy') and ($haproxy_package == 'haproxy') {
80 } else {
81 $initfile = "/etc/init.d/haproxy-${title}"
82 # systemd:
83 if $haproxy_package == 'haproxy' {
84 $wrapper = '/usr/sbin/haproxy-systemd-wrapper'
85 } else {
86 $wrapper = "/opt/${haproxy_package}/sbin/haproxy-systemd-wrapper"
87 }
88
89 if $facts['os']['family'] == 'RedHat' {
90 $unitfile = "/usr/lib/systemd/system/haproxy-${title}.service"
91 } else {
92 $unitfile = "/lib/systemd/system/haproxy-${title}.service"
93 }
94
95 $parameters = {
96 'title' => $title,
97 'wrapper' => $wrapper,
98 }
99 file { $unitfile:
100 ensure => file,
101 mode => '0644',
102 owner => 'root',
103 group => 'root',
104 content => epp($haproxy_unit_template, $parameters),
105 notify => Exec['systemctl daemon-reload'],
106 }
107 if (!defined(Exec['systemctl daemon-reload'])) {
108 exec { 'systemctl daemon-reload':
109 command => 'systemctl daemon-reload',
110 path => '/bin:/usr/bin:/usr/local/bin:/sbin:/usr/sbin',
111 refreshonly => true,
112 before => Service["haproxy-${title}"],
113 }
114 }
115 File[$haproxy_link] -> File[$unitfile]
116 # Clean up in case the old init.d-style file is still around.
117 file { $initfile:
118 ensure => absent,
119 before => Service["haproxy-${title}"],
120 }
121 }
122
123 Package[$haproxy_package] -> File[$bindir] -> File[$haproxy_link]
124 }