aboutsummaryrefslogtreecommitdiff
path: root/modules/profile/manifests/postgresql/backup_replication.pp
blob: a4edb8f2a9c526737aee8605fab48ab76d6ec4a3 (plain) (blame)
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
125
126
127
128
129
130
131
132
133
134
135
define profile::postgresql::backup_replication (
  String $base_path,
  Hash   $pg_infos,
  String $pg_user  = "postgres",
  String $pg_group = "postgres",
) {
  $host_cn = $title

  $host = find_host($facts["ldapvar"]["other"], $host_cn)
  if empty($host) {
    $pg_backup_host = $host_cn
  } elsif has_key($host["vars"], "host") {
    $pg_backup_host = $host["vars"]["host"][0]
  } else {
    $pg_backup_host = $host["vars"]["real_hostname"][0]
  }

  $pg_path = "$base_path/$pg_backup_host/postgresql"

  # Replication folder
  ensure_resource("file", "$base_path/$pg_backup_host", {
    ensure => directory,
  })

  file { $pg_path:
    ensure  => directory,
    owner   => $pg_user,
    group   => $pg_group,
    mode    => "0700",
    require => File["$base_path/$pg_backup_host"],
  }

  # pg_hba.conf
  profile::postgresql::base_pg_hba_rules { $pg_backup_host:
    pg_path => $pg_path
  }

  # postgresql.conf file and ssl
  concat { "$pg_path/postgresql.conf":
    owner => $pg_user,
    group => $pg_group,
    mode  => '0640',
    warn  => true,
  }

  if !empty($host) and has_key($host["vars"], "postgresql_backup_port") {
    $pg_listen_port = $host["vars"]["postgresql_backup_port"][0]

    profile::postgresql::ssl { $pg_path:
      certname             => $host_cn,
      handle_concat_config => true,
      before               => Service["postgresql_backup@$pg_backup_host"]
    }

    concat::fragment { "$pg_path/postgresql.conf listen":
      target  => "$pg_path/postgresql.conf",
      content => "listen_addresses = '*'\nport = $pg_listen_port\n",
    }

    profile::postgresql::replication { $host_cn:
      target => "$pg_path/pg_hba.conf",
    }
  } else {
    concat::fragment { "$pg_path/postgresql.conf listen":
      target  => "$pg_path/postgresql.conf",
      content => "listen_addresses = ''\n",
    }
  }

  concat::fragment { "$pg_path/postgresql.conf paths":
    target  => "$pg_path/postgresql.conf",
    content => "unix_socket_directories = '$pg_path'\ndata_directory = '$pg_path'\nwal_level = logical\n",
  }

  $password_seed = lookup("base_installation::puppet_pass_seed")
  $pg_host = $pg_backup_host
  $pg_port = $pg_infos["dbport"]
  $ldap_cn = lookup("base_installation::ldap_cn")
  $ldap_password = generate_password(24, $password_seed, "ldap")
  $pg_slot = regsubst($ldap_cn, '-', "_", "G")

  # recovery.conf file
  $primary_conninfo  = "host=$pg_host port=$pg_port user=$ldap_cn password=$ldap_password sslmode=require"
  $primary_slot_name = $pg_slot
  $standby_mode      = "on"

  file { "$pg_path/recovery.conf":
    owner   => $pg_user,
    group   => $pg_group,
    mode    => '0640',
    content => template('postgresql/recovery.conf.erb'),
  }

  # Initial replication
  exec { "pg_basebackup $pg_path":
    cwd         => $pg_path,
    user        => $pg_user,
    creates     => "$pg_path/PG_VERSION",
    environment => ["PGPASSWORD=$ldap_password"],
    command     => "/usr/bin/pg_basebackup -w -h $pg_host -p $pg_port -U $ldap_cn -D $pg_path -S $pg_slot",
    before      => [
      Concat["$pg_path/pg_hba.conf"],
      File["$pg_path/recovery.conf"],
      Concat["$pg_path/postgresql.conf"],
    ]
  }

  # Service
  ensure_resource("file", "/etc/systemd/system/postgresql_backup@.service", {
    mode    => "0644",
    owner   => "root",
    group   => "root",
    content => template("profile/postgresql/postgresql_backup@.service.erb"),
  })

  service { "postgresql_backup@$pg_backup_host":
    enable  => true,
    ensure  => "running",
    require => [
      File["/etc/systemd/system/postgresql_backup@.service"],
      Concat["$pg_path/pg_hba.conf"],
      File["$pg_path/recovery.conf"],
      Concat["$pg_path/postgresql.conf"],
    ],
    subscribe => [
      Concat["$pg_path/pg_hba.conf"],
      File["$pg_path/recovery.conf"],
      Concat["$pg_path/postgresql.conf"],
    ]
  }

  # Dumps
  profile::postgresql::backup_dump { "$base_path/$pg_backup_host": }

}