Skip to content

Commit

Permalink
Merge cf0846a into 88bc3ba
Browse files Browse the repository at this point in the history
  • Loading branch information
lslezak committed Jan 5, 2023
2 parents 88bc3ba + cf0846a commit 7051fc1
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 8 deletions.
42 changes: 36 additions & 6 deletions bin/yupdate
Expand Up @@ -109,10 +109,16 @@ module YUpdate
# a simple /etc/install.inf parser/writer,
# we need to disable the YaST self-update feature to avoid conflicts
class InstallInf
PATH = "/etc/install.inf".freeze

attr_reader :path

def self.exist?(path = PATH)
File.exist?(path)
end

# read the file
def initialize(path = "/etc/install.inf")
def initialize(path = PATH)
@path = path
@values = File.read(path).lines.map(&:chomp)
end
Expand Down Expand Up @@ -566,17 +572,20 @@ module YUpdate
end
end

# inst-sys test
class InstSys
# inst-sys or live medium test
class System
# check if the script is running in the inst-sys,
# the script might not work as expected in an installed system
# and using OverlayFS is potentially dangerous
def self.check!
# the inst-sys contains the /.packages.initrd file with a list of packages
return if File.exist?("/.packages.initrd")

# live medium uses overlay FS for the root
return if `mount`.match?(/^\w+ on \/ type overlay/)

# exit immediately if running in an installed system
warn "ERROR: This script can only work in the installation system (inst-sys)!"
warn "ERROR: This script can only work in the installation system (inst-sys) or Live medium!"
exit 1
end
end
Expand All @@ -590,10 +599,10 @@ module YUpdate
when "version"
VersionCommand.new
when "overlay"
InstSys.check!
System.check!
OverlayCommand.new(argv)
when "patch"
InstSys.check!
System.check!
PatchCommand.new(argv)
when "servers"
ServersCommand.new(argv)
Expand Down Expand Up @@ -700,10 +709,29 @@ module YUpdate
g.install_required_gems
end

def run_pre_script(download_dir)
script = File.join(download_dir, ".yupdate.pre")
run_script(script)
end

def run_post_script(download_dir)
script = File.join(download_dir, ".yupdate.post")
run_script(script)
end

def run_script(script)
return unless File.exist?(script)

puts "Running script #{File.basename(script)}..."
raise "Script #{File.basename(script)} failed" unless system(script)
end

def install_tar(downloader)
Dir.mktmpdir do |download_dir|
downloader.extract_to(download_dir)
run_pre_script(download_dir)
install_sources(download_dir)
run_post_script(download_dir)
end
end

Expand All @@ -712,6 +740,8 @@ module YUpdate

# disable the self update in the install.inf file
def disable_self_update
return unless InstallInf.exist?

inf = InstallInf.new
return if inf[SELF_UPDATE_KEY] == "0"

Expand Down
20 changes: 18 additions & 2 deletions test/yupdate/inst_sys_test.rb
Expand Up @@ -3,9 +3,14 @@
require_relative "../test_helper"
require_yupdate

describe YUpdate::InstSys do
describe YUpdate::System do
let(:file) { "/.packages.initrd" }

before do
allow(File).to receive(:exist?).with(file).and_return(false)
allow(described_class).to receive(:`).with("mount").and_return("")
end

describe ".check!" do
context "when running in an inst-sys" do
before do
Expand All @@ -18,9 +23,20 @@
end
end

context "when running on a live medium" do
before do
expect(described_class).to receive(:`).with("mount")
.and_return("LiveOS_rootfs on / type overlay (rw,relatime)")
end

it "does not exit" do
expect(described_class).to_not receive(:exit)
described_class.check!
end
end

context "when running in a normal system" do
before do
expect(File).to receive(:exist?).with(file).and_return(false)
allow(described_class).to receive(:exit).with(1)
end

Expand Down

0 comments on commit 7051fc1

Please sign in to comment.