Skip to content

Commit

Permalink
backup and remove the old repository setup at upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
lslezak committed Jun 12, 2014
1 parent b39cd4f commit 42c4909
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/clients/inst_scc.rb
Expand Up @@ -204,7 +204,11 @@ def show_registration_update_dialog
def update_registration
show_registration_update_dialog

# prepare the target system for upgrade
::Registration::SwMgmt.prepare_for_upgrade!(Installation.destdir)

if refresh_base_product
# TODO FIXME: refresh also the addons
return :next
else
# automatic registration refresh during system upgrade failed, register from scratch
Expand Down
41 changes: 40 additions & 1 deletion src/lib/registration/sw_mgmt.rb
Expand Up @@ -45,6 +45,10 @@ class SwMgmt

ZYPP_DIR = "/etc/zypp"

# directories to backup and remove at upgrade
BACKUP_DIRS = [ "/etc/zypp/repos.d", "/etc/zypp/services.d",
"/etc/zypp/credentials.d" ]

def self.init
# false = do not allow continuing without the libzypp lock
lock = PackageLock.Connect(false)
Expand Down Expand Up @@ -76,6 +80,41 @@ def self.zypp_config_writable!
`mount -o bind #{tmpdir}/zypp #{ZYPP_DIR}`
end

# backup SW management configuration to
# /var/adm/backup/upgrade/sw_mgmt-<time_stamp>.tar.gz file
# @param root_dir [String] mount point of the target system
def self.backup(root_dir)
time_stamp = Time.now.localtime.strftime("%Y%m%d-%H%M%S")
target_file = File.join(root_dir, "/var/adm/backup/upgrade/sw_mgmt-#{time_stamp}.tar.gz")
::FileUtils.mkdir_p(File.dirname(target_file))

backup_dirs = BACKUP_DIRS.select{|dir| File.exist?(File.join(root_dir, dir))}
backup_dirs.map!{|dir| File.join(root_dir, dir)}

log.info "Creating backup file #{target_file} with: #{backup_dirs}"

`tar cfzv #{target_file} #{backup_dirs.join(" ")}`

log.info "Backup file #{target_file} size: #{File.stat(target_file).size}"
end

# @param root_dir [String] mount point of the target system
def self.remove_old_repositories!(root_dir)
BACKUP_DIRS.each do |dir|
remove = File.join(root_dir, dir, "*")
log.info "Removing #{remove} files..."
::FileUtils.rm_rf(remove)
end
end

# prepare the target system for upgrade
# @param root_dir [String] mount point of the target system
def self.prepare_for_upgrade!(root_dir)
log.info "Preparing the target system at #{root_dir} for upgrade"
backup(root_dir)
remove_old_repositories!(root_dir)
end

def self.find_base_product
# during installation the products are :selected,
# on a running system the products are :installed
Expand Down Expand Up @@ -321,7 +360,7 @@ def self.each_repo(repo_aliases, &block)
end
end
end

# get libzypp repository aliases for a service
# (libzypp internally uses the service name as the prefix)
def self.service_aliases(service, aliases)
Expand Down
51 changes: 51 additions & 0 deletions test/sw_mgmt_spec.rb
Expand Up @@ -132,6 +132,57 @@
end
end

describe ".backup" do
let(:root_dir) { "/mnt" }

before do
expect(FileUtils).to receive(:mkdir_p).with("#{root_dir}/var/adm/backup/upgrade")

Registration::SwMgmt::BACKUP_DIRS.each do |dir|
expect(File).to receive(:exist?).with(File.join(root_dir, dir)).and_return(true)
end

expect(Registration::SwMgmt).to receive(:`) do |param|
expect(param).to match(/tar\scfzv\s
#{root_dir}\/var\/adm\/backup\/upgrade\/sw_mgmt-.*\.tar\.gz\s
#{root_dir}\/etc\/zypp\/repos.d\s
#{root_dir}\/etc\/zypp\/services.d\s
#{root_dir}\/etc\/zypp\/credentials.d/x)
end

File.stub_chain(:stat, :size => 42)
end

it "backs up the original repository and service setup" do
expect { Registration::SwMgmt.backup(root_dir) }.to_not raise_error
end
end

describe ".remove_old_repositories!" do
let(:root_dir) { "/mnt" }

before do
Registration::SwMgmt::BACKUP_DIRS.each do |dir|
expect(FileUtils).to receive(:rm_rf).with(File.join(root_dir, dir, "*"))
end
end

it "removes the old software management in at the target directory" do
expect { Registration::SwMgmt.remove_old_repositories!(root_dir) }.to_not raise_error
end
end

describe ".prepare_for_upgrade!" do
let(:root_dir) { "/mnt" }

it "backs up the old config and removes it from the target" do
expect(Registration::SwMgmt).to receive(:backup).with(root_dir)
expect(Registration::SwMgmt).to receive(:remove_old_repositories!).with(root_dir)

expect { Registration::SwMgmt.prepare_for_upgrade!(root_dir) }.to_not raise_error
end
end

describe ".copy_old_credentials" do
let(:root_dir) { "/mnt" }
let(:target_dir) {SUSE::Connect::Credentials::DEFAULT_CREDENTIALS_DIR}
Expand Down

0 comments on commit 42c4909

Please sign in to comment.