Module: VagrantPlugins::Ventriloquist::Cap::Linux::Download

Defined in:
lib/ventriloquist/cap/utils/linux/download.rb

Class Method Summary collapse

Class Method Details

.download(machine, srcs, destination) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/ventriloquist/cap/utils/linux/download.rb', line 6

def self.download(machine, srcs, destination)
  machine.communicate.tap do |comm|
    if comm.test('which wget')
      download_with_wget(machine, srcs, destination)

    elsif comm.test('which curl')
      download_with_curl(machine, srcs, destination)

    # If neither wget or curl could be found, try installing
    # curl
    elsif machine.capability?(:install_packages)
      machine.guest.capability(:install_packages, 'curl')
      download_with_curl(machine, srcs, destination)

    else
      raise 'Unable to download file for guest VM!'
    end
  end
end

.download_with_curl(machine, srcs, destination) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/ventriloquist/cap/utils/linux/download.rb', line 38

def self.download_with_curl(machine, srcs, destination)
  Array(srcs).each_with_index do |src, idx|
    machine.env.ui.info("Attempting to download '#{src}' to '#{destination}'")
    begin
      machine.communicate.execute("curl #{src} -o #{destination} || { rm -f #{destination} && exit 1; }")
      break
    rescue Vagrant::Errors::VagrantError => e
      raise if idx + 1 == srcs.size
    end
  end
end

.download_with_wget(machine, srcs, destination) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/ventriloquist/cap/utils/linux/download.rb', line 26

def self.download_with_wget(machine, srcs, destination)
  Array(srcs).each_with_index do |src, idx|
    machine.env.ui.info("Attempting to download '#{src}' to '#{destination}'")
    begin
      machine.communicate.execute("wget #{src} -O #{destination} || { rm -f #{destination} && exit 1; }")
      break
    rescue Vagrant::Errors::VagrantError => e
      raise if idx + 1 == srcs.size
    end
  end
end