Skip to content

Commit

Permalink
Use curl instead of open-uri when downloading driver updates
Browse files Browse the repository at this point in the history
  • Loading branch information
imobachgs committed Mar 11, 2016
1 parent 066c32b commit 5b19d0c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 24 deletions.
44 changes: 24 additions & 20 deletions src/lib/installation/driver_update.rb
Expand Up @@ -10,6 +10,9 @@ module Installation
class DriverUpdate
EXTRACT_CMD = "gzip -dc %<source>s | cpio --quiet --sparse -dimu --no-absolute-filenames"
APPLY_CMD = "/etc/adddir %<source>s/inst-sys /"
FETCH_CMD = "/usr/bin/curl --location --verbose --fail --max-time 300 --connect-timeout 15 " \
"%<uri>s --output '%<output>s'"
TEMP_FILENAME = "remote.dud"

attr_reader :uri, :local_path

Expand All @@ -31,14 +34,21 @@ def initialize(uri)
# FIXME: should it be called by the constructor?
def fetch(target)
@local_path = target
extract_to(download_file, local_path)
Dir.mktmpdir do |dir|
Dir.chdir(dir) do
temp_file = Pathname.pwd.join(TEMP_FILENAME)
download_file_to(temp_file)
extract(temp_file, local_path)
end
end
end

# Apply the DUD to the running system
#
# @return [Boolean] true if the DUD was applied; false otherwise.
#
# FIXME: remove the ! sign
# FIXME: handle update.{pre,post} scripts
def apply!
raise "Not fetched yet!" if local_path.nil?
cmd = format(APPLY_CMD, source: local_path)
Expand All @@ -53,16 +63,12 @@ def apply!
# @param source [Pathname]
#
# @see EXTRACT_CMD
def extract_to(source, target)
Dir.mktmpdir do |dir|
Dir.chdir(dir) do
cmd = format(EXTRACT_CMD, source: source.path)
out = Yast::SCR.Execute(Yast::Path.new(".target.bash_output"), cmd)
raise "Could not extract DUD" unless out["exit"].zero?
setup_target(target)
FileUtils.mv(update_dir, target)
end
end
def extract(source, target)
cmd = format(EXTRACT_CMD, source: source)
out = Yast::SCR.Execute(Yast::Path.new(".target.bash_output"), cmd)
raise "Could not extract DUD" unless out["exit"].zero?
setup_target(target)
FileUtils.mv(update_dir, target)
end

# Set up the target directory
Expand All @@ -75,16 +81,14 @@ def setup_target(dir)
FileUtils.mkdir_p(dir) unless dir.dirname.exist?
end

# Download the DUD to a temporal file
#
# @return [Tempfile] Temporal file where the DUD is stored
# Download the DUD to a file
#
# FIXME: use curl instead of open-uri to avoid problems with redirections.
def download_file
tempfile = Tempfile.new(["update", ".dud"])
content = open(uri).read
File.write(tempfile.path, content)
tempfile
# @return [True] True if download was successful
def download_file_to(path)
cmd = format(FETCH_CMD, uri: uri, output: path)
Yast::SCR.Execute(Yast::Path.new(".target.bash_output"), cmd)
raise NotFound unless path.exist?
true
end

# Directory which contains files within the DUD
Expand Down
6 changes: 2 additions & 4 deletions test/driver_update_test.rb
Expand Up @@ -15,7 +15,7 @@
TEST_DIR = Pathname.new(__FILE__).dirname
FIXTURES_DIR = TEST_DIR.join("fixtures")

let(:url) { URI("https://update.opensuse.com/0001.dud") }
let(:url) { URI("file://#{FIXTURES_DIR}/fake.dud") }

subject { Installation::DriverUpdate.new(url) }

Expand All @@ -31,7 +31,6 @@
it "downloads the file at #url and stores in the given directory" do
allow(Yast::Linuxrc).to receive(:InstallInf).with("UpdateDir")
.and_return("/linux/suse/x86_64-sles12")
expect(subject).to receive(:open).with(URI(url)).and_return(dud_io)
subject.fetch(target)
expect(target.join("dud.config")).to be_file
end
Expand All @@ -40,8 +39,7 @@
let(:url) { URI("http://non-existent-url.com/") }

it "raises an exception" do
expect(subject).to receive(:open).with(url).and_raise(SocketError)
expect { subject.fetch(target) }.to raise_error SocketError
expect { subject.fetch(target) }.to raise_error StandardError
end
end

Expand Down

0 comments on commit 5b19d0c

Please sign in to comment.