Revision 5f7fd34c
Added by Lukas Zapletal over 8 years ago
lib/proxy/http_download.rb | ||
---|---|---|
require 'proxy/file_lock'
|
||
|
||
module Proxy
|
||
class HttpDownload < Proxy::Util::CommandTask
|
||
include Util
|
||
|
||
def initialize(src, dst)
|
||
@dst = dst
|
||
wget = which("wget")
|
||
super("#{wget} --timeout=10 --tries=3 --no-check-certificate -nv -c \"#{escape_for_shell(src.to_s)}\" -O \"#{escape_for_shell(dst.to_s)}\"")
|
||
end
|
||
|
||
def start
|
||
lock = Proxy::FileLock.try_locking(@dst)
|
||
if lock.nil?
|
||
return false
|
||
else
|
||
super { Proxy::FileLock.unlock(lock) }
|
||
end
|
||
end
|
||
end
|
||
end
|
lib/proxy/http_downloads.rb | ||
---|---|---|
require 'proxy/file_lock'
|
||
|
||
module Proxy
|
||
class HttpDownloads
|
||
class << self
|
||
def start_download(src, dst)
|
||
lock = Proxy::FileLock.try_locking(dst)
|
||
unless lock.nil?
|
||
HttpDownload.new(src, dst, lock)
|
||
return true
|
||
end
|
||
false
|
||
end
|
||
end
|
||
end
|
||
|
||
class HttpDownload < Proxy::Util::CommandTask
|
||
include Util
|
||
|
||
def initialize(src, dst, lock)
|
||
super(command(src, dst)) { Proxy::FileLock.unlock(lock) }
|
||
end
|
||
|
||
def command(src, dst)
|
||
wget = which("wget")
|
||
"#{wget} --timeout=10 --tries=3 --no-check-certificate -nv -c \"#{escape_for_shell(src.to_s)}\" -O \"#{escape_for_shell(dst.to_s)}\""
|
||
end
|
||
end
|
||
end
|
lib/proxy/util.rb | ||
---|---|---|
module Proxy::Util
|
||
class CommandTask
|
||
include Proxy::Log
|
||
attr_reader :command
|
||
|
||
# create new task and spawn new thread logging all the cmd
|
||
# output to the proxy log. only the process' output is connected
|
||
# stderr is redirected to proxy error log, stdout to proxy debug log
|
||
def initialize(acommand, &blk)
|
||
def initialize(acommand)
|
||
@command = acommand
|
||
end
|
||
|
||
def start(&ensured_block)
|
||
# run the task in its own thread
|
||
logger.debug "Starting task: #{acommand}"
|
||
@task = Thread.new(acommand) do |cmd|
|
||
logger.debug "Starting task: #{@command}"
|
||
@task = Thread.new(@command) do |cmd|
|
||
begin
|
||
status = nil
|
||
Open3::popen3(cmd) do |stdin,stdout,stderr,thr|
|
||
... | ... | |
yield if block_given?
|
||
end
|
||
end
|
||
self
|
||
end
|
||
|
||
# wait for the task to finish and get the subprocess return code
|
lib/smart_proxy.rb | ||
---|---|---|
require 'proxy/settings/plugin'
|
||
require 'proxy/settings/global'
|
||
require 'proxy/util'
|
||
require 'proxy/http_downloads'
|
||
require 'proxy/http_download'
|
||
require 'proxy/helpers'
|
||
require 'proxy/pluggable'
|
||
require 'proxy/plugin'
|
lib/smart_proxy_for_testing.rb | ||
---|---|---|
require 'proxy/settings/plugin'
|
||
require 'proxy/settings/global'
|
||
require 'proxy/util'
|
||
require 'proxy/http_downloads'
|
||
require 'proxy/http_download'
|
||
require 'proxy/helpers'
|
||
require 'proxy/pluggable'
|
||
require 'proxy/plugin'
|
modules/tftp/server.rb | ||
---|---|---|
# as the dst might contain another sub directory
|
||
FileUtils.mkdir_p destination.parent
|
||
|
||
::Proxy::HttpDownloads.start_download(src.to_s, destination.to_s)
|
||
::Proxy::HttpDownload.new(src.to_s, destination.to_s).start
|
||
end
|
||
|
||
def self.boot_filename(dst, src)
|
test/http_download_test.rb | ||
---|---|---|
require 'test_helper'
|
||
require 'tmpdir'
|
||
|
||
class HttpDownloadsTest < Test::Unit::TestCase
|
||
def tmp(name)
|
||
File.join(Dir.tmpdir(), "http-download-#{name}.tmp")
|
||
end
|
||
|
||
def test_should_construct_escaped_wget_command
|
||
expected = "/wget --timeout=10 --tries=3 --no-check-certificate -nv -c \"src\" -O \"dst\""
|
||
Proxy::HttpDownload.any_instance.stubs(:which).returns('/wget')
|
||
assert_equal expected, Proxy::HttpDownload.new('src', 'dst').command
|
||
end
|
||
|
||
def test_should_skip_download_if_one_is_in_progress
|
||
locked = Proxy::FileLock.try_locking(tmp('other'))
|
||
assert_equal false, Proxy::HttpDownload.new('src', locked.path).start
|
||
ensure
|
||
File.delete(locked.path)
|
||
end
|
||
end
|
test/http_downloads_test.rb | ||
---|---|---|
require 'test_helper'
|
||
|
||
class Proxy::HttpDownload
|
||
def command(src, dst)
|
||
"echo test"
|
||
end
|
||
end
|
||
|
||
class HttpDownloadsTest < Test::Unit::TestCase
|
||
def test_should_download_a_file
|
||
assert Proxy::HttpDownloads.start_download 'source', 'destination'
|
||
assert File.exist?('destination')
|
||
ensure
|
||
File.delete('destination')
|
||
end
|
||
|
||
def test_should_skip_download_if_one_is_in_progress
|
||
locked = Proxy::FileLock.try_locking("another_destination")
|
||
assert !(Proxy::HttpDownloads.start_download 'another_source', 'another_destination')
|
||
ensure
|
||
File.delete(locked.path)
|
||
end
|
||
end
|
test/tftp/tftp_test.rb | ||
---|---|---|
end
|
||
|
||
def test_paths_inside_tftp_directory_dont_raise_errors
|
||
::Proxy::HttpDownloads.stubs(:start_download).returns(true)
|
||
::Proxy::HttpDownload.any_instance.stubs(:start).returns(true)
|
||
FileUtils.stubs(:mkdir_p).returns(true)
|
||
|
||
assert Proxy::TFTP.send(:fetch_boot_file,'/some/root/boot/file','http://localhost/file')
|
||
end
|
||
|
||
def test_paths_outside_tftp_directory_raise_errors
|
||
::Proxy::HttpDownloads.stubs(:start_download).returns(true)
|
||
::Proxy::HttpDownload.any_instance.stubs(:start).returns(true)
|
||
FileUtils.stubs(:mkdir_p).returns(true)
|
||
|
||
assert_raises RuntimeError do
|
test/util_test.rb | ||
---|---|---|
end
|
||
|
||
def test_commandtask_with_exit_0
|
||
t = Proxy::Util::CommandTask.new('true')
|
||
t = Proxy::Util::CommandTask.new('true').start
|
||
assert_equal t.join, 0
|
||
end
|
||
|
||
def test_commandtask_with_exit_1
|
||
t = Proxy::Util::CommandTask.new('false')
|
||
t = Proxy::Util::CommandTask.new('false').start
|
||
# In Ruby 1.8, the return code is always 0
|
||
assert_equal t.join, RUBY_VERSION =~ /^1\.8/ ? 0 : 1
|
||
end
|
Also available in: Unified diff
Fixes #11318 - HttpDownloads.start_download returns instance
instead of `true`. Allows to join processes, useful for discovery image
plugin.