update/software/scripts/create_postgresql_database.sh
Heitor Matsui af0a86e357 Improve logging for deploy start shell scripts
This commit improves logging during deploy start by:
1. Creating a common module to be sourced by shell scripts to
   load general-use functions, thus reducing code duplication
   between the scripts
2. Replacing plain "echo" commands on the scripts by logging
   functions present on the common module
3. Adding timestamps to the log messages
4. Centralizing all scripts logs into software.log, favouring
   the troubleshooting, now that log lines contain timestamps
   and the process/script that generated them

This commit also deletes the ostree_mounts.yaml file since
it would be used by apt-ostree integration, which was dropped.

Test Plan
PASS: run deploy start successfully and verify that deploy start
      log messages are logged with the expected format

Story: 2010676
Task: 49607

Change-Id: I0bdebde8147faa5b29a642e35bfaf26e9862ed0a
Signed-off-by: Heitor Matsui <heitorvieira.matsui@windriver.com>
2024-02-28 11:07:03 -03:00

84 lines
2.5 KiB
Bash

#!/bin/bash
#
# Copyright (c) 2023-2024 Wind River Systems, Inc.
#
# SPDX-License-Identifier: Apache-2.0
#
#
# This script is used create a 2nd instance of postgres on a DX upgrade.
# It needs the port number as parameter and it should be different from the default.
#
script_dir=$(dirname $0)
shell_utils=${script_dir}/shell-utils
if [ -f $shell_utils ]; then
source $shell_utils
else
echo "ERROR: ${shell_utils} module not found."
exit 1
fi
DEFAULT_POSTGRESQL_PORT=5432
POSTGRESQL_PATH=/var/lib/postgresql
POSTGRESQL_BIN_DIR=$(pg_config --bindir)
POSTGRESQL_RUNTIME=/var/run/postgresql
INFO_FILE=/etc/build.info
if [ -z "$1" ]; then
error "'Port' parameter is missing."
exit 1
fi
PORT="$1"
# Prevent issues with the default postgres port
if [ "$PORT" -eq "$DEFAULT_POSTGRESQL_PORT" ]; then
error "Port number should be different from the default."
exit 1
fi
cleanup_and_exit() {
local exit_code="$1"
local error_message="$2"
error "$error_message" >&2
error "Please check the error details and take appropriate action for recovery." >&2
exit "$exit_code"
}
SW_VERSION=$(grep -o 'SW_VERSION="[0-9\.]*"' "$INFO_FILE" | cut -d '"' -f 2) ||
cleanup_and_exit 1 "Failed to get software version"
POSTGRESQL_DATA_DIR=$POSTGRESQL_PATH/$SW_VERSION
# Remove existing data directory
rm -rf "$POSTGRESQL_DATA_DIR" ||
cleanup_and_exit 1 "Failed to remove existing data directory: $POSTGRESQL_DATA_DIR"
mkdir -p "$POSTGRESQL_DATA_DIR" ||
cleanup_and_exit 1 "Failed to create data directory: $POSTGRESQL_DATA_DIR"
chown postgres "$POSTGRESQL_DATA_DIR" ||
cleanup_and_exit 1 "Failed to change ownership of data directory: $POSTGRESQL_DATA_DIR"
sudo -u postgres "$POSTGRESQL_BIN_DIR/initdb" -D "$POSTGRESQL_DATA_DIR" ||
cleanup_and_exit 1 "Failed to initialize the PostgreSQL database"
chmod 700 "$POSTGRESQL_DATA_DIR" ||
cleanup_and_exit 1 "Failed to set permissions for data directory: $POSTGRESQL_DATA_DIR"
chown postgres "$POSTGRESQL_DATA_DIR" ||
cleanup_and_exit 1 "Failed to change ownership of data directory: $POSTGRESQL_DATA_DIR"
mkdir -p "$POSTGRESQL_RUNTIME" ||
cleanup_and_exit 1 "Failed to create runtime directory: ${POSTGRESQL_RUNTIME}"
chown postgres "$POSTGRESQL_RUNTIME" ||
cleanup_and_exit 1 "Failed to change ownership of runtime directory: ${POSTGRESQL_RUNTIME}"
sudo -u postgres "$POSTGRESQL_BIN_DIR/pg_ctl" -D "$POSTGRESQL_DATA_DIR" -o "-F -p $PORT" start ||
cleanup_and_exit 1 "Failed to start PostgreSQL"
exit 0