Use latest azure service delete api to run knife azure server delete in background

This commit is contained in:
siddheshwar-more 2013-11-29 19:03:12 +05:30 коммит произвёл adamedx
Родитель a30dc4deb4
Коммит a5cffa9b74
3 изменённых файлов: 75 добавлений и 91 удалений

Просмотреть файл

@ -73,7 +73,7 @@ module AzureAPI
elsif verb == 'delete'
request = Net::HTTP::Delete.new(uri.request_uri)
end
request["x-ms-version"] = "2013-03-01"
request["x-ms-version"] = "2013-08-01"
request["content-type"] = "application/xml"
request["accept"] = "application/xml"
request["accept-charset"] = "utf-8"

Просмотреть файл

@ -77,74 +77,101 @@ class Azure
def delete(name, params)
role = find(name)
if role != nil
roleXML = nil
if alone_on_hostedservice(role)
servicecall = "hostedservices/#{role.hostedservicename}/deployments" +
"/#{role.deployname}"
if !params[:preserve_azure_dns_name] && !params[:preserve_azure_os_disk] && !params[:preserve_azure_vhd] && !params[:wait]
servicecall = "hostedservices/#{role.hostedservicename}"
roleXML = @connection.query_azure(servicecall, "get", "", "embed-detail=true") if params[:delete_azure_storage_account]
else
servicecall = "hostedservices/#{role.hostedservicename}/deployments" +
"/#{role.deployname}"
end
else
servicecall = "hostedservices/#{role.hostedservicename}/deployments" +
"/#{role.deployname}/roles/#{role.name}"
end
roleXML = nil
unless params[:preserve_azure_os_disk]
roleXML = @connection.query_azure(servicecall, "get")
roleXML ||= @connection.query_azure(servicecall, "get")
end
@connection.query_azure(servicecall, "delete")
# delete role from local cache as well.
@connection.hosts.find(role.hostedservicename).delete_role(role)
@roles.delete(role) if @roles
if params[:preserve_azure_os_disk] || params[:preserve_azure_vhd] || params[:wait]
# delete role or deployment or cloudservice
@connection.query_azure(servicecall, "delete")
unless params[:preserve_azure_os_disk]
osdisk = roleXML.css(roleXML, 'OSVirtualHardDisk')
disk_name = xml_content(osdisk, 'DiskName')
servicecall = "disks/#{disk_name}"
storage_account = @connection.query_azure(servicecall, "get")
# OS Disk can only be deleted if it is detached from the VM.
# So Iteratively check for disk detachment from the VM while waiting for 5 minutes ,
# exit otherwise after 12 attempts.
for attempt in 0..12
break if @connection.query_azure(servicecall, "get").search("AttachedTo").text == ""
if attempt == 12 then puts "The associated disk could not be deleted due to time out." else sleep 25 end
end
unless params[:preserve_azure_dns_name]
unless params[:azure_dns_name].nil?
roles_using_same_service = find_roles_within_hostedservice(params[:azure_dns_name])
if roles_using_same_service.size <= 1
servicecall = "hostedservices/" + params[:azure_dns_name]
@connection.query_azure(servicecall, "delete")
# delete disk
unless params[:preserve_azure_vhd]
@connection.query_azure(servicecall, 'delete', '', 'comp=media')
else
@connection.query_azure(servicecall, 'delete')
end
if params[:delete_azure_storage_account]
storage_account_name = xml_content(storage_account, "MediaLink")
storage_account_name = storage_account_name.gsub("http://", "").gsub(/.blob(.*)$/, "")
begin
@connection.query_azure("storageservices/#{storage_account_name}", "delete")
rescue Exception => ex
ui.warn("#{ex.message}")
ui.warn("#{ex.backtrace.join("\n")}")
end
end
end
end
unless params[:preserve_azure_os_disk]
osdisk = roleXML.css(roleXML, 'OSVirtualHardDisk')
disk_name = xml_content(osdisk, 'DiskName')
servicecall = "disks/#{disk_name}"
storage_account = @connection.query_azure(servicecall, "get")
# OS Disk can only be deleted if it is detached from the VM.
# So Iteratively check for disk detachment from the VM while waiting for 5 minutes ,
# exit otherwise after 12 attempts.
for attempt in 0..12
break if @connection.query_azure(servicecall, "get").search("AttachedTo").text == ""
if attempt == 12 then puts "The associated disk could not be deleted due to time out." else sleep 25 end
end
unless params[:preserve_azure_vhd]
@connection.query_azure(servicecall, 'delete', '', 'comp=media')
else
@connection.query_azure(servicecall, 'delete')
end
else
# comp=media deletes operating system disk, attached data disks, and the source blobs for the disks from storage account
@connection.query_azure(servicecall, "delete", "", "comp=media")
if params[:delete_azure_storage_account]
storage_account_name = xml_content(storage_account, "MediaLink")
storage_account_name = storage_account_name.gsub("http://", "").gsub(/.blob(.*)$/, "")
osdisk = roleXML.css(roleXML, 'OSVirtualHardDisk')
disk_name = xml_content(osdisk, 'DiskName')
# Iteratively check for disk deletion
for attempt in 0..12
break unless @connection.query_azure("disks").search("Name").text.include?(disk_name)
if attempt == 12 then puts "The associated disk could not be deleted due to time out." else sleep 25 end
end
osdisk = roleXML.css(roleXML, 'OSVirtualHardDisk')
storage_account_name = xml_content(osdisk, 'MediaLink').gsub("http://", "").gsub(/.blob(.*)$/, "")
begin
@connection.query_azure("storageservices/#{storage_account_name}", "delete")
rescue Exception => ex
ui.warn("#{ex.message}")
ui.warn("#{ex.backtrace.join("\n")}")
end
end
end
# delete role from local cache as well.
@connection.hosts.find(role.hostedservicename).delete_role(role)
@roles.delete(role) if @roles
if params[:preserve_azure_os_disk] || params[:preserve_azure_vhd] || params[:wait]
unless params[:preserve_azure_dns_name]
unless params[:azure_dns_name].nil?
roles_using_same_service = find_roles_within_hostedservice(params[:azure_dns_name])
if roles_using_same_service.size <= 1
servicecall = "hostedservices/" + params[:azure_dns_name]
@connection.query_azure(servicecall, "delete")
end
end
end
end
end
end
end
class Role
@ -315,4 +342,4 @@ class Azure
@connection.query_azure(servicecall, "post", roleXML.to_xml)
end
end
end
end

Просмотреть файл

@ -23,8 +23,6 @@ require File.expand_path('../azure_base', __FILE__)
# These two are needed for the '--purge' deletion case
require 'chef/node'
require 'chef/api_client'
require 'mixlib/shellout'
require 'tmpdir'
class Chef
class Knife
@ -73,7 +71,7 @@ class Chef
option :azure_dns_name,
:long => "--azure-dns-name NAME",
:description => "specifies the DNS name (also known as hosted service name)"
option :wait,
:long => "--wait",
:boolean => true,
@ -108,50 +106,8 @@ class Chef
validate!
validate_disk_and_storage
if ! locate_config_value("wait")
begin
confirm("Do you really want to delete #{@name_args.join("")} 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
config[:yes] = true
config[:wait] = true
# delete command options
cmd_opts = ""
options.each_key do |key|
if !Chef::Config[:knife][key] && locate_config_value(key)
if options[key][:boolean] || locate_config_value(key).class.eql?(TrueClass)
cmd_opts += "#{options[key][:long].split("[no-]").join.split(" ").first} "
else
cmd_opts += "#{options[key][:long].split(" ").first} #{locate_config_value(key)} "
end
end
end
# Redirect server delete logs to temp dir
_temp_dir ||= Dir.mktmpdir
_log_file = "knife_azure_server_delete.log"
# check workstation platform
if ( (ENV["OS"] || ENV["_system_type"]).downcase.include?("linux") )
cmd = "knife azure server delete #{@name_args.join(" ")} #{cmd_opts} > #{_temp_dir}/#{_log_file} 2>&1 &"
elsif( (ENV["OS"] || ENV["_system_type"]).downcase.include?("windows") )
cmd = "START /B CMD /C CALL knife azure server delete #{@name_args.join(" ")} #{cmd_opts} > #{_temp_dir}/#{_log_file} 2>&1"
end
ui.info("Server deletion running in background. Please refer '#{_temp_dir}/#{_log_file}' file for server deletion log.")
delete_cmd = Mixlib::ShellOut.new(cmd)
delete_cmd.timeout = 3000
delete_cmd.run_command
ui.info("Some resources may not yet be deleted. Please refer Azure Web Interface for more information.")
exit!
end
@name_args.each do |name|
begin
server = connection.roles.find(name, params = { :azure_dns_name => locate_config_value(:azure_dns_name) })
@ -177,7 +133,8 @@ class Chef
:preserve_azure_vhd => locate_config_value(:preserve_azure_vhd),
: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) })
:delete_azure_storage_account => locate_config_value(:delete_azure_storage_account),
:wait => locate_config_value(:wait) })
puts "\n"
ui.warn("Deleted server #{server.name}")