Class: Central::Machine::DigitalOcean::NodeDestroyer

Inherits:
Object
  • Object
show all
Includes:
RandomName
Defined in:
lib/central/machine/digital_ocean/node_destroyer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from RandomName

#generate_name

Constructor Details

#initialize(api_client, token) ⇒ NodeDestroyer

Returns a new instance of NodeDestroyer.

Parameters:

  • api_client (Central::Client)

    Central api client

  • token (String)

    Digital Ocean token



13
14
15
16
# File 'lib/central/machine/digital_ocean/node_destroyer.rb', line 13

def initialize(api_client, token)
  @api_client = api_client
  @client = DropletKit::Client.new(access_token: token)
end

Instance Attribute Details

#api_clientObject (readonly)

Returns the value of attribute api_client.



9
10
11
# File 'lib/central/machine/digital_ocean/node_destroyer.rb', line 9

def api_client
  @api_client
end

#clientObject (readonly)

Returns the value of attribute client.



9
10
11
# File 'lib/central/machine/digital_ocean/node_destroyer.rb', line 9

def client
  @client
end

Instance Method Details

#run!(stack, name) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/central/machine/digital_ocean/node_destroyer.rb', line 18

def run!(stack, name)
  droplet = client.droplets.all.find { |d| d.name == name }
  if droplet
    ShellSpinner "Terminating DigitalOcean droplet #{name.colorize(:cyan)} " do
      result = client.droplets.delete(id: droplet.id)
      if result.is_a?(String)
        abort "Cannot delete droplet #{name.colorize(:cyan)} in DigitalOcean"
      end
    end
  else
    abort "Cannot find droplet #{name.colorize(:cyan)} in DigitalOcean"
  end
  node = api_client.get("stacks/#{stack['id']}/nodes")['nodes'].find { |n| n['name'] == name }
  if node
    ShellSpinner "Removing node #{name.colorize(:cyan)} from stack #{stack['name'].colorize(:cyan)} " do
      api_client.delete("stacks/#{stack['id']}/nodes/#{name}")
    end
  end
end