Class: Skytap::Commands::VmImportJob

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logger, username, api_token, vm, params = {}) ⇒ VmImportJob

Returns a new instance of VmImportJob.



188
189
190
191
192
193
194
195
196
197
# File 'lib/skytap/plugins/vm_upload.rb', line 188

def initialize(logger, username, api_token, vm, params = {})
  @logger = logger
  @username = username
  @api_token = api_token
  @vm = File.expand_path(vm)
  @params = params
  @vm_filename = File.basename(@vm)

  create_on_server
end

Instance Attribute Details

#api_tokenObject (readonly)

Returns the value of attribute api_token.



186
187
188
# File 'lib/skytap/plugins/vm_upload.rb', line 186

def api_token
  @api_token
end

#import_pathObject (readonly)

Returns the value of attribute import_path.



186
187
188
# File 'lib/skytap/plugins/vm_upload.rb', line 186

def import_path
  @import_path
end

#loggerObject (readonly)

Returns the value of attribute logger.



186
187
188
# File 'lib/skytap/plugins/vm_upload.rb', line 186

def logger
  @logger
end

#other_credentialsObject (readonly)

Returns the value of attribute other_credentials.



186
187
188
# File 'lib/skytap/plugins/vm_upload.rb', line 186

def other_credentials
  @other_credentials
end

#paramsObject (readonly)

Returns the value of attribute params.



186
187
188
# File 'lib/skytap/plugins/vm_upload.rb', line 186

def params
  @params
end

#usernameObject (readonly)

Returns the value of attribute username.



186
187
188
# File 'lib/skytap/plugins/vm_upload.rb', line 186

def username
  @username
end

#vmObject (readonly)

Returns the value of attribute vm.



186
187
188
# File 'lib/skytap/plugins/vm_upload.rb', line 186

def vm
  @vm
end

Instance Method Details

#at_capacity?(exception) ⇒ Boolean

Returns:

  • (Boolean)


212
213
214
# File 'lib/skytap/plugins/vm_upload.rb', line 212

def at_capacity?(exception)
  exception.message.include?('You cannot import a VM because you may not have more than')
end

#create_on_serverObject



199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/skytap/plugins/vm_upload.rb', line 199

def create_on_server
  setup
  begin
    import
  rescue Exception => ex
    if at_capacity?(ex)
      raise NoSlotsAvailable.new
    else
      raise
    end
  end
end

#import(force_reload = false) ⇒ Object



243
244
245
246
247
248
249
250
251
252
# File 'lib/skytap/plugins/vm_upload.rb', line 243

def import(force_reload=false)
  return @import unless @import.nil? || force_reload

  if @import
    id = @import['id']
    @import = Skytap.invoke!(username, api_token, "import show #{id}")
  else
    @import = Skytap.invoke!(username, api_token, 'import create', {}, :param => params)
  end
end

#setupObject



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/skytap/plugins/vm_upload.rb', line 216

def setup
  if File.directory?(vm)
    @import_path = File.join(vm, 'vm.7z')
    unless File.exist?(@import_path)
      raise Skytap::Error.new("Directory provided (#{vm}) but no vm.7z file found inside")
    end

     = File.join(vm, 'vm.yaml')
    if File.exist?()
      #TODO:NLA If hostname is present, truncate it to 15 chars. This is the max import hostname length.
      extra_params = YAML.load_file()
    end

  elsif File.exist?(vm)
    @import_path = vm
  else
    raise Skytap::Error.new("File does not exist: #{vm}")
  end

  @params = (extra_params || {}).merge(params)
  if @params['credentials'].is_a?(Array)
    cred = @params['credentials'].shift
    @other_credentials = @params['credentials']
    @params['credentials'] = cred
  end
end