diff --git a/ipaserver/advise/base.py b/ipaserver/advise/base.py index 32954a02b..64baba772 100644 --- a/ipaserver/advise/base.py +++ b/ipaserver/advise/base.py @@ -227,6 +227,7 @@ class _AdviceOutput: 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: 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 9a7a315ed..411124f93 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.'] )