Skip to content

Commit

Permalink
code review refactoring, will test
Browse files Browse the repository at this point in the history
  • Loading branch information
craigcomstock committed Sep 26, 2023
1 parent d26f978 commit 072a042
Showing 1 changed file with 33 additions and 30 deletions.
63 changes: 33 additions & 30 deletions packaging/common/cfengine-hub/postinstall.sh
Original file line number Diff line number Diff line change
Expand Up @@ -429,37 +429,40 @@ init_postgres_dir()
if [ -f "$BACKUP_DIR/data/postgresql.conf.modified" ]; then
# User-modified file from the previous old version of CFEngine exists, try to use it.
cp -a "$BACKUP_DIR/data/postgresql.conf.modified" "$PREFIX/state/pg/data/postgresql.conf"
# start subshell to disable error handling temporarily
(
set +e
(cd /tmp && su cfpostgres -c "$PREFIX/bin/pg_ctl -w -D $PREFIX/state/pg/data -l /var/log/postgresql.log start")
if [ $? = 0 ]; then
# Started successfully, stop it again, the migration requires it to be not running.
(cd /tmp && su cfpostgres -c "$PREFIX/bin/pg_ctl -w -D $PREFIX/state/pg/data -l /var/log/postgresql.log stop")

# Wait a while if we have to for the server to be stopped
if ! wait_for_cf_postgres_down; then
cf_console echo "Error: unable to shutdown postgresql server. Showing last of /var/log/postgresql.log for clues."
cf_console tail /var/log/postgresql.log
exit 1
fi
# Copy over the new config as well, user should take at look at it.
cf_console echo "Installing the $pgconfig_type postgresql.conf file as $PREFIX/state/pg/data/postgresql.conf.new."
cf_console echo "Please review it and update $PREFIX/state/pg/data/postgresql.conf accordingly."
cp -a "$new_pgconfig_file" "$PREFIX/state/pg/data/postgresql.conf.new"
chown cfpostgres "$PREFIX/state/pg/data/postgresql.conf.new"
else
# Failed to start, move the old file aside and use the new one.
mv "$PREFIX/state/pg/data/postgresql.conf" "$PREFIX/state/pg/data/postgresql.conf.old"
cf_console echo "Warning: failed to use the old postgresql.conf file, using the $pgconfig_type one."
cf_console echo "Please review the $PREFIX/state/pg/data/postgresql.conf file and update it accordingly."
cf_console echo "The original file was saved as $PREFIX/state/pg/data/postgresql.conf.old"
cf_console echo "last 10 lines of /var/log/postgresql.log for determining cause of failure"
cf_console tail -10 /var/log/postgresql.log
cp -a "$new_pgconfig_file" "$PREFIX/state/pg/data/postgresql.conf"
chown cfpostgres "$PREFIX/state/pg/data/postgresql.conf"
failure=0
(cd /tmp && su cfpostgres -c "$PREFIX/bin/pg_ctl -w -D $PREFIX/state/pg/data -l /var/log/postgresql.log start") || failure=1
if [ $failure = 0 ]; then
wait_for_cf_postgres || failure=1
fi
if [ $failure != 0 ]; then
# Started successfully, stop it again, the migration requires it to be not running.
(cd /tmp && su cfpostgres -c "$PREFIX/bin/pg_ctl -w -D $PREFIX/state/pg/data -l /var/log/postgresql.log stop") || failure=1
if [ $failure = 0 ]; then
wait_for_cfpostgres_down || failure=1
fi
if [ $failure != 0 ]; then
cf_console echo "Error: unable to shutdown postgresql server. Showing last of /var/log/postgresql.log for clues."
cf_console tail /var/log/postgresql.log
# this is a fatal error and so we exit instead of return
# steps after this init_postgres_dir() function should not continue if we can't start/stop the server
exit 1
fi
)
# Copy over the new config as well, user should take at look at it.
cf_console echo "Installing the $pgconfig_type postgresql.conf file as $PREFIX/state/pg/data/postgresql.conf.new."
cf_console echo "Please review it and update $PREFIX/state/pg/data/postgresql.conf accordingly."
cp -a "$new_pgconfig_file" "$PREFIX/state/pg/data/postgresql.conf.new"
chown cfpostgres "$PREFIX/state/pg/data/postgresql.conf.new"
else
# Failed to start, move the old file aside and use the new one.
mv "$PREFIX/state/pg/data/postgresql.conf" "$PREFIX/state/pg/data/postgresql.conf.old"
cf_console echo "Warning: failed to use the old postgresql.conf file, using the $pgconfig_type one."
cf_console echo "Please review the $PREFIX/state/pg/data/postgresql.conf file and update it accordingly."
cf_console echo "The original file was saved as $PREFIX/state/pg/data/postgresql.conf.old"
cf_console echo "last 10 lines of /var/log/postgresql.log for determining cause of failure"
cf_console tail -10 /var/log/postgresql.log
cp -a "$new_pgconfig_file" "$PREFIX/state/pg/data/postgresql.conf"
chown cfpostgres "$PREFIX/state/pg/data/postgresql.conf"
fi
else
# No user-modified file, just use the new recommended or default config (see generate_new_postgres_conf())
cp -a "$new_pgconfig_file" "$PREFIX/state/pg/data/postgresql.conf"
Expand Down

0 comments on commit 072a042

Please sign in to comment.