diff --git a/alts/worker/runners/base.py b/alts/worker/runners/base.py index 04c3451..18a9d2a 100644 --- a/alts/worker/runners/base.py +++ b/alts/worker/runners/base.py @@ -857,7 +857,22 @@ def detect_protected_packages(self): file_protected = [i.strip() for i in stdout.split('\n') if i.strip()] if file_protected: protected.extend(file_protected) - return protected + protected.append('kernel-core') + dnf_command = ( + r'dnf', '-q', '--qf=%{NAME}', 'repoquery', '--requires', '--resolve', '--recursive' + + ' '.join(protected) + ) + exit_code, stdout, stderr = self.exec_command(*dnf_command) + if exit_code != 0: + self._logger.warning( + 'Cannot resolve non-uninstallable packages via DNF: %s', + dnf_command + ) + return protected + dnf_protected = [i.strip() for i in stdout.split('\n') if i.strip()] + if dnf_protected: + protected.extend(dnf_protected) + return list(set(protected)) def _uninstall_package( self, @@ -1055,7 +1070,8 @@ def check_package_existence( if self.dist_name in CONFIG.rhel_flavors: cmd = ('rpm', '-q', package_name) elif self.dist_name in CONFIG.debian_flavors: - cmd = ('dpkg', '-l', package_name) + cmd = ('dpkg-query', '-Wf', r'${db:Status-Status} ${Package}\n', + package_name) else: raise ValueError(f'Unknown distribution: {self.dist_name}') exit_code, stdout, stderr = self.exec_command(*cmd) diff --git a/resources/ansible.cfg b/resources/ansible.cfg index 1164d60..c50d345 100644 --- a/resources/ansible.cfg +++ b/resources/ansible.cfg @@ -13,4 +13,4 @@ ssh_args = -o ControlMaster=auto -o ControlPersist=600s -o UserKnownHostsFile=/d pipelining = True [persistent_connection] -command_timeout = 1200 +command_timeout = 590 diff --git a/resources/roles/install_uninstall/tasks/main.yml b/resources/roles/install_uninstall/tasks/main.yml index af37d1c..d6c7d0c 100644 --- a/resources/roles/install_uninstall/tasks/main.yml +++ b/resources/roles/install_uninstall/tasks/main.yml @@ -1,5 +1,11 @@ --- +- set_fact: + short_deb_pkg_name: "{{ pkg_name.split('=')[0] }}" + when: ansible_distribution_file_variety == 'Debian' + tags: + - uninstall_package + - name: Enable module shell: dnf module enable -y "{{ module_name }}:{{ module_stream }}:{{ module_version }}" when: > @@ -27,6 +33,15 @@ tags: - install_package +- name: Check RPM package is installed + shell: + cmd: "rpm -q {{ pkg_name }}" + register: rpm_installed + failed_when: rpm_installed.rc not in [0, 1] + when: ansible_facts.os_family == 'RedHat' + tags: + - uninstall_package + - name: Uninstall RPM package shell: cmd: "rpm -e --nodeps {{ pkg_name }}" @@ -34,17 +49,26 @@ retries: 30 delay: 10 until: result.rc == 0 - when: ansible_facts.os_family == 'RedHat' + when: ansible_facts.os_family == 'RedHat' and rpm_installed.rc == 0 + tags: + - uninstall_package + +- name: Check DEB package is installed + shell: + cmd: "dpkg-query -Wf '${db:Status-Status} ${Package}\n' {{ short_deb_pkg_name }}" + register: deb_installed + failed_when: deb_installed.rc not in [0, 1] + when: ansible_distribution_file_variety == 'Debian' tags: - uninstall_package - name: Uninstall DEB package shell: - cmd: "dpkg -r --force-depends {{ pkg_name }}" + cmd: "dpkg -r --force-depends {{ short_deb_pkg_name }}" register: result retries: 30 delay: 10 until: result.rc == 0 - when: ansible_distribution_file_variety == 'Debian' + when: ansible_distribution_file_variety == 'Debian' and deb_installed.rc == 0 tags: - uninstall_package