Fix dependency loop in mirror_health when a service is set to absent: the file need...
[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
32   # if we enable the service, we want the files before the service, so we
33   #   subscribe the service to the files.
34   # if we remove the service, we want the service disabled before the files
35   #   go away, so we say the service needs the files to be handled before.
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   $ensure_service = $ensure ? {
45     present => running,
46     absent  => stopped,
47   }
48   $ensure_enable = $ensure ? {
49     present => true,
50     absent  => false,
51   }
52
53   file { $service_file:
54     ensure  => $ensure,
55     content => template('mirror_health/mirror-health.service.erb'),
56     notify  => Exec['systemctl daemon-reload'],
57   }
58   service { "mirror-health-${check_service}":
59     ensure    => $ensure_service,
60     enable    => $ensure_enable,
61     notify    => Exec['systemctl daemon-reload'],
62     before    => $service_before,
63     subscribe => $service_subscribe + [ File[ $mirror_health::script ] ],
64   }
65
66
67   $hosts_file = "${mirror_health::confdir}/${check_service}.hosts"
68   $tag = "mirror_health::service::${check_service}::hosts"
69   concat { $hosts_file:
70     ensure         => $ensure,
71     ensure_newline => true,
72     mode           => '0444',
73     notify         => Service["mirror-health-${check_service}"],
74   }
75   if $this_host_service_name and $ensure == 'present' {
76     @@concat::fragment { "mirror_health::service::${check_service}::hosts::${this_host_service_name}":
77       tag     => $tag,
78       target  => $hosts_file,
79       content => $this_host_service_name,
80     }
81   }
82   Concat::Fragment <<| tag == $tag |>>
83 }