Skip to content

Commit fdca2b6

Browse files
committed
Fixed hub install aborting when there is no PostgreSQL backup to preserve
init_postgres_dir() took the "preserve the old pg_*.conf" branch on any upgrade, but preinstall.sh only populates $BACKUP_DIR/data when a PostgreSQL major version migration is needed and $PREFIX/state/pg/data exists. Upgrading within a major version, or after removing the data directory as preinstall.sh suggests to skip the migration, made the glob match nothing and aborted the whole scriptlet under `set -e`. Ticket: ENT-11934 Changelog: title Signed-off-by: Nick Anderson <nick.anderson@northern.tech> (cherry picked from commit e1dfd1f)
1 parent 654930e commit fdca2b6

2 files changed

Lines changed: 22 additions & 8 deletions

File tree

packaging/common/cfengine-hub/postinstall.sh

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -434,15 +434,26 @@ init_postgres_dir()
434434
chown cfpostgres:cfpostgres /var/log/postgresql.log
435435
chmod 600 /var/log/postgresql.log
436436

437-
if ! is_upgrade; then
438-
# Not an upgrade, just use the recommended or default file (see generate_new_postgres_conf())
437+
# Always use the original pg_*.conf files, they define access control to
438+
# PostgreSQL. An unmatched glob expands to itself, so `test -f` is what tells
439+
# us preinstall.sh made no backup to take them from.
440+
preserved_pgconfig=0
441+
if is_upgrade; then
442+
for _pgconf in "$BACKUP_DIR"/data/pg_*.conf; do
443+
test -f "$_pgconf" || continue
444+
cp -a "$_pgconf" "$PREFIX/state/pg/data/"
445+
chown cfpostgres "$PREFIX/state/pg/data/${_pgconf##*/}"
446+
preserved_pgconfig=1
447+
done
448+
test $preserved_pgconfig = 1 ||
449+
cf_console echo "No PostgreSQL configuration backup in $BACKUP_DIR, using freshly generated configuration files."
450+
fi
451+
452+
if [ $preserved_pgconfig = 0 ]; then
453+
# Nothing to preserve, just use the recommended or default file (see generate_new_postgres_conf())
439454
cp -a "$new_pgconfig_file" $PREFIX/state/pg/data/postgresql.conf
440455
chown cfpostgres $PREFIX/state/pg/data/postgresql.conf
441456
else
442-
# Always use the original pg_*.conf files, they define access control to PostgreSQL
443-
cp -a "$BACKUP_DIR"/data/pg_*.conf "$PREFIX/state/pg/data/"
444-
chown cfpostgres "$PREFIX"/state/pg/data/pg_*.conf
445-
446457
# Determine which postgresql.conf file to use and put it in the right place.
447458
if [ -f "$BACKUP_DIR/data/postgresql.conf.modified" ]; then
448459
# User-modified file from the previous old version of CFEngine exists, try to use it.
@@ -817,7 +828,8 @@ if [ ! -f $PREFIX/state/pg/data/postgresql.conf ]; then
817828
cf_console echo "No existing postgresql.conf, initializing Postgres"
818829
init_postgres_dir "$new_pgconfig_file" "$pgconfig_type"
819830
fi
820-
if is_upgrade && [ -d "$BACKUP_DIR/data" ]; then
831+
# PG_VERSION tells us it is a real data directory and not an empty one moved aside.
832+
if is_upgrade && [ -f "$BACKUP_DIR/data/PG_VERSION" ]; then
821833
cf_console echo "Upgrade and BACKUP_DIR/data is present, proceeding with full database migration."
822834
do_migration "$new_pgconfig_file" "$pgconfig_type"
823835
else

packaging/common/cfengine-hub/preinstall.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,9 @@ if migrating_postgres; then
229229
exit 1
230230
fi
231231

232-
if ! diff "$BACKUP_DIR/data/postgresql.conf" "$PREFIX/share/postgresql/postgresql.conf.cfengine" > /dev/null; then
232+
# An empty data directory has no postgresql.conf to compare or preserve.
233+
if [ -f "$BACKUP_DIR/data/postgresql.conf" ] &&
234+
! diff "$BACKUP_DIR/data/postgresql.conf" "$PREFIX/share/postgresql/postgresql.conf.cfengine" > /dev/null; then
233235
# diff exits with 0 if the files are the same
234236
# the postgresql.conf file was modified, we should try to use it after migration
235237
cp -a "$BACKUP_DIR/data/postgresql.conf" "$BACKUP_DIR/data/postgresql.conf.modified"

0 commit comments

Comments
 (0)