diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml new file mode 100644 index 0000000..c93ebd4 --- /dev/null +++ b/.github/workflows/build.yaml @@ -0,0 +1,68 @@ +name: Build Artifacts +on: + release: + types: [created] + push: + branches: + - '**' + workflow_dispatch: + inputs: + publish_docker: + description: "Publish image to ghcr.io/netcracker/pgskipper-upgrade" + type: boolean + default: false + required: false + +env: + TAG_NAME: ${{ github.event.release.tag_name || github.ref }} + PUSH: ${{ github.event_name != 'workflow_dispatch' || inputs.publish_docker }} + +jobs: + multiplatform_build: + strategy: + fail-fast: false + matrix: + component: + - name: pgskipper-upgrade + file: Dockerfile + context: "" + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + - name: Set up QEMU + uses: docker/setup-qemu-action@v3 + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + - name: Login to Docker Hub + uses: docker/login-action@v3 + with: + registry: ghcr.io + username: ${GITHUB_ACTOR} + password: ${{secrets.GITHUB_TOKEN}} + - name: Prepare Tag + run: echo "TAG_NAME=$(echo ${TAG_NAME} | sed 's@refs/tags/@@;s@refs/heads/@@;s@/@_@g')" >> $GITHUB_ENV + - name: Get package IDs for delete + id: get-ids-for-delete + uses: Netcracker/get-package-ids@v0.0.1 + with: + component-name: ${{ matrix.component.name }} + component-tag: ${{ env.TAG_NAME }} + access-token: ${{secrets.GITHUB_TOKEN}} + if: ${{ env.PUSH }} + - name: Build and push + uses: docker/build-push-action@v6 + with: + no-cache: true + context: ${{ matrix.component.context }} + file: ${{ matrix.component.file }} + platforms: linux/amd64 #,linux/arm64 + push: ${{ env.PUSH }} + tags: ghcr.io/netcracker/${{ matrix.component.name }}:${{ env.TAG_NAME }} + provenance: false + - uses: actions/delete-package-versions@v5 + with: + package-name: ${{ matrix.component.name }} + package-type: 'container' + package-version-ids: ${{ steps.get-ids-for-delete.outputs.ids-for-delete }} + if: ${{ steps.get-ids-for-delete.outputs.ids-for-delete != '' }} diff --git a/.github/workflows/clean.yaml b/.github/workflows/clean.yaml new file mode 100644 index 0000000..96c763a --- /dev/null +++ b/.github/workflows/clean.yaml @@ -0,0 +1,35 @@ +name: Branch Deleted +on: delete + +env: + COMPONENT_NAME: pgskipper-upgrade + TAG_NAME: ${{ github.event.ref }} + +jobs: + delete: + if: github.event.ref_type == 'branch' + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + - name: Prepare Tag + run: echo "TAG_NAME=$(echo ${TAG_NAME} | sed 's@refs/heads/@@;s@/@_@g')" >> $GITHUB_ENV + - name: Login to Docker Hub + uses: docker/login-action@v3 + with: + registry: ghcr.io + username: ${GITHUB_ACTOR} + password: ${{secrets.GITHUB_TOKEN}} + - name: Get package IDs for delete + id: get-ids-for-delete + uses: Netcracker/get-package-ids@v0.0.1 + with: + component-name: ${{ env.COMPONENT_NAME }} + component-tag: ${{ env.TAG_NAME }} + access-token: ${{secrets.GITHUB_TOKEN}} + - uses: actions/delete-package-versions@v5 + with: + package-name: ${{ env.COMPONENT_NAME }} + package-type: 'container' + package-version-ids: ${{ steps.get-ids-for-delete.outputs.ids-for-delete }} + if: ${{ steps.get-ids-for-delete.outputs.ids-for-delete != '' }} diff --git a/.github/workflows/license.yaml b/.github/workflows/license.yaml new file mode 100644 index 0000000..6609d79 --- /dev/null +++ b/.github/workflows/license.yaml @@ -0,0 +1,24 @@ +name: Add License Header +on: + push: + branches: + - 'main' +env: + COPYRIGHT_COMPANY: 'NetCracker Technology Corporation' + COPYRIGHT_YEAR: '2024-2025' +jobs: + license: + runs-on: ubuntu-24.04 + steps: + - uses: actions/checkout@v4 + with: + token: ${{ secrets.GH_ACCESS_TOKEN }} + - run: docker run -v "${PWD}:/src" -i ghcr.io/google/addlicense -v -c "${{ env.COPYRIGHT_COMPANY }}" -y "${{ env.COPYRIGHT_YEAR }}" $(find . -type f -name "*.go" -o -type f -name "*.sh" -o -type f -name "*.py" | xargs echo) + - name: Create Pull Request + uses: peter-evans/create-pull-request@v7 + with: + commit-message: Auto-update license header + branch: license-update + title: Add License Header + body: Automated license header update + delete-branch: true \ No newline at end of file diff --git a/CODE-OF-CONDUCT.md b/CODE-OF-CONDUCT.md new file mode 100644 index 0000000..f5b511b --- /dev/null +++ b/CODE-OF-CONDUCT.md @@ -0,0 +1,73 @@ +# Code of Conduct + +This repository is governed by following code of conduct guidelines. + +We put collaboration, trust, respect and transparency as core values for our community. +Our community welcomes participants from all over the world with different experience, +opinion and ideas to share. + +We have adopted this code of conduct and require all contributors to agree with that to build a healthy, +safe and productive community for all. + +The guideline is aimed to support a community where all people should feel safe to participate, +introduce new ideas and inspire others, regardless of: + +* Age +* Gender +* Gender identity or expression +* Family status +* Marital status +* Ability +* Ethnicity +* Race +* Sex characteristics +* Sexual identity and orientation +* Education +* Native language +* Background +* Caste +* Religion +* Geographic location +* Socioeconomic status +* Personal appearance +* Any other dimension of diversity + +## Our Standards + +We are welcoming the following behavior: + +* Be respectful for different ideas, opinions and points of view +* Be constructive and professional +* Use inclusive language +* Be collaborative and show the empathy +* Focus on the best results for the community + +The following behavior is unacceptable: + +* Violence, threats of violence, or inciting others to commit self-harm +* Personal attacks, trolling, intentionally spreading misinformation, insulting/derogatory comments +* Public or private harassment +* Publishing others' private information, such as a physical or electronic address, without explicit permission +* Derogatory language +* Encouraging unacceptable behavior +* Other conduct which could reasonably be considered inappropriate in a professional community + +## Our Responsibilities + +Project maintainers are responsible for clarifying the standards of the Code of Conduct +and are expected to take appropriate actions in response to any instances of unacceptable behavior. + +Project maintainers have the right and responsibility to remove, edit, or reject comments, +commits, code, wiki edits, issues, and other contributions that are not aligned +to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors +that they deem inappropriate, threatening, offensive, or harmful. + +## Reporting + +If you believe you’re experiencing unacceptable behavior that will not be tolerated as outlined above, +please report to `opensourcegroup@netcracker.com`. All complaints will be reviewed and investigated and will result in a response +that is deemed necessary and appropriate to the circumstances. The project team is obligated to maintain confidentiality +with regard to the reporter of an incident. + +Please also report if you observe a potentially dangerous situation, someone in distress, or violations of these guidelines, +even if the situation is not happening to you. diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..292ce26 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,12 @@ +# Contribution Guide + +We'd love to accept patches and contributions to this project. +Please, follow these guidelines to make the contribution process easy and effective for everyone involved. + +## Contributor License Agreement + +You must sign the [Contributor License Agreement](https://pages.netcracker.com/cla-main.html) in order to contribute. + +## Code of Conduct + +Please make sure to read and follow the [Code of Conduct](CODE-OF-CONDUCT.md). diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..d62a6fc --- /dev/null +++ b/Dockerfile @@ -0,0 +1,68 @@ +FROM ubuntu:22.04 + +USER root + +ENV LC_ALL=en_US.UTF-8 \ + LANG=en_US.UTF-8 +ARG DEBIAN_FRONTEND=noninteractive + +RUN echo start +RUN mkdir -p /var/lib/pgsql/data/ && mkdir -p /var/lib/pgsql/tmp_data/ + +# fix for non-ha +RUN mkdir /properties && touch /properties/empty-conf.conf + +RUN echo "deb [trusted=yes] http://apt.postgresql.org/pub/repos/apt jammy-pgdg main" >> /etc/apt/sources.list.d/pgdg.list +RUN ls -la /etc/apt/ +RUN apt-get -y update +RUN apt-get -o DPkg::Options::="--force-confnew" -y dist-upgrade + +RUN groupmod -n postgres tape +RUN adduser -uid 26 -gid 26 postgres + +# Install like base image +RUN apt-get --no-install-recommends install -y gcc-12 python3.11 python3-pip python3-dev wget + +RUN apt-get -y update +RUN apt-get -o DPkg::Options::="--force-confnew" -y dist-upgrade +RUN apt-get --no-install-recommends install -y \ + postgresql-12 postgresql-server-dev-12 \ + postgresql-13 postgresql-server-dev-13 \ + postgresql-14 postgresql-server-dev-14 \ + postgresql-15 postgresql-server-dev-15 \ + postgresql-16 postgresql-server-dev-16 \ + postgresql-12-pg-track-settings postgresql-12-pg-wait-sampling postgresql-12-cron postgresql-12-set-user postgresql-12-pg-stat-kcache postgresql-12-pgaudit postgresql-12-pg-qualstats postgresql-12-hypopg postgresql-12-powa \ + postgresql-13-pg-track-settings postgresql-13-pg-wait-sampling postgresql-13-cron postgresql-13-set-user postgresql-13-pg-stat-kcache postgresql-13-pgaudit postgresql-13-pg-qualstats postgresql-13-hypopg postgresql-13-powa \ + postgresql-14-pg-track-settings postgresql-14-pg-wait-sampling postgresql-14-cron postgresql-14-set-user postgresql-14-postgis postgresql-14-pg-stat-kcache postgresql-14-pgaudit postgresql-14-pg-qualstats postgresql-14-hypopg postgresql-14-powa postgresql-14-pg-hint-plan postgresql-14-pgnodemx postgresql-14-decoderbufs \ + postgresql-15-pg-track-settings postgresql-15-pg-wait-sampling postgresql-15-cron postgresql-15-set-user postgresql-15-postgis postgresql-15-pg-stat-kcache postgresql-15-pgaudit postgresql-15-pg-qualstats postgresql-15-hypopg postgresql-15-powa postgresql-15-pg-hint-plan postgresql-15-pgnodemx postgresql-15-decoderbufs \ + postgresql-16-pg-track-settings postgresql-16-pg-wait-sampling postgresql-16-cron postgresql-16-set-user postgresql-16-postgis postgresql-16-pg-stat-kcache postgresql-16-pgaudit postgresql-16-pg-qualstats postgresql-16-hypopg postgresql-16-powa postgresql-16-pg-hint-plan postgresql-16-pgnodemx postgresql-16-decoderbufs + +RUN apt-get --no-install-recommends install -y libproj-dev libgdal30 libgeos3.10.2 libgeotiff5 libsfcgal1 + +RUN localedef -i en_US -f UTF-8 en_US.UTF-8 && \ + localedef -i es_PE -f UTF-8 es_PE.UTF-8 && \ + localedef -i es_ES -f UTF-8 es_ES.UTF-8 +# Migrate .rpm to .deb +RUN apt-get install -y alien + +RUN apt-get install -y protobuf-compiler + +WORKDIR /tmp + +COPY ./docker/start.sh /start.sh + + +RUN chgrp 0 /var/lib/pgsql/ && chmod g+w /var/lib/pgsql/ && chmod 777 /var/lib/pgsql && \ + chgrp 0 /var/run/postgresql/ && chmod g+w /var/run/postgresql/ && chmod 777 /var/run/postgresql && \ + chmod -R 777 /etc/passwd && \ + mv /usr/bin/python3.11 /usr/bin/python && ln -fs /usr/bin/python /usr/bin/python3 && \ + chmod +x /start.sh + + +RUN pg_config --version | grep -o "[0-9]*" | head -n 1 +VOLUME /var/lib/pgsql +VOLUME /var/run/postgresql + +CMD ["/bin/bash", "/start.sh"] + +USER 26 diff --git a/SECURITY.md b/SECURITY.md new file mode 100644 index 0000000..8162261 --- /dev/null +++ b/SECURITY.md @@ -0,0 +1,15 @@ +# Security Reporting Process + +Please, report any security issue to `opensourcegroup@netcracker.com` where the issue will be triaged appropriately. + +If you know of a publicly disclosed security vulnerability please IMMEDIATELY email `opensourcegroup@netcracker.com` +to inform the team about the vulnerability, so we may start the patch, release, and communication process. + +# Security Release Process + +If the vulnerability is found in the latest stable release, then it would be fixed in patch version for that release. +E.g., issue is found in 2.5.0 release, then 2.5.1 version with a fix will be released. +By default, older versions will not have security releases. + +If the issue doesn't affect any existing public releases, the fix for medium and high issues is performed +in a main branch before releasing a new version. For low priority issues the fix can be planned for future releases. diff --git a/docker/start.sh b/docker/start.sh new file mode 100755 index 0000000..7b28c19 --- /dev/null +++ b/docker/start.sh @@ -0,0 +1,241 @@ +#!/usr/bin/env bash +# Copyright 2024-2025 NetCracker Technology Corporation +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + + +RETRIES=100 +SLEEP_BETWEEN_ITERATIONS=5 + +[[ "${DEBUG}" == 'true' ]] && set -x + +function handle_master_upgrade() { + cd /var/lib/pgsql/data/ + echo "[$(date +%Y-%m-%dT%H:%M:%S)] cur path: `pwd`" + + + DB_SIZE_GB_FLOAT=$(du -sk /var/lib/pgsql/data/${DATA_DIR} | awk '{ print $1 / 1024 / 1024 }') + DB_SIZE_GB=`printf "%.0f\n" ${DB_SIZE_GB_FLOAT}` + PV_SIZE_GB=$(echo "${PV_SIZE}" | tr -dc '0-9') + + echo + echo '##########################' + echo "MIGRATION_PV_USED: $MIGRATION_PV_USED" + echo "CLEAN_MIGRATION_PV: $CLEAN_MIGRATION_PV" + echo "MIGRATION_PATH: $MIGRATION_PATH" + echo "INITDB_PARAMS: $INITDB_PARAMS" + echo "SIZE OF VOLUME: $PV_SIZE_GB Gb" + echo "SOURCE DB SIZE: $DB_SIZE_GB Gb" + echo "DATA_DIR: $DATA_DIR" + echo '##########################' + echo + + # check if there is enough space in case when migration pv is used + if [[ "${MIGRATION_PV_USED}" =~ ^[Tt]rue$ ]]; then + echo "[$(date +%Y-%m-%dT%H:%M:%S)] Migration PV is used, check if there is enough space for migration in migration PV" + if [[ $DB_SIZE_GB -gt $PV_SIZE_GB ]]; then + echo "[$(date +%Y-%m-%dT%H:%M:%S)] DB size is more than PV size, exiting ..." + exit 1 + fi + if [[ "${CLEAN_MIGRATION_PV}" =~ ^[Tt]rue$ ]]; then + echo "[$(date +%Y-%m-%dT%H:%M:%S)] CLEAN_MIGRATION_PV set to True, All data from MIGRATION PV will be deleted! " + rm -rf /var/lib/pgsql/tmp_data/* + fi + else + echo "[$(date +%Y-%m-%dT%H:%M:%S)] Migration PV is NOT used, check if there is enough space for migration in master PV" + DOUBLE_DB_SIZE="$((${DB_SIZE_GB} * 2))" + if [[ ${DOUBLE_DB_SIZE} -gt ${PV_SIZE_GB} ]]; then + echo "[$(date +%Y-%m-%dT%H:%M:%S)] DB size is more than PV size, exiting ..." + exit 1 + fi + fi + export PATH="/usr/lib/postgresql/${PG_VERSION_TARGET}/bin:${PATH}" + + [ -d "$MIGRATION_PATH/tmp/pg" ] && echo "[$(date +%Y-%m-%dT%H:%M:%S)] Prev upgrade dir exists, removing .." && rm -rf "$MIGRATION_PATH/tmp/pg" + + echo "[$(date +%Y-%m-%dT%H:%M:%S)] initializing target db with parameters $INITDB_PARAMS" + /usr/lib/postgresql/"${PG_VERSION_TARGET}"/bin/initdb ${INITDB_PARAMS} --pgdata="$MIGRATION_PATH/tmp/pg" + + + echo "[$(date +%Y-%m-%dT%H:%M:%S)] initialize complete, copying configs" + mkdir "/tmp/configs/" + cp /var/lib/pgsql/data/${DATA_DIR}/*.conf "/tmp/configs/" + + echo "turning off wal archiving" + sed -e '/archive_command/ s/^#*/#/' -i "$MIGRATION_PATH/tmp/pg/postgresql.conf" + sed -e '/archive_mode/ s/^#*/#/' -i "$MIGRATION_PATH/tmp/pg/postgresql.conf" + sed -e '/ssl/ s/^#*/#/' -i "$MIGRATION_PATH/tmp/pg/postgresql.conf" + sed -e '/archive_command/ s/^#*/#/' -i "/var/lib/pgsql/data/${DATA_DIR}/postgresql.conf" + sed -e '/archive_mode/ s/^#*/#/' -i "/var/lib/pgsql/data/${DATA_DIR}/postgresql.conf" + sed -e '/ssl/ s/^#*/#/' -i "/var/lib/pgsql/data/${DATA_DIR}/postgresql.conf" + + echo "[$(date +%Y-%m-%dT%H:%M:%S)] proceed with upgrade" + echo "[$(date +%Y-%m-%dT%H:%M:%S)] Content of /tmp/configs:" + ls -la /tmp/configs/ + echo "[$(date +%Y-%m-%dT%H:%M:%S)] Content of /tmp/configs/postgresql.conf file:" + cat /tmp/configs/postgresql.conf + + echo "[$(date +%Y-%m-%dT%H:%M:%S)] making chmod 750 to datadir" + + chmod 750 $MIGRATION_PATH/${DATA_DIR} + + cat "/var/lib/pgsql/data/${DATA_DIR}/postgresql.conf" | grep shared_preload_libraries >> $MIGRATION_PATH/tmp/pg/postgresql.conf + + ls -la $MIGRATION_PATH + + echo "[$(date +%Y-%m-%dT%H:%M:%S)] Check cluster before upgrade" + /usr/lib/postgresql/"${PG_VERSION_TARGET}"/bin/pg_upgrade \ + --old-datadir "/var/lib/pgsql/data/${DATA_DIR}" \ + --new-datadir "$MIGRATION_PATH/tmp/pg" \ + --old-bindir "/usr/lib/postgresql/${PG_VERSION}/bin" \ + --new-bindir "/usr/lib/postgresql/${PG_VERSION_TARGET}/bin" \ + --check \ + > /var/lib/pgsql/data/check_result + + CHECK_CODE=$? + + if [[ "CHECK_CODE" -ne 0 ]]; then + echo "[$(date +%Y-%m-%dT%H:%M:%S)] Check cluster before upgrade - Failed." + echo "check exit code: ${CHECK_CODE}" + cat /var/lib/pgsql/data/check_result + exit 13 + fi + + if [[ "$OPERATOR" =~ ^[Tt]rue$ ]]; then + echo "[$(date +%Y-%m-%dT%H:%M:%S)] using link parameter" + /usr/lib/postgresql/"${PG_VERSION_TARGET}"/bin/pg_upgrade \ + --link \ + --old-datadir "/var/lib/pgsql/data/${DATA_DIR}" \ + --new-datadir "$MIGRATION_PATH/tmp/pg" \ + --old-bindir "/usr/lib/postgresql/${PG_VERSION}/bin" \ + --new-bindir "/usr/lib/postgresql/${PG_VERSION_TARGET}/bin" + else + /usr/lib/postgresql/"${PG_VERSION_TARGET}"/bin/pg_upgrade \ + --old-datadir "/var/lib/pgsql/data/${DATA_DIR}" \ + --new-datadir "$MIGRATION_PATH/tmp/pg" \ + --old-bindir "/usr/lib/postgresql/${PG_VERSION}/bin" \ + --new-bindir "/usr/lib/postgresql/${PG_VERSION_TARGET}/bin" + fi + + EXIT_CODE=$? + if [[ "$EXIT_CODE" -ne 0 ]]; then + echo "[$(date +%Y-%m-%dT%H:%M:%S)] Error! Can not proceed, because upgrade failed, exiting" + ls -al /var/lib/pgsql/data + echo "[$(date +%Y-%m-%dT%H:%M:%S)] Printing all of the logs:" + awk '{print}' /var/lib/pgsql/data/*.log +# cat pg_upgrade_server.log + exit 1 + fi + + echo "[$(date +%Y-%m-%dT%H:%M:%S)] Upgrade Successful" + echo "[$(date +%Y-%m-%dT%H:%M:%S)] Sizing After Upgrade" + du -sh /var/lib/pgsql/data/ + + rm -rf "/var/lib/pgsql/data/${DATA_DIR}" + + echo "[$(date +%Y-%m-%dT%H:%M:%S)] moving new data to directory" + echo "[$(date +%Y-%m-%dT%H:%M:%S)] from -> $MIGRATION_PATH/tmp/pg" + echo "[$(date +%Y-%m-%dT%H:%M:%S)] to -> /var/lib/pgsql/data/${DATA_DIR}" + mv "$MIGRATION_PATH/tmp/pg" "/var/lib/pgsql/data/${DATA_DIR}" + + /usr/lib/postgresql/"${PG_VERSION_TARGET}"/bin/pg_ctl start -D "/var/lib/pgsql/data/${DATA_DIR}" + + until psql -c "select 1" > /dev/null 2>&1 || [ $RETRIES -eq 0 ]; do + echo "[$(date +%Y-%m-%dT%H:%M:%S)] Waiting for postgres server, $((RETRIES--)) remaining attempts..." + sleep ${SLEEP_BETWEEN_ITERATIONS} + done + + EXIT_CODE=$? + if [[ "$EXIT_CODE" -ne 0 ]]; then + echo "[$(date +%Y-%m-%dT%H:%M:%S)] Error! Can not proceed, because select failed, exiting" + cat pg_upgrade_server.log + exit 1 + fi + + /usr/lib/postgresql/"${PG_VERSION_TARGET}"/bin/psql -c "DROP ROLE IF EXISTS replicator; CREATE ROLE replicator with replication login password '$PGREPLPASSWORD';" + /usr/lib/postgresql/"${PG_VERSION_TARGET}"/bin/psql -c "ALTER ROLE postgres with password '$PGPASSWORD';" + + # update extensions + [ -f /var/lib/pgsql/data/update_extensions.sql ] && psql --username=postgres --file=/var/lib/pgsql/data/update_extensions.sql postgres + + /usr/lib/postgresql/"${PG_VERSION_TARGET}"/bin/pg_ctl stop -D "/var/lib/pgsql/data/${DATA_DIR}" + + [ -f /var/lib/pgsql/data/target_version ] && rm -rf /var/lib/pgsql/data/target_version + + echo "[$(date +%Y-%m-%dT%H:%M:%S)] moving configs to directory" + mv /tmp/configs/* "/var/lib/pgsql/data/${DATA_DIR}/" + echo "[$(date +%Y-%m-%dT%H:%M:%S)] exiting" +} + +function handle_replica_upgrade() { + rm -rf "/var/lib/pgsql/data/${DATA_DIR}/*" +} + +# restricted scc +function check_user(){ + cur_user=$(id -u) + if [[ "$cur_user" != "26" ]] + then + echo "[$(date +%Y-%m-%dT%H:%M:%S)] starting as not postgres user" + set -e + + echo "[$(date +%Y-%m-%dT%H:%M:%S)] Adding randomly generated uid to passwd file..." + + sed -i '/postgres/d' /etc/passwd + + if ! whoami &> /dev/null; then + if [[ -w /etc/passwd ]]; then + export USER_ID=$(id -u) + export GROUP_ID=$(id -g) + echo "postgres:x:${USER_ID}:${GROUP_ID}:PostgreSQL Server:${PGDATA}:/bin/bash" >> /etc/passwd + echo "UID added ..." + fi + fi + + fi +} + +function check_pgsql_version(){ + echo "[$(date +%Y-%m-%dT%H:%M:%S)] => Check Source And Target PGSQL version" + # get version of data files + PG_VERSION=$(head -n 1 "/var/lib/pgsql/data/${DATA_DIR}/PG_VERSION") + + if python -c "import sys; sys.exit(0 if 11.0 <= float("${PG_VERSION}") < 12.0 else 1)"; then + PG_VERSION_TARGET="12" + elif python -c "import sys; sys.exit(0 if 10.0 <= float("${PG_VERSION}") < 11.0 else 1)"; then + PG_VERSION_TARGET="11" + else + PG_VERSION_TARGET="10" + fi + + for i in {1..10}; do + echo "[$(date +%Y-%m-%dT%H:%M:%S)] Will try to find target_version file" + [ -f /var/lib/pgsql/data/target_version ] && echo "Target file found, will use version from this file" && \ + PG_VERSION_TARGET=`cat /var/lib/pgsql/data/target_version` && break + sleep 1 + done + + echo "[$(date +%Y-%m-%dT%H:%M:%S)] Target version of PostgreSQL is $PG_VERSION_TARGET" +} + +check_user + +check_pgsql_version + +if [[ "${TYPE}" == "master" ]]; then + handle_master_upgrade +elif [[ "${TYPE}" == "replica" ]]; then + handle_replica_upgrade +fi + +exit 0