Sha256: fa68293a80fee8386f8c43dd490e7dfae02513a7f02c99c532f058f4f06a3ce4

Contents?: true

Size: 1.66 KB

Versions: 2

Compression:

Stored size: 1.66 KB

Contents

require 'erubis'

module Resource

  class Template < Base

    class_attr :source

    block_attr :owner, :group, :mode, :source, :action, :variables, :backup, :target

    PREFIX = ''

    def run
      Execution.block 'Ensure template base', @target, @owner do |b|
        ensure_target_zone
        b.always_run @always_run
      end
      Execution.block 'Compiling template file', @target, @owner do |b|
        compile!
        b.always_run @always_run
      end
      Execution.block 'Ensuring file permissions', @target, 'root' do |b|
        b.run "chown #{@owner}:#{@owner} #{@target}"
        b.run "chmod #{unix_mode} #{@target}"
        b.always_run @always_run
      end
    end

    protected

    def initialize target, &block
      set_base_defaults
      @target = target
      # ensure_target_zone
      self.instance_eval(&block)
    end

    def target_path
      "#{PREFIX}#{@target}"
    end

    def needs_compilation?
      return true unless ::File.exist?(target_path)
      MD5.hexdigest(::File.read target_path) != MD5.hexdigest(compile_template)
    end

    def compile!
      Output.info 'Erubis is compiling', target_path
      ::File.open(target_path, 'w+') { |file| file.write(compile_template) }
    end

    def ensure_target_zone
      system "mkdir -p #{PREFIX}#{::File.dirname(@target)}"
      true
    end

    def find_source
      Dir['cookbooks/*/templates/*/*.*'].each do |file|
        return file if file.include? ::File.basename(@source)
      end
    end

    def compile_template
      @compile_template ||=
      Erubis::Eruby.new(::File.read(find_source)).evaluate(
        { :node => Node.get }.merge(@variables || {})
      )
    end

  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
atesta-0.0.4 lib/resource/template.rb
atesta-0.0.3 lib/resource/template.rb