Module: VagrantPlugins::Berkshelf::Helpers

Includes:
Vagrant::Util
Included in:
Action::Base
Defined in:
lib/vagrant-berkshelf/helpers.rb

Overview

A module of common helper functions that can be mixed into Berkshelf::Vagrant actions

Instance Method Summary collapse

Instance Method Details

#berks(command, args) ⇒ String #berks(command, args, options) ⇒ String

Execute a berkshelf command with the given arguments and flags.

Overloads:

  • #berks(command, args) ⇒ String

    Parameters:

    • berks (String)

      CLI command to run

    • any (Object)

      number of arguments to pass to CLI

  • #berks(command, args, options) ⇒ String

    Parameters:

    • berks (String)

      CLI command to run

    • any (Object)

      number of arguments to pass to CLI

    • options (Hash)

      to convert to flags for the CLI

Returns:

  • (String)

    output of the command

Raises:

  • (InvalidBerkshelfVersionError)

    version of Berks installed does not satisfy the application’s constraint

  • (BerksNotFoundError)

    berks command is not found in the user’s path



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/vagrant-berkshelf/helpers.rb', line 29

def berks(command, *args)
  options = args.last.is_a?(Hash) ? args.pop : {}
  args = args.dup

  if options[:berksfile_path]
    args << "--berksfile"
    args << options[:berksfile_path]
  end

  if !options.fetch(:except, []).empty?
    args << "--except"
    args += options[:except]
  end

  if !options.fetch(:only, []).empty?
    args << "--only"
    args += options[:only]
  end

  if !options.fetch(:args, []).empty?
    args += options[:args]
  end

  final_command = [berks_bin, command, *args]

  Bundler.with_clean_env do
    r = Subprocess.execute(*final_command)
    if r.exit_code != 0
      raise BerksCommandFailed.new(final_command.join(' '), r.stdout, r.stderr)
    end
    r
  end
end

#berks_binString?

The path to the Berkshelf binary on disk.

Returns:

  • (String, nil)


65
66
67
# File 'lib/vagrant-berkshelf/helpers.rb', line 65

def berks_bin
  Which.which("berks")
end

#berkshelf_enabled?(env) ⇒ Boolean

Determine if the Berkshelf plugin should be run for the given environment

Parameters:

  • env (Vagrant::Environment)

Returns:

  • (Boolean)


120
121
122
# File 'lib/vagrant-berkshelf/helpers.rb', line 120

def berkshelf_enabled?(env)
  env[:machine].config.berkshelf.enabled
end

#chef_client?(env) ⇒ Boolean

Determine if the given vagrant environment contains a chef_client provisioner

Parameters:

  • env (Vagrant::Environment)

Returns:

  • (Boolean)


111
112
113
# File 'lib/vagrant-berkshelf/helpers.rb', line 111

def chef_client?(env)
  provisioners(:chef_client, env).any?
end

#chef_solo?(env) ⇒ Boolean

Determine if the given vagrant environment contains a chef_solo provisioner

Parameters:

  • env (Vagrant::Environment)

Returns:

  • (Boolean)


93
94
95
# File 'lib/vagrant-berkshelf/helpers.rb', line 93

def chef_solo?(env)
  provisioners(:chef_solo, env).any?
end

#chef_zero?(env) ⇒ Boolean

Determine if the given vagrant environment contains a chef_zero provisioner

Parameters:

  • env (Vagrant::Environment)

Returns:

  • (Boolean)


102
103
104
# File 'lib/vagrant-berkshelf/helpers.rb', line 102

def chef_zero?(env)
  provisioners(:chef_zero, env).any?
end

#datafile_path(env) ⇒ String

The path to the Vagrant Berkshelf data file inside the machine’s data directory.

Returns:

  • (String)


137
138
139
# File 'lib/vagrant-berkshelf/helpers.rb', line 137

def datafile_path(env)
  env[:machine].data_dir.join("berkshelf")
end

#provision_enabled?(env) ⇒ Boolean

Determine if –no-provision was specified

Parameters:

  • env (Vagrant::Environment)

Returns:

  • (Boolean)


129
130
131
# File 'lib/vagrant-berkshelf/helpers.rb', line 129

def provision_enabled?(env)
  env.fetch(:provision_enabled, true)
end

#provisioners(type, env) ⇒ Array

Filter all of the provisioners of the given vagrant environment with the given name

Parameters:

  • name (Symbol)

    name of provisioner to filter

  • env (Vagrant::Environment, Hash)

    environment to inspect

Returns:

  • (Array)


77
78
79
80
81
82
83
84
85
86
# File 'lib/vagrant-berkshelf/helpers.rb', line 77

def provisioners(type, env)
  env[:machine].config.vm.provisioners.select do |provisioner|
    # Vagrant 1.7 changes provisioner.name to provisioner.type
    if provisioner.respond_to? :type
      provisioner.type.to_sym == type
    else
      provisioner.name.to_sym == type
    end
  end
end

#with_clean_env(&block) ⇒ Object

Execute the given command, removing any Ruby-specific environment variables. This is an “enhanced” version of Bundler.with_clean_env, which only removes Bundler-specific values. We need to remove all values, specifically:

  • _ORIGINAL_GEM_PATH

  • GEM_PATH

  • GEM_HOME

  • GEM_ROOT

  • BUNDLE_BIN_PATH

  • BUNDLE_GEMFILE

  • RUBYLIB

  • RUBYOPT

  • RUBY_ENGINE

  • RUBY_ROOT

  • RUBY_VERSION

This will escape Vagrant’s environment entirely, which is required if calling an executable that lives in another Ruby environment. The original environment restored at the end of this call.

Parameters:

  • block (Proc)

    the block to execute with the cleaned environment



166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/vagrant-berkshelf/helpers.rb', line 166

def with_clean_env(&block)
  original = ENV.to_hash

  ENV.delete("_ORIGINAL_GEM_PATH")
  ENV.delete_if { |k,_| k.start_with?("BUNDLE_") }
  ENV.delete_if { |k,_| k.start_with?("GEM_") }
  ENV.delete_if { |k,_| k.start_with?("RUBY") }

  yield
ensure
  ENV.replace(original.to_hash)
end