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
62
63
64
65
66
67
68
69
# 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[:freeze] == false
    args << "--no-freeze"
  end

  if options[:force]
    args << "--force"
  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)


73
74
75
# File 'lib/vagrant-berkshelf/helpers.rb', line 73

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)


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

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

#chef_client?(env) ⇒ Boolean

Determine if the given vagrant environment contains a chef_client provisioner

Parameters:

  • env (Vagrant::Environment)

Returns:

  • (Boolean)


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

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)


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

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)


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

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)


145
146
147
# File 'lib/vagrant-berkshelf/helpers.rb', line 145

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)


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

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)


85
86
87
88
89
90
91
92
93
94
# File 'lib/vagrant-berkshelf/helpers.rb', line 85

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



174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/vagrant-berkshelf/helpers.rb', line 174

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