Skip to content

Commit

Permalink
Improve PostgreSQL init script and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
andyone committed Nov 1, 2023
1 parent 005c59d commit ac4267e
Show file tree
Hide file tree
Showing 15 changed files with 444 additions and 721 deletions.
78 changes: 1 addition & 77 deletions specs/postgresql-10/SOURCES/postgresql.init
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,14 @@ pg_engine="${PG_ENGINE:-/usr/${short_name}-${MAJOR_VERSION}/bin}"
pg_postmaster="${PG_POSTMASTER:-${pg_engine}/postmaster}"
pg_data="${PG_DATA:-/var/lib/${short_name}/${MAJOR_VERSION}/data}"
pg_log="${PG_LOG:-/var/lib/${short_name}/${MAJOR_VERSION}/pgstartup.log}"
pg_uplog="${PG_UPLOG:-/var/lib/${short_name}/${MAJOR_VERSION}/pgupgrade.log}"
pg_socket_dir="${PG_SOCKET_DIR:-/var/run/postgresql}"
timeout="${TIMEOUT:-300}"
disable_auto_numa="${DISABLE_AUTO_NUMA}"

pg_ctl="$pg_engine/pg_ctl"
pg_upgrade="$pg_engine/pg_upgrade"
pg_initdb="$pg_engine/initdb"

export pg_data
export pg_port

kv[user]="$USER_NAME"
kv[search_pattern]="$pg_engine"
Expand All @@ -63,17 +60,14 @@ kv[oom_adj]="$OOM_DISABLE"
###############################################################################

kv.addCommand "initdb" "Init new database" "initdb"
kv.addCommand "upgrade" "Upgrade data" "upgrade"
kv.addCommand "reload" "Reload configuration files" "reload"
kv.addCommand "promote" "Trigger failover" "promote"

kv.addHandler "start" "startServiceHandler"
kv.addHandler "start" "preStartServiceHandler" "pre"
kv.addHandler "stop" "stopServiceHandler"

kv.addCommandArgs "stop" "force"
kv.addCommandArgs "initdb" "locale"
kv.addCommandArgs "upgrade" "current-version" "prev-version"

kv.disableOutputRedirect "start" "pre"

Expand Down Expand Up @@ -148,66 +142,8 @@ initdb() {
return $status
}

upgrade() {
kv[log]="$pg_uplog"

local cur_version="${1-$MAJOR_VERSION}"
local prev_version="${2-$PREV_VERSION}"
local cur_init="postgresql-$cur_version"
local prev_init="postgresql-$prev_version"

if [[ ! -f "$INIT_DIR/$cur_init" ]] ; then
kv.error "Couldn't find init script $INIT_DIR/$cur_init" $ACTION_ERROR
fi

if [[ ! -f "$INIT_DIR/$prev_init" ]] ; then
kv.error "Couldn't find init script $INIT_DIR/$prev_init"
kv.error "Please install ${PROG_NAME}${prev_version/./}-server RPM first."
return $ACTION_ERROR
fi

if [[ ! -x "$pg_engine/pg_upgrade" ]] ; then
kv.show "Please install the ${PROG_NAME}${PKG_VERSION}-contrib RPM for pg_upgrade command."
return $ACTION_ERROR
fi

local old_data=$(sed -n 's/^PGDATA=//p' "$INIT_DIR/postgresql-$cur_version")
local old_port=$(sed -n 's/^PGPORT=//p' "$INIT_DIR/postgresql-$prev_version")
local new_data=$(sed -n 's/^PGDATA=//p' "$INIT_DIR/postgresql-$cur_version")
local new_port=$(sed -n 's/^PGPORT=//p' "$INIT_DIR/postgresql-$prev_version")

/sbin/service "${PROG_NAME}${PKG_VERSION}" initdb

if [[ $? -ne 0 ]] ; then
kv.error "Initialization failed!" $ACTION_ERROR
fi

kv.run "$pg_upgrade" -b "/usr/${SHORT_NAME}-${prev_version}/bin/" -B $pg_engine/ -d "$old_data" -D "$new_data" -p "$old_port" -P "$new_port" -c

if [[ $? -ne 0 ]] ; then
kv.error "Cluster check failed. Please see the output above." $ACTION_ERROR
fi

kv.show "Clusters checked successfully, proceeding with upgrade from $prev_version to $cur_version..."

/sbin/service "$prev_init" stop

rm -f "$pg_uplog" &> /dev/null
touch "$pg_uplog" || return 1
kv.chown "$USER_NAME:$GROUP_NAME" "$pg_uplog"
kv.chmod go-rwx "$PGUPLOG"

kv.restoreSEContext "$pg_uplog"

kv.show "Performing upgrade..."

kv.run "$pg_upgrade" -b "/usr/${SHORT_NAME}-${prev_version}/bin/" -B "$pg_engine/" -d "$old_data" -D "$new_data" -p "$old_port" -P "$new_port"

kv.show "Upgrade complete" $GREEN
}

reload() {
kv.showProcessMessage "Reloading configs"
kv.showProcessMessage "Reloading configuration"

kv.run "$pg_ctl" reload -D "$pg_data" -s

Expand All @@ -218,18 +154,6 @@ reload() {
return $status
}

promote() {
kv.showProcessMessage "Promoting"

kv.run "$pg_ctl" promote -D "$pg_data" -s

local status=$?

kv.showStatusMessage "$status"

return $status
}

###############################################################################

initNewDB() {
Expand Down
78 changes: 1 addition & 77 deletions specs/postgresql-11/SOURCES/postgresql.init
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,14 @@ pg_engine="${PG_ENGINE:-/usr/${short_name}-${MAJOR_VERSION}/bin}"
pg_postmaster="${PG_POSTMASTER:-${pg_engine}/postmaster}"
pg_data="${PG_DATA:-/var/lib/${short_name}/${MAJOR_VERSION}/data}"
pg_log="${PG_LOG:-/var/lib/${short_name}/${MAJOR_VERSION}/pgstartup.log}"
pg_uplog="${PG_UPLOG:-/var/lib/${short_name}/${MAJOR_VERSION}/pgupgrade.log}"
pg_socket_dir="${PG_SOCKET_DIR:-/var/run/postgresql}"
timeout="${TIMEOUT:-300}"
disable_auto_numa="${DISABLE_AUTO_NUMA}"

pg_ctl="$pg_engine/pg_ctl"
pg_upgrade="$pg_engine/pg_upgrade"
pg_initdb="$pg_engine/initdb"

export pg_data
export pg_port

kv[user]="$USER_NAME"
kv[search_pattern]="$pg_engine"
Expand All @@ -63,17 +60,14 @@ kv[oom_adj]="$OOM_DISABLE"
###############################################################################

kv.addCommand "initdb" "Init new database" "initdb"
kv.addCommand "upgrade" "Upgrade data" "upgrade"
kv.addCommand "reload" "Reload configuration files" "reload"
kv.addCommand "promote" "Trigger failover" "promote"

kv.addHandler "start" "startServiceHandler"
kv.addHandler "start" "preStartServiceHandler" "pre"
kv.addHandler "stop" "stopServiceHandler"

kv.addCommandArgs "stop" "force"
kv.addCommandArgs "initdb" "locale"
kv.addCommandArgs "upgrade" "current-version" "prev-version"

kv.disableOutputRedirect "start" "pre"

Expand Down Expand Up @@ -148,66 +142,8 @@ initdb() {
return $status
}

upgrade() {
kv[log]="$pg_uplog"

local cur_version="${1-$MAJOR_VERSION}"
local prev_version="${2-$PREV_VERSION}"
local cur_init="postgresql-$cur_version"
local prev_init="postgresql-$prev_version"

if [[ ! -f "$INIT_DIR/$cur_init" ]] ; then
kv.error "Couldn't find init script $INIT_DIR/$cur_init" $ACTION_ERROR
fi

if [[ ! -f "$INIT_DIR/$prev_init" ]] ; then
kv.error "Couldn't find init script $INIT_DIR/$prev_init"
kv.error "Please install ${PROG_NAME}${prev_version/./}-server RPM first."
return $ACTION_ERROR
fi

if [[ ! -x "$pg_engine/pg_upgrade" ]] ; then
kv.show "Please install the ${PROG_NAME}${PKG_VERSION}-contrib RPM for pg_upgrade command."
return $ACTION_ERROR
fi

local old_data=$(sed -n 's/^PGDATA=//p' "$INIT_DIR/postgresql-$cur_version")
local old_port=$(sed -n 's/^PGPORT=//p' "$INIT_DIR/postgresql-$prev_version")
local new_data=$(sed -n 's/^PGDATA=//p' "$INIT_DIR/postgresql-$cur_version")
local new_port=$(sed -n 's/^PGPORT=//p' "$INIT_DIR/postgresql-$prev_version")

/sbin/service "${PROG_NAME}${PKG_VERSION}" initdb

if [[ $? -ne 0 ]] ; then
kv.error "Initialization failed!" $ACTION_ERROR
fi

kv.run "$pg_upgrade" -b "/usr/${SHORT_NAME}-${prev_version}/bin/" -B $pg_engine/ -d "$old_data" -D "$new_data" -p "$old_port" -P "$new_port" -c

if [[ $? -ne 0 ]] ; then
kv.error "Cluster check failed. Please see the output above." $ACTION_ERROR
fi

kv.show "Clusters checked successfully, proceeding with upgrade from $prev_version to $cur_version..."

/sbin/service "$prev_init" stop

rm -f "$pg_uplog" &> /dev/null
touch "$pg_uplog" || return 1
kv.chown "$USER_NAME:$GROUP_NAME" "$pg_uplog"
kv.chmod go-rwx "$PGUPLOG"

kv.restoreSEContext "$pg_uplog"

kv.show "Performing upgrade..."

kv.run "$pg_upgrade" -b "/usr/${SHORT_NAME}-${prev_version}/bin/" -B "$pg_engine/" -d "$old_data" -D "$new_data" -p "$old_port" -P "$new_port"

kv.show "Upgrade complete" $GREEN
}

reload() {
kv.showProcessMessage "Reloading configs"
kv.showProcessMessage "Reloading configuration"

kv.run "$pg_ctl" reload -D "$pg_data" -s

Expand All @@ -218,18 +154,6 @@ reload() {
return $status
}

promote() {
kv.showProcessMessage "Promoting"

kv.run "$pg_ctl" promote -D "$pg_data" -s

local status=$?

kv.showStatusMessage "$status"

return $status
}

###############################################################################

initNewDB() {
Expand Down
78 changes: 1 addition & 77 deletions specs/postgresql-12/SOURCES/postgresql.init
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,14 @@ pg_engine="${PG_ENGINE:-/usr/${short_name}-${MAJOR_VERSION}/bin}"
pg_postmaster="${PG_POSTMASTER:-${pg_engine}/postmaster}"
pg_data="${PG_DATA:-/var/lib/${short_name}/${MAJOR_VERSION}/data}"
pg_log="${PG_LOG:-/var/lib/${short_name}/${MAJOR_VERSION}/pgstartup.log}"
pg_uplog="${PG_UPLOG:-/var/lib/${short_name}/${MAJOR_VERSION}/pgupgrade.log}"
pg_socket_dir="${PG_SOCKET_DIR:-/var/run/postgresql}"
timeout="${TIMEOUT:-300}"
disable_auto_numa="${DISABLE_AUTO_NUMA}"

pg_ctl="$pg_engine/pg_ctl"
pg_upgrade="$pg_engine/pg_upgrade"
pg_initdb="$pg_engine/initdb"

export pg_data
export pg_port

kv[user]="$USER_NAME"
kv[search_pattern]="$pg_engine"
Expand All @@ -63,17 +60,14 @@ kv[oom_adj]="$OOM_DISABLE"
###############################################################################

kv.addCommand "initdb" "Init new database" "initdb"
kv.addCommand "upgrade" "Upgrade data" "upgrade"
kv.addCommand "reload" "Reload configuration files" "reload"
kv.addCommand "promote" "Trigger failover" "promote"

kv.addHandler "start" "startServiceHandler"
kv.addHandler "start" "preStartServiceHandler" "pre"
kv.addHandler "stop" "stopServiceHandler"

kv.addCommandArgs "stop" "force"
kv.addCommandArgs "initdb" "locale"
kv.addCommandArgs "upgrade" "current-version" "prev-version"

kv.disableOutputRedirect "start" "pre"

Expand Down Expand Up @@ -148,66 +142,8 @@ initdb() {
return $status
}

upgrade() {
kv[log]="$pg_uplog"

local cur_version="${1-$MAJOR_VERSION}"
local prev_version="${2-$PREV_VERSION}"
local cur_init="postgresql-$cur_version"
local prev_init="postgresql-$prev_version"

if [[ ! -f "$INIT_DIR/$cur_init" ]] ; then
kv.error "Couldn't find init script $INIT_DIR/$cur_init" $ACTION_ERROR
fi

if [[ ! -f "$INIT_DIR/$prev_init" ]] ; then
kv.error "Couldn't find init script $INIT_DIR/$prev_init"
kv.error "Please install ${PROG_NAME}${prev_version/./}-server RPM first."
return $ACTION_ERROR
fi

if [[ ! -x "$pg_engine/pg_upgrade" ]] ; then
kv.show "Please install the ${PROG_NAME}${PKG_VERSION}-contrib RPM for pg_upgrade command."
return $ACTION_ERROR
fi

local old_data=$(sed -n 's/^PGDATA=//p' "$INIT_DIR/postgresql-$cur_version")
local old_port=$(sed -n 's/^PGPORT=//p' "$INIT_DIR/postgresql-$prev_version")
local new_data=$(sed -n 's/^PGDATA=//p' "$INIT_DIR/postgresql-$cur_version")
local new_port=$(sed -n 's/^PGPORT=//p' "$INIT_DIR/postgresql-$prev_version")

/sbin/service "${PROG_NAME}${PKG_VERSION}" initdb

if [[ $? -ne 0 ]] ; then
kv.error "Initialization failed!" $ACTION_ERROR
fi

kv.run "$pg_upgrade" -b "/usr/${SHORT_NAME}-${prev_version}/bin/" -B $pg_engine/ -d "$old_data" -D "$new_data" -p "$old_port" -P "$new_port" -c

if [[ $? -ne 0 ]] ; then
kv.error "Cluster check failed. Please see the output above." $ACTION_ERROR
fi

kv.show "Clusters checked successfully, proceeding with upgrade from $prev_version to $cur_version..."

/sbin/service "$prev_init" stop

rm -f "$pg_uplog" &> /dev/null
touch "$pg_uplog" || return 1
kv.chown "$USER_NAME:$GROUP_NAME" "$pg_uplog"
kv.chmod go-rwx "$PGUPLOG"

kv.restoreSEContext "$pg_uplog"

kv.show "Performing upgrade..."

kv.run "$pg_upgrade" -b "/usr/${SHORT_NAME}-${prev_version}/bin/" -B "$pg_engine/" -d "$old_data" -D "$new_data" -p "$old_port" -P "$new_port"

kv.show "Upgrade complete" $GREEN
}

reload() {
kv.showProcessMessage "Reloading configs"
kv.showProcessMessage "Reloading configuration"

kv.run "$pg_ctl" reload -D "$pg_data" -s

Expand All @@ -218,18 +154,6 @@ reload() {
return $status
}

promote() {
kv.showProcessMessage "Promoting"

kv.run "$pg_ctl" promote -D "$pg_data" -s

local status=$?

kv.showStatusMessage "$status"

return $status
}

###############################################################################

initNewDB() {
Expand Down
Loading

0 comments on commit ac4267e

Please sign in to comment.