Class: Vanagon::Component::Source::Http
- Inherits:
-
Object
- Object
- Vanagon::Component::Source::Http
- Includes:
- Utilities
- Defined in:
- lib/vanagon/component/source/http.rb
Constant Summary collapse
- ARCHIVE_EXTENSIONS =
Extensions for files we intend to unpack during the build
['.tar.gz', '.tgz', '.zip'].freeze
Instance Attribute Summary collapse
-
#cleanup ⇒ String
Return the correct incantation to cleanup the source archive and source directory for a given source.
-
#extension ⇒ Object
Returns the value of attribute extension.
-
#file ⇒ Object
Returns the value of attribute file.
-
#sum ⇒ Object
Returns the value of attribute sum.
-
#url ⇒ Object
Returns the value of attribute url.
-
#workdir ⇒ Object
Returns the value of attribute workdir.
Instance Method Summary collapse
-
#dirname ⇒ String
The dirname to reference when building from the source.
-
#download(target_url, target_file = nil) ⇒ Object
Downloads the file from @url into the @workdir.
-
#extract(tar) ⇒ String?
Gets the command to extract the archive given if needed (uses @extension).
-
#fetch ⇒ Object
Download the source from the url specified.
-
#get_extension ⇒ String
Returns the extension for @file.
-
#initialize(url, sum, workdir) ⇒ Http
constructor
Constructor for the Http source type.
-
#verify ⇒ Object
Verify the downloaded file matches the provided sum.
Methods included from Utilities
#erb_file, #erb_string, #ex, #find_program_on_path, #get_md5sum, #get_sum, #git, #git_version, #http_request, #is_git_repo?, #local_command, #remote_ssh_command, #retry_with_timeout, #rsync_from, #rsync_to, #ssh_command
Constructor Details
#initialize(url, sum, workdir) ⇒ Http
Constructor for the Http source type
21 22 23 24 25 26 27 28 |
# File 'lib/vanagon/component/source/http.rb', line 21 def initialize(url, sum, workdir) unless sum fail "sum is required to validate the http source" end @url = url @sum = sum @workdir = workdir end |
Instance Attribute Details
#cleanup ⇒ String
Return the correct incantation to cleanup the source archive and source directory for a given source
108 109 110 |
# File 'lib/vanagon/component/source/http.rb', line 108 def cleanup @cleanup end |
#extension ⇒ Object
Returns the value of attribute extension.
10 11 12 |
# File 'lib/vanagon/component/source/http.rb', line 10 def extension @extension end |
#file ⇒ Object
Returns the value of attribute file.
10 11 12 |
# File 'lib/vanagon/component/source/http.rb', line 10 def file @file end |
#sum ⇒ Object
Returns the value of attribute sum.
10 11 12 |
# File 'lib/vanagon/component/source/http.rb', line 10 def sum @sum end |
#url ⇒ Object
Returns the value of attribute url.
10 11 12 |
# File 'lib/vanagon/component/source/http.rb', line 10 def url @url end |
#workdir ⇒ Object
Returns the value of attribute workdir.
10 11 12 |
# File 'lib/vanagon/component/source/http.rb', line 10 def workdir @workdir end |
Instance Method Details
#dirname ⇒ String
The dirname to reference when building from the source
137 138 139 140 141 142 143 |
# File 'lib/vanagon/component/source/http.rb', line 137 def dirname if ARCHIVE_EXTENSIONS.include?(@extension) return @file.chomp(@extension) else return './' end end |
#download(target_url, target_file = nil) ⇒ Object
Downloads the file from @url into the @workdir
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/vanagon/component/source/http.rb', line 51 def download(target_url, target_file = nil) # rubocop:disable Metrics/AbcSize uri = URI.parse(target_url.to_s) target_file ||= File.basename(uri.path) puts "Downloading file '#{target_file}' from url '#{target_url}'" Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http| http.request(Net::HTTP::Get.new(uri)) do |response| case response when Net::HTTPRedirection # By parsing the location header, we get either an absolute # URI or a URI with a relative `path`. Adding it to `uri` # should correctly update the relative `path` or overwrite # the entire URI if it's absolute. location = URI.parse(response.header['location']) download(uri + location, target_file) when Net::HTTPSuccess open(File.join(@workdir, target_file), 'w') do |io| response.read_body { |chunk| io.write(chunk) } end else fail "Error: #{response.code.to_s}. Unable to get source from #{target_url}" end end end target_file rescue Errno::ETIMEDOUT, Timeout::Error, Errno::EINVAL, Errno::EACCES, Errno::ECONNRESET, EOFError, Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError => e raise Vanagon::Error.wrap(e, "Problem downloading #{target_file} from '#{@url}'. Please verify you have the correct uri specified.") end |
#extract(tar) ⇒ String?
Gets the command to extract the archive given if needed (uses @extension)
90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/vanagon/component/source/http.rb', line 90 def extract(tar) if ARCHIVE_EXTENSIONS.include?(@extension) case @extension when ".tar.gz", ".tgz" return "gunzip -c '#{@file}' | '#{tar}' xf -" when ".zip" return "unzip '#{@file}' || 7za x -r -tzip -o'#{File.basename(@file, '.zip')}' '#{@file}'" end else # Extension does not appear to be an archive return ':' end end |
#fetch ⇒ Object
Download the source from the url specified. Sets the full path to the file as @file and the @extension for the file as a side effect.
32 33 34 35 |
# File 'lib/vanagon/component/source/http.rb', line 32 def fetch @file = download(@url) @extension = get_extension end |
#get_extension ⇒ String
Returns the extension for @file
120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/vanagon/component/source/http.rb', line 120 def get_extension extension_match = @file.match(/.*(#{Regexp.union(ARCHIVE_EXTENSIONS)})/) unless extension_match if @file.split('.').last.include?('.') return '.' + @file.split('.').last else # This is the case where the file has no extension return @file end end extension_match[1] end |
#verify ⇒ Object
Verify the downloaded file matches the provided sum
40 41 42 43 44 45 46 |
# File 'lib/vanagon/component/source/http.rb', line 40 def verify puts "Verifying file: #{@file} against sum: '#{@sum}'" actual = get_md5sum(File.join(@workdir, @file)) unless @sum == actual fail "Unable to verify '#{@file}'. Expected: '#{@sum}', got: '#{actual}'" end end |