Class: Bosh::Director::VmCreator

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

Overview

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

Constant Summary

Constants included from PasswordHelper

PasswordHelper::PASSWORD_LENGTH, PasswordHelper::SALT_MAX_LENGTH_IN_BYTES

Instance Method Summary collapse

Methods included from PasswordHelper

#sha512_hashed_password

Methods included from EncryptionHelper

#generate_agent_credentials

Constructor Details

#initialize(cloud, logger, vm_deleter, disk_manager, job_renderer, arp_flusher) ⇒ VmCreator

Returns a new instance of VmCreator.



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

def initialize(cloud, logger, vm_deleter, disk_manager, job_renderer, arp_flusher)
  @cloud = cloud
  @logger = logger
  @vm_deleter = vm_deleter
  @disk_manager = disk_manager
  @job_renderer = job_renderer
  @arp_flusher = arp_flusher
end

Instance Method Details

#create_for_instance_plan(instance_plan, disks) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/bosh/director/vm_creator.rb', line 49

def create_for_instance_plan(instance_plan, disks)
  instance = instance_plan.instance
  instance_model = instance.model
  @logger.info('Creating VM')

  create(
    instance_model,
    instance.stemcell,
    instance.cloud_properties,
    instance_plan.network_settings_hash,
    disks,
    instance.env,
  )

  begin
    VmMetadataUpdater.build.update(instance_model, {})
    agent_client = AgentClient.with_vm_credentials_and_agent_id(instance_model.credentials, instance_model.agent_id)
    agent_client.wait_until_ready

    if Config.flush_arp
      ip_addresses = instance_plan.network_settings_hash.map do |index,network|
        network['ip']
      end.compact

      @arp_flusher.delete_arp_entries(instance_model.vm_cid, ip_addresses)
    end

    instance.update_trusted_certs
    instance.update_cloud_properties!
  rescue Exception => e
    @logger.error("Failed to create/contact VM #{instance_model.vm_cid}: #{e.inspect}")
    if Config.keep_unreachable_vms
      @logger.info('Keeping the VM for debugging')
    else
      @vm_deleter.delete_for_instance(instance_model)
    end
    raise e
  end

  @disk_manager.attach_disks_if_needed(instance_plan)

  apply_initial_vm_state(instance_plan)

  instance_plan.mark_desired_network_plans_as_existing
end

#create_for_instance_plans(instance_plans, ip_provider) ⇒ Object



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

def create_for_instance_plans(instance_plans, ip_provider)
  return @logger.info('No missing vms to create') if instance_plans.empty?

  total = instance_plans.size
  event_log_stage = Config.event_log.begin_stage('Creating missing vms', total)
  ThreadPool.new(max_threads: Config.max_threads, logger: @logger).wrap do |pool|
    instance_plans.each do |instance_plan|
      instance = instance_plan.instance

      pool.process do
        with_thread_name("create_missing_vm(#{instance.model}/#{total})") do
          event_log_stage.advance_and_track(instance.model.to_s) do
            @logger.info('Creating missing VM')
            disks = [instance.model.persistent_disk_cid].compact
            create_for_instance_plan(instance_plan, disks)

            instance_plan.network_plans
              .select(&:obsolete?)
              .each do |network_plan|
              reservation = network_plan.reservation
              ip_provider.release(reservation)
            end
            instance_plan.release_obsolete_network_plans
          end
        end
      end
    end
  end
end