From 86a6b3fd507c80bef411c4f1ceb06a69dd797507 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Stucke?= Date: Mon, 18 Nov 2024 13:01:45 +0100 Subject: [PATCH] Update postgres to v17 (#1245) * update installed postgres version from 14 to 17 * added warning in installation when old version detected * (related) added upgrade instructions to wiki --- src/helperFunctions/install.py | 4 ++-- src/init_postgres.py | 2 +- src/install/db.py | 44 ++++++++++++++++++++++++---------- src/test/conftest.py | 4 ++-- 4 files changed, 36 insertions(+), 18 deletions(-) diff --git a/src/helperFunctions/install.py b/src/helperFunctions/install.py index 0a9ea7b5a..7e701a38b 100644 --- a/src/helperFunctions/install.py +++ b/src/helperFunctions/install.py @@ -244,13 +244,13 @@ def check_distribution(allow_unsupported=False): return 'noble' if codename in debian_code_names: logging.debug('Debian/Kali detected') - return 'debian' + return codename if distro.id() == 'fedora': logging.debug('Fedora detected') return 'fedora' msg = ( f'Your Distribution ({distro.id()} {distro.version()}) is not supported. ' - 'FACT Installer requires Ubuntu 20.04/22.04, Debian 11/12 or compatible!' + 'FACT Installer requires Ubuntu 20.04/22.04/24.04, Debian 11/12 or compatible!' ) if allow_unsupported: logging.info(msg) diff --git a/src/init_postgres.py b/src/init_postgres.py index 09d4ef67d..7f8b6bf2c 100755 --- a/src/init_postgres.py +++ b/src/init_postgres.py @@ -26,7 +26,7 @@ def execute_psql_command( # See https://www.postgresql.org/docs/current/auth-methods.html if host in ['localhost', '127.0.0.1']: - shell_cmd = f'sudo runuser -u {user} -- psql -c "{psql_command}"' + shell_cmd = f'sudo runuser -u {user} -- psql --port={port} -c "{psql_command}"' else: shell_cmd = f'psql --host={host} --port={port} --username={user} -c "{psql_command}"' diff --git a/src/install/db.py b/src/install/db.py index 8fb598bab..12c02503b 100644 --- a/src/install/db.py +++ b/src/install/db.py @@ -1,17 +1,27 @@ +from __future__ import annotations + import logging +import re from contextlib import suppress from pathlib import Path from shlex import split from subprocess import PIPE, CalledProcessError, run -from helperFunctions.install import InstallationError, OperateInDirectory +from helperFunctions.install import InstallationError, OperateInDirectory, check_distribution + +POSTGRES_VERSION = 17 +POSTGRES_UPDATE_MESSAGE = ( + 'Please see "https://github.com/fkie-cad/FACT_core/wiki/Upgrading-the-PostgreSQL-Database" for information on how' + 'to upgrade your PostgreSQL version.' +) -def install_postgres(version: int = 14): +def install_postgres(version: int = POSTGRES_VERSION): # based on https://www.postgresql.org/download/linux/ubuntu/ + codename = check_distribution() command_list = [ 'sudo apt-get install -y postgresql-common', - 'sudo /usr/share/postgresql-common/pgdg/apt.postgresql.org.sh -y', + f'sudo /usr/share/postgresql-common/pgdg/apt.postgresql.org.sh -y {codename}', f'sudo apt-get -y install postgresql-{version}', ] for command in command_list: @@ -20,7 +30,9 @@ def install_postgres(version: int = 14): raise InstallationError(f'Failed to set up PostgreSQL: {process.stderr}') -def configure_postgres(version: int = 14): +def configure_postgres(version: int | None = None): + if version is None: + version = POSTGRES_VERSION config_path = f'/etc/postgresql/{version}/main/postgresql.conf' # increase the maximum number of concurrent connections run(f'sudo sed -i -E "s/max_connections = [0-9]+/max_connections = 999/g" {config_path}', shell=True, check=True) @@ -31,21 +43,27 @@ def configure_postgres(version: int = 14): run('sudo service postgresql restart', shell=True, check=True) -def postgres_is_installed(): - try: - run(split('psql --version'), check=True) - return True - except (CalledProcessError, FileNotFoundError): - return False +def get_postgres_version() -> int: + proc = run(split('psql --version'), text=True, capture_output=True, check=True) + match = re.search(r'PostgreSQL\)? (\d+).\d+', proc.stdout) + if match: + return int(match.groups()[0]) + raise InstallationError( + f'PostgreSQL is installed but version could not be identified: {proc.stdout}. {POSTGRES_UPDATE_MESSAGE}' + ) def main(): - if postgres_is_installed(): + postgres_version: int | None = None + try: + postgres_version = get_postgres_version() + if not postgres_version >= POSTGRES_VERSION: + logging.warning(f'PostgreSQL is installed but the version is not up to date. {POSTGRES_UPDATE_MESSAGE}') logging.info('Skipping PostgreSQL installation. Reason: Already installed.') - else: + except (CalledProcessError, FileNotFoundError): # psql binary was not found logging.info('Setting up PostgreSQL database') install_postgres() - configure_postgres() + configure_postgres(postgres_version) # initializing DB logging.info('Initializing PostgreSQL database') diff --git a/src/test/conftest.py b/src/test/conftest.py index cdb356a3b..b3ccfaf03 100644 --- a/src/test/conftest.py +++ b/src/test/conftest.py @@ -150,8 +150,8 @@ def _database_interfaces(): # noqa: PT005 'rw-pw': config.common.postgres.rw_pw, 'del-user': config.common.postgres.del_user, 'del-pw': config.common.postgres.del_pw, - 'admin-user': config.common.postgres.del_user, - 'admin-pw': config.common.postgres.del_pw, + 'admin-user': config.common.postgres.admin_user, + 'admin-pw': config.common.postgres.admin_pw, }, 'redis': { 'fact-db': config.common.redis.test_db, # Note: This is unused in testing