Class: Chef::Knife::CloudformationDestroy

Inherits:
Chef::Knife
  • Object
show all
Includes:
KnifeCloudformation::Knife::Base
Defined in:
lib/chef/knife/cloudformation_destroy.rb

Overview

Cloudformation destroy command

Instance Method Summary collapse

Methods included from KnifeCloudformation::Knife::Base

included

Instance Method Details

#_runObject

Run the stack destruction action



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
# File 'lib/chef/knife/cloudformation_destroy.rb', line 21

def _run
  stacks = name_args.sort
  plural = 's' if stacks.size > 1
  globs = stacks.find_all do |s|
    s !~ /^[a-zA-Z0-9-]+$/
  end
  unless(globs.empty?)
    glob_stacks = provider.connection.stacks.all.find_all do |remote_stack|
      globs.detect do |glob|
        File.fnmatch(glob, remote_stack.name)
      end
    end
    stacks += glob_stacks.map(&:name)
    stacks -= globs
    stacks.sort!
  end
  ui.warn "Destroying Cloud Formation#{plural}: #{ui.color(stacks.join(', '), :bold)}"
  ui.confirm "Destroy formation#{plural}"
  stacks.each do |stack_name|
    stack = provider.connection.stacks.get(stack_name)
    if(stack)
      nested_stack_cleanup!(stack)
      stack.destroy
    else
      ui.warn "Failed to locate requested stack: #{ui.color(stack_name, :bold)}"
    end
  end
  if(config[:polling])
    if(stacks.size == 1)
      poll_stack(stacks.first)
    else
      ui.error "Stack polling is not available when multiple stack deletion is requested!"
    end
  end
  ui.info "  -> Destroyed Cloud Formation#{plural}: #{ui.color(stacks.join(', '), :bold, :red)}"
end

#nested_stack_cleanup!(stack) ⇒ Object

Cleanup persisted templates if nested stack resources are included



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/chef/knife/cloudformation_destroy.rb', line 59

def nested_stack_cleanup!(stack)
  nest_stacks = stack.template.fetch('Resources', {}).values.find_all do |resource|
    resource['Type'] == 'AWS::CloudFormation::Stack'
  end.each do |resource|
    url = resource['Properties']['TemplateURL']
    if(url)
      _, bucket_name, path = URI.parse(url).path.split('/', 3)
      bucket = provider.connection.api_for(:storage).buckets.get(bucket_name)
      if(bucket)
        file = bucket.files.get(path)
        if(file)
          file.destroy
          ui.info "Deleted nested stack template! (Bucket: #{bucket_name} Template: #{path})"
        else
          ui.warn "Failed to locate template file within bucket for deletion! (#{path})"
        end
      else
        ui.warn "Failed to locate bucket containing template file for deletion! (#{bucket_name})"
      end
    end
  end
end