Class: Skytap::Commands::Uploader

Inherits:
Thread
  • Object
show all
Defined in:
lib/skytap/plugins/vm_upload.rb

Constant Summary collapse

MAX_WAIT =
2.days
IMPORT_CHECK_PERIOD =
5

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(job) ⇒ Uploader

Returns a new instance of Uploader.



285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
# File 'lib/skytap/plugins/vm_upload.rb', line 285

def initialize(job)
  @job = job
  @bytes_transferred = @bytes_total = 0
  path, basename = File.split(File.expand_path(import_path))
  @vm_filename = File.join(File.basename(path), basename) # E.g., "myfolder/vm.7z"
  @vm_filename << " (#{params['template_name']})" if params['template_name']

  super do
    begin
      run
    rescue Exception => ex
      @result = Response.build(ex)
    end
  end
end

Instance Attribute Details

#bytes_totalObject (readonly)

Returns the value of attribute bytes_total.



282
283
284
# File 'lib/skytap/plugins/vm_upload.rb', line 282

def bytes_total
  @bytes_total
end

#bytes_transferredObject (readonly)

Returns the value of attribute bytes_transferred.



282
283
284
# File 'lib/skytap/plugins/vm_upload.rb', line 282

def bytes_transferred
  @bytes_transferred
end

#jobObject (readonly)

Returns the value of attribute job.



282
283
284
# File 'lib/skytap/plugins/vm_upload.rb', line 282

def job
  @job
end

#resultObject (readonly)

Returns the value of attribute result.



282
283
284
# File 'lib/skytap/plugins/vm_upload.rb', line 282

def result
  @result
end

Instance Method Details

#finished?Boolean

Returns:

  • (Boolean)


311
312
313
# File 'lib/skytap/plugins/vm_upload.rb', line 311

def finished?
  !!@finished
end

#runObject



301
302
303
304
305
306
307
308
# File 'lib/skytap/plugins/vm_upload.rb', line 301

def run
  ftp_upload
  Skytap.invoke!(username, api_token, "import update #{id}", {}, :param => {'status' => 'processing'})
  wait_until_ready
  add_other_credentials
  Skytap.invoke!(username, api_token, "import destroy #{id}")
  @result = Response.build(import['template_url'])
end

#statusObject



323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
# File 'lib/skytap/plugins/vm_upload.rb', line 323

def status
  if result.try(:error?)
    @finished = true
    "Error: #{result.error_message}".color(:red).bright
  elsif result
    @finished = true
    "Uploaded to: #{result.payload}".color(:green).bright
  elsif bytes_transferred == 0
    'Starting'.color(:yellow)
  elsif bytes_transferred >= bytes_total
    'Importing'.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
    "Uploading #{'%0.1f' % percent_done}% (#{'%0.1f' % gb_transferred} / #{'%0.1f' % gb_total} GB)".color(:yellow)
  end
end

#status_lineObject



319
320
321
# File 'lib/skytap/plugins/vm_upload.rb', line 319

def status_line
  "#{@vm_filename}: #{status}"
end

#success?Boolean

Returns:

  • (Boolean)


315
316
317
# File 'lib/skytap/plugins/vm_upload.rb', line 315

def success?
  result && !result.error?
end