Puppet Function: recurse_directory

Defined in:
lib/puppet/parser/functions/recurse_directory.rb
Function type:
Ruby 3.x API

Overview

recurse_directory()Any

expects an args containing: args

  • The source module and directory inside of templates

  • We will insert templates/ after the module name in this code

  • required: true

args

  • The destination directory for the interpolated templates to

  • go on the client machine

  • required: true

args

  • The file mode for the finished files on the client

  • required: false

  • default: 0600

args

  • The owner of the file

  • required: false

  • default: owner of puppet running process

args

  • The group ownership of the file

  • required: false

  • default: owner of puppet running process

args

  • The directory mode

  • required: false

  • default: 0700

args

  • Flag used to merge only erb templates.

  • required: false

  • default: false

Returns:

  • (Any)


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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/puppet/parser/functions/recurse_directory.rb', line 41

newfunction(:recurse_directory, :type => :rvalue) do |args|
  source_dir = args[0]
  destination_dir = args[1]
  file_mode = args[2]
  file_owner = args[3]
  file_group = args[4]
  dir_mode = args[5]
  merge_erb_only = args[6]


  file_path = Puppet::Parser::Files.find_template(source_dir, compiler.environment)

  created_resources = Hash.new

  created_resources[destination_dir] = {
      'ensure' => 'directory',
      'mode' => dir_mode,
      'owner' => file_owner,
      'group' => file_group
  }

  function_ensure_resource(['file', destination_dir, created_resources[destination_dir]])

  Find.find(file_path) do |f|
    full_path = f
    f.slice!(file_path + "/")
    if f == file_path or f == '' or !f
      next
    end

    if not File.directory?("#{file_path}/#{f}")
      title = f.gsub(/\.erb$/, '')
      debug("File in loop #{f}")
      debug("Title in loop #{title}")
      destination_full_path = "#{destination_dir}/#{title}"
      file = "#{file_path}/#{f}"

      if merge_erb_only and title == f
        # The file is not a template (ie : not erb file).
        created_resources[destination_full_path] = {
            'ensure' => 'file',
            'source' => file,
            'mode' => file_mode,
            'owner' => file_owner,
            'group' => file_group
        }
      else
        # The file is a template
        debug "Retrieving template #{file}"

        wrapper = Puppet::Parser::TemplateWrapper.new(self)
        wrapper.file = file
        begin
          wrapper.result
        rescue => detail
          info = detail.backtrace.first.split(':')
          raise Puppet::ParseError,
                "Failed to parse template #{file}:\n  Filepath: #{info[0]}\n  Line: #{info[1]}\n  Detail: #{detail}\n"
        end
        template_content = wrapper.result

        created_resources[destination_full_path] = {
            'ensure' => 'file',
            'content' => template_content,
            'mode' => file_mode,
            'owner' => file_owner,
            'group' => file_group
        }
        debug("Resource: #{destination_full_path} #{file_mode}")
      end
    elsif File.directory?("#{file_path}/#{f}") and f != '.' and f != '..'

      title = f
      destination_full_path = "#{destination_dir}/#{title}"

      created_resources[destination_full_path] = {
          'ensure' => 'directory',
          'mode' => dir_mode,
          'owner' => file_owner,
          'group' => file_group
      }
      debug("Resource: #{destination_full_path} #{dir_mode}")
    end

    function_ensure_resource(['file', destination_full_path, created_resources[destination_full_path]])
  end

  debug("Source Dir #{source_dir}")
  debug("Destination Dir #{destination_dir}")
  debug("File Path #{file_path}")

  return created_resources
end