From e831c9fba3d5105a5bde1bee1f4d1076142ea80c Mon Sep 17 00:00:00 2001 From: Mustafa Baser Date: Fri, 23 Dec 2022 21:14:53 +0300 Subject: [PATCH] fix: jans-linux-setup centos/rhel pgsql installation --- .../jans_setup/setup_app/installers/rdbm.py | 20 ++++++++++++++++++- .../setup_app/utils/package_utils.py | 10 ++++++++-- .../jans_setup/setup_app/utils/setup_utils.py | 20 ++++++++++++++++--- 3 files changed, 44 insertions(+), 6 deletions(-) diff --git a/jans-linux-setup/jans_setup/setup_app/installers/rdbm.py b/jans-linux-setup/jans_setup/setup_app/installers/rdbm.py index 5cd9849fc08..fb7a502803b 100644 --- a/jans-linux-setup/jans_setup/setup_app/installers/rdbm.py +++ b/jans-linux-setup/jans_setup/setup_app/installers/rdbm.py @@ -7,6 +7,7 @@ from string import Template +from setup_app import paths from setup_app.static import AppType, InstallOption from setup_app.config import Config from setup_app.utils import base @@ -92,7 +93,12 @@ def local_install(self): elif Config.rdbm_type == 'pgsql': if base.clone_type == 'rpm': - self.restart('postgresql') + self.run(['postgresql-setup', 'initdb']) + elif base.clone_type == 'deb': + self.run([paths.cmd_chmod, '640', '/etc/ssl/private/ssl-cert-snakeoil.key']) + + self.restart('postgresql') + cmd_create_db = '''su - postgres -c "psql -U postgres -d postgres -c \\"CREATE DATABASE {};\\""'''.format(Config.rdbm_db) cmd_create_user = '''su - postgres -c "psql -U postgres -d postgres -c \\"CREATE USER {} WITH PASSWORD '{}';\\""'''.format(Config.rdbm_user, Config.rdbm_password) cmd_grant_previlages = '''su - postgres -c "psql -U postgres -d postgres -c \\"GRANT ALL PRIVILEGES ON DATABASE {} TO {};\\""'''.format(Config.rdbm_db, Config.rdbm_user) @@ -100,6 +106,18 @@ def local_install(self): for cmd in (cmd_create_db, cmd_create_user, cmd_grant_previlages): self.run(cmd, shell=True) + if base.clone_type == 'rpm': + hba_file_path_query = self.run('''su - postgres -c "psql -U postgres -d postgres -t -c \\"SHOW hba_file;\\""''', shell=True) + if hba_file_path_query and hba_file_path_query.strip(): + self.stop('postgresql') + hba_file_path = hba_file_path_query.strip() + hba_file_content = self.readFile(hba_file_path) + hba_file_content = 'host\t{0}\t{1}\t127.0.0.1/32\tmd5\nhost\t{0}\t{1}\t::1/128\tmd5\n'.format(Config.rdbm_db, Config.rdbm_user) + hba_file_content + self.writeFile(hba_file_path, hba_file_content) + self.start('postgresql') + + self.enable('postgresql') + self.dbUtils.bind(force=True) def get_sql_col_type(self, attrname, table=None): diff --git a/jans-linux-setup/jans_setup/setup_app/utils/package_utils.py b/jans-linux-setup/jans_setup/setup_app/utils/package_utils.py index d8999979729..b5e7dd452ec 100644 --- a/jans-linux-setup/jans_setup/setup_app/utils/package_utils.py +++ b/jans-linux-setup/jans_setup/setup_app/utils/package_utils.py @@ -57,9 +57,15 @@ def check_and_install_packages(self): if base.argsp.local_rdbm == 'mysql' or (Config.get('rdbm_install_type') == InstallTypes.LOCAL and Config.rdbm_type == 'mysql'): package_list[os_type_version]['mandatory'] += ' mysql-server' if base.argsp.local_rdbm == 'pgsql' or (Config.get('rdbm_install_type') == InstallTypes.LOCAL and Config.rdbm_type == 'pgsql'): - package_list[os_type_version]['mandatory'] += ' postgresql python3-psycopg2' + package_list[os_type_version]['mandatory'] += ' postgresql python3-psycopg2 postgresql-contrib' if base.clone_type == 'deb': - package_list[os_type_version]['mandatory'] += ' postgresql-contrib' + package_list[os_type_version]['mandatory'] += '' + elif base.clone_type == 'rpm': + package_list[os_type_version]['mandatory'] += ' postgresql-server' + self.run(['dnf', '-y', 'module', 'disable', 'postgresql']) + self.run(['dnf', '-y', 'module', 'reset', 'postgresql']) + self.run(['dnf', '-y', 'module', 'enable', 'postgresql:12']) + for pypackage in package_list[os_type_version]['python']: try: diff --git a/jans-linux-setup/jans_setup/setup_app/utils/setup_utils.py b/jans-linux-setup/jans_setup/setup_app/utils/setup_utils.py index 2e03c721bae..248096317b9 100644 --- a/jans-linux-setup/jans_setup/setup_app/utils/setup_utils.py +++ b/jans-linux-setup/jans_setup/setup_app/utils/setup_utils.py @@ -11,6 +11,7 @@ import string import random import hashlib +import grp from pathlib import Path from urllib.parse import urlparse @@ -406,9 +407,21 @@ def renderTemplate(self, filePath): def createUser(self, userName, homeDir, shell='/bin/bash'): + try: + grp.getgrnam(userName) + user_group_exists = True + except KeyError: + user_group_exists = False + try: useradd = '/usr/sbin/useradd' - cmd = [useradd, '--system', '--user-group', '--shell', shell, userName] + cmd = [useradd, '--system', '--shell', shell, userName] + if user_group_exists: + cmd.insert(1, '-g') + cmd.insert(2, userName) + else: + cmd.insert(1, '--user-group') + if homeDir: cmd.insert(-1, '--create-home') cmd.insert(-1, '--home-dir') @@ -420,8 +433,9 @@ def createUser(self, userName, homeDir, shell='/bin/bash'): self.logOSChanges("User %s with homedir %s was created" % (userName, homeDir)) else: self.logOSChanges("User %s without homedir was created" % (userName)) - except: - self.logIt("Error adding user", True) + + except Exception as e: + self.logIt("Error adding user: {}".format(e), True) def createGroup(self, groupName): try: