Class: Vagrant::Puppetfile::Evaluator::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant/puppetfile.rb

Direct Known Subclasses

Librarian, Puppet, R10k

Constant Summary collapse

ENV_CLEAN_KEYS =
%w(GEM_HOME GEM_PATH)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, logger) ⇒ Base

Returns a new instance of Base.



160
161
162
163
164
165
166
167
168
# File 'lib/vagrant/puppetfile.rb', line 160

def initialize(path, logger)
  @logger = logger
  @path = path
  # Make sure the evaluator actually exists and can be called.
  unless available?
    logger.warn("Unable to find command: #{path}")
    fail EvaluatorError, path
  end
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



155
156
157
# File 'lib/vagrant/puppetfile.rb', line 155

def logger
  @logger
end

#pathObject (readonly)

Returns the value of attribute path.



156
157
158
# File 'lib/vagrant/puppetfile.rb', line 156

def path
  @path
end

Instance Method Details

#command(*args) ⇒ Object



191
192
193
194
195
# File 'lib/vagrant/puppetfile.rb', line 191

def command(*args)
  with_clean_env do
    Open3.capture2e(path, *args.map(&:shellescape))
  end
end

#run(*args) ⇒ Object



197
198
199
200
201
202
203
204
205
# File 'lib/vagrant/puppetfile.rb', line 197

def run(*args)
  output, status = command(*args)
  if status.success?
    output.lines.each { |l| logger.detail(l.chomp) }
  else
    output.lines.each { |l| logger.error(l.chomp) }
    fail InstallationError, output
  end
end

#validate(puppetfile) ⇒ Object



170
171
172
173
174
175
176
# File 'lib/vagrant/puppetfile.rb', line 170

def validate(puppetfile)
  # Check for the puppetfile and bomb if it doesn't exist.
  unless File.readable?(puppetfile)
    logger.warn("Unable to read puppetfile: #{puppetfile}")
    fail ConfigurationError, puppetfile
  end
end

#with_clean_envObject



178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/vagrant/puppetfile.rb', line 178

def with_clean_env
  env = ENV_CLEAN_KEYS.map { |k| [k, ENV.delete(k)] }
  if not defined? ::Bundler
    yield
  else
    ::Bundler.with_clean_env do
      yield
    end
  end
ensure
  env.each { |(k, v)| ENV[k] = v }
end