From 622feda734f351461255ddbd222ff1b633b2ff9c Mon Sep 17 00:00:00 2001 From: Serdar Sutay Date: Thu, 21 Aug 2014 12:40:42 -0700 Subject: Deprecate --distro and --template-file options in favor of --template. --- lib/chef/config.rb | 1 + lib/chef/knife/bootstrap.rb | 61 +++++++++++++++++++++++++-------------- spec/unit/knife/bootstrap_spec.rb | 10 +++---- 3 files changed, 44 insertions(+), 28 deletions(-) diff --git a/lib/chef/config.rb b/lib/chef/config.rb index c1de4546ac..85bea50a99 100644 --- a/lib/chef/config.rb +++ b/lib/chef/config.rb @@ -505,6 +505,7 @@ class Chef default :ssh_gateway, nil default :bootstrap_version, nil default :bootstrap_proxy, nil + default :bootstrap_template, "chef-full" default :identity_file, nil default :host_key_verify, nil default :forward_agent, nil diff --git a/lib/chef/knife/bootstrap.rb b/lib/chef/knife/bootstrap.rb index a055a75981..9a28833471 100644 --- a/lib/chef/knife/bootstrap.rb +++ b/lib/chef/knife/bootstrap.rb @@ -90,11 +90,21 @@ class Chef :description => "Do not proxy locations for the node being bootstrapped; this option is used internally by Opscode", :proc => Proc.new { |np| Chef::Config[:knife][:bootstrap_no_proxy] = np } + # DEPR: Remove this option in Chef 13 option :distro, :short => "-d DISTRO", :long => "--distro DISTRO", - :description => "Bootstrap a distro using a template", - :default => "chef-full" + :description => "Bootstrap a distro using a template. [DEPRECATED] Use -t / --template option instead.", + :proc => Proc.new { |t| + Chef::Log.warn("[DEPRECATED] -d / --distro option is deprecated. Use -t / --template option instead.") + Chef::Config[:knife][:bootstrap_template] = t + } + + option :template, + :short => "-t TEMPLATE", + :long => "--template TEMPLATE", + :description => "Bootstrap Chef using a built-in or custom template. Set to the full path of an erb template or use one of the built-in templates.", + :proc => Proc.new { |t| Chef::Config[:knife][:bootstrap_template] = t } option :use_sudo, :long => "--sudo", @@ -106,10 +116,14 @@ class Chef :description => "Execute the bootstrap via sudo with password", :boolean => false + # DEPR: Remove this option in Chef 13 option :template_file, :long => "--template-file TEMPLATE", - :description => "Full path to location of template to use", - :default => false + :description => "Full path to location of template to use. [DEPRECATED] Use -t / --template option instead.", + :proc => Proc.new { |t| + Chef::Log.warn("[DEPRECATED] --template-file option is deprecated. Use -t / --template option instead.") + Chef::Config[:knife][:bootstrap_template] = t + } option :run_list, :short => "-r RUN_LIST", @@ -137,7 +151,8 @@ class Chef :proc => Proc.new { |h| Chef::Config[:knife][:hints] ||= Hash.new name, path = h.split("=") - Chef::Config[:knife][:hints][name] = path ? Chef::JSONCompat.parse(::File.read(path)) : Hash.new } + Chef::Config[:knife][:hints][name] = path ? Chef::JSONCompat.parse(::File.read(path)) : Hash.new + } option :secret, :short => "-s SECRET", @@ -170,32 +185,34 @@ class Chef :description => "Add options to curl when install chef-client", :proc => Proc.new { |co| Chef::Config[:knife][:bootstrap_curl_options] = co } - def find_template(template=nil) - # Are we bootstrapping using an already shipped template? - if config[:template_file] - bootstrap_files = config[:template_file] - else - bootstrap_files = [] - bootstrap_files << File.join(File.dirname(__FILE__), 'bootstrap', "#{config[:distro]}.erb") - bootstrap_files << File.join(Knife.chef_config_dir, "bootstrap", "#{config[:distro]}.erb") if Knife.chef_config_dir - bootstrap_files << File.join(ENV['HOME'], '.chef', 'bootstrap', "#{config[:distro]}.erb") if ENV['HOME'] - bootstrap_files << Gem.find_files(File.join("chef","knife","bootstrap","#{config[:distro]}.erb")) - bootstrap_files.flatten! - end + def find_template + template = Chef::Config[:knife][:bootstrap_template] + + # Use the template directly if it's a path to an actual file + return template if File.exists?(template) + + # Otherwise search the template directories until we find the right one + + bootstrap_files = [] + bootstrap_files << File.join(File.dirname(__FILE__), 'bootstrap', "#{template}.erb") + bootstrap_files << File.join(Knife.chef_config_dir, "bootstrap", "#{template}.erb") if Knife.chef_config_dir + bootstrap_files << File.join(ENV['HOME'], '.chef', 'bootstrap', "#{template}.erb") if ENV['HOME'] + bootstrap_files << Gem.find_files(File.join("chef","knife","bootstrap","#{template}.erb")) + bootstrap_files.flatten! - template = Array(bootstrap_files).find do |bootstrap_template| + template_file = Array(bootstrap_files).find do |bootstrap_template| Chef::Log.debug("Looking for bootstrap template in #{File.dirname(bootstrap_template)}") File.exists?(bootstrap_template) end - unless template - ui.info("Can not find bootstrap definition for #{config[:distro]}") + unless template_file + ui.info("Can not find bootstrap definition for #{config[:template]}") raise Errno::ENOENT end Chef::Log.debug("Found bootstrap template in #{File.dirname(template)}") - template + template_file end def render_template(template=nil) @@ -210,7 +227,7 @@ class Chef def run validate_name_args! warn_chef_config_secret_key - @template_file = find_template(config[:bootstrap_template]) + @template_file = find_template @node_name = Array(@name_args).first # back compat--templates may use this setting: config[:server_name] = @node_name diff --git a/spec/unit/knife/bootstrap_spec.rb b/spec/unit/knife/bootstrap_spec.rb index 999a845c83..6a65fbde53 100644 --- a/spec/unit/knife/bootstrap_spec.rb +++ b/spec/unit/knife/bootstrap_spec.rb @@ -39,8 +39,7 @@ describe Chef::Knife::Bootstrap do end it "should error if template can not be found" do - @knife.config[:template_file] = false - @knife.config[:distro] = 'penultimate' + Chef::Config[:knife][:bootstrap_template] = 'penultimate' lambda { @knife.find_template }.should raise_error end @@ -57,7 +56,7 @@ describe Chef::Knife::Bootstrap do end it "should load the specified template" do - @knife.config[:distro] = 'fedora13-gems' + @knife.config[:template] = 'fedora13-gems' lambda { @knife.find_template }.should_not raise_error end @@ -66,7 +65,7 @@ describe Chef::Knife::Bootstrap do Gem.stub(:find_files).and_return(["/Users/schisamo/.rvm/gems/ruby-1.9.2-p180@chef-0.10/gems/knife-windows-0.5.4/lib/chef/knife/bootstrap/fake-bootstrap-template.erb"]) File.stub(:exists?).and_return(true) IO.stub(:read).and_return('random content') - @knife.config[:distro] = 'fake-bootstrap-template' + @knife.config[:template] = 'fake-bootstrap-template' lambda { @knife.find_template }.should_not raise_error end @@ -253,9 +252,8 @@ describe Chef::Knife::Bootstrap do end context "validating use_sudo_password" do before do - @knife.config[:distro] = "ubuntu" @knife.config[:ssh_password] = "password" - @knife.stub(:read_template).and_return(IO.read(@knife.find_template).chomp) + @knife.stub(:read_template).and_return("This is the template contents.") end it "use_sudo_password contains description and long params for help" do -- cgit v1.2.1