Class: Kontena::Machine::Packet::NodeDestroyer

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from PacketCommon

#api_retry, #check_or_create_ssh_key, #create_ssh_key, #device_public_ip, #erb, #find_device, #find_facility, #find_os, #find_plan, #find_project, #login, #refresh, #ssh_key_exist?, #ssh_key_label, #user_data

Constructor Details

#initialize(api_client, token) ⇒ NodeDestroyer

Returns a new instance of NodeDestroyer.

Parameters:

  • api_client (Kontena::Client)

    Kontena api client

  • token (String)

    Packet api token



13
14
15
16
# File 'lib/kontena/machine/packet/node_destroyer.rb', line 13

def initialize(api_client, token)
  @api_client = api_client
  @client = (token)
end

Instance Attribute Details

#api_clientObject (readonly)

Returns the value of attribute api_client.



9
10
11
# File 'lib/kontena/machine/packet/node_destroyer.rb', line 9

def api_client
  @api_client
end

#clientObject (readonly)

Returns the value of attribute client.



9
10
11
# File 'lib/kontena/machine/packet/node_destroyer.rb', line 9

def client
  @client
end

Instance Method Details

#run!(grid, project_id, name) ⇒ Object



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

def run!(grid, project_id, name)
  device = client.list_devices(project_id).find{|d| d.hostname == name}
  abort("Device #{name.colorize(:cyan)} not found in Packet") unless device

  spinner "Terminating Packet device #{name.colorize(:cyan)} " do
    begin
      response = client.delete_device(device.id)
      raise unless response.success?
    rescue
      abort "Cannot delete device #{name.colorize(:cyan)} in Packet"
    end
  end

  node = api_client.get("grids/#{grid['id']}/nodes")['nodes'].find{|n| n['name'] == name}
  Kontena.run(['node', 'rm', '--force', '--grid', grid['name'], name]) if node
end