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 test_auth_file_doesnt_exist_means_we_dont_remove_or_write_it( self, m_get_apt_auth_file, m_write_file, m_unlink, tmpdir): """If the auth file doesn't exist, we shouldn't do anything to it""" m_get_apt_auth_file.return_value = tmpdir.join("nonexistent").strpath remove_repo_from_apt_auth_file("http://url") assert 0 == m_write_file.call_count assert 0 == m_unlink.call_count
def test_file_removal(self, m_get_apt_auth_file, m_write_file, m_unlink, tmpdir, trailing_slash, repo_url, auth_file_content): """Check that auth file is rm'd if empty or contains just our line""" auth_file = tmpdir.join('auth_file') auth_file.write(auth_file_content, 'wb') m_get_apt_auth_file.return_value = auth_file.strpath remove_repo_from_apt_auth_file( repo_url + ('' if not trailing_slash else '/')) assert 0 == m_write_file.call_count assert [mock.call(auth_file.strpath)] == m_unlink.call_args_list
def test_file_rewrite(self, m_get_apt_auth_file, m_unlink, tmpdir, repo_url, before_content, after_content, trailing_slash): """Check that auth file is rewritten to only exclude our line""" auth_file = tmpdir.join('auth_file') auth_file.write(before_content, 'wb') m_get_apt_auth_file.return_value = auth_file.strpath remove_repo_from_apt_auth_file( repo_url + ('' if not trailing_slash else '/')) assert 0 == m_unlink.call_count assert 0o600 == stat.S_IMODE(os.lstat(auth_file.strpath).st_mode) assert after_content == auth_file.read('rb')
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 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 )