Skip to content

Commit

Permalink
Various cleanup around validate-all action
Browse files Browse the repository at this point in the history
PG_VERNUM should not be computed and set from validate_all
action. Because of this grip, I did some various cleanup around
the pgsql_validate_all sub:

* move PG_VERNUM computing in its own sub
* move datadir and pgdata sanity checks outside validate_all as
  they are required by PG_VERNUM
* exit sooner for meta-data and methods actions
  • Loading branch information
ioguix committed Nov 13, 2019
1 parent b508f82 commit 692ccc5
Showing 1 changed file with 75 additions and 66 deletions.
141 changes: 75 additions & 66 deletions script/pgsqlms
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,35 @@ sub _get_controldata {
exit $OCF_ERR_ARGS;
}

# Pead major version from datadir/PG_VERSION and return it as numeric version
sub _get_pg_version {
my $fh;
my $PGVERSION;
my $PGVERNUM;

# check PG_VERSION
if ( ! -s "$datadir/PG_VERSION" ) {
ocf_exit_reason( 'PG_VERSION does not exist in "%s"', $datadir );
exit $OCF_ERR_ARGS;
}

unless ( open( $fh, '<', "$datadir/PG_VERSION" ) ) {
ocf_exit_reason( "Could not open file \"$datadir/PG_VERSION\": $!" );
exit $OCF_ERR_ARGS;
}

read( $fh, $PGVERSION, 32 );
close $fh;

chomp $PGVERSION;

$PGVERSION =~ /^(\d+)(?:\.(\d+))?$/;
$PGVERNUM = $1 * 10000;
$PGVERNUM += $2 * 100 if $1 < 10; # no 2nd num in the major version from v10

return $PGVERNUM;
}

# Use pg_controldata to check the state of the PostgreSQL server. This
# function returns codes depending on this state, so we can find whether the
# instance is a primary or a secondary, or use it to detect any inconsistency
Expand Down Expand Up @@ -1169,7 +1198,7 @@ sub ocf_meta_data {
</actions>
</resource-agent>
};
return;
return $OCF_SUCCESS;
}


Expand Down Expand Up @@ -1244,7 +1273,8 @@ sub ocf_methods {
meta-data
validate-all
};
return;

return $OCF_SUCCESS;
}

############################################################
Expand All @@ -1253,7 +1283,6 @@ sub ocf_methods {
sub pgsql_validate_all {
my $fh;
my $ans = '';
my $PGVERSION;
my @content;
my %cdata;

Expand All @@ -1264,7 +1293,7 @@ sub pgsql_validate_all {
'PAF %s is compatible with Pacemaker 1.1.13 and greater',
$VERSION
);
exit $OCF_ERR_INSTALLED;
return $OCF_ERR_INSTALLED;
}

# check notify=true
Expand All @@ -1275,7 +1304,7 @@ sub pgsql_validate_all {
ocf_exit_reason(
'You must set meta parameter notify=true for your master resource'
);
exit $OCF_ERR_INSTALLED;
return $OCF_ERR_INSTALLED;
}

# check master-max=1
Expand All @@ -1286,58 +1315,40 @@ sub pgsql_validate_all {
ocf_exit_reason(
'You must set meta parameter master-max=1 for your master resource'
);
exit $OCF_ERR_INSTALLED;
}

# check pgdata
if ( ! -d $pgdata ) {
ocf_exit_reason( 'PGDATA "%s" does not exist', $pgdata );
exit $OCF_ERR_ARGS;
}

# check datadir
if ( ! -d $datadir ) {
ocf_exit_reason( 'data_directory "%s" does not exist', $datadir );
exit $OCF_ERR_ARGS;
}

# check PG_VERSION
if ( ! -s "$datadir/PG_VERSION" ) {
ocf_exit_reason( 'PG_VERSION does not exist in "%s"', $datadir );
exit $OCF_ERR_ARGS;
return $OCF_ERR_INSTALLED;
}

# check recovery template
if ( ! -f $recovery_tpl ) {
ocf_exit_reason( 'Recovery template file "%s" does not exist',
$recovery_tpl );
exit $OCF_ERR_ARGS;
return $OCF_ERR_ARGS;
}

# check content of the recovery template file
unless ( open( $fh, '<', $recovery_tpl ) ) {
ocf_exit_reason( 'Could not open file "%s": %s', $recovery_tpl, $! );
exit $OCF_ERR_ARGS;
return $OCF_ERR_ARGS;
}
@content = <$fh>;
close $fh;

unless ( looks_like_number($maxlag) ) {
ocf_exit_reason( 'maxlag is not a number: "%s"', $maxlag );
exit $OCF_ERR_INSTALLED;
return $OCF_ERR_INSTALLED;
}

unless ( grep /^\s*standby_mode\s*=\s*'?on'?\s*$/, @content ) {
ocf_exit_reason(
'Recovery template file must contain "standby_mode = on"' );
exit $OCF_ERR_ARGS;
return $OCF_ERR_ARGS;
}

unless ( grep /^\s*recovery_target_timeline\s*=\s*'?latest'?\s*$/, @content ) {
ocf_exit_reason(
"Recovery template file must contain \"recovery_target_timeline = 'latest'\""
);
exit $OCF_ERR_ARGS;
return $OCF_ERR_ARGS;
}

unless (
Expand All @@ -1347,36 +1358,19 @@ sub pgsql_validate_all {
ocf_exit_reason(
'Recovery template file must contain in primary_conninfo parameter "application_name=%s"',
$nodename );
exit $OCF_ERR_ARGS;
return $OCF_ERR_ARGS;
}

# check system user
unless ( defined getpwnam $system_user ) {
ocf_exit_reason( 'System user "%s" does not exist', $system_user );
exit $OCF_ERR_ARGS;
return $OCF_ERR_ARGS;
}

# require 9.3 minimum
unless ( open( $fh, '<', "$datadir/PG_VERSION" ) ) {
ocf_exit_reason( "Could not open file \"$datadir/PG_VERSION\": $!" );
exit $OCF_ERR_ARGS;
}
read( $fh, $PGVERSION, 64 );
close $fh;

chomp $PGVERSION;

$PGVERSION =~ /^(\d+)(?:\.(\d+))?$/;

$PGVERNUM = $1 * 10000;

# postgresql >= 10 do not include the 2nd num in the major release
$PGVERNUM += $2 * 100 if $1 < 10;

if ( $PGVERNUM < $PGVER_93 ) {
ocf_exit_reason( "PostgreSQL %s not supported. Require 9.3 and more",
$PGVERSION );
exit $OCF_ERR_INSTALLED;
ocf_exit_reason( "Require 9.3 and more" );
return $OCF_ERR_INSTALLED;
}

$PGWALDUMP = "$bindir/pg_xlogdump" if $PGVERNUM < $PGVER_10;
Expand All @@ -1388,15 +1382,15 @@ sub pgsql_validate_all {
ocf_exit_reason(
"Missing one or more binary. Check following path: %s, %s, %s, %s, %s",
$PGCTL, $PGPSQL, $PGCTRLDATA, $PGISREADY, $PGWALDUMP );
exit $OCF_ERR_ARGS;
return $OCF_ERR_ARGS;
}

# require wal_level >= hot_standby
%cdata = _get_controldata();
unless ( $cdata{'wal_level'} =~ m{hot_standby|logical|replica} ) {
ocf_exit_reason(
'wal_level must be one of "hot_standby", "logical" or "replica"' );
exit $OCF_ERR_ARGS;
return $OCF_ERR_ARGS;
}

return $OCF_SUCCESS;
Expand Down Expand Up @@ -2179,29 +2173,44 @@ sub pgsql_reload {
############################################################
#### MAIN

exit ocf_meta_data() if $OCF_ACTION eq 'meta-data';
exit ocf_methods() if $OCF_ACTION eq 'methods';

# Avoid "could not change directory" when executing commands as "system-user".
chdir File::Spec->tmpdir();

# mandatory sanity checks
# check pgdata
if ( ! -d $pgdata ) {
ocf_exit_reason( 'PGDATA "%s" does not exist', $pgdata );
exit $OCF_ERR_ARGS;
}

# check datadir
if ( ! -d $datadir ) {
ocf_exit_reason( 'data_directory "%s" does not exist', $datadir );
exit $OCF_ERR_ARGS;
}

# Set PostgreSQL version
$PGVERNUM = _get_pg_version();

# Set current node name.
$nodename = ocf_local_nodename();

if ( $OCF_ACTION =~ /^(?:start|stop|reload|monitor|promote|demote|notify)$/ ) {
pgsql_validate_all();
# No need to validate for meta-data, methods or validate-all.
}
$exit_code = pgsql_validate_all();

exit $exit_code if $exit_code != $OCF_SUCCESS or $OCF_ACTION eq 'validate-all';

# Run action
for ( $OCF_ACTION ) {
if ( /^start$/ ) { $exit_code = pgsql_start() }
elsif ( /^stop$/ ) { $exit_code = pgsql_stop() }
elsif ( /^monitor$/ ) { $exit_code = pgsql_monitor() }
elsif ( /^promote$/ ) { $exit_code = pgsql_promote() }
elsif ( /^demote$/ ) { $exit_code = pgsql_demote() }
elsif ( /^notify$/ ) { $exit_code = pgsql_notify() }
elsif ( /^reload$/ ) { $exit_code = pgsql_reload() }
elsif ( /^validate-all$/ ) { $exit_code = pgsql_validate_all() }
elsif ( /^meta-data$/ ) { ocf_meta_data() }
elsif ( /^methods$/ ) { ocf_methods() }
if ( /^start$/ ) { $exit_code = pgsql_start() }
elsif ( /^stop$/ ) { $exit_code = pgsql_stop() }
elsif ( /^monitor$/ ) { $exit_code = pgsql_monitor() }
elsif ( /^promote$/ ) { $exit_code = pgsql_promote() }
elsif ( /^demote$/ ) { $exit_code = pgsql_demote() }
elsif ( /^notify$/ ) { $exit_code = pgsql_notify() }
elsif ( /^reload$/ ) { $exit_code = pgsql_reload() }
else { $exit_code = $OCF_ERR_UNIMPLEMENTED }
}

Expand Down

0 comments on commit 692ccc5

Please sign in to comment.