class Comfy::Templater

Class used for preparing and filling file templates.

Attributes

data[R]

Public Class Methods

new(data) click to toggle source

Creates an instance of Templater.

@param data [Hash] prepared data with distro, provisioners, files, password and identifier info.

# File lib/comfy/templater.rb, line 13
def initialize(data)
  @data = data
end

Public Instance Methods

prepare_files() click to toggle source

Prepares *.json and *.cfg files from templates for selected distribution

# File lib/comfy/templater.rb, line 18
def prepare_files
  prepare_file('cfg')
  prepare_file('packer', true)
end

Private Instance Methods

populate_template(template) click to toggle source

Actual filling of .erb fils with given info.

@param template [String] path to template file.

# File lib/comfy/templater.rb, line 56
def populate_template(template)
  logger.debug("Populating template '#{template}'")
  erb = ERB.new(File.read(template), nil, '-')
  erb.filename = template
  erb.result(binding)
end
prepare_file(name, packer = false) click to toggle source

Method prepares .erb file with given data.

@param name [String] type of file for preparation. @param packer [Boolean] (implicite value = false).

# File lib/comfy/templater.rb, line 29
def prepare_file(name, packer = false)
  logger.debug("Creating temporary #{name} file...")
  tmp = Tempfile.new("comfy_#{name}")
  logger.debug("Temporary file '#{tmp.path}' was created.")

  output = File.join(data[:server_dir], "#{data[:distribution]}.#{name}")

  logger.debug("Writing to temporary #{name} file...")
  template_path = File.join(data[:'template-dir'], data[:distribution], "#{data[:distribution]}.#{name}.erb")
  template_path = File.join(data[:'template-dir'], 'packer.erb') if packer
  write_to_tmp(tmp, populate_template(template_path))

  logger.debug("Copying #{name} file to its proper location...")
  FileUtils.cp(tmp.path, output)

  logger.debug("Cleaning temporary #{name} file...")
  tmp.close(true)
end
write_to_tmp(tmp, data) click to toggle source
# File lib/comfy/templater.rb, line 48
def write_to_tmp(tmp, data)
  tmp.write(data)
  tmp.flush
end