summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordanielsdeleo <dan@chef.io>2015-09-22 17:22:32 -0700
committerdanielsdeleo <dan@chef.io>2015-09-24 12:37:26 -0700
commit4141c33df8e31b78359fb316315547d36f837df6 (patch)
tree20fdac7fbca02cffe9fc873b6f5592011b3f525e
parente92665d448eaaf92fdfb5bb3cdb4be3a2ddad9ac (diff)
downloadchef-4141c33df8e31b78359fb316315547d36f837df6.tar.gz
Add --policy-name and --policy-group opts to knife bootstrap
-rw-r--r--lib/chef/knife/bootstrap.rb35
-rw-r--r--spec/unit/knife/bootstrap_spec.rb58
2 files changed, 90 insertions, 3 deletions
diff --git a/lib/chef/knife/bootstrap.rb b/lib/chef/knife/bootstrap.rb
index f173b6b909..93236225a2 100644
--- a/lib/chef/knife/bootstrap.rb
+++ b/lib/chef/knife/bootstrap.rb
@@ -143,6 +143,16 @@ class Chef
:proc => lambda { |o| o.split(/[\s,]+/) },
:default => []
+ option :policy_name,
+ :long => "--policy-name POLICY_NAME",
+ :description => "Policyfile name to use (--policy-group must also be given)",
+ :default => nil
+
+ option :policy_group,
+ :long => "--policy-group POLICY_GROUP",
+ :description => "Policy group name to use (--policy-name must also be given)",
+ :default => nil
+
option :tags,
:long => "--tags TAGS",
:description => "Comma separated list of tags to apply to the node",
@@ -315,6 +325,7 @@ class Chef
def run
validate_name_args!
+ validate_options!
$stdout.sync = true
@@ -363,6 +374,17 @@ class Chef
end
end
+ def validate_options!
+ if incomplete_policyfile_options?
+ ui.error("--policy-name and --policy-group must be specified together")
+ exit 1
+ elsif policyfile_and_run_list_given?
+ ui.error("Policyfile options and --run-list are exclusive")
+ exit 1
+ end
+ true
+ end
+
def knife_ssh
ssh = Chef::Knife::Ssh.new
ssh.ui = ui
@@ -395,6 +417,19 @@ class Chef
command
end
+
+ private
+
+ # True if policy_name and run_list are both given
+ def policyfile_and_run_list_given?
+ !config[:run_list].empty? && !!config[:policy_name]
+ end
+
+ # True if one of policy_name or policy_group was given, but not both
+ def incomplete_policyfile_options?
+ (!!config[:policy_name] ^ config[:policy_group])
+ end
+
end
end
end
diff --git a/spec/unit/knife/bootstrap_spec.rb b/spec/unit/knife/bootstrap_spec.rb
index 0195e6d406..48aae3e61b 100644
--- a/spec/unit/knife/bootstrap_spec.rb
+++ b/spec/unit/knife/bootstrap_spec.rb
@@ -250,14 +250,14 @@ describe Chef::Knife::Bootstrap do
it "should create a hint file when told to" do
knife.parse_options(["--hint", "openstack"])
knife.merge_configs
- expect(knife.render_template).to match /\/etc\/chef\/ohai\/hints\/openstack.json/
+ expect(knife.render_template).to match(/\/etc\/chef\/ohai\/hints\/openstack.json/)
end
it "should populate a hint file with JSON when given a file to read" do
allow(::File).to receive(:read).and_return('{ "foo" : "bar" }')
knife.parse_options(["--hint", "openstack=hints/openstack.json"])
knife.merge_configs
- expect(knife.render_template).to match /\{\"foo\":\"bar\"\}/
+ expect(knife.render_template).to match(/\{\"foo\":\"bar\"\}/)
end
end
@@ -395,6 +395,58 @@ describe Chef::Knife::Bootstrap do
end
end
+ describe "handling policyfile options" do
+
+ context "when only policy_name is given" do
+
+ let(:bootstrap_cli_options) { %w[ --policy-name my-app-server ] }
+
+ it "returns an error stating that policy_name and policy_group must be given together" do
+ expect { knife.validate_options! }.to raise_error(SystemExit)
+ expect(stderr.string).to include("ERROR: --policy-name and --policy-group must be specified together")
+ end
+
+ end
+
+ context "when only policy_group is given" do
+
+ let(:bootstrap_cli_options) { %w[ --policy-group staging ] }
+
+ it "returns an error stating that policy_name and policy_group must be given together" do
+ expect { knife.validate_options! }.to raise_error(SystemExit)
+ expect(stderr.string).to include("ERROR: --policy-name and --policy-group must be specified together")
+ end
+
+ end
+
+ context "when both policy_name and policy_group are given, but run list is also given" do
+
+ let(:bootstrap_cli_options) { %w[ --policy-name my-app --policy-group staging --run-list cookbook ] }
+
+ it "returns an error stating that policyfile and run_list are exclusive" do
+ expect { knife.validate_options! }.to raise_error(SystemExit)
+ expect(stderr.string).to include("ERROR: Policyfile options and --run-list are exclusive")
+ end
+
+ end
+
+ context "when policy_name and policy_group are given with no conflicting options" do
+
+ let(:bootstrap_cli_options) { %w[ --policy-name my-app --policy-group staging ] }
+
+ it "passes options validation" do
+ expect { knife.validate_options! }.to_not raise_error
+ end
+
+ it "passes them into the bootstrap context" do
+ expect(knife.bootstrap_context.first_boot).to have_key(:policy_name)
+ expect(knife.bootstrap_context.first_boot).to have_key(:policy_group)
+ end
+
+ end
+
+ end
+
describe "when configuring the underlying knife ssh command" do
context "from the command line" do
let(:knife_ssh) do
@@ -525,7 +577,7 @@ describe Chef::Knife::Bootstrap do
it "verifies that a server to bootstrap was given as a command line arg" do
knife.name_args = nil
expect { knife.run }.to raise_error(SystemExit)
- expect(stderr.string).to match /ERROR:.+FQDN or ip/
+ expect(stderr.string).to match(/ERROR:.+FQDN or ip/)
end
describe "when running the bootstrap" do