Sha256: e1f23838c74b2a28110793475e3cc6ecdf235a7c2b4091063ea5e486ab6c22b8

Contents?: true

Size: 1.76 KB

Versions: 28

Compression:

Stored size: 1.76 KB

Contents

require 'pe_build/release'

require 'erb'

# A sub-provisioner which generates answer file content.
#
# This is an internal provisioner which is invoked by
# `PEBuild::Provisioner::PEBootstrap`.
#
# @api private
class PEBuild::Provisioner::PEBootstrap::AnswersFile

  attr_reader :template

  # @param machine [Vagrant::Machine]
  # @param config [Object < Vagrant.plugin('2', :config)]
  # @param work_dir [String]
  def initialize(machine, config, work_dir)
    @machine, @config = machine, config
    @work_dir = Pathname.new(work_dir)

    @logger = Log4r::Logger.new('vagrant::provisioner::pe_bootstrap::answers_file')

    @answer_dir  = @work_dir.join('answers')
    @output_file = @answer_dir.join "#{@machine.name}.txt"

    set_template_path
  end

  def generate
    @logger.info "Writing answers file for #{@machine.inspect} to #{@output_file}"
    @answer_dir.mkpath unless @answer_dir.exist?

    @output_file.open('w') { |fh| fh.write(render_answers) }
  end

  def render_answers
    answer_template = template_data
    unless @config.answer_extras.empty?
      answer_template += ("\n" + @config.answer_extras.map {|e| e.to_s}.join("\n") + "\n")
    end

    ERB.new(answer_template).result(binding)
  end

  private

  def set_template_path
    if @config.answer_file
      @template = @config.answer_file
      mode = 'explicit'
    else
      release_info = PEBuild::Release[@config.version.split('-').first]

      @template = release_info.answer_file(@config.role)
      mode = 'default'
    end

    @logger.info "Using #{mode} answers file template #{@template} for #{@machine.inspect}"
  end

  # Separated for easy stubbing in spec tests.
  def template_data
    File.read(@template)
  end

  def machine_hostname
    @machine.config.vm.hostname || @machine.name
  end
end

Version data entries

28 entries across 28 versions & 1 rubygems

Version Path
vagrant-pe_build-0.19.2 lib/pe_build/provisioner/pe_bootstrap/answers_file.rb
vagrant-pe_build-0.19.1 lib/pe_build/provisioner/pe_bootstrap/answers_file.rb
vagrant-pe_build-0.19.0 lib/pe_build/provisioner/pe_bootstrap/answers_file.rb
vagrant-pe_build-0.18.2 lib/pe_build/provisioner/pe_bootstrap/answers_file.rb
vagrant-pe_build-0.18.1 lib/pe_build/provisioner/pe_bootstrap/answers_file.rb
vagrant-pe_build-0.18.0 lib/pe_build/provisioner/pe_bootstrap/answers_file.rb
vagrant-pe_build-0.17.14 lib/pe_build/provisioner/pe_bootstrap/answers_file.rb
vagrant-pe_build-0.17.13 lib/pe_build/provisioner/pe_bootstrap/answers_file.rb
vagrant-pe_build-0.17.12 lib/pe_build/provisioner/pe_bootstrap/answers_file.rb
vagrant-pe_build-0.17.11 lib/pe_build/provisioner/pe_bootstrap/answers_file.rb
vagrant-pe_build-0.17.10 lib/pe_build/provisioner/pe_bootstrap/answers_file.rb
vagrant-pe_build-0.17.9 lib/pe_build/provisioner/pe_bootstrap/answers_file.rb
vagrant-pe_build-0.17.8 lib/pe_build/provisioner/pe_bootstrap/answers_file.rb
vagrant-pe_build-0.17.7 lib/pe_build/provisioner/pe_bootstrap/answers_file.rb
vagrant-pe_build-0.17.6 lib/pe_build/provisioner/pe_bootstrap/answers_file.rb
vagrant-pe_build-0.17.5 lib/pe_build/provisioner/pe_bootstrap/answers_file.rb
vagrant-pe_build-0.17.4 lib/pe_build/provisioner/pe_bootstrap/answers_file.rb
vagrant-pe_build-0.17.3 lib/pe_build/provisioner/pe_bootstrap/answers_file.rb
vagrant-pe_build-0.17.2 lib/pe_build/provisioner/pe_bootstrap/answers_file.rb
vagrant-pe_build-0.17.1 lib/pe_build/provisioner/pe_bootstrap/answers_file.rb