Module: VagrantPlugins::Berkshelf::Helpers
Overview
A module of common helper functions that can be mixed into Berkshelf::Vagrant actions
Instance Method Summary collapse
-
#berks(command, *args) ⇒ String
Execute a berkshelf command with the given arguments and flags.
-
#berks_bin ⇒ String?
The path to the Berkshelf binary on disk.
-
#berkshelf_enabled?(env) ⇒ Boolean
Determine if the Berkshelf plugin should be run for the given environment.
-
#chef_client?(env) ⇒ Boolean
Determine if the given vagrant environment contains a chef_client provisioner.
-
#chef_solo?(env) ⇒ Boolean
Determine if the given vagrant environment contains a chef_solo provisioner.
-
#chef_zero?(env) ⇒ Boolean
Determine if the given vagrant environment contains a chef_zero provisioner.
-
#datafile_path(env) ⇒ String
The path to the Vagrant Berkshelf data file inside the machine’s data directory.
-
#provision_enabled?(env) ⇒ Boolean
Determine if –no-provision was specified.
-
#provisioners(type, env) ⇒ Array
Filter all of the provisioners of the given vagrant environment with the given name.
-
#with_clean_env(&block) ⇒ Object
Execute the given command, removing any Ruby-specific environment variables.
Instance Method Details
#berks(command, args) ⇒ String #berks(command, args, options) ⇒ String
Execute a berkshelf command with the given arguments and flags.
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) = args.last.is_a?(Hash) ? args.pop : {} args = args.dup if [:berksfile_path] args << "--berksfile" args << [:berksfile_path] end if !.fetch(:except, []).empty? args << "--except" args += [:except] end if !.fetch(:only, []).empty? args << "--only" args += [:only] end if [:freeze] == false args << "--no-freeze" end if [:force] args << "--force" end if !.fetch(:args, []).empty? args += [: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_bin ⇒ String?
The path to the Berkshelf binary on disk.
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
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
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
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
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.
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
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
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.
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 |