Module: Bosh::Director::CloudcheckHelper

Included in:
ProblemHandlers::Base
Defined in:
lib/bosh/director/cloudcheck_helper.rb

Constant Summary collapse

DEFAULT_AGENT_TIMEOUT =

This timeout has been made pretty short mainly to avoid long cloudchecks, however 10 seconds should still be pretty generous interval for agent to respond.

10

Instance Method Summary collapse

Instance Method Details

#delete_vm(instance) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/bosh/director/cloudcheck_helper.rb', line 27

def delete_vm(instance)
  # Paranoia: don't blindly delete VMs with persistent disk
  disk_list = agent_timeout_guard(instance.vm_cid, instance.credentials, instance.agent_id) { |agent| agent.list_disk }
  if disk_list.size != 0
    handler_error('VM has persistent disk attached')
  end

  vm_deleter.delete_for_instance(instance)
end

#delete_vm_from_cloud(instance_model) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/bosh/director/cloudcheck_helper.rb', line 41

def delete_vm_from_cloud(instance_model)
  @logger.debug("Deleting Vm: #{instance_model})")

  validate_spec(instance_model.spec)
  validate_env(instance_model.vm_env)

  begin
    vm_deleter = VmDeleter.new(cloud, @logger, false, Config.enable_virtual_delete_vms)
    vm_deleter.delete_for_instance(instance_model)
  rescue Bosh::Clouds::VMNotFound
    # One situation where this handler is actually useful is when
    # VM has already been deleted but something failed after that
    # and it is still referenced in DB. In that case it makes sense
    # to ignore "VM not found" errors in `delete_vm` and let the method
    # proceed creating a new VM. Other errors are not forgiven.

    @logger.warn("VM '#{instance_model.vm_cid}' might have already been deleted from the cloud")
  end
end

#delete_vm_reference(instance) ⇒ Object



37
38
39
# File 'lib/bosh/director/cloudcheck_helper.rb', line 37

def delete_vm_reference(instance)
  instance.update(vm_cid: nil, agent_id: nil, trusted_certs_sha1: nil, credentials: nil)
end

#reboot_vm(instance) ⇒ Object



16
17
18
19
20
21
22
23
24
25
# File 'lib/bosh/director/cloudcheck_helper.rb', line 16

def reboot_vm(instance)
  cloud.reboot_vm(instance.vm_cid)
  begin
    agent_client(instance.credentials, instance.agent_id).wait_until_ready
  rescue Bosh::Director::RpcTimeout
    handler_error('Agent still unresponsive after reboot')
  rescue Bosh::Director::TaskCancelled
    handler_error('Task was cancelled')
  end
end

#recreate_vm(instance_model, run_post_start = true) ⇒ Object



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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/bosh/director/cloudcheck_helper.rb', line 65

def recreate_vm(instance_model, run_post_start = true)
  @logger.debug("Recreating Vm: #{instance_model})")
  delete_vm_from_cloud(instance_model)

  existing_vm_env = instance_model.vm_env
  instance_plan_to_create = create_instance_plan(instance_model, existing_vm_env)

  vm_creator.create_for_instance_plan(
    instance_plan_to_create,
    Array(instance_model.persistent_disk_cid)
  )

  dns_manager = DnsManagerProvider.create
  dns_names_to_ip = {}

  apply_spec = instance_plan_to_create.existing_instance.spec
  apply_spec['networks'].each do |network_name, network|
    index_dns_name = dns_manager.dns_record_name(instance_model.index, instance_model.job, network_name, instance_model.deployment.name)
    dns_names_to_ip[index_dns_name] = network['ip']
    id_dns_name = dns_manager.dns_record_name(instance_model.uuid, instance_model.job, network_name, instance_model.deployment.name)
    dns_names_to_ip[id_dns_name] = network['ip']
  end

  @logger.debug("Updating DNS record for instance: #{instance_model.inspect}; to: #{dns_names_to_ip.inspect}")
  dns_manager.update_dns_record_for_instance(instance_model, dns_names_to_ip)
  dns_manager.flush_dns_cache

  cloud_check_procedure = lambda do
    cleaner = RenderedJobTemplatesCleaner.new(instance_model, App.instance.blobstores.blobstore, @logger)

    # for backwards compatibility with instances that don't have update config
    update_config = apply_spec['update'].nil? ? nil : DeploymentPlan::UpdateConfig.new(apply_spec['update'])

    InstanceUpdater::StateApplier.new(
      instance_plan_to_create,
      agent_client(instance_model.credentials, instance_model.agent_id),
      cleaner,
      @logger,
      {}
    ).apply(update_config, run_post_start)
  end
  InstanceUpdater::InstanceState.with_instance_update(instance_model, &cloud_check_procedure)
end

#recreate_vm_skip_post_start(instance_model) ⇒ Object



61
62
63
# File 'lib/bosh/director/cloudcheck_helper.rb', line 61

def recreate_vm_skip_post_start(instance_model)
  recreate_vm(instance_model, false)
end