aboutsummaryrefslogtreecommitdiff
path: root/modules/profile/manifests/postgresql/backup_pgbouncer.pp
blob: 45b8ed5a5528a89cfde528dda73bc421441e4b02 (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
define profile::postgresql::backup_pgbouncer (
  String $base_path,
  Hash   $pg_infos,
  String $pg_user  = "postgres",
  String $pg_group = "postgres",
) {
  include "profile::postgresql::pam_ldap_pgbouncer"
  ensure_packages(["pgbouncer"])

  $host_cn = $title

  $host = find_host($facts["ldapvar"]["other"], $host_cn)
  if empty($host) {
    fail("No host found for pgbouncer")
  } 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"

  if has_key($host["vars"], "postgresql_backup_port") {
    $pg_port = " port=${host[vars][postgresql_backup_port][0]}"
  } else {
    $pg_port = ""
  }

  # Config
  ensure_resource("concat", "/etc/pgbouncer/pgbouncer.ini", {
    mode           => "0644",
    owner          => "root",
    group          => "root",
    ensure_newline => true,
    notify         => Service["pgbouncer"],
    before         => Service["pgbouncer"],
  })

  ensure_resource("concat::fragment", "pgbouncer_head", {
    target => "/etc/pgbouncer/pgbouncer.ini",
    order  => 01,
    source => "puppet:///modules/profile/postgresql/pgbouncer_head.ini",
  })

  concat::fragment { "pgbouncer_$pg_backup_host":
    target  => "/etc/pgbouncer/pgbouncer.ini",
    order   => 02,
    content => "${pg_infos[pgbouncer_dbname]} = host=$pg_path$pg_port user=${pg_infos[dbuser]} dbname=${pg_infos[dbname]}",
  }

  # pg_hba for accessed cluster
  postgresql::server::pg_hba_rule { "$pg_backup_host - local access as ${pg_infos[dbuser]} user":
    description => "Allow local access to ${pg_infos[dbuser]} user",
    type        => 'local',
    database    => $pg_infos["dbname"],
    user        => $pg_infos["dbuser"],
    auth_method => 'trust',
    order       => "01-00",
    target      => "$pg_path/pg_hba.conf",
    postgresql_version => "10",
  }

  # service
  ensure_resource("file", "/etc/systemd/system/pgbouncer.service.d", {
    ensure => "directory",
    mode   => "0644",
    owner  => "root",
    group  => "root",
  })

  ensure_resource("file", "/etc/systemd/system/pgbouncer.service.d/override.conf", {
    ensure  => "present",
    mode    => "0644",
    owner   => "root",
    group   => "root",
    content => "[Service]\nUser=\nUser=$pg_user\n",
    notify  => Service["pgbouncer"],
    before  => Service["pgbouncer"],
  })

  ensure_resource("service", "pgbouncer", {
    ensure  => "running",
    enable  => true,
    require => [
      Package["pgbouncer"],
      File["/etc/systemd/system/pgbouncer.service.d/override.conf"],
      Concat["/etc/pgbouncer/pgbouncer.ini"]
    ],
  })


}