Skip to content

Commit

Permalink
Refresh Addons
Browse files Browse the repository at this point in the history
  • Loading branch information
lslezak committed Mar 16, 2018
1 parent 60dcfa3 commit a1de17e
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 18 deletions.
54 changes: 37 additions & 17 deletions src/lib/registration/ui/offline_migration_workflow.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#

require "yast"
require "uri"

module Registration
module UI
Expand All @@ -27,6 +28,7 @@ class OfflineMigrationWorkflow
Yast.import "Installation"
Yast.import "Wizard"
Yast.import "Pkg"
Yast.import "AddOnProduct"

# the constructor
def initialize
Expand All @@ -52,19 +54,19 @@ def main
if Registration.is_registered?
log.info("Restoring the previous registration")
rollback
restore_installation_repos
end

return :back
else
backup_installation_repos
end

# run the main registration migration
ui = migration_repos

rollback if ui == :rollback

# refresh the add-on records
update_addon_records

# go back in the upgrade workflow after rollback or abort,
# maybe the user justelected a wrong partition to upgrade
ui = :back if ui == :abort || ui == :rollback
Expand All @@ -78,6 +80,8 @@ def main
def rollback
Yast::WFM.CallFunction("registration_sync")

# remove the copied credentials file from the target system to not be
# used again by mistake (skip if accidentally called in a running system)
if Yast::Stage.initial && File.exist?(SUSE::Connect::YaST::GLOBAL_CREDENTIALS_FILE)
log.info("Removing #{SUSE::Connect::YaST::GLOBAL_CREDENTIALS_FILE}...")
File.delete(SUSE::Connect::YaST::GLOBAL_CREDENTIALS_FILE)
Expand All @@ -88,22 +92,38 @@ def migration_repos
Yast::WFM.CallFunction("inst_migration_repos", [{ "enable_back" => true }])
end

def backup_installation_repos
ids = Yast::Pkg.SourceGetCurrent(false)
@@repos_backup = ids.map { |r| Yast::Pkg.SourceGeneralData(r) }
end

def restore_installation_repos
return unless @@repos_backup
Yast::Pkg.SourceFinishAll
Yast::Pkg.TargetFinish
Yast::Pkg.TargetInitialize("/")
# update the repository IDs in the AddOnProduct records
def update_addon_records
Yast::AddOnProduct.add_on_products.each do |addon|
next unless addon["media_url"]

url = URI(addon["media_url"])
dir = addon["product_dir"]
log.info("Refreshing repository ID for addon #{addon["product"]} (#{url})")

# remove the alias from the URL if it is preset, it is removed by Pkg bindings
# when adding the repository so it would not match
if url.query
# params is a list of pairs, "foo=bar" => [["foo, "bar]]
params = URI.decode_www_form(url.query)
params.reject! { |p| p.first == "alias" }
# avoid empty query after "?" in URL
url.query = params.empty? ? nil : URI.encode_www_form(params)
end

@@repos_backup.each { |r| Yast::Pkg.RepositoryAdd(r) }
new_id = Yast::Pkg.SourceGetCurrent(false).find do |repo|
data = Yast::Pkg.SourceGeneralData(repo)
# the same URL and product dir
URI(data["url"]) == url && data["product_dir"] == dir
end

Yast::Pkg.SourceLoad
Yast::Pkg.TargetFinish
Yast::Pkg.TargetInitialize(Yast::Installation.destdir)
if new_id
log.info("Updating ID: #{addon["media"]} -> #{new_id}")
addon["media"] = new_id
else
log.warn("Addon not found")
end
end
end
end
end
Expand Down
5 changes: 4 additions & 1 deletion test/offline_migration_workflow_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
describe Registration::UI::OfflineMigrationWorkflow do
describe "#main" do
before do
allow(Yast::Wizard).to receive(:SetContents)
allow(Yast::Wizard).to receive(:ClearContents)
allow(Yast::Packages).to receive(:init_called=)
allow(Yast::Packages).to receive(:Initialize)
allow(Yast::GetInstArgs).to receive(:going_back)
allow(Yast::AddOnProduct).to receive(:add_on_products).and_return([])
allow(File).to receive(:delete)
end

it "runs the 'inst_migration_repos' client" do
Expand Down

0 comments on commit a1de17e

Please sign in to comment.