Skip to content

Commit

Permalink
Merge pull request chef#54 from opscode/adamed-oc-9390
Browse files Browse the repository at this point in the history
OC-9390: Handle exit of knife plugin, with correct exit status and from common place in code
  • Loading branch information
Adam Edwards committed Aug 20, 2013
2 parents 8740709 + 433d2da commit 503e0d3
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 32 deletions.
26 changes: 17 additions & 9 deletions lib/chef/knife/openstack_server_create.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
require 'chef/knife/cloud/openstack_server_create_options'
require 'chef/knife/cloud/openstack_service'
require 'chef/knife/cloud/openstack_service_options'
require 'chef/knife/cloud/exceptions'

class Chef
class Knife
Expand Down Expand Up @@ -70,11 +71,17 @@ def after_exec_command
#floating requested without value
if floating_address.nil?
free_floating = addresses.find_index {|a| a.fixed_ip.nil?}
if free_floating.nil? #no free floating IP found
ui.error("Unable to assign a Floating IP from allocated IPs.")
exit 1
else
floating_address = addresses[free_floating].ip
begin
if free_floating.nil? #no free floating IP found
error_message = "Unable to assign a Floating IP from allocated IPs."
ui.fatal(error_message)
raise CloudExceptions::ServerSetupError, error_message
else
floating_address = addresses[free_floating].ip
end
rescue CloudExceptions::ServerSetupError => e
cleanup_on_failure
raise e
end
end
server.associate_address(floating_address)
Expand All @@ -97,8 +104,9 @@ def before_bootstrap
bootstrap_ip_address = primary_private_ip_address(server.addresses) if config[:private_network]
Chef::Log.debug("Bootstrap IP Address: #{bootstrap_ip_address}")
if bootstrap_ip_address.nil?
ui.error("No IP address available for bootstrapping.")
raise "No IP address available for bootstrapping."
error_message = "No IP address available for bootstrapping."
ui.error(error_message)
raise CloudExceptions::BootstrapError, error_message
end
config[:bootstrap_ip_address] = bootstrap_ip_address
end
Expand All @@ -110,8 +118,8 @@ def validate_params!
errors << "You must provide SSH Key." if locate_config_value(:bootstrap_protocol) == 'ssh' && !locate_config_value(:identity_file).nil? && locate_config_value(:openstack_ssh_key_id).nil?

errors << "You must provide --image-os-type option [windows/linux]" if ! (%w(windows linux).include?(locate_config_value(:image_os_type)))

exit 1 if errors.each{|e| ui.error(e)}.any?
error_message = ""
raise CloudExceptions::ValidationError, error_message if errors.each{|e| ui.error(e); error_message = "#{error_message} #{e}."}.any?
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions spec/unit/openstack_flavor_list_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,19 @@
it "raise error on openstack_username missing" do
Chef::Config[:knife].delete(:openstack_username)
instance.ui.should_receive(:error).with("You did not provide a valid 'Openstack Username' value.")
instance.validate!
expect { instance.validate! }.to raise_error(Chef::Knife::Cloud::CloudExceptions::ValidationError)
end

it "raise error on openstack_password missing" do
Chef::Config[:knife].delete(:openstack_password)
instance.ui.should_receive(:error).with("You did not provide a valid 'Openstack Password' value.")
instance.validate!
expect { instance.validate! }.to raise_error(Chef::Knife::Cloud::CloudExceptions::ValidationError)
end

it "raise error on openstack_auth_url missing" do
Chef::Config[:knife].delete(:openstack_auth_url)
instance.ui.should_receive(:error).with("You did not provide a valid 'Openstack Auth Url' value.")
instance.validate!
expect { instance.validate! }.to raise_error(Chef::Knife::Cloud::CloudExceptions::ValidationError)
end
end

Expand Down
6 changes: 3 additions & 3 deletions spec/unit/openstack_group_list_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,19 @@
it "raise error on openstack_username missing" do
Chef::Config[:knife].delete(:openstack_username)
instance.ui.should_receive(:error).with("You did not provide a valid 'Openstack Username' value.")
instance.validate!
expect { instance.validate! }.to raise_error(Chef::Knife::Cloud::CloudExceptions::ValidationError)
end

it "raise error on openstack_password missing" do
Chef::Config[:knife].delete(:openstack_password)
instance.ui.should_receive(:error).with("You did not provide a valid 'Openstack Password' value.")
instance.validate!
expect { instance.validate! }.to raise_error(Chef::Knife::Cloud::CloudExceptions::ValidationError)
end

it "raise error on openstack_auth_url missing" do
Chef::Config[:knife].delete(:openstack_auth_url)
instance.ui.should_receive(:error).with("You did not provide a valid 'Openstack Auth Url' value.")
instance.validate!
expect { instance.validate! }.to raise_error(Chef::Knife::Cloud::CloudExceptions::ValidationError)
end
end

Expand Down
6 changes: 3 additions & 3 deletions spec/unit/openstack_image_list_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,19 @@
it "raise error on openstack_username missing" do
Chef::Config[:knife].delete(:openstack_username)
instance.ui.should_receive(:error).with("You did not provide a valid 'Openstack Username' value.")
instance.validate!
expect { instance.validate! }.to raise_error(Chef::Knife::Cloud::CloudExceptions::ValidationError)
end

it "raise error on openstack_password missing" do
Chef::Config[:knife].delete(:openstack_password)
instance.ui.should_receive(:error).with("You did not provide a valid 'Openstack Password' value.")
instance.validate!
expect { instance.validate! }.to raise_error(Chef::Knife::Cloud::CloudExceptions::ValidationError)
end

it "raise error on openstack_auth_url missing" do
Chef::Config[:knife].delete(:openstack_auth_url)
instance.ui.should_receive(:error).with("You did not provide a valid 'Openstack Auth Url' value.")
instance.validate!
expect { instance.validate! }.to raise_error(Chef::Knife::Cloud::CloudExceptions::ValidationError)
end
end

Expand Down
16 changes: 8 additions & 8 deletions spec/unit/openstack_server_create_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,36 +37,36 @@
it "raise error on openstack_username missing and exit immediately." do
Chef::Config[:knife].delete(:openstack_username)
instance.ui.should_receive(:error).with("You did not provide a valid 'Openstack Username' value.")
instance.validate!
expect { instance.validate! }.to raise_error(Chef::Knife::Cloud::CloudExceptions::ValidationError)
end

it "raise error on openstack_auth_url missing and exit immediately." do
Chef::Config[:knife].delete(:openstack_auth_url)
instance.ui.should_receive(:error).with("You did not provide a valid 'Openstack Auth Url' value.")
instance.validate!
expect { instance.validate! }.to raise_error(Chef::Knife::Cloud::CloudExceptions::ValidationError)
end

it "validates ssh params" do
Chef::Config[:knife][:image_os_type] = "linux"
Chef::Config[:knife][:bootstrap_protocol] = "ssh"
instance.ui.should_receive(:error).with("You must provide either Identity file or SSH Password.")
instance.validate_params!
expect { instance.validate_params! }.to raise_error(Chef::Knife::Cloud::CloudExceptions::ValidationError)
end

it "raise_error on invalid image_os_type params" do
Chef::Config[:knife][:ssh_password] = "ssh_password"
Chef::Config[:knife][:openstack_ssh_key_id] = "ssh_key"
Chef::Config[:knife][:image_os_type] = "other_than_windows_linux"
instance.ui.should_receive(:error).with("You must provide --image-os-type option [windows/linux]")
instance.validate_params!
expect { instance.validate_params! }.to raise_error(Chef::Knife::Cloud::CloudExceptions::ValidationError)
end

it "raise_error on mising image_os_type params" do
Chef::Config[:knife][:image_os_type] = "other_than_windows_linux"
Chef::Config[:knife][:ssh_password] = "ssh_password"
Chef::Config[:knife][:openstack_ssh_key_id] = "ssh_key"
instance.ui.should_receive(:error).with("You must provide --image-os-type option [windows/linux]")
instance.validate_params!
expect { instance.validate_params! }.to raise_error(Chef::Knife::Cloud::CloudExceptions::ValidationError)
end

context "bootstrap protocol: Ssh " do
Expand All @@ -79,14 +79,14 @@
Chef::Config[:knife][:identity_file] = nil
Chef::Config[:knife][:ssh_password] = nil
instance.ui.should_receive(:error).with("You must provide either Identity file or SSH Password.")
instance.validate_params!
expect { instance.validate_params! }.to raise_error(Chef::Knife::Cloud::CloudExceptions::ValidationError)
end

it "raise error when Identity file is provided but SSH key is not provided and exits immediately." do
Chef::Config[:knife][:identity_file] = "identity_file_path"
Chef::Config[:knife][:openstack_ssh_key_id] = nil
instance.ui.should_receive(:error).with("You must provide SSH Key.")
instance.validate_params!
expect { instance.validate_params! }.to raise_error(Chef::Knife::Cloud::CloudExceptions::ValidationError)
end

it "validates gracefully when SSH password is provided." do
Expand Down Expand Up @@ -132,7 +132,7 @@
Chef::Config[:knife][:winrm_password] = nil
instance.config[:winrm_password] = nil
instance.ui.should_receive(:error).with("You must provide Winrm Password.")
instance.validate_params!
expect { instance.validate_params! }.to raise_error(Chef::Knife::Cloud::CloudExceptions::ValidationError)
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions spec/unit/openstack_server_delete_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,19 @@
it "raise error on openstack_username missing" do
Chef::Config[:knife].delete(:openstack_username)
instance.ui.should_receive(:error).with("You did not provide a valid 'Openstack Username' value.")
instance.validate!
expect { instance.validate! }.to raise_error(Chef::Knife::Cloud::CloudExceptions::ValidationError)
end

it "raise error on openstack_password missing" do
Chef::Config[:knife].delete(:openstack_password)
instance.ui.should_receive(:error).with("You did not provide a valid 'Openstack Password' value.")
instance.validate!
expect { instance.validate! }.to raise_error(Chef::Knife::Cloud::CloudExceptions::ValidationError)
end

it "raise error on openstack_auth_url missing" do
Chef::Config[:knife].delete(:openstack_auth_url)
instance.ui.should_receive(:error).with("You did not provide a valid 'Openstack Auth Url' value.")
instance.validate!
expect { instance.validate! }.to raise_error(Chef::Knife::Cloud::CloudExceptions::ValidationError)
end
end

Expand Down
6 changes: 3 additions & 3 deletions spec/unit/openstack_server_list_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,19 @@
it "raise error on openstack_username missing" do
Chef::Config[:knife].delete(:openstack_username)
instance.ui.should_receive(:error).with("You did not provide a valid 'Openstack Username' value.")
instance.validate!
expect { instance.validate! }.to raise_error(Chef::Knife::Cloud::CloudExceptions::ValidationError)
end

it "raise error on openstack_password missing" do
Chef::Config[:knife].delete(:openstack_password)
instance.ui.should_receive(:error).with("You did not provide a valid 'Openstack Password' value.")
instance.validate!
expect { instance.validate! }.to raise_error(Chef::Knife::Cloud::CloudExceptions::ValidationError)
end

it "raise error on openstack_auth_url missing" do
Chef::Config[:knife].delete(:openstack_auth_url)
instance.ui.should_receive(:error).with("You did not provide a valid 'Openstack Auth Url' value.")
instance.validate!
expect { instance.validate! }.to raise_error(Chef::Knife::Cloud::CloudExceptions::ValidationError)
end
end

Expand Down

0 comments on commit 503e0d3

Please sign in to comment.