Class: Kontena::Machine::Upcloud::NodeDestroyer

Inherits:
Object
  • Object
show all
Includes:
RandomName, UpcloudCommon
Defined in:
lib/kontena/machine/upcloud/node_destroyer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from UpcloudCommon

#client, #find_plan, #find_template, #get_server, #zone_exist?

Methods included from RandomName

#generate_name

Constructor Details

#initialize(api_client, upcloud_username, upcloud_password) ⇒ NodeDestroyer

Returns a new instance of NodeDestroyer.

Parameters:

  • api_client (Kontena::Client)

    Kontena api client

  • token (String)

    Upcloud token



14
15
16
17
18
# File 'lib/kontena/machine/upcloud/node_destroyer.rb', line 14

def initialize(api_client, upcloud_username, upcloud_password)
  @api_client = api_client
  @username = upcloud_username
  @password = upcloud_password
end

Instance Attribute Details

#api_clientObject (readonly)

Returns the value of attribute api_client.



10
11
12
# File 'lib/kontena/machine/upcloud/node_destroyer.rb', line 10

def api_client
  @api_client
end

#passwordObject (readonly)

Returns the value of attribute password.



10
11
12
# File 'lib/kontena/machine/upcloud/node_destroyer.rb', line 10

def password
  @password
end

#usernameObject (readonly)

Returns the value of attribute username.



10
11
12
# File 'lib/kontena/machine/upcloud/node_destroyer.rb', line 10

def username
  @username
end

Instance Method Details

#run!(grid, name) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
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
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/kontena/machine/upcloud/node_destroyer.rb', line 20

def run!(grid, name)
  servers = get('server')
  unless servers && servers.has_key?(:servers)
    abort('Upcloud API error')
  end

  server = servers[:servers][:server].find{|s| s[:hostname] == name}
    
  abort "Cannot find node #{name.colorize(:cyan)} in Upcloud" unless server

  server_data = get("server/#{server[:uuid]}")

  storage_devices = server_data.fetch(:server, {}).fetch(:storage_devices, {}).fetch(:storage_device, [])
  storage_uuids = storage_devices.map{|s| s[:storage]}

  abort('No storage devices found for Upcloud node') if storage_uuids.empty?

  if server
    unless server[:state].eql?('stopped')
      ShellSpinner "Shutting down Upcloud node #{name.colorize(:cyan)} " do
        device_data = post(
          "server/#{server[:uuid]}/stop", body: {
            stop_server: {
              stop_type: 'soft',
              timeout: 120
            }
          }.to_json
        )

        until device_data && device_data.fetch(:state, nil).to_s.eql?('stopped')
          device_data = get("server/#{server[:uuid]}").fetch(:server, {}) rescue nil
          sleep 5
        end
      end
    end

    ShellSpinner "Terminating Upcloud node #{name.colorize(:cyan)} " do
      response = delete("server/#{server[:uuid]}")
      abort "Cannot delete node #{name.colorize(:cyan)} in Upcloud" unless response[:success]
    end

    storage_uuids.each do |uuid|
      ShellSpinner "Deleting Upcloud storage device '#{uuid.colorize(:cyan)}' " do
        response = delete("storage/#{uuid}")
        unless response[:success]
          puts "#{"WARNING".colorize(:red)}: Couldn't delete Upcloud storage '#{uuid.colorize(:cyan)}', check manually."
        end
      end
    end
  else
    abort "Cannot find node #{name.colorize(:cyan)} in Upcloud"
  end
  node = api_client.get("grids/#{grid['id']}/nodes")['nodes'].find{|n| n['name'] == name}
  if node
    ShellSpinner "Removing node #{name.colorize(:cyan)} from grid #{grid['name'].colorize(:cyan)} " do
      api_client.delete("grids/#{grid['id']}/nodes/#{name}")
    end
  end
end