Skip to content

Commit

Permalink
Update postgres to v17 (#1245)
Browse files Browse the repository at this point in the history
* update installed postgres version from 14 to 17
* added warning in installation when old version detected
* (related) added upgrade instructions to wiki
  • Loading branch information
jstucke authored Nov 18, 2024
1 parent aa041a9 commit 86a6b3f
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 18 deletions.
4 changes: 2 additions & 2 deletions src/helperFunctions/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/init_postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}"'

Expand Down
44 changes: 31 additions & 13 deletions src/install/db.py
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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)
Expand All @@ -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')
Expand Down
4 changes: 2 additions & 2 deletions src/test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 86a6b3f

Please sign in to comment.