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

Constant Summary collapse

MAX_CREATE_VM_TRIES =

this is used to retry VM creation on clouds that are unreliable (e.g. AWS)

5

Class Method Summary collapse

Instance Method Summary collapse

Methods included from EncryptionHelper

#generate_agent_credentials

Constructor Details

#initializeVmCreator

Returns a new instance of VmCreator.



15
16
17
18
# File 'lib/bosh/director/vm_creator.rb', line 15

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

Class Method Details

.create(*args) ⇒ Object



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

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

.generate_agent_idObject



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

def self.generate_agent_id
  SecureRandom.uuid
end

Instance Method Details

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



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
61
62
63
# File 'lib/bosh/director/vm_creator.rb', line 20

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 < MAX_CREATE_VM_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



65
66
67
68
69
# File 'lib/bosh/director/vm_creator.rb', line 65

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



75
76
77
# File 'lib/bosh/director/vm_creator.rb', line 75

def logger
  Config.logger
end