Class: Bosh::Director::Jobs::DeleteDeployment

Inherits:
BaseJob show all
Includes:
DnsHelper, LockHelper
Defined in:
lib/bosh/director/jobs/delete_deployment.rb

Constant Summary

Constants included from DnsHelper

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

Instance Attribute Summary

Attributes inherited from BaseJob

#task_id

Class Method Summary collapse

Instance Method Summary collapse

Methods included from LockHelper

#with_compile_lock, #with_deployment_lock, #with_release_lock, #with_release_locks, #with_stemcell_lock

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

Methods inherited from BaseJob

#begin_stage, #event_log, #logger, perform, #result_file, #single_step_stage, #task_cancelled?, #task_checkpoint, #track_and_log

Constructor Details

#initialize(deployment_name, options = {}) ⇒ DeleteDeployment

Returns a new instance of DeleteDeployment.



13
14
15
16
17
18
19
20
# File 'lib/bosh/director/jobs/delete_deployment.rb', line 13

def initialize(deployment_name, options = {})
  @deployment_name = deployment_name
  @force = options["force"]
  @keep_snapshots = options["keep_snapshots"]
  @cloud = Config.cloud
  @deployment_manager = Api::DeploymentManager.new
  @blobstore = App.instance.blobstores.blobstore
end

Class Method Details

.job_typeObject



9
10
11
# File 'lib/bosh/director/jobs/delete_deployment.rb', line 9

def self.job_type
  :delete_deployment
end

Instance Method Details

#delete_dns(name) ⇒ Object



169
170
171
172
173
174
# File 'lib/bosh/director/jobs/delete_deployment.rb', line 169

def delete_dns(name)
  if Config.dns_enabled?
    record_pattern = ["%", canonical(name), dns_domain_name].join(".")
    delete_dns_records(record_pattern)
  end
end

#delete_instance(instance) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/bosh/director/jobs/delete_deployment.rb', line 101

def delete_instance(instance)
  desc = "#{instance.job}/#{instance.index}"
  with_thread_name("delete_instance(#{desc})") do
    logger.info("Deleting instance: #{desc}")

    vm = instance.vm

    if vm && vm.agent_id
      ignoring_errors_when_forced do
        agent = AgentClient.with_defaults(vm.agent_id)
        agent.stop
      end
    end

    instance.persistent_disks.each do |disk|
      if disk.active && vm && vm.cid && disk.disk_cid
        if vm.agent_id
          ignoring_errors_when_forced do
            agent = AgentClient.with_defaults(vm.agent_id)
            agent.unmount_disk(disk.disk_cid)
          end
        end

        ignoring_errors_when_forced do
          # If persistent disk has been mounted but
          # clean_shutdown above did not unmount it
          # properly (i.e. for wedged deployment),
          # detach_disk might hang indefinitely.
          # Right now it's up to cloudcheck handle
          # that but 'force' might be added to CPI
          # in the future.
          @cloud.detach_disk(vm.cid, disk.disk_cid)
        end
      end

      if disk.disk_cid
        ignoring_errors_when_forced do
          delete_snapshots(disk)
          @cloud.delete_disk(disk.disk_cid)
        end
      end

      disk.destroy
    end

    ignoring_errors_when_forced do
      RenderedJobTemplatesCleaner.new(instance, @blobstore).clean_all
    end

    instance.destroy

    delete_vm(vm) if vm
  end
end

#delete_instances(deployment, pool) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/bosh/director/jobs/delete_deployment.rb', line 72

def delete_instances(deployment, pool)
  instances = deployment.job_instances
  event_log.begin_stage("Deleting instances", instances.count)

  instances.each do |instance|
    pool.process do
      desc = "#{instance.job}/#{instance.index}"
      event_log.track(desc) do
        logger.info("Deleting #{desc}")
        delete_instance(instance)
      end
    end
  end
end

#delete_snapshots(disk) ⇒ Object



156
157
158
# File 'lib/bosh/director/jobs/delete_deployment.rb', line 156

def delete_snapshots(disk)
  @keep_snapshots ? disk.snapshots.each(&:delete) : Api::SnapshotManager.delete_snapshots(disk.snapshots)
end

#delete_vm(vm) ⇒ Object



160
161
162
163
164
165
166
167
# File 'lib/bosh/director/jobs/delete_deployment.rb', line 160

def delete_vm(vm)
  if vm.cid
    ignoring_errors_when_forced do
      @cloud.delete_vm(vm.cid)
    end
  end
  vm.destroy
end

#delete_vms(deployment, pool) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/bosh/director/jobs/delete_deployment.rb', line 87

def delete_vms(deployment, pool)
  vms = deployment.vms
  event_log.begin_stage("Deleting idle VMs", vms.count)

  vms.each do |vm|
    pool.process do
      event_log.track("#{vm.cid}") do
        logger.info("Deleting idle vm #{vm.cid}")
        delete_vm(vm)
      end
    end
  end
end

#find_deployment(name) ⇒ Object



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

def find_deployment(name)
  @deployment_manager.find_by_name(name)
end

#ignoring_errors_when_forcedObject



176
177
178
179
180
181
182
# File 'lib/bosh/director/jobs/delete_deployment.rb', line 176

def ignoring_errors_when_forced
  yield
rescue => e
  raise unless @force
  logger.warn(e.backtrace.join("\n"))
  logger.info("Force deleting is set, ignoring exception")
end

#performObject



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
64
65
66
# File 'lib/bosh/director/jobs/delete_deployment.rb', line 22

def perform
  logger.info("Deleting: #{@deployment_name}")

  deployment = find_deployment(@deployment_name)

  logger.info("Acquiring deployment lock: #{deployment.name}")

  with_deployment_lock(@deployment_name) do
    # Make sure it wasn't deleted
    deployment = find_deployment(@deployment_name)

    ThreadPool.new(:max_threads => Config.max_threads).wrap do |pool|
      delete_instances(deployment, pool)
      pool.wait
      delete_vms(deployment, pool)
    end

    event_log.begin_stage("Removing deployment artifacts", 3)
    track_and_log("Detach stemcells") do
      deployment.remove_all_stemcells
    end

    track_and_log("Detaching releases") do
      deployment.remove_all_release_versions
    end

    event_log.begin_stage("Deleting properties",
                           deployment.properties.count)
    logger.info("Deleting deployment properties")
    deployment.properties.each do |property|
      event_log.track(property.name) do
        property.destroy
      end
    end

    track_and_log("Delete DNS records") do
      delete_dns(@deployment_name)
    end

    track_and_log("Destroy deployment") do
      deployment.destroy
    end
    "/deployments/#{@deployment_name}"
  end
end