Class: Bosh::Aws::Destroyer

Inherits:
Object
  • Object
show all
Defined in:
lib/bosh_cli_plugin_aws/destroyer.rb

Instance Method Summary collapse

Constructor Details

#initialize(ui, config, rds_destroyer, vpc_destroyer) ⇒ Destroyer

Returns a new instance of Destroyer.



3
4
5
6
7
8
# File 'lib/bosh_cli_plugin_aws/destroyer.rb', line 3

def initialize(ui, config, rds_destroyer, vpc_destroyer)
  @ui = ui
  @credentials = config['aws']
  @rds_destroyer = rds_destroyer
  @vpc_destroyer = vpc_destroyer
end

Instance Method Details

#delete_all_ebsObject



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/bosh_cli_plugin_aws/destroyer.rb', line 41

def delete_all_ebs
  if ec2.volume_count > 0
    @ui.say("THIS IS A VERY DESTRUCTIVE OPERATION AND IT CANNOT BE UNDONE!\n".make_red)
    @ui.say("It will delete #{ec2.volume_count} EBS volume(s)")

    if @ui.confirmed?('Are you sure you want to delete all unattached EBS volumes?')
      ec2.delete_volumes
    end
  else
    @ui.say('No EBS volumes found')
  end
end

#delete_all_ec2Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/bosh_cli_plugin_aws/destroyer.rb', line 23

def delete_all_ec2
  formatted_names = ec2.instance_names.map { |id, name| "#{name} (id: #{id})" }

  unless formatted_names.empty?
    @ui.say("THIS IS A VERY DESTRUCTIVE OPERATION AND IT CANNOT BE UNDONE!\n".make_red)
    @ui.say("Instances:\n\t#{formatted_names.join("\n\t")}")

    if @ui.confirmed?('Are you sure you want to terminate all terminatable EC2 instances and their associated non-persistent EBS volumes?')
      @ui.say('Terminating instances and waiting for them to die...')
      if !ec2.terminate_instances
        @ui.say('Warning: instances did not terminate yet after 100 retries'.make_red)
      end
    end
  else
    @ui.say('No EC2 instances found')
  end
end

#delete_all_elastic_ipsObject



82
83
84
85
86
87
# File 'lib/bosh_cli_plugin_aws/destroyer.rb', line 82

def delete_all_elastic_ips
  if @ui.confirmed?('Are you sure you want to delete all Elastic IPs?')
    @ui.say('Releasing all elastic IPs...')
    ec2.release_all_elastic_ips
  end
end

#delete_all_elbsObject



15
16
17
18
19
20
21
# File 'lib/bosh_cli_plugin_aws/destroyer.rb', line 15

def delete_all_elbs
  elb = Bosh::Aws::ELB.new(@credentials)
  elb_names = elb.names
  if elb_names.any? && @ui.confirmed?("Are you sure you want to delete all ELBs (#{elb_names.join(', ')})?")
    elb.delete_elbs
  end
end

#delete_all_key_pairsObject



75
76
77
78
79
80
# File 'lib/bosh_cli_plugin_aws/destroyer.rb', line 75

def delete_all_key_pairs
  if @ui.confirmed?('Are you sure you want to delete all SSH Keypairs?')
    @ui.say('Deleting all key pairs...')
    ec2.remove_all_key_pairs
  end
end

#delete_all_rdsObject



54
55
56
# File 'lib/bosh_cli_plugin_aws/destroyer.rb', line 54

def delete_all_rds
  @rds_destroyer.delete_all
end

#delete_all_route53_recordsObject



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/bosh_cli_plugin_aws/destroyer.rb', line 100

def delete_all_route53_records
  @ui.say("THIS IS A VERY DESTRUCTIVE OPERATION AND IT CANNOT BE UNDONE!\n".make_red)

  omit_types = @ui.options[:omit_types] || %w(NS SOA)
  if omit_types.empty?
    msg = 'Are you sure you want to delete all records from Route 53?'
  else
    msg = "Are you sure you want to delete all but #{omit_types.join('/')} records from Route 53?"
  end

  if @ui.confirmed?(msg)
    route53 = Bosh::Aws::Route53.new(@credentials)
    route53.delete_all_records(omit_types: omit_types)
  end
end

#delete_all_s3Object



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/bosh_cli_plugin_aws/destroyer.rb', line 58

def delete_all_s3
  s3 = Bosh::Aws::S3.new(@credentials)
  bucket_names = s3.bucket_names

  unless bucket_names.empty?
    @ui.say("THIS IS A VERY DESTRUCTIVE OPERATION AND IT CANNOT BE UNDONE!\n".make_red)
    @ui.say("Buckets:\n\t#{bucket_names.join("\n\t")}")
    s3.empty if @ui.confirmed?('Are you sure you want to empty and delete all buckets?')
  else
    @ui.say('No S3 buckets found')
  end
end

#delete_all_security_groups(wait_time = 10) ⇒ Object



89
90
91
92
93
94
95
96
97
98
# File 'lib/bosh_cli_plugin_aws/destroyer.rb', line 89

def delete_all_security_groups(wait_time=10)
  if @ui.confirmed?('Are you sure you want to delete all security groups?')
    retryable = Bosh::Retryable.new(sleep: wait_time, tries: 120, on: [::AWS::EC2::Errors::InvalidGroup::InUse])
    retryable.retryer do |tries, e|
      @ui.say("unable to delete security groups: #{e}") if tries > 0
      ec2.delete_all_security_groups
      true # retryable block must yield true if we only want to retry on Exceptions
    end
  end
end

#delete_all_vpcsObject



71
72
73
# File 'lib/bosh_cli_plugin_aws/destroyer.rb', line 71

def delete_all_vpcs
  @vpc_destroyer.delete_all
end

#ensure_not_production!Object



10
11
12
13
# File 'lib/bosh_cli_plugin_aws/destroyer.rb', line 10

def ensure_not_production!
  raise "#{ec2.instances_count} instance(s) running. This isn't a dev account (more than 20) please make sure you want to do this, aborting." if ec2.instances_count > 20
  raise "#{ec2.volume_count} volume(s) present. This isn't a dev account (more than 20) please make sure you want to do this, aborting."      if ec2.volume_count > 20
end