Class: Bosh::Director::InstanceDeleter

Inherits:
Object
  • Object
show all
Includes:
DnsHelper
Defined in:
lib/bosh/director/instance_deleter.rb

Overview

Coordinates the safe deletion of an instance and all associates resources.

Constant Summary

Constants included from DnsHelper

DnsHelper::SOA, DnsHelper::TTL_4H, DnsHelper::TTL_5M

Instance Method Summary collapse

Methods included from DnsHelper

#add_default_dns_server, #canonical, #default_dns_server, #delete_dns_records, #delete_empty_domain, #dns_domain_name, #dns_ns_record, #dns_servers, #invalid_dns, #reverse_domain, #reverse_host, #update_dns_a_record, #update_dns_ptr_record

Constructor Details

#initialize(deployment_plan) ⇒ InstanceDeleter

Returns a new instance of InstanceDeleter.



6
7
8
9
10
11
# File 'lib/bosh/director/instance_deleter.rb', line 6

def initialize(deployment_plan)
  @deployment_plan = deployment_plan
  @cloud = Config.cloud
  @logger = Config.logger
  @blobstore = App.instance.blobstores.blobstore
end

Instance Method Details

#delete_dns(job, index) ⇒ void

This method returns an undefined value.

Deletes the DNS records

Parameters:

  • job (String)

    job name

  • index (Numeric)

    job index



100
101
102
103
104
105
106
# File 'lib/bosh/director/instance_deleter.rb', line 100

def delete_dns(job, index)
  if Config.dns_enabled?
    record_pattern = [index, canonical(job), "%",
                      @deployment_plan.canonical_name, dns_domain_name].join(".")
    delete_dns_records(record_pattern, @deployment_plan.dns_domain.id)
  end
end

#delete_instance(instance, event_log_stage) ⇒ void

This method returns an undefined value.

Deletes a single instance and attached persistent disks

Parameters:



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/bosh/director/instance_deleter.rb', line 29

def delete_instance(instance, event_log_stage)
  vm = instance.vm
  @logger.info("Delete unneeded instance: #{vm.cid}")

  event_log_stage.advance_and_track(vm.cid) do
    drain(vm.agent_id)
    @cloud.delete_vm(vm.cid)
    delete_snapshots(instance)
    delete_persistent_disks(instance.persistent_disks)
    delete_dns(instance.job, instance.index)

    RenderedJobTemplatesCleaner.new(instance, @blobstore).clean_all

    vm.db.transaction do
      instance.destroy
      vm.destroy
    end
  end
end

#delete_instances(instances, event_log_stage, options = {}) ⇒ void

This method returns an undefined value.

Deletes a list of instances

Parameters:

  • instances (Array<Models::Instance>)

    list of instances to delete

  • options (Hash) (defaults to: {})

    optional list of options controlling concurrency



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

def delete_instances(instances, event_log_stage, options = {})
  max_threads = options[:max_threads] || Config.max_threads
  ThreadPool.new(:max_threads => max_threads).wrap do |pool|
    instances.each do |instance|
      pool.process { delete_instance(instance, event_log_stage) }
    end
  end
end

#delete_persistent_disks(persistent_disks) ⇒ void

This method returns an undefined value.

Delete persistent disks

Parameters:

  • persistent_disks (Array<Model::PersistentDisk>)

    disks



82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/bosh/director/instance_deleter.rb', line 82

def delete_persistent_disks(persistent_disks)
  persistent_disks.each do |disk|
    @logger.info("Deleting disk: `#{disk.disk_cid}', " +
                 "#{disk.active ? "active" : "inactive"}")
    begin
      @cloud.delete_disk(disk.disk_cid)
    rescue Bosh::Clouds::DiskNotFound => e
      @logger.warn("Disk not found: #{disk.disk_cid}")
      raise if disk.active
    end
    disk.destroy
  end
end

#delete_snapshots(instance) ⇒ Object



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

def delete_snapshots(instance)
  snapshots = instance.persistent_disks.map { |disk| disk.snapshots }.flatten
  Bosh::Director::Api::SnapshotManager.delete_snapshots(snapshots)
end

#drain(agent_id) ⇒ void

This method returns an undefined value.

Drain the instance

Parameters:

  • agent_id (String)

    agent id



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/bosh/director/instance_deleter.rb', line 52

def drain(agent_id)
  agent = AgentClient.with_defaults(agent_id)

  drain_time = agent.drain("shutdown")
  while drain_time < 0
    drain_time = drain_time.abs
    begin
      Config.job_cancelled?
      @logger.info("Drain - check back in #{drain_time} seconds")
      sleep(drain_time)
      drain_time = agent.drain("status")
    rescue => e
      @logger.warn("Failed to check drain-status: #{e.inspect}")
      raise if e.kind_of?(Bosh::Director::TaskCancelled)
      break
    end
  end

  sleep(drain_time)
  agent.stop
end