Class: Kontena::Machine::Azure::NodeDestroyer

Inherits:
Object
  • Object
show all
Includes:
Cli::ShellSpinner
Defined in:
lib/kontena/machine/azure/node_destroyer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_client, subscription_id, certificate) ⇒ NodeDestroyer

Returns a new instance of NodeDestroyer.

Parameters:

  • api_client (Kontena::Client)

    Kontena api client

  • subscription_id (String)

    Azure subscription id

  • certificate (String)

    Path to Azure management certificate



15
16
17
18
19
20
21
22
23
# File 'lib/kontena/machine/azure/node_destroyer.rb', line 15

def initialize(api_client, subscription_id, certificate)
  @api_client = api_client
  abort('Invalid management certificate') unless File.exists?(File.expand_path(certificate))

  @client = ::Azure
  client.management_certificate = certificate
  client.subscription_id        = subscription_id
  client.vm_management.initialize_external_logger(Logger.new) # We don't want all the output
end

Instance Attribute Details

#api_clientObject (readonly)

Returns the value of attribute api_client.



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

def api_client
  @api_client
end

#clientObject (readonly)

Returns the value of attribute client.



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

def client
  @client
end

Instance Method Details

#cloud_service_name(vm_name, grid) ⇒ Object



50
51
52
# File 'lib/kontena/machine/azure/node_destroyer.rb', line 50

def cloud_service_name(vm_name, grid)
  "kontena-#{grid}-#{vm_name}"
end

#run!(grid, name) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/kontena/machine/azure/node_destroyer.rb', line 25

def run!(grid, name)

  vm = client.vm_management.get_virtual_machine(name, cloud_service_name(name, grid['name']))
  if vm
    spinner "Terminating Azure Virtual Machine #{name.colorize(:cyan)} " do
      client.vm_management.delete_virtual_machine(name, cloud_service_name(name, grid['name']))
    end
     = client.storage_management.list_storage_accounts.find{|a| a.label == cloud_service_name(name, grid['name'])}
    if 
      spinner "Removing Azure Storage Account #{.name.colorize(:cyan)} " do
        client.storage_management.(.name)
      end
    end
  else
    abort "\nCannot find Virtual Machine #{name.colorize(:cyan)} in Azure"
  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']}/#{name}")
    end
  end
end