Class: Babushka::Resource

Inherits:
Object show all
Extended by:
LogHelpers, PathHelpers, ShellHelpers
Defined in:
lib/babushka/resource.rb

Class Method Summary collapse

Methods included from LogHelpers

debug, deprecated!, log, log_block, log_error, log_ok, log_stderr, log_warn, removed!

Methods included from ShellHelpers

cmd_dir, current_username, log_shell, login_shell, raw_shell, shell, shell!, shell?, shell_cmd, sudo, which

Methods included from PathHelpers

cd, in_build_dir, in_download_dir

Class Method Details

.download(url, filename = url.to_s.p.basename) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/babushka/resource.rb', line 30

def self.download url, filename = url.to_s.p.basename
  if filename.p.exists? && !filename.p.empty?
    log_ok "Already downloaded #{filename}."
    filename
  elsif (result = shell(%Q{curl -I -X GET "#{url}"})).nil?
    log_error "Couldn't download #{url}: `curl` exited with non-zero status."
  else
    response_code = result.val_for(/HTTP\/1\.\d/) # not present for ftp://, etc.
    if response_code && response_code[/^[23]/].nil?
      log_error "Couldn't download #{url}: #{response_code}."
    elsif !(location = result.val_for('Location')).nil?
      log "Following redirect from #{url}"
      download URI.escape(location), location.p.basename
    else
      success = log_block "Downloading #{url}" do
        shell('curl', '-#', '-o', "#{filename}.tmp", url.to_s, :progress => /[\d\.]+%/) &&
        shell('mv', '-f', "#{filename}.tmp", filename)
      end
      filename if success
    end
  end
end

.extract(url, &block) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/babushka/resource.rb', line 22

def self.extract url, &block
  get url do |download_path|
    in_build_dir {
      Asset.for(download_path).extract(&block)
    }
  end
end

.get(url, &block) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/babushka/resource.rb', line 7

def self.get url, &block
  filename = URI.unescape(url.to_s).p.basename
  if filename.to_s.blank?
    log_error "Not a valid URL to download: #{url}"
  elsif url.to_s[%r{^git://}]
    GitHelpers.git(url, &block)
  else
    download_path = in_download_dir {|path|
      downloaded_file = download(url, filename)
      path / downloaded_file if downloaded_file
    }
    block.call download_path unless download_path.nil?
  end
end