File: instance_service.pp

package info (click to toggle)
puppet-module-puppetlabs-haproxy 8.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 816 kB
  • sloc: ruby: 3,979; sh: 14; makefile: 4
file content (124 lines) | stat: -rw-r--r-- 4,294 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# @summary
#   Set up the environment for an haproxy service.
#
# @note 
#   * Associate an haproxy instance with the haproxy package it should use.
#   * Create the start/restart/stop functions needed by Service[].
#   In other words: sets things up so that Service[$instance_name] will work.
#
#   In particular:
#   * Create a link to the binary an instance will be using. This
#     way each instance can link to a different binary.
#     If you have an instance called "foo", you know "haproxy-foo"
#     is a link to the binary it should be using.
#   * Create an init.d file named after the instance. This way
#     Service[$instance] can start/restart the service.
#
# @note
#  This manifest is just one example of how to set up Service[$instance].
#  Other sites may choose to do it very differently. In that case, do
#  not call haproxy::instance_service; write your own module.  The only
#  requirement is that before haproxy::instance{$instance:} is called,
#  Service[$instance] must be defined.
#
#  FIXME: This hasn't been tested on FreeBSD.
#  FIXME: This should take advantage of systemd when available.
#
#
# @param haproxy_package
#   The name of the package to be installed. This is useful if
#   you package your own custom version of haproxy.
#   Defaults to 'haproxy'
#
# @param bindir
#   Where to put symlinks to the binary used for each instance.
#   Defaults to '/opt/haproxy'
#
# @param haproxy_init_source
#   The init.d script that will start/restart/reload this instance.
#
# @param haproxy_unit_template
#   The template that will be used to create an unit file.
#
define haproxy::instance_service (
  Optional[String]      $haproxy_init_source    = undef,
  String                $haproxy_unit_template  = 'haproxy/instance_service_unit.epp',
  String                $haproxy_package        = 'haproxy',
  Stdlib::Absolutepath  $bindir                 = '/opt/haproxy/bin',
) {
  ensure_resource('package', $haproxy_package, {
      'ensure' => 'present',
  })

  # Manage the parent directory.
  ensure_resource('file', $bindir, {
      ensure => directory,
      owner   => 'root',
      group   => 'root',
      mode    => '0744',
  })

  # Create a link named after the instance. This just makes it easier
  # to manage difference instances using different versions of haproxy.
  # If you have an instance called "foo", you know "haproxy-foo"
  # is the binary.
  $haproxy_link = "${bindir}/haproxy-${title}"
  if $haproxy_package == 'haproxy' {
    $haproxy_target = '/usr/sbin/haproxy'
  } else {
    $haproxy_target = "/opt/${haproxy_package}/sbin/haproxy"
  }
  file { $haproxy_link:
    ensure => link,
    target => $haproxy_target,
  }

  # Create init.d or systemd files so that "service haproxy-$instance start"
  # or "systemd start haproxy-$instance" works.
  # This is not required if the standard instance is being used.
  if ($title == 'haproxy') and ($haproxy_package == 'haproxy') {
  } else {
    $initfile = "/etc/init.d/haproxy-${title}"
# systemd:
    if $haproxy_package == 'haproxy' {
      $wrapper = '/usr/sbin/haproxy-systemd-wrapper'
    } else {
      $wrapper = "/opt/${haproxy_package}/sbin/haproxy-systemd-wrapper"
    }

    if $facts['os']['family'] == 'RedHat' {
      $unitfile = "/usr/lib/systemd/system/haproxy-${title}.service"
    } else {
      $unitfile = "/lib/systemd/system/haproxy-${title}.service"
    }

    $parameters = {
      'title'   => $title,
      'wrapper' => $wrapper,
    }
    file { $unitfile:
      ensure  => file,
      mode    => '0644',
      owner   => 'root',
      group   => 'root',
      content => epp($haproxy_unit_template, $parameters),
      notify  => Exec['systemctl daemon-reload'],
    }
    if (!defined(Exec['systemctl daemon-reload'])) {
      exec { 'systemctl daemon-reload':
        command     => 'systemctl daemon-reload',
        path        => '/bin:/usr/bin:/usr/local/bin:/sbin:/usr/sbin',
        refreshonly => true,
        before      => Service["haproxy-${title}"],
      }
    }
    File[$haproxy_link] -> File[$unitfile]
    # Clean up in case the old init.d-style file is still around.
    file { $initfile:
      ensure => absent,
      before => Service["haproxy-${title}"],
    }
  }

  Package[$haproxy_package] -> File[$bindir] -> File[$haproxy_link]
}