Start with puppetizing postgres cluster backup configuration. for now, only deal...
[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 }
11
12 class postgres::backup_server {
13         include postgres::backup_server::globals
14
15         package { 'postgresql-client-9.1':
16                 ensure => installed
17         }
18         package { 'postgresql-client-9.4':
19                 ensure => installed
20         }
21
22         concat { $postgres::backup_server::globals::make_base_backups:
23         }
24         concat::fragment { 'make-base-backups-header':
25                 target => $postgres::backup_server::globals::make_base_backups,
26                 content => template('postgres/backup_server/postgres-make-base-backups.erb'),
27                 order  => '00',
28         }
29         Concat::Fragment <<| tag == $postgres::backup_server::globals::tag_base_backup |>>
30         concat::fragment { 'make-base-backups-tail':
31                 target => $postgres::backup_server::globals::make_base_backups,
32                 content  => @(EOTEMPLATE),
33                                 # EOF by make-base-backups-tail fragment
34                                 EOF
35                                 | EOTEMPLATE
36                 order  => '99',
37         }
38         if $::hostname in [backuphost] {
39                 file { '/etc/cron.d/puppet-postgres-make-base-backups':
40                         content  => "20 1 * * 0 debbackup chronic ${$postgres::backup_server::globals::make_base_backups}\n",
41                 }
42         } else  {
43                 file { '/etc/cron.d/puppet-postgres-make-base-backups':
44                         content  => "20 0 * * 6 debbackup chronic ${$postgres::backup_server::globals::make_base_backups}\n",
45                 }
46         }
47
48         # Maintain authorized_keys file on backup servers for WAL shipping
49         #
50         # do not let other hosts directly build our authorized_keys file,
51         # instead go via a script that somewhat validates intput
52         file { '/etc/dsa/postgresql-backup':
53                 ensure => 'directory',
54         }
55         file { '/usr/local/bin/postgres-make-backup-sshauthkeys':
56                 content => template('postgres/backup_server/postgres-make-backup-sshauthkeys.erb'),
57                 mode   => '0555',
58                 notify  => Exec['postgres-make-backup-sshauthkeys'],
59         }
60         file { '/etc/dsa/postgresql-backup/sshkeys-manual':
61                 content => template('postgres/backup_server/sshkeys-manual.erb'),
62                 notify  => Exec['postgres-make-backup-sshauthkeys'],
63         }
64         concat { $postgres::backup_server::globals::sshkeys_sources:
65                 notify  => Exec['postgres-make-backup-sshauthkeys'],
66         }
67         concat::fragment { 'postgresql-backup/source-sshkeys-header':
68                 target => $postgres::backup_server::globals::sshkeys_sources ,
69                 content  => @(EOF),
70                                 # <name> <ip addresses> <key>
71                                 | EOF
72                 order  => '00',
73         }
74         Concat::Fragment <<| tag == $postgres::backup_server::globals::tag_source_sshkey |>>
75         exec { "postgres-make-backup-sshauthkeys":
76                 command => "/usr/local/bin/postgres-make-backup-sshauthkeys",
77                 refreshonly => true,
78         }
79
80         # Maintain .pgpass file on backup servers
81         concat { $postgres::backup_server::globals::pgpassfile:
82                 owner => 'debbackup',
83                 group => 'debbackup',
84                 mode  => '0400'
85         }
86         concat::fragment{ 'pgpass-local':
87                 target => $postgres::backup_server::globals::pgpassfile,
88                 source => '/home/debbackup/.pgpass-local',
89                 order  => '00'
90         }
91         Concat::Fragment <<| tag == $postgres::backup_server::globals::tag_source_pgpassline |>>
92 }
93
94 define postgres::backup_server::register_backup_clienthost (
95         $sshpubkey = $::postgresql_key,
96         $ipaddrlist = join(getfromhash($site::nodeinfo, 'ldap', 'ipHostNumber'), ","),
97         $hostname = $::hostname,
98 ) {
99         include postgres::backup_server::globals
100
101         if $sshpubkey {
102                 $addr = assert_type(String[1], $ipaddrlist)
103                 @@concat::fragment { "postgresql::server::backup-source-clienthost::$name::$fqdn":
104                         target => $postgres::backup_server::globals::sshkeys_sources ,
105                         content  => @("EOF"),
106                                         ${hostname} ${addr} ${sshpubkey}
107                                         | EOF
108                         tag     => $postgres::backup_server::globals::tag_source_sshkey,
109                 }
110         }
111 }
112
113 define postgres::backup_server::register_backup_cluster (
114         $hostname = $::fqdn,
115         $pg_port,
116         $pg_role,
117         $pg_password,
118         $pg_cluster,
119         $pg_version,
120 ) {
121         include postgres::backup_server::globals
122
123         # foobar.debian.org:5432:*:debian-backup:swordfish
124         @@concat::fragment { "postgresql::server::backup-source-pgpassline::$hostname::$pg_port::$pg_role":
125                 target => $postgres::backup_server::globals::pgpassfile,
126                 content => @("EOF"),
127                                 ${hostname}:${pg_port}:*:${pg_role}:${pg_password}
128                                 | EOF
129                 tag     => $postgres::backup_server::globals::tag_source_pgpassline,
130         }
131         #
132         # vittoria.debian.org   5432    debian-backup           main            9.6
133         @@concat::fragment { "postgresql::server::backup-source-make-base-backup-entry::$hostname::$pg_port::$pg_role":
134                 target => $postgres::backup_server::globals::make_base_backups,
135                 content => @("EOF"),
136                                 ${hostname}     ${pg_port}      ${pg_role}      ${pg_cluster}   ${pg_version}
137                                 | EOF
138                 tag     => $postgres::backup_server::globals::tag_base_backup,
139         }
140 }