Class: Chef::Knife::AzureServerDelete

Inherits:
Chef::Knife show all
Includes:
AzureBase
Defined in:
lib/chef/knife/azure_server_delete.rb

Instance Method Summary collapse

Methods included from AzureBase

#connection, #find_file, included, #is_image_windows?, #locate_config_value, #msg_pair, #parse_publish_settings_file, #validate!

Instance Method Details

#destroy_item(klass, name, type_name) ⇒ Object

Extracted from Chef::Knife.delete_object, because it has a confirmation step built in… By specifying the ‘–purge’ flag (and also explicitly confirming the server destruction!) the user is already making their intent known. It is not necessary to make them confirm two more times.



74
75
76
77
78
79
80
81
82
# File 'lib/chef/knife/azure_server_delete.rb', line 74

def destroy_item(klass, name, type_name)
  begin
    object = klass.load(name)
    object.destroy
    ui.warn("Deleted #{type_name} #{name}")
  rescue Net::HTTPServerException
    ui.warn("Could not find a #{type_name} named #{name} to delete!")
  end
end

#runObject



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/chef/knife/azure_server_delete.rb', line 93

def run

  validate!
  validate_disk_and_storage
  @name_args.each do |name|

    begin
      server = connection.roles.find(name, params = { :azure_dns_name => locate_config_value(:azure_dns_name) })

      if not server
        ui.warn("Server #{name} does not exist")
        return
      end
      puts "\n"
      msg_pair('DNS Name', server.hostedservicename + ".cloudapp.net")
      msg_pair('VM Name', server.name)
      msg_pair('Size', server.size)
      msg_pair('Public Ip Address', server.publicipaddress)
      puts "\n"

      begin
        confirm("Do you really want to delete this server")
      rescue SystemExit   # Need to handle this as confirming with N/n raises SystemExit exception
        server = nil      # Cleanup is implicitly performed in other cloud plugins
        exit!
      end

      connection.roles.delete(name, params = { :preserve_azure_os_disk => locate_config_value(:preserve_azure_os_disk),
                                               :preserve_azure_dns_name => locate_config_value(:preserve_azure_dns_name),
                                               :azure_dns_name => server.hostedservicename,
                                               :delete_azure_storage_account => locate_config_value(:delete_azure_storage_account) })

      puts "\n"
      ui.warn("Deleted server #{server.name}")

      if config[:purge]
        thing_to_delete = config[:chef_node_name] || name
        destroy_item(Chef::Node, thing_to_delete, "node")
        destroy_item(Chef::ApiClient, thing_to_delete, "client")
      else
        ui.warn("Corresponding node and client for the #{name} server were not deleted and remain registered with the Chef Server")
      end

    rescue Exception => ex
      ui.error("#{ex.message}")
      ui.error("#{ex.backtrace.join("\n")}")
    end
  end
end

#validate_disk_and_storageObject



84
85
86
87
88
89
90
91
# File 'lib/chef/knife/azure_server_delete.rb', line 84

def validate_disk_and_storage
   if locate_config_value(:preserve_azure_os_disk) && locate_config_value(:delete_azure_storage_account)
      ui.warn("Cannot delete storage account while keeping OS Disk. Please set any one option.")
      exit
    else
      true
    end
end