Puppet Class: cd4peadm::component::postgres

Defined in:
manifests/component/postgres.pp

Summary

installs and configures postgres as the database

Overview

Parameters:

  • config (Cd4peadm::Config::Postgres)

    subset of Cd4peadm::Config specific to postgres



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
# File 'manifests/component/postgres.pp', line 4

class cd4peadm::component::postgres (
  Cd4peadm::Config::Postgres $config
) {
  include cd4peadm::log_rotation

  $container = $config['container']
  $log_directory = '/opt/bitnami/postgresql/logs'

  $preinit_script = '/etc/puppetlabs/cd4pe/preinitdb.sh'
  file { $preinit_script:
    ensure  => 'file',
    content => epp('cd4peadm/postgres/preinitdb.sh.epp', { 'log_directory' => $log_directory }),
    owner   => 'root',
    group   => 'root',
    seltype => 'container_file_t',
  }

  $init_script = '/etc/puppetlabs/cd4pe/initdb.sql'
  file { $init_script:
    ensure  => 'file',
    content => epp('cd4peadm/postgres/initdb.sql.epp',
      {
        'cd4pe_db_username' => $config['cd4pe_db_username'],
        'cd4pe_db_password' => $config['cd4pe_db_password'].unwrap,
        'query_db_username' => $config['query_db_username'],
        'query_db_password' => $config['query_db_password'].unwrap,
      }
    ),
    owner   => 'root',
    group   => 'root',
    seltype => 'container_file_t',
  }

  $postgres_dir = '/bitnami/postgresql'
  $conf_destination = "${postgres_dir}/conf"
  $pgdata = "${postgres_dir}/data"
  $hba_file = '/etc/puppetlabs/cd4pe/pg_hba.conf'
  file { $hba_file:
    ensure  => 'file',
    source  => 'puppet:///modules/cd4peadm/postgres/pg_hba.conf',
    owner   => 'root',
    group   => 'root',
    seltype => 'container_file_t',
    notify  => Cd4peadm::Runtime::Run[$container['name']],
  }

  $conf_file = '/etc/puppetlabs/cd4pe/postgresql.conf'
  file { $conf_file:
    ensure  => 'file',
    content => epp('cd4peadm/postgres/postgresql.conf.epp', { 'log_directory' => $log_directory, 'log_level' => $config['log_level'] }),
    owner   => 'root',
    group   => 'root',
    seltype => 'container_file_t',
    notify  => Cd4peadm::Runtime::Run[$container['name']],
  }

  cd4peadm::runtime::volume { 'postgres':
    ensure  => 'present',
    runtime => $config['runtime'],
  }
  cd4peadm::runtime::volume { $container['log_volume_name']:
    ensure  => 'present',
    runtime => $config['runtime'],
  }

  cd4peadm::runtime::run { $container['name']:
    runtime          => $config['runtime'],
    image            => $container['image'],
    net              => 'cd4pe',
    ports            => ['5432:5432'],
    volumes          => [
      "postgres:${postgres_dir}",
      "${preinit_script}:/docker-entrypoint-preinitdb.d/preinitdb.sh",
      "${init_script}:/docker-entrypoint-initdb.d/initdb.sql",
      "${conf_file}:${conf_destination}/postgresql.conf",
      "${hba_file}:${conf_destination}/pg_hba.conf",
      "${container['log_volume_name']}:${log_directory}",
    ],
    env              => [
      "POSTGRESQL_PASSWORD=${config['admin_db_password'].unwrap}",
      'BITNAMI_DEBUG=true',
    ],
    pull_on_start    => false,
    require          => [
      File[$init_script],
      File[$hba_file],
      File[$conf_file],
    ],
    extra_parameters => "--platform=linux/amd64 ${container['extra_parameters']}",
    before_stop      => "${config['runtime']} exec ${container['name']} pg_ctl stop --mode=fast --pgdata=${pgdata}",
  }

  cd4peadm::logrotate_config { 'postgres':
    path            => "${cd4peadm::runtime::volume_dir()}/${container['log_volume_name']}/_data/*.log",
    size_mb         => $config['max_log_size_mb'],
    post_rotate_cmd => "${config['runtime']} exec ${container['name']} pg_ctl logrotate --pgdata=${pgdata}",
    keep_files      => $config['keep_log_files'],
  }
}