Skip to content

Commit

Permalink
Validate that node_name & client_key are set when running plugins.
Browse files Browse the repository at this point in the history
This gives the user a hint that their local knife.rb is not currently
set up enough (or correctly) and where to go for help (the project
website).

Closes #10
Closes #9
  • Loading branch information
fnichol committed Sep 12, 2014
1 parent 1eeddd8 commit f58d1b1
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 4 deletions.
21 changes: 21 additions & 0 deletions lib/chef/knife/server_bootstrap_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,29 @@ def self.included(included_class) # rubocop:disable Metrics/MethodLength
end
end

def run
validate!
end

private

def validate!
knife_fail = "You did not set {{KEY}} in your knife.rb, which is a " \
"required setting. Please generate an initial knife.rb or read " \
"the setup instructions at http://fnichol.github.io/knife-server/"

# rubocop:disable Style/DeprecatedHashMethods
if !Chef::Config.has_key?(:node_name)
ui.error knife_fail.gsub(/{{KEY}}/, "node_name")
exit 1
end
if !Chef::Config.has_key?(:client_key)
ui.error knife_fail.gsub(/{{KEY}}/, "client_key")
exit 1
end
# rubocop:enable Style/DeprecatedHashMethods
end

def fetch_validation_key
credentials_client.install_validation_key
end
Expand Down
4 changes: 3 additions & 1 deletion lib/chef/knife/server_bootstrap_ec2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class ServerBootstrapEc2 < Knife
:proc => proc { |groups| groups.split(",") }

def run
validate!
super
config_security_group
ec2_bootstrap.run
fetch_validation_key
Expand Down Expand Up @@ -104,6 +104,8 @@ def server_dns_name
private

def validate!
super

if config[:chef_node_name].nil?
ui.error "You did not provide a valid --node-name value."
exit 1
Expand Down
4 changes: 3 additions & 1 deletion lib/chef/knife/server_bootstrap_linode.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class ServerBootstrapLinode < Knife
end

def run
validate!
super
linode_bootstrap.run
fetch_validation_key
create_root_client
Expand Down Expand Up @@ -92,6 +92,8 @@ def server_ip_address
private

def validate!
super

if config[:chef_node_name].nil?
ui.error "You did not provide a valid --node-name value."
exit 1
Expand Down
4 changes: 3 additions & 1 deletion lib/chef/knife/server_bootstrap_openstack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class ServerBootstrapOpenstack < Knife
end

def run
validate!
super
openstack_bootstrap.run
fetch_validation_key
create_root_client
Expand Down Expand Up @@ -96,6 +96,8 @@ def server_ip_address
private

def validate!
super

if config[:chef_node_name].nil?
ui.error "You did not provide a valid --node-name value."
exit 1
Expand Down
4 changes: 3 additions & 1 deletion lib/chef/knife/server_bootstrap_standalone.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class ServerBootstrapStandalone < Knife
:description => "Hostname or IP address of host to bootstrap"

def run
validate!
super
check_ssh_connection
standalone_bootstrap.run
fetch_validation_key
Expand All @@ -73,6 +73,8 @@ def standalone_bootstrap
private

def validate!
super

if config[:chef_node_name].nil?
ui.error "You did not provide a valid --node-name value."
exit 1
Expand Down
12 changes: 12 additions & 0 deletions spec/chef/knife/server_bootstrap_ec2_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,18 @@
let(:ssh) { double }
let(:credentials) { double.as_null_object }

it "exits if Chef::Config[:node_name] is missing" do
Chef::Config.delete(:node_name)

expect { @knife.run }.to raise_error SystemExit
end

it "exits if Chef::Config[:client_key] is missing" do
Chef::Config.delete(:client_key)

expect { @knife.run }.to raise_error SystemExit
end

it "exits if node_name option is missing" do
def @knife.exit(_); end
@knife.config.delete(:chef_node_name)
Expand Down
12 changes: 12 additions & 0 deletions spec/chef/knife/server_bootstrap_linode_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,18 @@
let(:ssh) { double }
let(:credentials) { double.as_null_object }

it "exits if Chef::Config[:node_name] is missing" do
Chef::Config.delete(:node_name)

expect { @knife.run }.to raise_error SystemExit
end

it "exits if Chef::Config[:client_key] is missing" do
Chef::Config.delete(:client_key)

expect { @knife.run }.to raise_error SystemExit
end

it "exits if node_name option is missing" do
@knife.config.delete(:chef_node_name)

Expand Down
6 changes: 6 additions & 0 deletions spec/chef/knife/server_bootstrap_openstack_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,12 @@
let(:ssh) { double }
let(:credentials) { double.as_null_object }

it "exits if Chef::Config[:client_key] is missing" do
Chef::Config.delete(:client_key)

expect { @knife.run }.to raise_error SystemExit
end

it "exits if node_name option is missing" do
@knife.config.delete(:chef_node_name)

Expand Down
12 changes: 12 additions & 0 deletions spec/chef/knife/server_bootstrap_standalone_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,18 @@
let(:ssh) { double(:exec! => true) }
let(:credentials) { double.as_null_object }

it "exits if Chef::Config[:node_name] is missing" do
Chef::Config.delete(:node_name)

expect { @knife.run }.to raise_error SystemExit
end

it "exits if Chef::Config[:client_key] is missing" do
Chef::Config.delete(:client_key)

expect { @knife.run }.to raise_error SystemExit
end

it "exits if node_name option is missing" do
@knife.config.delete(:chef_node_name)

Expand Down

0 comments on commit f58d1b1

Please sign in to comment.