Permalink
Browse files

[CHEF-3160] Allow Ohai hints to be set when using knife-bootstrap.

Ohai hints allow the user to provide ohai with hints
in the form of json files in /etc/chef/ohai/hints.  These
hints can then be used by plugins to provide better data
in cases where automatic detection of conditions is difficult.
  • Loading branch information...
1 parent 9224673 commit 388fc86f4ce65a08f3fabf647e99a622a0a5375e @stevendanna stevendanna committed with btm Jun 5, 2012
@@ -31,6 +31,9 @@ __knife__ __bootstrap__ _(options)_
Bootstrap a distro using a template
* `--[no-]host-key-verify`:
Enable host key verification, which is the default behavior.
+ * `--hint HINT_NAME[=HINT_FILE]`:
+ Provide the name of a hint (with option JSON file) to set for use by
+ Ohai plugins.
## DESCRIPTION
@@ -105,7 +105,7 @@ class Bootstrap < Knife
:description => "Comma separated list of roles/recipes to apply",
:proc => lambda { |o| o.split(/[\s,]+/) },
:default => []
-
+
option :first_boot_attributes,
:short => "-j JSON_ATTRIBS",
:long => "--json-attributes",
@@ -119,6 +119,14 @@ class Bootstrap < Knife
:boolean => true,
:default => true
+ Chef::Config[:knife][:hints] ||= Hash.new
+ option :hint,
+ :long => "--hint HINT_NAME[=HINT_FILE]",
+ :description => "Specify Ohai Hint to be set on the bootstrap target. Use multiple --hint options to specify multiple hints.",
+ :proc => Proc.new { |h|
+ name, path = h.split("=")
+ Chef::Config[:knife][:hints][name] = path ? JSON.parse(::File.read(path)) : Hash.new }
+
def load_template(template=nil)
# Are we bootstrapping using an already shipped template?
if config[:template_file]
@@ -28,6 +28,19 @@ awk NF /tmp/encrypted_data_bag_secret > /etc/chef/encrypted_data_bag_secret
rm /tmp/encrypted_data_bag_secret
<% end -%>
+<%# Generate Ohai Hints -%>
+<% unless @chef_config[:knife][:hints].nil? || @chef_config[:knife][:hints].empty? -%>
+mkdir -p /etc/chef/ohai/hints
+<% end -%>
+
+<% @chef_config[:knife][:hints].each do |name, hash| -%>
+(
+cat <<'EOP'
+<%= hash.to_json %>
+EOP
+) > /etc/chef/ohai/hints/<%= name %>.json
+<% end -%>
+
(
cat <<'EOP'
log_level :info
@@ -38,7 +51,7 @@ validation_client_name "<%= @chef_config[:validation_client_name] %>"
node_name "<%= @config[:chef_node_name] %>"
<% else -%>
# Using default node name (fqdn)
-<% end -%>
+<% end -%>
# ArchLinux follows the Filesystem Hierarchy Standard
file_cache_path "/var/cache/chef"
file_backup_path "/var/lib/chef/backup"
@@ -41,6 +41,19 @@ awk NF /tmp/encrypted_data_bag_secret > /etc/chef/encrypted_data_bag_secret
rm /tmp/encrypted_data_bag_secret
<% end -%>
+<%# Generate Ohai Hints -%>
+<% unless @chef_config[:knife][:hints].nil? || @chef_config[:knife][:hints].empty? -%>
+mkdir -p /etc/chef/ohai/hints
+<% end -%>
+
+<% @chef_config[:knife][:hints].each do |name, hash| -%>
+(
+cat <<'EOP'
+<%= hash.to_json %>
+EOP
+) > /etc/chef/ohai/hints/<%= name %>.json
+<% end -%>
+
(
cat <<'EOP'
<%= config_content %>
@@ -44,6 +44,19 @@ awk NF /tmp/encrypted_data_bag_secret > /etc/chef/encrypted_data_bag_secret
rm /tmp/encrypted_data_bag_secret
<% end -%>
+<%# Generate Ohai Hints -%>
+<% unless @chef_config[:knife][:hints].nil? || @chef_config[:knife][:hints].empty? -%>
+mkdir -p /etc/chef/ohai/hints
+<% end -%>
+
+<% @chef_config[:knife][:hints].each do |name, hash| -%>
+(
+cat <<'EOP'
+<%= hash.to_json %>
+EOP
+) > /etc/chef/ohai/hints/<%= name %>.json
+<% end -%>
+
(
cat <<'EOP'
<%= config_content %>
@@ -28,6 +28,19 @@ awk NF /tmp/encrypted_data_bag_secret > /etc/chef/encrypted_data_bag_secret
rm /tmp/encrypted_data_bag_secret
<% end -%>
+<%# Generate Ohai Hints -%>
+<% unless @chef_config[:knife][:hints].nil? || @chef_config[:knife][:hints].empty? -%>
+mkdir -p /etc/chef/ohai/hints
+<% end -%>
+
+<% @chef_config[:knife][:hints].each do |name, hash| -%>
+(
+cat <<'EOP'
+<%= hash.to_json %>
+EOP
+) > /etc/chef/ohai/hints/<%= name %>.json
+<% end -%>
+
(
cat <<'EOP'
<%= config_content %>
@@ -28,6 +28,19 @@ awk NF /tmp/encrypted_data_bag_secret > /etc/chef/encrypted_data_bag_secret
rm /tmp/encrypted_data_bag_secret
<% end -%>
+<%# Generate Ohai Hints -%>
+<% unless @chef_config[:knife][:hints].nil? || @chef_config[:knife][:hints].empty? -%>
+mkdir -p /etc/chef/ohai/hints
+<% end -%>
+
+<% @chef_config[:knife][:hints].each do |name, hash| -%>
+(
+cat <<'EOP'
+<%= hash.to_json %>
+EOP
+) > /etc/chef/ohai/hints/<%= name %>.json
+<% end -%>
+
<% unless @chef_config[:validation_client_name] == "chef-validator" -%>
[ `grep -qx "validation_client_name \"<%= @chef_config[:validation_client_name] %>\"" /etc/chef/client.rb` ] || echo "validation_client_name \"<%= @chef_config[:validation_client_name] %>\"" >> /etc/chef/client.rb
<% end -%>
@@ -35,6 +35,19 @@ awk NF /tmp/encrypted_data_bag_secret > /etc/chef/encrypted_data_bag_secret
rm /tmp/encrypted_data_bag_secret
<% end -%>
+<%# Generate Ohai Hints -%>
+<% unless @chef_config[:knife][:hints].nil? || @chef_config[:knife][:hints].empty? -%>
+mkdir -p /etc/chef/ohai/hints
+<% end -%>
+
+<% @chef_config[:knife][:hints].each do |name, hash| -%>
+(
+cat <<'EOP'
+<%= hash.to_json %>
+EOP
+) > /etc/chef/ohai/hints/<%= name %>.json
+<% end -%>
+
(
cat <<'EOP'
<%= config_content %>
@@ -30,6 +30,19 @@ awk NF /tmp/encrypted_data_bag_secret > /etc/chef/encrypted_data_bag_secret
rm /tmp/encrypted_data_bag_secret
<% end -%>
+<%# Generate Ohai Hints -%>
+<% unless @chef_config[:knife][:hints].nil? || @chef_config[:knife][:hints].empty? -%>
+mkdir -p /etc/chef/ohai/hints
+<% end -%>
+
+<% @chef_config[:knife][:hints].each do |name, hash| -%>
+(
+cat <<'EOP'
+<%= hash.to_json %>
+EOP
+) > /etc/chef/ohai/hints/<%= name %>.json
+<% end -%>
+
(
cat <<'EOP'
<%= config_content %>
@@ -0,0 +1,12 @@
+<%# Generate Ohai Hints -%>
+<% unless @chef_config[:knife][:hints].nil? || @chef_config[:knife][:hints].empty? -%>
+mkdir -p /etc/chef/ohai/hints
+<% end -%>
+
+<% @chef_config[:knife][:hints].each do |name, hash| -%>
+(
+cat <<'EOP'
+<%= hash.to_json %>
+EOP
+) > /etc/chef/ohai/hints/<%= name %>.json
+<% end -%>
@@ -6,9 +6,9 @@
# 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.
@@ -72,15 +72,30 @@
@knife.parse_options(["-r", "role[base],recipe[cupcakes]"])
@knife.render_template(template_string).should == '{"run_list":["role[base]","recipe[cupcakes]"]}'
end
-
+
it "should have foo => {bar => baz} in the first_boot" do
template_string = @knife.load_template(@knife.config[:template_file])
@knife.parse_options(["-j", '{"foo":{"bar":"baz"}}'])
expected_hash = Yajl::Parser.new.parse('{"foo":{"bar":"baz"},"run_list":[]}')
actual_hash = Yajl::Parser.new.parse(@knife.render_template(template_string))
actual_hash.should == expected_hash
end
-
+
+ it "should create a hint file when told to" do
+ @knife.config[:template_file] = File.expand_path(File.join(CHEF_SPEC_DATA, "bootstrap", "test-hints.erb"))
+ template_string = @knife.load_template()
+ @knife.parse_options(["--hint", "openstack"])
+ @knife.render_template(template_string).should match /\/etc\/chef\/ohai\/hints\/openstack.json/
+ end
+
+ it "should populate a hint file with JSON when given a file to read" do
+ @knife.config[:template_file] = File.expand_path(File.join(CHEF_SPEC_DATA, "bootstrap", "test-hints.erb"))
+ ::File.stub!(:read).and_return('{ "foo" : "bar" }')
+ template_string = @knife.load_template()
+ @knife.parse_options(["--hint", "openstack=hints/openstack.json"])
+ @knife.render_template(template_string).should match /\{\"foo\":\"bar\"\}/
+ end
+
it "should take the node name from ARGV" do
@knife.name_args = ['barf']

0 comments on commit 388fc86

Please sign in to comment.