Class: Vanagon::Component::Source::Http

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

Parameters:

  • url (String)

    url of the http source to fetch

  • sum (String)

    sum to verify the download against

  • workdir (String)

    working directory to download into

Raises:

  • (RuntimeError)

    an exception is raised is sum is nil



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

#cleanupString

Return the correct incantation to cleanup the source archive and source directory for a given source

Returns:

  • (String)

    command to cleanup the source

Raises:

  • (RuntimeError)

    an exception is raised if there is no known extraction method for @extension



108
109
110
# File 'lib/vanagon/component/source/http.rb', line 108

def cleanup
  @cleanup
end

#extensionObject

Returns the value of attribute extension.



10
11
12
# File 'lib/vanagon/component/source/http.rb', line 10

def extension
  @extension
end

#fileObject

Returns the value of attribute file.



10
11
12
# File 'lib/vanagon/component/source/http.rb', line 10

def file
  @file
end

#sumObject

Returns the value of attribute sum.



10
11
12
# File 'lib/vanagon/component/source/http.rb', line 10

def sum
  @sum
end

#urlObject

Returns the value of attribute url.



10
11
12
# File 'lib/vanagon/component/source/http.rb', line 10

def url
  @url
end

#workdirObject

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

#dirnameString

The dirname to reference when building from the source

Returns:

  • (String)

    the directory that should be traversed into to build this source

Raises:

  • (RuntimeError)

    if the @extension for the @file isn’t currently handled by the method



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

Parameters:

  • target_url (String, URI, Addressable::URI)

    url of an http source to retrieve with GET

Raises:

  • (RuntimeError, Vanagon::Error)

    an exception is raised if the URI scheme cannot be handled



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)

Parameters:

  • tar (String)

    the tar command to use

Returns:

  • (String, nil)

    command to extract the source

Raises:

  • (RuntimeError)

    an exception is raised if there is no known extraction method for @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

#fetchObject

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_extensionString

Returns the extension for @file

Returns:

  • (String)

    the extension of @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

#verifyObject

Verify the downloaded file matches the provided sum

Raises:

  • (RuntimeError)

    an exception is raised if the sum does not match the sum of the file



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