Class: VagrantPlugins::Save::Uploader

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-save/uploader.rb

Instance Method Summary collapse

Constructor Details

#initialize(env, logger) ⇒ Uploader

Returns a new instance of Uploader.

Parameters:

  • env (Vagrant::Environment)
  • logger (Log4r::Logger)


16
17
18
19
# File 'lib/vagrant-save/uploader.rb', line 16

def initialize(env, logger)
  @env = env
  @logger = logger
end

Instance Method Details

#clean(machine, keep) ⇒ Object

Returns int.

Parameters:

  • machine (Vagrant::Machine)
  • keep (int)

Returns:

  • int



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/vagrant-save/uploader.rb', line 117

def clean(machine, keep)

  client = HTTPClient.new

  client.connect_timeout = 10000
  client.send_timeout    = 10000
  client.receive_timeout = 10000

  data_url = make_url(machine)

  @logger.debug("Load versions from #{data_url}")

  res = client.get(data_url)

  @logger.debug("Got response #{res.inspect}")

  data = JSON.parse(res.body)

  @logger.debug("Traverse versions in #{data.inspect}")

  saved_versions = data['versions'].map{ |v|
    @logger.debug("Received version #{v.inspect}")
    v['version']
  }

  @logger.debug("Got #{saved_versions.length} versions: #{saved_versions.inspect}")

  if saved_versions.length > keep
    machine.ui.info('Cleaning up old versions')

    saved_versions.sort.reverse.slice(keep, saved_versions.length).each { |v|
      delete_url = data_url + '/' + v

      machine.ui.info("Deleting version #{v}")
      @logger.debug("Sending delete #{delete_url}")

      client.delete(delete_url)
    }
  end

  0
end

#format_bytes(num) ⇒ Object



105
106
107
108
109
110
111
112
# File 'lib/vagrant-save/uploader.rb', line 105

def format_bytes(num)
  units = ["Byte", "KB", "MB", "GB", "TB"]
  index = (Math.log(num) / Math.log(2)).to_i / 10
  bytes = num / ( 1024 ** index )
  bytes = bytes.round(2).to_s.gsub(/\.0+$/, "")

  "#{bytes} #{units[index]}"
end

#make_url(machine) ⇒ Object

Returns string.

Parameters:

  • machine (Vagrant::Machine)

Returns:

  • string

Raises:

  • (Vagrant::Errors::BoxServerNotSet)


162
163
164
165
166
167
168
169
# File 'lib/vagrant-save/uploader.rb', line 162

def make_url(machine)
  name = machine.box.name.gsub(/_+/, '/')
  base_url = Vagrant.server_url(machine.config.vm.box_server_url).to_s

  raise Vagrant::Errors::BoxServerNotSet unless base_url

  base_url + '/' + name
end

#send(machine, file, version) ⇒ Object

Returns int.

Parameters:

  • machine (Vagrant::Machine)
  • file (string)
  • version (string)

Returns:

  • int

Raises:



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/vagrant-save/uploader.rb', line 25

def send(machine, file, version)

  machine.ui.info('Uploading now')

  @logger.debug("Preparing to send file #{file}")

  provider = machine.provider_name.to_s

  if provider =~ /vmware/
    provider = 'vmware_desktop'
  end

  ping_url = make_url(machine)
  post_url = ping_url + '/' + version + '/' + provider

  @logger.debug("Pinging #{ping_url}")

  client = HTTPClient.new

  client.connect_timeout = 10000
  client.send_timeout    = 10000
  client.receive_timeout = 10000

  res = client.options(ping_url)

  raise VagrantPlugins::Save::Errors::CannotContactBoxServer unless res.http_header.status_code == 200

  @logger.debug("Sending file to #{post_url}")

  @env.ui.info('Uploading', new_line: false)

  File.open(file) do |f|

    uri       = URI.parse(post_url)
    full_size = f.size
    full      = format_bytes(full_size.to_f)

    http = Net::HTTP.new(uri.host, uri.port)

    http.open_timeout = 10000
    http.read_timeout = 10000

    req = Net::HTTP::Post.new(uri.path)
    req.set_form({"box" => f}, 'multipart/form-data')

    previous_info = ""
    previous_out  = 0.0

    Net::HTTP::UploadProgress.new(req) do |progress|
       if progress.upload_size.to_f >= full_size.to_f
        info = "Upload complete, commiting new box"
      else
        frac    = progress.upload_size.to_f / full_size.to_f
        percent = (frac * 100).round.to_s + "%"
        part    = format_bytes(progress.upload_size.to_f)
        info = "#{percent} (#{part} / #{full})"
      end

      t = Time.now.to_f

      if info != previous_info && t - previous_out > 0.7
        previous_out = t
        previous_info = info

        @env.ui.clear_line
        @env.ui.info(info, new_line: false)
      end
    end
    res = http.request(req)
  end

  @env.ui.clear_line

  raise VagrantPlugins::Save::Errors::UploadFailed unless res.code == '200'

  machine.ui.info('Upload successful')

  provider
end