Use the new presence helpers in the Chef provisioner

This commit is contained in:
Seth Vargo 2015-11-23 18:14:32 -05:00
parent 0698743724
commit 8c3f833e8e
9 changed files with 45 additions and 18 deletions

View File

@ -14,7 +14,11 @@ module Vagrant
case obj case obj
when String when String
!obj.strip.empty? !obj.strip.empty?
when Array, Hash when Symbol
!obj.to_s.strip.empty?
when Array
!obj.compact.empty?
when Hash
!obj.empty? !obj.empty?
when TrueClass, FalseClass when TrueClass, FalseClass
obj obj

View File

@ -1,7 +1,11 @@
require "vagrant/util/presence"
module VagrantPlugins module VagrantPlugins
module Chef module Chef
module Config module Config
class Base < Vagrant.plugin("2", :config) class Base < Vagrant.plugin("2", :config)
include Vagrant::Util::Presence
# The path to Chef's bin/ directory. # The path to Chef's bin/ directory.
# @return [String] # @return [String]
attr_accessor :binary_path attr_accessor :binary_path
@ -114,18 +118,12 @@ EOH
def validate_base(machine) def validate_base(machine)
errors = _detected_errors errors = _detected_errors
if missing?(log_level) if !present?(log_level)
errors << I18n.t("vagrant.provisioners.chef.log_level_empty") errors << I18n.t("vagrant.provisioners.chef.log_level_empty")
end end
errors errors
end end
# Determine if the given string is "missing" (blank)
# @return [true, false]
def missing?(obj)
obj.to_s.strip.empty?
end
end end
end end
end end

View File

@ -1,9 +1,13 @@
require "vagrant/util/presence"
require_relative "base" require_relative "base"
module VagrantPlugins module VagrantPlugins
module Chef module Chef
module Config module Config
class ChefApply < Base class ChefApply < Base
include Vagrant::Util::Presence
# The raw recipe text (as a string) to execute via chef-apply. # The raw recipe text (as a string) to execute via chef-apply.
# @return [String] # @return [String]
attr_accessor :recipe attr_accessor :recipe
@ -30,11 +34,11 @@ module VagrantPlugins
def validate(machine) def validate(machine)
errors = validate_base(machine) errors = validate_base(machine)
if missing?(recipe) if !present?(recipe)
errors << I18n.t("vagrant.provisioners.chef.recipe_empty") errors << I18n.t("vagrant.provisioners.chef.recipe_empty")
end end
if missing?(upload_path) if !present?(upload_path)
errors << I18n.t("vagrant.provisioners.chef.upload_path_empty") errors << I18n.t("vagrant.provisioners.chef.upload_path_empty")
end end

View File

@ -1,3 +1,4 @@
require "vagrant/util/presence"
require "vagrant/util/which" require "vagrant/util/which"
require_relative "base_runner" require_relative "base_runner"
@ -6,6 +7,8 @@ module VagrantPlugins
module Chef module Chef
module Config module Config
class ChefClient < BaseRunner class ChefClient < BaseRunner
include Vagrant::Util::Presence
# The URL endpoint to the Chef Server. # The URL endpoint to the Chef Server.
# @return [String] # @return [String]
attr_accessor :chef_server_url attr_accessor :chef_server_url
@ -55,11 +58,11 @@ module VagrantPlugins
def validate(machine) def validate(machine)
errors = validate_base(machine) errors = validate_base(machine)
if chef_server_url.to_s.strip.empty? if !present?(chef_server_url)
errors << I18n.t("vagrant.config.chef.server_url_empty") errors << I18n.t("vagrant.config.chef.server_url_empty")
end end
if validation_key_path.to_s.strip.empty? if !present?(validation_key_path)
errors << I18n.t("vagrant.config.chef.validation_key_path") errors << I18n.t("vagrant.config.chef.validation_key_path")
end end

View File

@ -1,9 +1,13 @@
require "vagrant/util/presence"
require_relative "base_runner" require_relative "base_runner"
module VagrantPlugins module VagrantPlugins
module Chef module Chef
module Config module Config
class ChefSolo < BaseRunner class ChefSolo < BaseRunner
include Vagrant::Util::Presence
# The path on disk where Chef cookbooks are stored. # The path on disk where Chef cookbooks are stored.
# Default is "cookbooks". # Default is "cookbooks".
# @return [String] # @return [String]
@ -82,11 +86,11 @@ module VagrantPlugins
def validate(machine) def validate(machine)
errors = validate_base(machine) errors = validate_base(machine)
if [cookbooks_path].flatten.compact.empty? if !present?(Array(cookbooks_path))
errors << I18n.t("vagrant.config.chef.cookbooks_path_empty") errors << I18n.t("vagrant.config.chef.cookbooks_path_empty")
end end
if environment && environments_path.empty? if environment && !present?(environments_path)
errors << I18n.t("vagrant.config.chef.environment_path_required") errors << I18n.t("vagrant.config.chef.environment_path_required")
end end

View File

@ -1,9 +1,13 @@
require "vagrant/util/presence"
require_relative "chef_solo" require_relative "chef_solo"
module VagrantPlugins module VagrantPlugins
module Chef module Chef
module Config module Config
class ChefZero < BaseRunner class ChefZero < BaseRunner
include Vagrant::Util::Presence
# The path on disk where Chef cookbooks are stored. # The path on disk where Chef cookbooks are stored.
# Default is "cookbooks". # Default is "cookbooks".
# @return [String] # @return [String]
@ -69,11 +73,11 @@ module VagrantPlugins
def validate(machine) def validate(machine)
errors = validate_base(machine) errors = validate_base(machine)
if [cookbooks_path].flatten.compact.empty? if !present?(Array(cookbooks_path))
errors << I18n.t("vagrant.config.chef.cookbooks_path_empty") errors << I18n.t("vagrant.config.chef.cookbooks_path_empty")
end end
if [nodes_path].flatten.compact.empty? if !present?(Array(nodes_path))
errors << I18n.t("vagrant.config.chef.nodes_path_empty") errors << I18n.t("vagrant.config.chef.nodes_path_empty")
end end

View File

@ -1,5 +1,6 @@
require 'tempfile' require 'tempfile'
require "vagrant/util/presence"
require "vagrant/util/template_renderer" require "vagrant/util/template_renderer"
require_relative "../installer" require_relative "../installer"
@ -11,6 +12,8 @@ module VagrantPlugins
# chef-solo and chef-client provisioning are stored. This is **not an actual # chef-solo and chef-client provisioning are stored. This is **not an actual
# provisioner**. Instead, {ChefSolo} or {ChefServer} should be used. # provisioner**. Instead, {ChefSolo} or {ChefServer} should be used.
class Base < Vagrant.plugin("2", :provisioner) class Base < Vagrant.plugin("2", :provisioner)
include Vagrant::Util::Presence
class ChefError < Vagrant::Errors::VagrantError class ChefError < Vagrant::Errors::VagrantError
error_namespace("vagrant.provisioners.chef") error_namespace("vagrant.provisioners.chef")
end end
@ -20,7 +23,7 @@ module VagrantPlugins
@logger = Log4r::Logger.new("vagrant::provisioners::chef") @logger = Log4r::Logger.new("vagrant::provisioners::chef")
if @config.node_name.to_s.empty? if !present?(@config.node_name)
cache = @machine.data_dir.join("chef_node_name") cache = @machine.data_dir.join("chef_node_name")
if !cache.exist? if !cache.exist?

View File

@ -1,6 +1,7 @@
require 'pathname' require 'pathname'
require 'vagrant' require 'vagrant'
require 'vagrant/util/presence'
require 'vagrant/util/subprocess' require 'vagrant/util/subprocess'
require_relative "base" require_relative "base"
@ -11,6 +12,8 @@ module VagrantPlugins
# This class implements provisioning via chef-client, allowing provisioning # This class implements provisioning via chef-client, allowing provisioning
# with a chef server. # with a chef server.
class ChefClient < Base class ChefClient < Base
include Vagrant::Util::Presence
def configure(root_config) def configure(root_config)
raise ChefError, :server_validation_key_required if @config.validation_key_path.nil? raise ChefError, :server_validation_key_required if @config.validation_key_path.nil?
raise ChefError, :server_validation_key_doesnt_exist if !File.file?(validation_key_path) raise ChefError, :server_validation_key_doesnt_exist if !File.file?(validation_key_path)
@ -132,7 +135,7 @@ module VagrantPlugins
def delete_from_chef_server(deletable) def delete_from_chef_server(deletable)
node_name = @config.node_name || @machine.config.vm.hostname node_name = @config.node_name || @machine.config.vm.hostname
if node_name.to_s.empty? if !present?(node_name)
@machine.ui.warn(I18n.t("vagrant.provisioners.chef.missing_node_name", @machine.ui.warn(I18n.t("vagrant.provisioners.chef.missing_node_name",
deletable: deletable, deletable: deletable,
)) ))

View File

@ -26,6 +26,10 @@ describe Vagrant::Util::Presence do
expect(subject.presence([])).to be(false) expect(subject.presence([])).to be(false)
end end
it "returns false for an array with nil values" do
expect(subject.presence([nil, nil])).to be(false)
end
it "returns false for an empty hash" do it "returns false for an empty hash" do
expect(subject.presence({})).to be(false) expect(subject.presence({})).to be(false)
end end