diff options
author | Daniel DeLeo <dan@opscode.com> | 2011-04-27 09:01:56 -0700 |
---|---|---|
committer | Daniel DeLeo <dan@opscode.com> | 2011-04-27 13:40:44 -0700 |
commit | 2fe7c40e8d70c3386cb1d73c144c8145b0074647 (patch) | |
tree | 730e9b6fe5c8d5a4fbd8a71b0bb701eb457bef7d | |
parent | 54ee051e72bc6abf94af6ffdd992f5e7de81d375 (diff) | |
download | chef-2fe7c40e8d70c3386cb1d73c144c8145b0074647.tar.gz |
[CHEF-2219] add a bootstrap context class
centralizes some repeated code, provides a foundation to refactor
bootstrap along these lines more.
-rw-r--r-- | chef/lib/chef/knife/core/bootstrap_context.rb | 81 | ||||
-rw-r--r-- | chef/spec/unit/knife/core/bootstrap_context_spec.rb | 104 |
2 files changed, 185 insertions, 0 deletions
diff --git a/chef/lib/chef/knife/core/bootstrap_context.rb b/chef/lib/chef/knife/core/bootstrap_context.rb new file mode 100644 index 0000000000..26510851c7 --- /dev/null +++ b/chef/lib/chef/knife/core/bootstrap_context.rb @@ -0,0 +1,81 @@ +# +# Author:: Daniel DeLeo (<dan@opscode.com>) +# Copyright:: Copyright (c) 2011 Opscode, Inc. +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +require 'chef/run_list' +class Chef + class Knife + module Core + # Instances of BootstrapContext are the context objects (i.e., +self+) for + # bootstrap templates. For backwards compatability, they +must+ set the + # following instance variables: + # * @config - a hash of knife's config values + # * @run_list - the run list for the node to boostrap + # + class BootstrapContext + + def initialize(config, run_list, chef_config) + @config = config + @run_list = run_list + @chef_config = chef_config + end + + def bootstrap_version_string + if @config[:prerelease] + "--prerelease" + else + version = knife_config[:bootstrap_version] || Chef::VERSION + "--version #{version}" + end + end + + def bootstrap_environment + @chef_config[:environment] || '_default' + end + + def validation_key + IO.read(@chef_config[:validation_key]) + end + + def config_content + client_rb = <<-CONFIG +log_level :info +log_location STDOUT +chef_server_url "#{@chef_config[:chef_server_url]}" +validation_client_name "#{@chef_config[:validation_client_name]}" +CONFIG + if @config[:chef_node_name] + client_rb << %Q{node_name "#{@config[:chef_node_name]}"\n} + else + client_rb << "# Using default node name (fqdn)\n" + end + client_rb + end + + def start_chef + "/usr/bin/chef-client -j /etc/chef/first-boot.json -E #{bootstrap_environment}" + end + + def knife_config + @chef_config.key?(:knife) ? @chef_config[:knife] : {} + end + + end + end + end +end + diff --git a/chef/spec/unit/knife/core/bootstrap_context_spec.rb b/chef/spec/unit/knife/core/bootstrap_context_spec.rb new file mode 100644 index 0000000000..0f85d9a173 --- /dev/null +++ b/chef/spec/unit/knife/core/bootstrap_context_spec.rb @@ -0,0 +1,104 @@ +# +# Author:: Daniel DeLeo (<dan@opscode.com>) +# Copyright:: Copyright (c) 2011 Opscode, Inc. +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +require File.expand_path('../../../../spec_helper', __FILE__) +require 'chef/knife/core/bootstrap_context' + +describe Chef::Knife::Core::BootstrapContext do + before do + @config = {:foo => :bar} + @run_list = Chef::RunList.new('recipe[tmux]', 'role[base]') + @chef_config = {:validation_key => File.join(CHEF_SPEC_DATA, 'ssl', 'private_key.pem')} + @chef_config[:chef_server_url] = 'http://chef.example.com:4444' + @chef_config[:validation_client_name] = 'chef-validator-testing' + @context = Chef::Knife::Core::BootstrapContext.new(@config, @run_list, @chef_config) + end + + describe "to support compatability with existing templates" do + it "sets the @config instance variable" do + @context.instance_eval { @config }.should == {:foo => :bar} + end + + it "sets the @run_list instance variable" do + @context.instance_eval { @run_list }.should equal(@run_list) + end + end + + it "installs the same version of chef on the remote host" do + @context.bootstrap_version_string.should == "--version #{Chef::VERSION}" + end + + it "runs chef with the first-boot.json in the _default environment" do + @context.start_chef.should == "/usr/bin/chef-client -j /etc/chef/first-boot.json -E _default" + end + + it "reads the validation key" do + @context.validation_key.should == IO.read(File.join(CHEF_SPEC_DATA, 'ssl', 'private_key.pem')) + end + + it "generates the config file data" do + expected=<<-EXPECTED +log_level :info +log_location STDOUT +chef_server_url "http://chef.example.com:4444" +validation_client_name "chef-validator-testing" +# Using default node name (fqdn) +EXPECTED + @context.config_content.should == expected + end + + describe "when an explicit node name is given" do + before do + @config[:chef_node_name] = 'foobar.example.com' + end + it "sets the node name in the client.rb" do + @context.config_content.should match(/node_name "foobar\.example\.com"/) + end + end + + describe "when bootstrapping into a specific environment" do + before do + @chef_config[:environment] = "prodtastic" + end + + it "starts chef in the configured environment" do + @context.start_chef.should == '/usr/bin/chef-client -j /etc/chef/first-boot.json -E prodtastic' + end + end + + describe "when installing a prerelease version of chef" do + before do + @config[:prerelease] = true + end + it "supplies --prerelease as the version string" do + @context.bootstrap_version_string.should == '--prerelease' + end + end + + describe "when installing an explicit version of chef" do + before do + @context = Chef::Knife::Core::BootstrapContext.new(@config, @run_list, :knife => { :bootstrap_version => '123.45.678' }) + end + + it "gives --version $VERSION as the version string" do + @context.bootstrap_version_string.should == '--version 123.45.678' + end + end + +end + |