def disable(self, silent=False, force=False): """Disable specific entitlement @return: True on success, False otherwise. """ if not self.can_disable(silent, force): return False series = util.get_platform_info('series') repo_filename = self.repo_list_file_tmpl.format(name=self.name, series=series) keyring_file = os.path.join(apt.APT_KEYS_DIR, self.repo_key_file) entitlement_cfg = self.cfg.read_cache('machine-access-%s' % self.name)['entitlement'] access_directives = entitlement_cfg.get('directives', {}) repo_url = access_directives.get('aptURL', self.repo_url) if not repo_url: repo_url = self.repo_url apt.remove_auth_apt_repo(repo_filename, repo_url, keyring_file) apt.remove_apt_list_files(repo_url, series) print('Removing packages: %s' % ', '.join(self.packages)) try: util.subp(['apt-get', 'remove', '--assume-yes'] + self.packages) except util.ProcessExecutionError: pass return True
def disable(self, silent=False, force=False): """Disable specific entitlement @return: True on success, False otherwise. """ if not self.can_disable(silent, force): return False series = util.get_platform_info('series') repo_filename = self.repo_list_file_tmpl.format(name=self.name, series=series) keyring_file = os.path.join(apt.APT_KEYS_DIR, self.repo_key_file) entitlement_cfg = self.cfg.read_cache('machine-access-%s' % self.name)['entitlement'] access_directives = entitlement_cfg.get('directives', {}) repo_url = access_directives.get('aptURL', self.repo_url) if not repo_url: repo_url = self.repo_url apt.remove_auth_apt_repo(repo_filename, repo_url, keyring_file) if self.repo_pin_priority: repo_pref_file = self.repo_pref_file_tmpl.format(name=self.name, series=series) if os.path.exists(repo_pref_file): os.unlink(repo_pref_file) if not silent: print(status.MESSAGE_DISABLED_TMPL.format(title=self.title)) return True
def disable(self, silent=False, force=False): if not self.can_disable(silent, force): return False if force: # Force config cleanup as broke during setup attempt. series = util.get_platform_info('series') repo_filename = self.repo_list_file_tmpl.format(name=self.name, series=series) keyring_file = os.path.join(apt.APT_KEYS_DIR, self.repo_key_file) entitlement = self.cfg.read_cache( 'machine-access-%s' % self.name).get('entitlement', {}) access_directives = entitlement.get('directives', {}) repo_url = access_directives.get('aptURL', self.repo_url) if not repo_url: repo_url = self.repo_url apt.remove_auth_apt_repo(repo_filename, repo_url, keyring_file) if self.repo_pin_priority: repo_pref_file = self.repo_pref_file_tmpl.format( name=self.name, series=series) if os.path.exists(repo_pref_file): os.unlink(repo_pref_file) apt.remove_apt_list_files(repo_url, series) try: util.subp(['apt-get', 'remove', '--assume-yes'] + self.packages) except util.ProcessExecutionError: pass if not silent: print('Warning: no option to disable {title}'.format( title=self.title)) return False
def remove_apt_config(self): """Remove any repository apt configuration files.""" series = util.get_platform_info()['series'] repo_filename = self.repo_list_file_tmpl.format( name=self.name, series=series) keyring_file = os.path.join(apt.APT_KEYS_DIR, self.repo_key_file) entitlement = self.cfg.read_cache( 'machine-access-%s' % self.name).get('entitlement', {}) access_directives = entitlement.get('directives', {}) repo_url = access_directives.get('aptURL', self.repo_url) if not repo_url: repo_url = self.repo_url if self.disable_apt_auth_only: # We only remove the repo from the apt auth file, because ESM # is a special-case: we want to be able to report on the # available ESM updates even when it's disabled apt.remove_repo_from_apt_auth_file(repo_url) apt.restore_commented_apt_list_file(repo_filename) else: apt.remove_auth_apt_repo(repo_filename, repo_url, keyring_file) apt.remove_apt_list_files(repo_url, series) if self.repo_pin_priority: repo_pref_file = self.repo_pref_file_tmpl.format( name=self.name, series=series) if self.repo_pin_priority == 'never': # Disable the repo with a pinning file apt.add_ppa_pinning( repo_pref_file, repo_url, self.origin, self.repo_pin_priority) elif os.path.exists(repo_pref_file): os.unlink(repo_pref_file)
def process_contract_deltas( self, orig_access: Dict[str, Any], deltas: Dict[str, Any], allow_enable: bool = False, ) -> bool: """Process any contract access deltas for this entitlement. :param orig_access: Dictionary containing the original resourceEntitlement access details. :param deltas: Dictionary which contains only the changed access keys and values. :param allow_enable: Boolean set True if allowed to perform the enable operation. When False, a message will be logged to inform the user about the recommended enabled service. :return: True when delta operations are processed; False when noop. """ if super().process_contract_deltas(orig_access, deltas, allow_enable): return True # Already processed parent class deltas delta_entitlement = deltas.get("entitlement", {}) delta_directives = delta_entitlement.get("directives", {}) delta_apt_url = delta_directives.get("aptURL") delta_packages = delta_directives.get("additionalPackages") status_cache = self.cfg.read_cache("status-cache") if delta_directives and status_cache: application_status = self._check_application_status_on_cache() else: application_status, _ = self.application_status() if application_status == ApplicationStatus.DISABLED: return False if not self._check_apt_url_is_applied(delta_apt_url): logging.info( "Updating '%s' apt sources list on changed directives.", self.name, ) orig_entitlement = orig_access.get("entitlement", {}) old_url = orig_entitlement.get("directives", {}).get("aptURL") if old_url: # Remove original aptURL and auth and rewrite repo_filename = self.repo_list_file_tmpl.format(name=self.name) apt.remove_auth_apt_repo(repo_filename, old_url) self.remove_apt_config() self.setup_apt_config() if delta_packages: logging.info( "Installing packages on changed directives: {}".format( ", ".join(delta_packages))) self.install_packages(package_list=delta_packages) return True
def test_remove_from_auth_file_called(self, m_remove_repo, _mock, __mock, remove_auth_apt_repo_kwargs): """Ensure that remove_repo_from_apt_auth_file is called.""" repo_filename, repo_url = mock.sentinel.filename, mock.sentinel.url remove_auth_apt_repo(repo_filename, repo_url, **remove_auth_apt_repo_kwargs) assert mock.call(repo_url) in m_remove_repo.call_args_list
def test_repo_file_deleted(self, m_del_file, _mock, __mock, remove_auth_apt_repo_kwargs): """Ensure that repo_filename is deleted, regardless of other params.""" repo_filename, repo_url = mock.sentinel.filename, mock.sentinel.url remove_auth_apt_repo(repo_filename, repo_url, **remove_auth_apt_repo_kwargs) assert mock.call(repo_filename) in m_del_file.call_args_list
def test_keyring_file_deleted_if_given(self, m_del_file, _mock, __mock, remove_auth_apt_repo_kwargs): """We should always delete the keyring file if it is given""" repo_filename, repo_url = mock.sentinel.filename, mock.sentinel.url remove_auth_apt_repo(repo_filename, repo_url, **remove_auth_apt_repo_kwargs) keyring_file = remove_auth_apt_repo_kwargs.get("keyring_file") if keyring_file: assert mock.call(keyring_file) in m_del_file.call_args_list else: assert mock.call(keyring_file) not in m_del_file.call_args_list
def remove_apt_config(self, run_apt_update: bool = True, silent: bool = False): """Remove any repository apt configuration files. :param run_apt_update: If after removing the apt update command after removing the apt files. """ series = util.get_platform_info()["series"] repo_filename = self.repo_list_file_tmpl.format(name=self.name) entitlement = self.cfg.entitlements[self.name].get("entitlement", {}) access_directives = entitlement.get("directives", {}) repo_url = access_directives.get("aptURL") if not repo_url: raise exceptions.MissingAptURLDirective(self.name) if self.disable_apt_auth_only: # We only remove the repo from the apt auth file, because # UA Infra: ESM is a special-case: we want to be able to report on # the available UA Infra: ESM updates even when it's disabled apt.remove_repo_from_apt_auth_file(repo_url) apt.restore_commented_apt_list_file(repo_filename) else: apt.remove_auth_apt_repo(repo_filename, repo_url, self.repo_key_file) apt.remove_apt_list_files(repo_url, series) if self.repo_pin_priority: repo_pref_file = self.repo_pref_file_tmpl.format(name=self.name) if self.repo_pin_priority == "never": # Disable the repo with a pinning file apt.add_ppa_pinning( repo_pref_file, repo_url, self.origin, self.repo_pin_priority, ) elif os.path.exists(repo_pref_file): os.unlink(repo_pref_file) if run_apt_update: if not silent: event.info(messages.APT_UPDATING_LISTS) apt.run_apt_update_command()
def process_contract_deltas( self, orig_access: "Dict[str, Any]", deltas: "Dict[str, Any]", allow_enable: bool = False, ) -> bool: """Process any contract access deltas for this entitlement. :param orig_access: Dictionary containing the original resourceEntitlement access details. :param deltas: Dictionary which contains only the changed access keys and values. :param allow_enable: Boolean set True if allowed to perform the enable operation. When False, a message will be logged to inform the user about the recommended enabled service. :return: True when delta operations are processed; False when noop. """ if super().process_contract_deltas(orig_access, deltas, allow_enable): return True # Already processed parent class deltas application_status, _ = self.application_status() if application_status == status.ApplicationStatus.DISABLED: return True logging.info( "Updating '%s' apt sources list on changed directives.", self.name ) delta_entitlement = deltas.get("entitlement", {}) if delta_entitlement.get("directives", {}).get("aptURL"): orig_entitlement = orig_access.get("entitlement", {}) old_url = orig_entitlement.get("directives", {}).get("aptURL") if old_url: # Remove original aptURL and auth and rewrite series = util.get_platform_info()["series"] repo_filename = self.repo_list_file_tmpl.format( name=self.name, series=series ) apt.remove_auth_apt_repo(repo_filename, old_url) self.remove_apt_config() self.setup_apt_config() return True
def remove_apt_config(self): """Remove any repository apt configuration files.""" series = util.get_platform_info()["series"] repo_filename = self.repo_list_file_tmpl.format( name=self.name, series=series ) entitlement = self.cfg.entitlements[self.name].get("entitlement", {}) access_directives = entitlement.get("directives", {}) repo_url = access_directives.get("aptURL") if not repo_url: raise exceptions.MissingAptURLDirective(self.name) if self.disable_apt_auth_only: # We only remove the repo from the apt auth file, because ESM Infra # is a special-case: we want to be able to report on the # available ESM Infra updates even when it's disabled apt.remove_repo_from_apt_auth_file(repo_url) apt.restore_commented_apt_list_file(repo_filename) else: apt.remove_auth_apt_repo( repo_filename, repo_url, self.repo_key_file ) apt.remove_apt_list_files(repo_url, series) if self.repo_pin_priority: repo_pref_file = self.repo_pref_file_tmpl.format( name=self.name, series=series ) if self.repo_pin_priority == "never": # Disable the repo with a pinning file apt.add_ppa_pinning( repo_pref_file, repo_url, self.origin, self.repo_pin_priority, ) elif os.path.exists(repo_pref_file): os.unlink(repo_pref_file) print(status.MESSAGE_APT_UPDATING_LISTS) apt.run_apt_command( ["apt-get", "update"], status.MESSAGE_APT_UPDATE_FAILED )