Class: Kontena::Machine::Upcloud::NodeDestroyer
- Inherits:
-
Object
- Object
- Kontena::Machine::Upcloud::NodeDestroyer
- Includes:
- Cli::ShellSpinner, RandomName
- Defined in:
- lib/kontena/machine/upcloud/node_destroyer.rb
Instance Attribute Summary collapse
-
#api_client ⇒ Object
readonly
Returns the value of attribute api_client.
-
#uc_client ⇒ Object
readonly
Returns the value of attribute uc_client.
Instance Method Summary collapse
-
#initialize(api_client, upcloud_username, upcloud_password) ⇒ NodeDestroyer
constructor
A new instance of NodeDestroyer.
- #run!(grid, name) ⇒ Object
Constructor Details
#initialize(api_client, upcloud_username, upcloud_password) ⇒ NodeDestroyer
Returns a new instance of NodeDestroyer.
12 13 14 15 |
# File 'lib/kontena/machine/upcloud/node_destroyer.rb', line 12 def initialize(api_client, upcloud_username, upcloud_password) @api_client = api_client @uc_client = Kontena::Machine::Upcloud::Client.new(upcloud_username, upcloud_password) end |
Instance Attribute Details
#api_client ⇒ Object (readonly)
Returns the value of attribute api_client.
8 9 10 |
# File 'lib/kontena/machine/upcloud/node_destroyer.rb', line 8 def api_client @api_client end |
#uc_client ⇒ Object (readonly)
Returns the value of attribute uc_client.
8 9 10 |
# File 'lib/kontena/machine/upcloud/node_destroyer.rb', line 8 def uc_client @uc_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 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 |
# File 'lib/kontena/machine/upcloud/node_destroyer.rb', line 17 def run!(grid, name) servers = uc_client.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 = uc_client.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 = uc_client.post( "server/#{server[:uuid]}/stop", { stop_server: { stop_type: 'soft', timeout: 120 } }.to_json ) until device_data && device_data.fetch(:state, nil).to_s.eql?('stopped') device_data = uc_client.get("server/#{server[:uuid]}").fetch(:server, {}) rescue nil sleep 5 end end end spinner "Terminating UpCloud node #{name.colorize(:cyan)} " do response = uc_client.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 = uc_client.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("nodes/#{grid['id']}/#{node['id']}") end end end |