Skip to content

Commit

Permalink
repmgrd starting continuous standby node monitoring
Browse files Browse the repository at this point in the history
  • Loading branch information
abessifi committed Jul 8, 2014
1 parent 8292fcd commit cc319cb
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 12 deletions.
1 change: 1 addition & 0 deletions TODO
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fix stop_standby
41 changes: 30 additions & 11 deletions manifests/config.pp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
$pg_contribdir = '/usr/share/postgresql/9.1/contrib'

$repmgr_config_file = '/etc/repmgr/repmgr.conf'
#$repmgr_opts = join(['--force', '--verbose'], ' ')
$repmgrd_logdir = '/var/log/repmgr'
$repmgr_opts = join(['--force', '--verbose'], ' ')

Account['postgres'] -> File['repmgr_config_file'] -> File['pg_ssh_config']
Expand All @@ -51,6 +51,15 @@
path => $repmgr_config_file,
}

# Assert that repmgrd log directory exists
file {'repmgrd_log_dir':
ensure => directory,
path => $repmgrd_logdir,
owner => postgres,
group => postgres,
mode => 644,
}

# Assert postgres user exists
account {'postgres':
ensure => present,
Expand All @@ -71,25 +80,25 @@
content => 'StrictHostKeyChecking no',
}


if $node_role == 'master' {

# Create the user and database to manage replication
Exec['create_repmgr_db_user'] -> Exec['create_repmgr_db'] -> Exec['master_register']

# Create the user and database to manage replication
exec {'create_repmgr_db_user':
path => ['/bin/','/usr/bin'],
command => 'createuser -s repmgr',
user => 'postgres',
onlyif => '[ `psql template1 -c "\du" | grep -c repmgr` -eq "0" ]',
}

exec{'create_repmgr_db':
path => ['/bin', '/usr/bin'],
command => "createdb -O repmgr repmgr && psql -f $pg_contribdir/repmgr_funcs.sql repmgr",
user => 'postgres',
onlyif => '[ `psql -l | grep -c repmgr` -eq "0" ]',
}

}
# Register master node if it's not and only if repmgr database has been created
exec {'master_register':
path => ['/bin', '/usr/bin'],
command => "repmgr -f $repmgr_config_file master register",
Expand All @@ -102,22 +111,32 @@
}
elsif $node_role in ['slave', 'witness'] {

Exec['stop_standby'] -> Exec['clone_master']
Exec['stop_standby'] -> Exec['clone_master'] ~> Service['postgresql'] -> Exec['standby_register'] -> Exec['start_repmgrd']

# Stop standby node if running
exec {'stop_standby':
path => ['/usr/bin', '/usr/lib/postgresql/9.1/bin'],
command => "sudo -u postgres $pg_ctl -D $pg_data stop -l $pg_logfile",
onlyif => "[ killall -0 postgres | grep -c postgres` -eq 0 ]",
command => "sudo -u postgres $pg_ctl -D $pg_data stop -m fast -l $pg_logfile",
onlyif => "[ `killall -0 postgres 2>&1 | grep -c postgres` -eq 0 ]",
}
# Clone the master and start standby
exec {'clone_master':
path => ['/usr/bin'],
command => "sudo -u postgres repmgr -f $repmgr_config_file -D $pg_data -d repmgr -U repmgr -R postgres standby clone $master_node $repmgr_opts",
notify => Service['postgresql'],
#notify => Service['postgresql'],
}
# Register the standby server if it's not
exec {'standby_register':
path => ['/bin', '/usr/bin'],
command => "sudo -u postgres repmgr -f $repmgr_config_file standby register",
onlyif => "[ `repmgr -f $repmgr_config_file cluster show | grep -c 'standby | host=$node_name'` -eq 0 ]"
}
# Register the standby server
# Start repmgrd daemon
# NOTE running repmgrd via an init script will be better
exec {'start_repmgrd':
path => ['/bin', '/usr/bin'],
command => "sudo -u postgres repmgrd -f $repmgr_config_file -d --monitoring-history > ${repmgrd_logdir}/repmgrd.log 2>&1",
onlyif => "[ `killall -0 repmgrd 2>&1 | grep -c repmgrd` -ne 0 ]",
}
}

}
2 changes: 1 addition & 1 deletion manifests/postgresql.pp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
start => "sudo -u postgres $pg_ctl -D $pg_data start -o '-c config_file=$pg_configdir/postgresql.conf' -l $pg_logfile && sleep 10",
# The real Debian way is to use pg_ctlcluster like so :
# sudo -u postgres pg_ctlcluster 9.3 main start
stop => "sudo -u postgres $pg_ctl -D $pg_data stop -l $pg_logfile",
stop => "sudo -u postgres $pg_ctl -D $pg_data stop -m fast -l $pg_logfile",
restart => "sudo -u postgres $pg_ctl -D $pg_data restart -l $pg_logfile && sleep 10",
}

Expand Down

0 comments on commit cc319cb

Please sign in to comment.