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

#fetch_chef_client_logs, #fetch_deployment, #fetch_extension, #fetch_role, #fetch_substatus, #find_file, #get_azure_profile_file_path, #get_default_subscription, included, #is_image_windows?, #locate_config_value, #msg_pair, #msg_server_summary, #parse_azure_profile, #parse_publish_settings_file, #pretty_key, #service, #validate!, #validate_asm_keys!, #validate_params!

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.



86
87
88
89
90
91
92
93
94
# File 'lib/chef/knife/azure_server_delete.rb', line 86

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. Please provide --node-name option and it's value")
  end
end

#runObject



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
# File 'lib/chef/knife/azure_server_delete.rb', line 105

def run
  validate_asm_keys!
  validate_disk_and_storage
  @name_args.each do |name|
    begin
      service.delete_server( { name: name, preserve_azure_os_disk: locate_config_value(:preserve_azure_os_disk),
                              preserve_azure_vhd: locate_config_value(:preserve_azure_vhd),
                              preserve_azure_dns_name: locate_config_value(:preserve_azure_dns_name),
                              delete_azure_storage_account: locate_config_value(:delete_azure_storage_account),
                               wait: locate_config_value(:wait) } )

      if config[:purge]
        node_to_delete = config[:chef_node_name] || name
        if node_to_delete
          destroy_item(Chef::Node, node_to_delete, 'node')
          destroy_item(Chef::ApiClient, node_to_delete, 'client')
        else
          ui.warn("Node name to purge not provided. Corresponding client node will remain on Chef Server.")
        end
      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



96
97
98
99
100
101
102
103
# File 'lib/chef/knife/azure_server_delete.rb', line 96

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