fced5182cf7c789ed10a2a152ee80f8dfafa6bae
[mirror/dsa-puppet.git] / modules / mirror_health / manifests / service.pp
1 # service instance for Debian's mirror-health checker
2 #
3 # for each defined service, a daemon will regularly check if the
4 # local version of <url> matches the majority version among the
5 # set of all hosts that define a <service_name>.
6 #
7 # the result of this check is then exposed via <health_url>
8 #
9 # @param url URL to check for freshness on each host (normally a Release file)
10 # @param health_url URL to the health marker
11 # @param this_host_service_name this host's service name for this service.
12 #                               Other nodes will connect to this hostname to
13 #                               fetch url for checking.
14 #                               If this is undef, other hosts will not
15 #                               check this host.
16 # @param check_service name of this service
17 # @param check_interval how often to check
18 # @param ensure  present or absent
19 define mirror_health::service (
20   String $url,
21   String $health_url,
22   Optional[String] $this_host_service_name = undef,
23   String $check_service = $name,
24   Integer $check_interval = 60,
25   Enum['present','absent'] $ensure = 'present',
26 ) {
27   include mirror_health
28
29   $service_file = "/etc/systemd/system/mirror-health-${check_service}.service"
30
31   file { $service_file:
32     content => template('mirror_health/mirror-health.service.erb'),
33     notify  => [Exec['systemctl daemon-reload'], Service["mirror-health-${check_service}"]],
34   }
35
36   $service_before = $ensure ? {
37     present => [],
38     default => [ File[$service_file], ],
39   }
40   $service_subscribe = $ensure ? {
41     present => [ File[$service_file], ],
42     default => [],
43   }
44
45   $ensure_service = $ensure ? {
46     present => running,
47     absent  => stopped,
48   }
49   $ensure_enable = $ensure ? {
50     present => true,
51     absent  => false,
52   }
53   service { "mirror-health-${check_service}":
54     ensure    => $ensure_service,
55     enable    => $ensure_enable,
56     require   => Exec['systemctl daemon-reload'],
57     before    => $service_before,
58     subscribe => $service_subscribe + [ File[ $mirror_health::script ] ],
59   }
60
61   $hosts_file = "${mirror_health::confdir}/${check_service}.hosts"
62   $tag = "mirror_health::service::${check_service}::hosts"
63   concat { $hosts_file:
64     ensure         => $ensure,
65     ensure_newline => true,
66     mode           => '0444',
67     notify         => Service["mirror-health-${check_service}"],
68   }
69   if $this_host_service_name and $ensure == 'present' {
70     @@concat::fragment { "mirror_health::service::${check_service}::hosts::${this_host_service_name}":
71       tag     => $tag,
72       target  => $hosts_file,
73       content => $this_host_service_name,
74     }
75   }
76   Concat::Fragment <<| tag == $tag |>>
77 }