Class: Bosh::Director::VmCreator

Inherits:
Object
  • Object
show all
Includes:
EncryptionHelper
Defined in:
lib/bosh/director/vm_creator.rb

Overview

Creates VM model and call out to CPI to create VM in IaaS

Class Method Summary collapse

Instance Method Summary collapse

Methods included from EncryptionHelper

#generate_agent_credentials

Constructor Details

#initializeVmCreator

Returns a new instance of VmCreator.



12
13
14
15
# File 'lib/bosh/director/vm_creator.rb', line 12

def initialize
  @cloud = Config.cloud
  @logger = Config.logger
end

Class Method Details

.create(*args) ⇒ Object



8
9
10
# File 'lib/bosh/director/vm_creator.rb', line 8

def self.create(*args)
  new.create(*args)
end

.generate_agent_idObject



68
69
70
# File 'lib/bosh/director/vm_creator.rb', line 68

def self.generate_agent_id
  SecureRandom.uuid
end

Instance Method Details

#create(deployment, stemcell, cloud_properties, network_settings, disks = nil, env = {}) ⇒ Object



17
18
19
20
21
22
23
24
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
# File 'lib/bosh/director/vm_creator.rb', line 17

def create(deployment, stemcell, cloud_properties, network_settings,
           disks=nil, env={})
  vm = nil
  vm_cid = nil

  env = Bosh::Common::DeepCopy.copy(env)

  agent_id = self.class.generate_agent_id

  options = {
      :deployment => deployment,
      :agent_id => agent_id,
      :env => env
  }

  if Config.encryption?
    credentials = generate_agent_credentials
    env['bosh'] ||= {}
    env['bosh']['credentials'] = credentials
    options[:credentials] = credentials
  end

  count = 0
  begin
    vm_cid = @cloud.create_vm(agent_id, stemcell.cid, cloud_properties, network_settings, disks, env)
  rescue Bosh::Clouds::VMCreationFailed => e
    count += 1
    logger.error("failed to create VM, retrying (#{count})")
    retry if e.ok_to_retry && count < Config.max_vm_create_tries
    raise e
  end

  options[:cid] = vm_cid
  vm = Models::Vm.new(options)

  vm.save
  VmMetadataUpdater.build.update(vm, {})
  vm
rescue => e
  logger.error("error creating vm: #{e.message}")
  delete_vm(vm_cid) if vm_cid
  vm.destroy if vm
  raise e
end

#delete_vm(vm_cid) ⇒ Object



62
63
64
65
66
# File 'lib/bosh/director/vm_creator.rb', line 62

def delete_vm(vm_cid)
  @cloud.delete_vm(vm_cid)
rescue => e
  logger.error("error cleaning up #{vm_cid}: #{e.message}\n#{e.backtrace.join("\n")}")
end

#loggerObject



72
73
74
# File 'lib/bosh/director/vm_creator.rb', line 72

def logger
  Config.logger
end