From 6ed90a2ac08c070e8e5c47a1eb3c52d7d30cabb8 Mon Sep 17 00:00:00 2001 From: Christian Heimes Date: Dec 14 2018 08:53:01 +0000 Subject: Add install/remove package helpers to advise The smart card advise scripts assume that yum is installed. However Fedora has dnf and the yum wrapper is not installed by default. Installation and removal of packages is now provided by two helper methods that detect the package manager. Signed-off-by: Christian Heimes Reviewed-By: Alexander Bokovoy Reviewed-By: Rob Crittenden --- diff --git a/ipaserver/advise/base.py b/ipaserver/advise/base.py index 07b1431..ec65113 100644 --- a/ipaserver/advise/base.py +++ b/ipaserver/advise/base.py @@ -227,6 +227,7 @@ class _AdviceOutput(object): self.content = [] self.prefix = '# ' self.options = None + self.pkgmgr_detected = False self._indentation_tracker = _IndentationTracker( spaces_per_indent=DEFAULT_INDENTATION_INCREMENT) @@ -312,6 +313,41 @@ class _AdviceOutput(object): self.command('exit 1') + def detect_pkgmgr(self): + self.commands_on_predicate( + 'which yum >/dev/null', + commands_to_run_when_true=['PKGMGR=yum'], + commands_to_run_when_false=['PKGMGR=dnf'] + ) + self.pkgmgr_detected = True + + def install_packages(self, names, error_message_lines): + assert isinstance(names, list) + self.detect_pkgmgr() + self.command('rpm -qi {} > /dev/null'.format(' '.join(names))) + self.commands_on_predicate( + '[ "$?" -ne "0" ]', + ['$PKGMGR install -y {}'.format(' '.join(names))] + ) + self.exit_on_predicate( + '[ "$?" -ne "0" ]', + error_message_lines + ) + + def remove_package(self, name, error_message_lines): + # remove only supports one package name + assert ' ' not in name + self.detect_pkgmgr() + self.command('rpm -qi {} > /dev/null'.format(name)) + self.commands_on_predicate( + '[ "$?" -eq "0" ]', + ['$PKGMGR remove -y {} || exit 1'.format(name)] + ) + self.exit_on_predicate( + '[ "$?" -ne "0" ]', + error_message_lines + ) + @contextmanager def unbranched_if(self, predicate): with self._compound_statement(UnbranchedIfStatement, predicate): diff --git a/ipaserver/advise/plugins/smart_card_auth.py b/ipaserver/advise/plugins/smart_card_auth.py index 9a7a315..411124f 100644 --- a/ipaserver/advise/plugins/smart_card_auth.py +++ b/ipaserver/advise/plugins/smart_card_auth.py @@ -135,9 +135,10 @@ class config_server_for_smart_card_auth(common_smart_card_auth_config): self.log.comment('make sure bind-utils are installed so that we can ' 'dig for ipa-ca records') - self.log.exit_on_failed_command( - 'yum install -y bind-utils', - ['Failed to install bind-utils']) + self.log.install_packages( + ['bind-utils'], + ['Failed to install bind-utils'] + ) self.log.comment('make sure ipa-ca records are resolvable, ' 'otherwise error out and instruct') @@ -272,26 +273,23 @@ class config_client_for_smart_card_auth(common_smart_card_auth_config): self.restart_sssd() def check_and_remove_pam_pkcs11(self): - self.log.command('rpm -qi pam_pkcs11 > /dev/null') - self.log.commands_on_predicate( - '[ "$?" -eq "0" ]', - [ - 'yum remove -y pam_pkcs11' - ] + self.log.remove_package( + 'pam_pkcs11', + ['Could not remove pam_pkcs11 package'] ) def install_opensc_and_dconf_packages(self): self.log.comment( 'authconfig often complains about missing dconf, ' 'install it explicitly') - self.log.exit_on_failed_command( - 'yum install -y {} dconf'.format(self.opensc_module_name.lower()), + self.log.install_packages( + [self.opensc_module_name.lower(), 'dconf'], ['Could not install OpenSC package'] ) def install_krb5_client_dependencies(self): - self.log.exit_on_failed_command( - 'yum install -y krb5-pkinit-openssl', + self.log.install_packages( + ['krb5-pkinit-openssl'], ['Failed to install Kerberos client PKINIT extensions.'] )