Maintain /etc/nagios/dsa-check-backuppg.conf with puppet
[mirror/dsa-puppet.git] / modules / postgres / manifests / backup_server.pp
1 #
2 class postgres::backup_server::globals {
3         $make_base_backups = '/usr/local/bin/postgres-make-base-backups'
4         $pgpassfile = '/home/debbackup/.pgpass'
5         $sshkeys_sources = '/etc/dsa/postgresql-backup/sshkeys-sources'
6
7         $tag_base_backup = "postgresql::server::backup-source-make-base-backup-entry"
8         $tag_source_sshkey = "postgresql::server::backup-source-sshkey"
9         $tag_source_pgpassline = "postgresql::server::backup-source-pgpassline"
10         $tag_dsa_check_backupp = "postgresql::server::backup-dsa-check-backuppg"
11 }
12
13 class postgres::backup_server {
14         include postgres::backup_server::globals
15
16         ####
17         # Regularly pull base backups
18         #
19         ensure_packages ( "postgresql-client-9.1", { ensure => 'installed' })
20         ensure_packages ( "postgresql-client-9.4", { ensure => 'installed' })
21
22         concat { $postgres::backup_server::globals::make_base_backups:
23                 mode => '0555',
24         }
25         concat::fragment { 'make-base-backups-header':
26                 target => $postgres::backup_server::globals::make_base_backups,
27                 content => template('postgres/backup_server/postgres-make-base-backups.erb'),
28                 order  => '00',
29         }
30         Concat::Fragment <<| tag == $postgres::backup_server::globals::tag_base_backup |>>
31         concat::fragment { 'make-base-backups-tail':
32                 target => $postgres::backup_server::globals::make_base_backups,
33                 content  => @(EOTEMPLATE),
34                                 # EOF by make-base-backups-tail fragment
35                                 EOF
36                                 | EOTEMPLATE
37                 order  => '99',
38         }
39         if $::hostname in [backuphost] {
40                 file { '/etc/cron.d/puppet-postgres-make-base-backups':
41                         content  => "20 1 * * 0 debbackup chronic ${$postgres::backup_server::globals::make_base_backups}\n",
42                 }
43         } else  {
44                 file { '/etc/cron.d/puppet-postgres-make-base-backups':
45                         content  => "20 0 * * 6 debbackup chronic ${$postgres::backup_server::globals::make_base_backups}\n",
46                 }
47         }
48
49         ####
50         # Maintain authorized_keys file on backup servers for WAL shipping
51         #
52         # do not let other hosts directly build our authorized_keys file,
53         # instead go via a script that somewhat validates intput
54         file { '/etc/dsa/postgresql-backup':
55                 ensure => 'directory',
56         }
57         file { '/usr/local/bin/postgres-make-backup-sshauthkeys':
58                 content => template('postgres/backup_server/postgres-make-backup-sshauthkeys.erb'),
59                 mode   => '0555',
60                 notify  => Exec['postgres-make-backup-sshauthkeys'],
61         }
62         file { '/etc/dsa/postgresql-backup/sshkeys-manual':
63                 content => template('postgres/backup_server/sshkeys-manual.erb'),
64                 notify  => Exec['postgres-make-backup-sshauthkeys'],
65         }
66         concat { $postgres::backup_server::globals::sshkeys_sources:
67                 notify  => Exec['postgres-make-backup-sshauthkeys'],
68         }
69         concat::fragment { 'postgresql-backup/source-sshkeys-header':
70                 target => $postgres::backup_server::globals::sshkeys_sources ,
71                 content  => @(EOF),
72                                 # <name> <ip addresses> <key>
73                                 | EOF
74                 order  => '00',
75         }
76         Concat::Fragment <<| tag == $postgres::backup_server::globals::tag_source_sshkey |>>
77         exec { "postgres-make-backup-sshauthkeys":
78                 command => "/usr/local/bin/postgres-make-backup-sshauthkeys",
79                 refreshonly => true,
80         }
81
82         ####
83         # Maintain /etc/nagios/dsa-check-backuppg.conf
84         #
85         file { '/etc/dsa/postgresql-backup/dsa-check-backuppg.conf.d':
86                 ensure => 'directory',
87                 purge   => true,
88                 force   => true,
89                 recurse => true,
90                 source  => 'puppet:///files/empty/',
91                 notify => Exec['update dsa-check-backuppg-manual.conf'],
92         }
93         file { '/etc/dsa/postgresql-backup/dsa-check-backuppg.conf.d/manual.conf':
94                 content => template('postgres/backup_server/dsa-check-backuppg-manual.conf.erb'),
95                 notify => Exec['update dsa-check-backuppg-manual.conf']
96         }
97         file { '/etc/dsa/postgresql-backup/dsa-check-backuppg.conf.d/moszumanska.conf':
98                 ensure => ($::hostname in [backuphost]) ? {
99                                 true    => 'absent',
100                                 default => 'present'
101                         },
102                 content  => @(EOF),
103                                 --- 
104                                 backups:
105                                   moszumanska:
106                                     main:
107                                 | EOF
108                 notify => Exec['update dsa-check-backuppg-manual.conf']
109         }
110         File<<| tag == $postgres::backup_server::globals::tag_dsa_check_backupp |>>
111         ensure_packages ( "libhash-merge-simple-perl", { ensure => 'installed' })
112         exec { "update dsa-check-backuppg-manual.conf":
113                 command  => @(EOF),
114                                 perl -MYAML=LoadFile,Dump -MHash::Merge::Simple=merge -E 'say Dump(merge(map{LoadFile($_)}@ARGV))' /etc/dsa/postgresql-backup/dsa-check-backuppg.conf.d/*.conf > /etc/nagios/dsa-check-backuppg.conf
115                                 | EOF
116                 provider => shell,
117                 refreshonly => true,
118         }
119
120         ####
121         # Maintain .pgpass file on backup servers
122         # #
123         concat { $postgres::backup_server::globals::pgpassfile:
124                 owner => 'debbackup',
125                 group => 'debbackup',
126                 mode  => '0400'
127         }
128         concat::fragment{ 'pgpass-local':
129                 target => $postgres::backup_server::globals::pgpassfile,
130                 source => '/home/debbackup/.pgpass-local',
131                 order  => '00'
132         }
133         Concat::Fragment <<| tag == $postgres::backup_server::globals::tag_source_pgpassline |>>
134 }
135
136 define postgres::backup_server::register_backup_clienthost (
137         $sshpubkey = $::postgresql_key,
138         $ipaddrlist = join(getfromhash($site::nodeinfo, 'ldap', 'ipHostNumber'), ","),
139         $hostname = $::hostname,
140 ) {
141         include postgres::backup_server::globals
142
143         if $sshpubkey {
144                 $addr = assert_type(String[1], $ipaddrlist)
145                 @@concat::fragment { "postgresql::server::backup-source-clienthost::$name::$fqdn":
146                         target => $postgres::backup_server::globals::sshkeys_sources ,
147                         content  => @("EOF"),
148                                         ${hostname} ${addr} ${sshpubkey}
149                                         | EOF
150                         tag     => $postgres::backup_server::globals::tag_source_sshkey,
151                 }
152         }
153 }
154
155 define postgres::backup_server::register_backup_cluster (
156         $hostname = $::hostname,
157         $fqdn = $::fqdn,
158         $pg_port,
159         $pg_role,
160         $pg_password,
161         $pg_cluster,
162         $pg_version,
163 ) {
164         include postgres::backup_server::globals
165
166         # foobar.debian.org:5432:*:debian-backup:swordfish
167         @@concat::fragment { "postgresql::server::backup-source-pgpassline::$hostname::$pg_port::$pg_role":
168                 target => $postgres::backup_server::globals::pgpassfile,
169                 content => @("EOF"),
170                                 ${fqdn}:${pg_port}:*:${pg_role}:${pg_password}
171                                 | EOF
172                 tag     => $postgres::backup_server::globals::tag_source_pgpassline,
173         }
174         #
175         # vittoria.debian.org   5432    debian-backup           main            9.6
176         @@concat::fragment { "postgresql::server::backup-source-make-base-backup-entry::$hostname::$pg_port::$pg_role":
177                 target => $postgres::backup_server::globals::make_base_backups,
178                 content => @("EOF"),
179                                 ${fqdn} ${pg_port}      ${pg_role}      ${pg_cluster}   ${pg_version}
180                                 | EOF
181                 tag     => $postgres::backup_server::globals::tag_base_backup,
182         }
183
184         @@file { "/etc/dsa/postgresql-backup/dsa-check-backuppg.conf.d/${hostname}-${pg_cluster}.conf":
185                 content  => @("EOF"),
186                                 --- 
187                                 backups:
188                                   ${hostname}:
189                                     ${pg_cluster}:
190                                 | EOF
191                 tag     => $postgres::backup_server::globals::tag_dsa_check_backupp,
192                 notify  => Exec['update dsa-check-backuppg-manual.conf']
193         }
194 }