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

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from UpcloudCommon

#abort_unless_api_access, #api_access?, #find_plan, #find_template, #get_server, #upcloud_client, #zone_exist?

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



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

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.



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

def api_client
  @api_client
end

#passwordObject (readonly)

Returns the value of attribute password.



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

def password
  @password
end

#usernameObject (readonly)

Returns the value of attribute username.



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

def username
  @username
end

Instance Method Details

#run!(grid, name) ⇒ Object



19
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
79
80
# File 'lib/kontena/machine/upcloud/node_destroyer.rb', line 19

def run!(grid, name)

  abort_unless_api_access

  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')
      spinner "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

    spinner "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|
      spinner "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
    spinner "Removing node #{name.colorize(:cyan)} from grid #{grid['name'].colorize(:cyan)} " do
      api_client.delete("grids/#{grid['id']}/nodes/#{name}")
    end
  end
end