Skip to content

Commit 4874ff3

Browse files
author
sdelano
committed
[CHEF-2195] bootstrap a specific version of Chef
1 parent e2f21ea commit 4874ff3

6 files changed

Lines changed: 113 additions & 6 deletions

File tree

‎chef/lib/chef/knife/bootstrap.rb‎

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#
1818

1919
require 'chef/knife'
20+
require 'erubis'
2021

2122
class Chef
2223
class Knife
@@ -25,7 +26,6 @@ class Bootstrap < Knife
2526
deps do
2627
require 'chef/json_compat'
2728
require 'tempfile'
28-
require 'erubis'
2929
require 'highline'
3030
require 'net/ssh'
3131
require 'net/ssh/multi'
@@ -65,6 +65,11 @@ class Bootstrap < Knife
6565
:long => "--prerelease",
6666
:description => "Install the pre-release chef gems"
6767

68+
option :bootstrap_version,
69+
:long => "--bootstrap-version",
70+
:description => "The version of Chef to install",
71+
:proc => lambda { |v| Chef::Config[:bootstrap_version] = v }
72+
6873
option :distro,
6974
:short => "-d DISTRO",
7075
:long => "--distro DISTRO",
@@ -188,7 +193,36 @@ def ssh_command
188193
command
189194
end
190195

196+
module TemplateHelper
197+
198+
#
199+
# == Chef::Knife::Bootstrap::TemplateHelper
200+
#
201+
# The methods in the TemplateHelper module expect to have access to
202+
# the instance varialbles set above as part of the context in the
203+
# Chef::Knife::Bootstrap#render_context method. Those instance
204+
# variables are:
205+
#
206+
# * @config - a hash of knife's config values
207+
# * @run_list - the run list for the node to boostrap
208+
#
209+
210+
::Erubis::Context.send(:include, Chef::Knife::Bootstrap::TemplateHelper)
211+
212+
def bootstrap_version_string(type=nil)
213+
version = Chef::Config[:bootstrap_version] || Chef::VERSION
214+
case type
215+
when :gems
216+
if @config[:prerelease]
217+
"--prerelease"
218+
else
219+
"--version #{version}"
220+
end
221+
else
222+
version
223+
end
224+
end
225+
end
191226
end
192227
end
193228
end
194-

‎chef/lib/chef/knife/bootstrap/archlinux-gems.erb‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ if [ ! -f /usr/bin/chef-client ]; then
33
pacman -Syy
44
pacman -S --noconfirm ruby ntp base-devel
55
ntpdate -u pool.ntp.org
6-
gem install ohai chef --no-rdoc --no-ri --verbose <%= '--prerelease' if @config[:prerelease] %>
6+
gem install ohai --no-rdoc --no-ri --verbose
7+
gem install chef --no-rdoc --no-ri --verbose <%= bootstrap_version_string(:gems) %>
78
fi
89

910
mkdir -p /etc/chef

‎chef/lib/chef/knife/bootstrap/centos5-gems.erb‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ if [ ! -f /usr/bin/chef-client ]; then
1212
ruby setup.rb --no-format-executable
1313
fi
1414

15-
gem install ohai chef --no-rdoc --no-ri --verbose <%= '--prerelease' if @config[:prerelease] %>
15+
gem update --system
16+
gem update
17+
gem install ohai --no-rdoc --no-ri --verbose
18+
gem install chef --no-rdoc --no-ri --verbose <%= bootstrap_version_string(:gems) %>
1619

1720
mkdir -p /etc/chef
1821

‎chef/lib/chef/knife/bootstrap/fedora13-gems.erb‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ yum install -q -y ruby ruby-devel gcc gcc-c++ automake autoconf rubygems make
33

44
gem update --system
55
gem update
6-
gem install ohai chef --no-rdoc --no-ri --verbose <%= '--prerelease' if @config[:prerelease] %>
6+
gem install ohai --no-rdoc --no-ri --verbose
7+
gem install chef --no-rdoc --no-ri --verbose <%= bootstrap_version_string(:gems) %>
78

89
mkdir -p /etc/chef
910

‎chef/lib/chef/knife/bootstrap/ubuntu10.04-gems.erb‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ if [ ! -f /usr/bin/chef-client ]; then
88
cd rubygems-1.3.7
99
ruby setup.rb --no-format-executable
1010
fi
11-
gem install ohai chef --no-rdoc --no-ri --verbose <%= '--prerelease' if @config[:prerelease] %>
11+
12+
gem install ohai --no-rdoc --no-ri --verbose
13+
gem install chef --no-rdoc --no-ri --verbose <%= bootstrap_version_string(:gems) %>
1214

1315
mkdir -p /etc/chef
1416

‎chef/spec/unit/knife/bootstrap_spec.rb‎

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,4 +150,70 @@
150150

151151
end
152152

153+
describe "render_template" do
154+
155+
end
156+
157+
end
158+
159+
describe Chef::Knife::Bootstrap::TemplateHelper do
160+
before(:each) do
161+
@context = Erubis::Context.new({
162+
:config => Hash.new
163+
})
164+
end
165+
166+
describe "bootstrap_version_string" do
167+
after(:each) do
168+
Chef::Config.delete :bootstrap_version
169+
end
170+
171+
formats = {
172+
:gems => /^--version \d+\.\d+\.\d+/,
173+
:nil => '\d+\.\d+\.\d+'
174+
}
175+
176+
context "by default" do
177+
formats.each do |sym, format|
178+
it "should return the current version of Chef for :#{sym.to_s}" do
179+
@context.bootstrap_version_string(sym).should include Chef::VERSION
180+
end
181+
182+
it "should match the correct output format for :#{sym.to_s}" do
183+
@context.bootstrap_version_string(sym).should match format
184+
end
185+
end
186+
end
187+
188+
context "with Chef::Config[:bootstrap_version] set" do
189+
before(:each) do
190+
@config_version = "0.9.12"
191+
Chef::Config[:bootstrap_version] = @config_version
192+
end
193+
194+
formats.each do |sym, format|
195+
it "should return the specified bootstrap version for :#{sym.to_s}" do
196+
@context.bootstrap_version_string(sym).should include @config_version
197+
end
198+
199+
it "should match the correct output format for :#{sym.to_s}" do
200+
@context.bootstrap_version_string(sym).should match format
201+
end
202+
end
203+
end
204+
205+
context "with config[:prerelease] set" do
206+
before(:each) do
207+
@context[:config][:prerelease] = true
208+
end
209+
210+
it "should return --prerelease only for :gems" do
211+
formats.each do |sym, format|
212+
version_string = @context.bootstrap_version_string(sym)
213+
version_string.should == "--prerelease" if sym == :gems
214+
version_string.should_not == "--prerelease" if sym != :gems
215+
end
216+
end
217+
end
218+
end
153219
end

0 commit comments

Comments
 (0)