From 7983e70b7d80ac5063fd7b9182d6fd41ff39c5c3 Mon Sep 17 00:00:00 2001 From: Matt Wrock Date: Tue, 22 Dec 2015 12:56:08 -0800 Subject: no longer wait on node search to refresh vault but pass created ApiClient instead --- lib/chef/knife/bootstrap.rb | 2 +- lib/chef/knife/bootstrap/chef_vault_handler.rb | 31 ++++++++++---------------- lib/chef/knife/bootstrap/client_builder.rb | 4 +++- 3 files changed, 16 insertions(+), 21 deletions(-) (limited to 'lib/chef/knife') diff --git a/lib/chef/knife/bootstrap.rb b/lib/chef/knife/bootstrap.rb index 30ea3cac6c..d958ddf336 100644 --- a/lib/chef/knife/bootstrap.rb +++ b/lib/chef/knife/bootstrap.rb @@ -377,7 +377,7 @@ class Chef client_builder.run - chef_vault_handler.run(node_name: config[:chef_node_name]) + chef_vault_handler.run(client_builder.client) bootstrap_context.client_pem = client_builder.client_path else diff --git a/lib/chef/knife/bootstrap/chef_vault_handler.rb b/lib/chef/knife/bootstrap/chef_vault_handler.rb index f658957499..9d0dfec621 100644 --- a/lib/chef/knife/bootstrap/chef_vault_handler.rb +++ b/lib/chef/knife/bootstrap/chef_vault_handler.rb @@ -28,8 +28,8 @@ class Chef # @return [Chef::Knife::UI] ui object for output attr_accessor :ui - # @return [String] name of the node (technically name of the client) - attr_reader :node_name + # @return [Chef::ApiClient] vault client + attr_reader :client # @param knife_config [Hash] knife merged config, typically @config # @param ui [Chef::Knife::UI] ui object for output @@ -38,18 +38,15 @@ class Chef @ui = ui end - # Updates the chef vault items for the newly created node. + # Updates the chef vault items for the newly created client. # - # @param node_name [String] name of the node (technically name of the client) - # @todo: node_name should be mandatory (ruby 2.0 compat) - def run(node_name: nil) + # @param client [Chef::ApiClient] vault client + def run(client) return unless doing_chef_vault? sanity_check - @node_name = node_name - - ui.info("Updating Chef Vault, waiting for client to be searchable..") while wait_for_client + @client = client update_bootstrap_vault_json! end @@ -126,7 +123,7 @@ class Chef def update_vault(vault, item) require_chef_vault! bootstrap_vault_item = load_chef_bootstrap_vault_item(vault, item) - bootstrap_vault_item.clients("name:#{node_name}") + bootstrap_vault_item.clients(client) bootstrap_vault_item.save end @@ -141,22 +138,18 @@ class Chef public :load_chef_bootstrap_vault_item # for stubbing - # Helper used to spin waiting for the client to appear in search. - # - # @return [Boolean] true if the client is searchable - def wait_for_client - sleep 1 - !Chef::Search::Query.new.search(:client, "name:#{node_name}")[0] - end - # Helper to very lazily require the chef-vault gem def require_chef_vault! @require_chef_vault ||= begin + error_message = "Knife bootstrap needs version 2.6.0 or higher of the chef-vault gem to configure chef vault items" require 'chef-vault' + if Gem::Version.new(ChefVault::VERSION) < Gem::Version.new('2.6.0') + raise error_message + end true rescue LoadError - raise "Knife bootstrap cannot configure chef vault items when the chef-vault gem is not installed" + raise error_message end end diff --git a/lib/chef/knife/bootstrap/client_builder.rb b/lib/chef/knife/bootstrap/client_builder.rb index 7eb1e22628..6414ac5c72 100644 --- a/lib/chef/knife/bootstrap/client_builder.rb +++ b/lib/chef/knife/bootstrap/client_builder.rb @@ -34,6 +34,8 @@ class Chef attr_accessor :chef_config # @return [Chef::Knife::UI] ui object for output attr_accessor :ui + # @return [Chef::ApiClient] client saved on run + attr_reader :client # @param knife_config [Hash] Hash of knife config settings # @param chef_config [Hash] Hash of chef config settings @@ -51,7 +53,7 @@ class Chef ui.info("Creating new client for #{node_name}") - create_client! + @client = create_client! ui.info("Creating new node for #{node_name}") -- cgit v1.2.1