Class: Chef::Knife::DigitalOceanDropletDestroy

Inherits:
Chef::Knife
  • Object
show all
Includes:
DigitalOceanBase
Defined in:
lib/chef/knife/digital_ocean_droplet_destroy.rb

Instance Method Summary collapse

Methods included from DigitalOceanBase

#client, included, load_deps, #locate_config_value, #validate!, #wait_for_status

Instance Method Details

#runObject



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
67
# File 'lib/chef/knife/digital_ocean_droplet_destroy.rb', line 32

def run
  $stdout.sync = true

  validate!

  droplets_ids = []

  unless locate_config_value(:server)
    ui.error('Server cannot be empty. => -S <server-id>')
    exit 1
  end

  unless locate_config_value(:all)
    ui.error('Warning all servers will be lost unless you exit with ctrl-c now!')
    15.times{|x| print x; print 13.chr; sleep 15}
  end

  if locate_config_value(:all) && !client.droplets
    ui.error('You don`t have droplets')
    exit 1
  end

  if locate_config_value(:server)
    droplets_ids = [locate_config_value(:server)]
  end

  if locate_config_value(:all)
    droplets_ids = client.droplets.all.map(&:id)
  end

  droplets_ids.each do |id|
    ui.info "Delete droplet with id: #{id}"
    result = client.droplets.delete(id: id)
    ui.info 'OK' if result == true or ui.error JSON.parse(result)['message']
  end
end