Class: DanarchyDeploy::Templater

Inherits:
Object
  • Object
show all
Defined in:
lib/danarchy_deploy/templater.rb

Class Method Summary collapse

Class Method Details

.load_source(source) ⇒ Object



6
7
8
9
10
11
12
13
# File 'lib/danarchy_deploy/templater.rb', line 6

def self.load_source(source)
  if source =~ /^builtin::/
    source = File.expand_path('../../',  __dir__) + '/templates/' + source.split('::').last
  end

  abort("Source file does not exist at: #{source}") if ! File.exist?(source)
  source
end

.new(templates, options) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/danarchy_deploy/templater.rb', line 15

def self.new(templates, options)
  puts "\n" + self.name

  if templates.map(&:keys).flatten.include?(:remove)
    puts ' > Removing templates tagged for removal.'
    templates = remove_templates(templates, options)
  end

  templates.each do |template|
    next if template[:remove]
    source = if !template[:target]
               abort("No target destination set for template: #{template[:source]}!")
             elsif template[:source].nil? && template[:data].nil?
               abort("No source or data for template: #{template[:target]}")
             elsif template[:data]
               '-- encoded data --'
             else
               load_source(template[:source])
             end

    target = template[:target]
    dir_perms = template[:dir_perms]
    file_perms = template[:file_perms]
    @variables = template[:variables] || nil
    puts "\n >  Checking: #{target}"
    puts "    Source:   #{source}"
    puts "    |> Dir  Permissions: #{dir_perms || '--undefined--'}"
    puts "    |> File Permissions: #{file_perms || '--undefined--'}"
    if options[:vars_verbose] && @variables
      puts '    |> Variables: '
      var = DanarchyDeploy::Helpers.hash_except(@variables, '^pass')
      puts var
    end

    tmpdir = options[:deploy_dir] + '/' + File.basename(File.dirname(target))
    Dir.exist?(tmpdir) || FileUtils.mkdir_p(tmpdir, mode: 0755)
    tmpfile = tmpdir + '/' + File.basename(target) + '.tmp'

    if source == '-- encoded data --'
      data = DanarchyDeploy::Helpers.decode_base64(template[:data])
      source = tmpfile + '.erb'
      write_tmpfile(source, data)
    end

    File.open(tmpfile, 'w') do |f|
      result = if RUBY_VERSION >= '2.6'
                 ERB.new(File.read(source), trim_mode: '-').result(binding)
               else
                 ERB.new(File.read(source), nil, '-').result(binding)
               end
      f.write(result)
      f.close
    end

    result = { write_erb: [], verify_permissions: {} }
    if options[:pretend]
      diff(target, tmpfile) if options[:vars_verbose]
      puts "\n    - Fake Run: Not changing '#{target}'."
      result[:verify_permissions][File.dirname(tmpfile)] = verify_permissions(File.dirname(tmpfile), dir_perms, options)
      result[:verify_permissions][tmpfile] = verify_permissions(tmpfile, file_perms, options)
    elsif files_identical?(target,tmpfile)
      puts "\n    - No change in '#{target}': Nothing to update here."
      result[:verify_permissions][File.dirname(target)] = verify_permissions(File.dirname(target), dir_perms, options)
      result[:verify_permissions][target] = verify_permissions(target, file_perms, options)
    else
      diff(target, tmpfile) if options[:vars_verbose]
      result[:write_erb] = enable_erb(target, tmpfile)
      puts "       |+ #{target} was updated!"
      result[:verify_permissions][File.dirname(target)] = verify_permissions(File.dirname(target), dir_perms, options)
      result[:verify_permissions][target] = verify_permissions(target, file_perms, options)
    end

    FileUtils.rm_rf(File.dirname(tmpfile))
    result
  end
end