Class: Skytap::Commands::Downloader
- Inherits:
-
Thread
- Object
- Thread
- Skytap::Commands::Downloader
- Defined in:
- lib/skytap/plugins/vm_download.rb
Constant Summary collapse
- MAX_WAIT =
2.days
- EXPORT_CHECK_PERIOD =
5
Instance Attribute Summary collapse
-
#bytes_total ⇒ Object
readonly
Returns the value of attribute bytes_total.
-
#bytes_transferred ⇒ Object
readonly
Returns the value of attribute bytes_transferred.
-
#job ⇒ Object
readonly
Returns the value of attribute job.
-
#result ⇒ Object
readonly
Returns the value of attribute result.
Instance Method Summary collapse
- #download_data ⇒ Object
- #finished? ⇒ Boolean
- #ftp_download ⇒ Object
- #id ⇒ Object
-
#initialize(job) ⇒ Downloader
constructor
A new instance of Downloader.
- #run ⇒ Object
- #status ⇒ Object
- #status_line ⇒ Object
- #success? ⇒ Boolean
- #wait_until_ready ⇒ Object
Constructor Details
#initialize(job) ⇒ Downloader
Returns a new instance of Downloader.
247 248 249 250 251 252 253 254 255 256 257 258 |
# File 'lib/skytap/plugins/vm_download.rb', line 247 def initialize(job) @job = job @bytes_transferred = @bytes_total = 0 super do begin run rescue Exception => ex @result = Response.build(ex) end end end |
Instance Attribute Details
#bytes_total ⇒ Object (readonly)
Returns the value of attribute bytes_total.
244 245 246 |
# File 'lib/skytap/plugins/vm_download.rb', line 244 def bytes_total @bytes_total end |
#bytes_transferred ⇒ Object (readonly)
Returns the value of attribute bytes_transferred.
244 245 246 |
# File 'lib/skytap/plugins/vm_download.rb', line 244 def bytes_transferred @bytes_transferred end |
#job ⇒ Object (readonly)
Returns the value of attribute job.
244 245 246 |
# File 'lib/skytap/plugins/vm_download.rb', line 244 def job @job end |
#result ⇒ Object (readonly)
Returns the value of attribute result.
244 245 246 |
# File 'lib/skytap/plugins/vm_download.rb', line 244 def result @result end |
Instance Method Details
#download_data ⇒ Object
317 318 319 320 321 322 323 324 325 326 327 |
# File 'lib/skytap/plugins/vm_download.rb', line 317 def download_data vm = Skytap.invoke!(username, api_token, "vm show #{vm_id}") template_id = export['template_url'] =~ /templates\/(\d+)/ && $1 template = Skytap.invoke!(username, api_token, "template show #{template_id}") exportable_vm = ExportableVm.new(vm, template) File.open(File.join(export_dir, 'vm.yaml'), 'w') do |f| f << YAML.dump(exportable_vm.data) end end |
#finished? ⇒ Boolean
268 269 270 |
# File 'lib/skytap/plugins/vm_download.rb', line 268 def finished? !!@finished end |
#ftp_download ⇒ Object
302 303 304 305 306 307 308 309 310 311 312 313 314 315 |
# File 'lib/skytap/plugins/vm_download.rb', line 302 def ftp_download remote_path = export['filename'] local_path = File.join(export_dir, File.basename(export['filename'])) FileUtils.mkdir_p(export_dir) ftp = Net::FTP.new(export['ftp_host']) ftp.login(export['ftp_user_name'], export['ftp_password']) ftp.chdir(File.dirname(remote_path)) @bytes_total = ftp.size(File.basename(remote_path)) ftp.getbinaryfile(File.basename(remote_path), local_path) do |data| @bytes_transferred += data.size end ftp.close end |
#id ⇒ Object
329 330 331 |
# File 'lib/skytap/plugins/vm_download.rb', line 329 def id export['id'] end |
#run ⇒ Object
260 261 262 263 264 265 266 |
# File 'lib/skytap/plugins/vm_download.rb', line 260 def run wait_until_ready ftp_download download_data Skytap.invoke!(username, api_token, "export destroy #{id}") @result = Response.build(export_dir) end |
#status ⇒ Object
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 |
# File 'lib/skytap/plugins/vm_download.rb', line 285 def status if result.try(:error?) @finished = true "Error: #{result.}".color(:red).bright elsif result @finished = true "Downloaded: #{result.payload}".color(:green).bright elsif bytes_transferred == 0 'Exporting'.color(:yellow) else gb_transferred = bytes_transferred / 1.gigabyte.to_f gb_total = bytes_total / 1.gigabyte.to_f percent_done = 100.0 * bytes_transferred / bytes_total "Downloading #{'%0.1f' % percent_done}% (#{'%0.1f' % gb_transferred} / #{'%0.1f' % gb_total} GB)".color(:yellow) end end |
#status_line ⇒ Object
276 277 278 279 280 281 282 283 |
# File 'lib/skytap/plugins/vm_download.rb', line 276 def status_line prefix = "VM #{vm_id}".tap do |str| if vm str << " (#{vm['name']})" end end prefix << ': ' << status end |
#success? ⇒ Boolean
272 273 274 |
# File 'lib/skytap/plugins/vm_download.rb', line 272 def success? result && !result.error? end |
#wait_until_ready ⇒ Object
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 |
# File 'lib/skytap/plugins/vm_download.rb', line 333 def wait_until_ready cutoff = MAX_WAIT.from_now finished = nil while Time.now < cutoff case export(true)['status'] when 'processing' when 'complete' finished = true break else raise Skytap::Error.new "Export job had unexpected state of #{export['status'].inspect}" end sleep EXPORT_CHECK_PERIOD end unless finished raise Skytap::Error.new 'Timed out waiting for export job to complete' end end |