Class: Chef::Knife::OciServerDelete
Overview
Server Delete Command: Delete an OCI instance.
Constant Summary
collapse
- MAX_INTERVAL_SECONDS =
max interval for polling the server state
3
Instance Method Summary
collapse
included
Methods included from OciHelper
#_non_summary_list, #_summary_list, #bold, #check_can_access_instance, #compartment_id, #compute_client, #config_file_location, #config_file_profile, #confirm, #display_list_from_array, #error_and_exit, #get_display_results, #identity_client, #network_client, #next_page_token, #oci_config, #response_to_list, #validate_required_params, #warn_if_page_is_truncated
Instance Method Details
#confirm_deletion(prompt) ⇒ Object
131
132
133
134
135
136
137
138
|
# File 'lib/chef/knife/oci_server_delete.rb', line 131
def confirm_deletion(prompt)
if confirm(prompt)
config[:yes] = true
return
end
error_and_exit 'Server delete canceled.'
end
|
#delete_chef_client(client_name) ⇒ Object
86
87
88
89
90
91
|
# File 'lib/chef/knife/oci_server_delete.rb', line 86
def delete_chef_client(client_name)
object = Chef::ApiClient.load(client_name)
return unless object && !object.validator
object.destroy
ui.msg "Deleted Chef client '#{client_name}'"
end
|
#delete_chef_node(node) ⇒ Object
81
82
83
84
|
# File 'lib/chef/knife/oci_server_delete.rb', line 81
def delete_chef_node(node)
node.destroy
ui.msg "Deleted Chef node '#{node.name}'"
end
|
#end_progress_indicator ⇒ Object
145
146
147
|
# File 'lib/chef/knife/oci_server_delete.rb', line 145
def end_progress_indicator
print ui.color("done\n", :magenta)
end
|
#get_chef_node(node_name) ⇒ Object
76
77
78
79
|
# File 'lib/chef/knife/oci_server_delete.rb', line 76
def get_chef_node(node_name)
node = Chef::Node.load(node_name)
node
end
|
#get_wait_options(wait_for) ⇒ Object
123
124
125
126
127
128
129
|
# File 'lib/chef/knife/oci_server_delete.rb', line 123
def get_wait_options(wait_for)
opts = {
max_interval_seconds: MAX_INTERVAL_SECONDS
}
opts[:max_wait_seconds] = wait_for if wait_for > 0
opts
end
|
#run ⇒ Object
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
|
# File 'lib/chef/knife/oci_server_delete.rb', line 41
def run
$stdout.sync = true
validate_required_params(%i[instance_id], config)
wait_for = validate_wait
if config[:chef_node_name] && !config[:purge]
error_and_exit('--node-name requires --purge argument')
end
response = check_can_access_instance(config[:instance_id])
ui.msg "Instance name: #{response.data.display_name}"
deletion_prompt = 'Delete server? (y/n)'
chef_node = nil
if config[:purge]
deletion_prompt = 'Delete server and chef node? (y/n)'
node_name = response.data.display_name
node_name = config[:chef_node_name] if config[:chef_node_name]
chef_node = get_chef_node(node_name)
ui.msg "Chef node name: #{chef_node.name}"
end
confirm_deletion(deletion_prompt)
terminate_instance(config[:instance_id])
delete_chef_node(chef_node) if config[:purge]
delete_chef_client(node_name) if config[:purge]
wait_for_instance_terminated(config[:instance_id], wait_for) if wait_for
end
|
#show_progress ⇒ Object
140
141
142
143
|
# File 'lib/chef/knife/oci_server_delete.rb', line 140
def show_progress
print ui.color('.', :magenta)
$stdout.flush
end
|
#terminate_instance(instance_id) ⇒ Object
70
71
72
73
74
|
# File 'lib/chef/knife/oci_server_delete.rb', line 70
def terminate_instance(instance_id)
compute_client.terminate_instance(instance_id)
ui.msg "Initiated delete of instance #{instance_id}"
end
|
#validate_wait ⇒ Object
114
115
116
117
118
119
120
121
|
# File 'lib/chef/knife/oci_server_delete.rb', line 114
def validate_wait
wait_for = nil
if config[:wait]
wait_for = Integer(config[:wait])
error_and_exit 'Wait value must be 0 or greater' if wait_for < 0
end
wait_for
end
|
#wait_for_instance_terminated(instance_id, wait_for) ⇒ Object
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
|
# File 'lib/chef/knife/oci_server_delete.rb', line 93
def wait_for_instance_terminated(instance_id, wait_for)
print ui.color('Waiting for instance to terminate...', :magenta)
begin
begin
compute_client.get_instance(instance_id).wait_until(:lifecycle_state,
OCI::Core::Models::Instance::LIFECYCLE_STATE_TERMINATED,
get_wait_options(wait_for)) do
show_progress
end
ensure
end_progress_indicator
end
rescue OCI::Waiter::Errors::MaximumWaitTimeExceededError
error_and_exit 'Timeout exceeded while waiting for instance to terminate'
rescue OCI::Errors::ServiceError => service_error
raise unless service_error.service_code == 'NotAuthorizedOrNotFound'
ui.warn 'Instance not authorized or not found'
end
end
|