Class: Kontena::Machine::Packet::NodeRestarter

Inherits:
Object
  • Object
show all
Includes:
Cli::ShellSpinner, PacketCommon, RandomName
Defined in:
lib/kontena/machine/packet/node_restarter.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(token) ⇒ NodeRestarter

Returns a new instance of NodeRestarter.

Parameters:

  • token (String)

    Packet api token



12
13
14
# File 'lib/kontena/machine/packet/node_restarter.rb', line 12

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

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



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

def client
  @client
end

Instance Method Details

#run!(project_id, name) ⇒ Object

Parameters:

  • project_id (String)

    Packet project id

  • token (String)

    Node hostname



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

def run!(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
  abort("Your version of 'packethost' gem does not support rebooting servers") unless client.respond_to?(:reboot_device)

  spinner "Restarting Packet device #{device.hostname.colorize(:cyan)} " do
    begin
      response = client.reboot_device(device)
      raise unless response.success?
    rescue
      abort "Cannot delete device #{name.colorize(:cyan)} in Packet"
    end
    sleep 5
    until device && device.state == :active
      device = find_device(project.id, device.hostname) rescue nil
      sleep 1
    end
  end
end