Skip to content

Commit

Permalink
Merge 58f3339 into aff448e
Browse files Browse the repository at this point in the history
  • Loading branch information
dgdavid committed Sep 12, 2018
2 parents aff448e + 58f3339 commit b70a734
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 17 deletions.
6 changes: 6 additions & 0 deletions package/yast2-update.changes
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
-------------------------------------------------------------------
Wed Sep 12 11:44:09 UTC 2018 - dgonzalez@suse.com

- Avoid to restore old backups when upgrade fails (bsc#1097297)
- 4.1.3

-------------------------------------------------------------------
Tue Sep 11 15:34:53 CEST 2018 - aschnell@suse.com

Expand Down
2 changes: 1 addition & 1 deletion package/yast2-update.spec
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@


Name: yast2-update
Version: 4.1.2
Version: 4.1.3
Release: 0

BuildRoot: %{_tmppath}/%{name}-%{version}-build
Expand Down
34 changes: 24 additions & 10 deletions src/modules/Update.rb
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ def ProductsCompatible
found = Builtins.find(to_install) { |u| u == i }
found != nil
end
@_products_compatible = equal_product != nil
@_products_compatible = equal_product != nil
# no product name found
# bugzilla #218720, valid without testing according to comment #10
else
Expand Down Expand Up @@ -430,7 +430,7 @@ def GetProductName

# Remove 'Beta...' from product release
if Builtins.regexpmatch(old_name, "Beta")
old_name = Builtins.regexpsub(old_name, "^(.*)[ \t]+Beta.*$", "\\1")
old_name = Builtins.regexpsub(old_name, "^(.*)[ \t]+Beta.*$", "\\1")
# Remove 'Alpha...' from product release
elsif Builtins.regexpmatch(old_name, "Alpha")
old_name = Builtins.regexpsub(old_name, "^(.*)[ \t]+Alpha.*$", "\\1")
Expand Down Expand Up @@ -467,7 +467,7 @@ def GetProductName
Installation.installedVersion,
"major",
Builtins.tointeger(inst_ver)
)
)
# openSUSE
elsif Builtins.regexpmatch(inst_ver, "^[0123456789]+.[0123456789]+$")
Ops.set(
Expand Down Expand Up @@ -550,7 +550,7 @@ def GetProductName
Ops.subtract(num, 1),
0
)
end
end
# default for !Stage::normal
else
update_to_source = Packages.GetBaseSourceID
Expand Down Expand Up @@ -624,7 +624,7 @@ def GetProductName
Installation.updateVersion,
"major",
Builtins.tointeger(new_ver)
)
)
# openSUSE
elsif Builtins.regexpmatch(new_ver, "^[0123456789]+.[0123456789]$")
Ops.set(
Expand Down Expand Up @@ -801,16 +801,25 @@ def clean_backup
:force => true, :secure => true)
end

# restores backup
# Restores valid backups
#
# It will look for "restore-*.sh" files inside the BACKUP_DIR, executing only those created
# after last boot (bsc#1097297)
def restore_backup
log.info "Restoring backup"
mounted_root = Installation.destdir
script_glob = File.join(mounted_root, BACKUP_DIR,"restore-*.sh")
script_glob = File.join(mounted_root, BACKUP_DIR, "restore-*.sh")
# sort the scripts to execute them in the expected order
::Dir.glob(script_glob).sort.each do |path|
cmd = "sh #{path} #{File.join("/", mounted_root)}"
res = SCR.Execute(path(".target.bash_output"), cmd)
log.info "Restoring with script #{cmd} result: #{res}"
backup_time = File.stat(path).ctime.to_i

if boot_time < backup_time
cmd = "sh #{path} #{File.join("/", mounted_root)}"
res = SCR.Execute(path(".target.bash_output"), cmd)
log.info "Restoring with script #{cmd} result: #{res}"
else
log.info "Discarding #{path} (created before last boot)"
end
end
end

Expand Down Expand Up @@ -850,6 +859,11 @@ def restore_backup

private

# @return [Integer] boot time found in /proc/stat
def boot_time
@boot_time ||= IO.read("/proc/stat")[/btime.*/].split.last.to_i
end

def create_tarball(tarball_path, root, paths)
# tar reports an error if a file does not exist.
# So we have to check this before.
Expand Down
39 changes: 33 additions & 6 deletions test/update_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,39 @@ def default_SetDesktopPattern_stubs
end

describe "#restore_backup" do
it "call all restore scripts in backup directory" do
expect(::Dir).to receive(:glob).and_return(["restore-a.sh", "restore-b.sh"])
expect(Yast::SCR).to receive(:Execute).with(Yast::Path.new(".target.bash_output"), /sh .*restore-a.sh \/mnt/).
and_return({"exit" => 0})
expect(Yast::SCR).to receive(:Execute).with(Yast::Path.new(".target.bash_output"), /sh .*restore-b.sh \/mnt/).
and_return({"exit" => 0})
let(:boot_time) { 1514764800 }
let(:valid_backup_time) { boot_time + 1000 }
let(:invalid_backup_time) { boot_time - 1000 }
let(:first_backup) { "000-restore-c.sh" }
let(:second_backup) { "restore-a.sh" }
let(:third_backup) { "restore-b.sh" }
let(:mock_stat) { double({ ctime: nil }) }

before do
allow(::Dir).to receive(:glob).and_return([third_backup, first_backup, second_backup])

allow(::File).to receive(:stat).with(anything).and_return(mock_stat)
allow(mock_stat).to receive(:ctime)
.and_return(valid_backup_time, valid_backup_time, invalid_backup_time)

allow(Yast::Update).to receive(:boot_time).and_return(boot_time)
end

it "found all available backups" do
expect(::Dir).to receive(:glob).and_return(["restore-b.sh", "000-restore-c.sh", "restore-a.sh"])

Yast::Update.restore_backup
end

it "execute only backups created after boot" do
expect(Yast::SCR).to receive(:Execute)
.with(Yast::Path.new(".target.bash_output"), /sh .*000-restore-c.sh \/mnt/)
.and_return({"exit" => 0})
expect(Yast::SCR).to receive(:Execute)
.with(Yast::Path.new(".target.bash_output"), /sh .*restore-a.sh \/mnt/)
.and_return({"exit" => 0})
expect(Yast::SCR).to_not receive(:Execute)
.with(Yast::Path.new(".target.bash_output"), /sh .*restore-b.sh \/mnt/)

Yast::Update.restore_backup
end
Expand Down

0 comments on commit b70a734

Please sign in to comment.