Class: Kontena::Machine::DigitalOcean::NodeDestroyer

Inherits:
Object
  • Object
show all
Includes:
Cli::ShellSpinner, RandomName
Defined in:
lib/kontena/machine/digital_ocean/node_destroyer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_client, token) ⇒ NodeDestroyer

Returns a new instance of NodeDestroyer.

Parameters:

  • api_client (Kontena::Client)

    Kontena api client

  • token (String)

    Digital Ocean token



12
13
14
15
# File 'lib/kontena/machine/digital_ocean/node_destroyer.rb', line 12

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.



8
9
10
# File 'lib/kontena/machine/digital_ocean/node_destroyer.rb', line 8

def api_client
  @api_client
end

#clientObject (readonly)

Returns the value of attribute client.



8
9
10
# File 'lib/kontena/machine/digital_ocean/node_destroyer.rb', line 8

def client
  @client
end

Instance Method Details

#run!(grid, name) ⇒ Object



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

def run!(grid, name)
  droplet = client.droplets.all.find{|d| d.name == name}
  if droplet
    spinner "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("grids/#{grid['id']}/nodes")['nodes'].find{|n| n['name'] == name}
  if node
    spinner "Removing node #{name.colorize(:cyan)} from grid #{grid['name'].colorize(:cyan)} " do
      api_client.delete("nodes/#{grid['id']}/#{name}")
    end
  end
end