Puppet Function: bareos_settings

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

Overview

bareos_settings()Any

Helper function to parse settings for bareos and return prepared lines for config file

Returns:

  • (Any)


2
3
4
5
6
7
8
9
10
11
12
13
14
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
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
# File 'lib/puppet/parser/functions/bareos_settings.rb', line 2

newfunction(:bareos_settings, type: :rvalue, doc: <<-'ENDHEREDOC') do |args|
  Helper function to parse settings for bareos and return prepared lines for config file
  ENDHEREDOC

  final_settings = []
  args.each do |setting|
    begin
      raise 'Invalid or incomplete setting' unless setting.length > 2 && setting.is_a?(Array)
      value_setting = setting[0] # value for this setting
      directive = setting[1] # Directive Keyword of this setting
      type = setting[2] # bareos variable type
      required = setting[3] # boolean, undef allowed or not
      indent = setting[4] || '  ' # Internally used, just for beatufying

      raise 'Name of directive config key is invalid' unless directive =~ %r{^[a-zA-z ]+$}

      # check array if allowed
      values = if (%w[acl runscript].include?(type) || type =~ %r{[_-]list$}) && value_setting.is_a?(Array)
                 value_setting
               else
                 [value_setting]
               end
      type.gsub!(%r{([_-]list)$}, '')

      values.each do |value|
        # ignore undef if not required
        next if required == false && value == :undef
        raise 'This directive is required, please set value' if value == :undef

        # defaults:
        # quote value
        quote = false
        # check regex
        regex = nil
        # check in array
        value_in_array = nil
        # required for addresses/hashes
        hash_separator = ' '

        # validation by type
        case type
        # maybe check more than it is an int
        when 'int32', 'pint16', 'pint32', 'port', 'max_blocksize'
          # type casting raise error
          Integer(value)
        when 'name', 'res', 'resource'
          quote = true
          regex = %r{^[a-z][a-z0-9\.\-_ \$]{0,126}$}i
        # @todo validate net-address for domain name or ip
        when 'acl', 'messages', 'type', 'string_noquote', 'schedule_run_command'
          raise 'Value need to be an string' unless value.is_a?(String)
        # type md5password is missleading, it is an plain password and not md5 hashed
        when 'audit_command', 'runscript_short', 'autopassword', 'md5password', 'directory', 'string', 'strname', 'address', 'device', 'plugin_names'
          # array
          quote = true
          raise 'Value need to be an string' unless value.is_a?(String)
        when 'speed'
          regex = %r{^\d+\W*(k|kb|m|mb)\/s$}i
        when 'size64'
          regex = %r{^(\d+(\.\d+)?)\W*(k|kb|m|mb|g|gb)$}i
        when 'time'
          regex = %r{^(\d+|(\d+\W+(seconds|sec|s|minutes|min|hours|h|days|d|weeks|w|months|m|quarters|q|years|y)\W*)+)$}i
        when 'boolean', 'bit'
          value_in_array = %w[yes no on off true false]
        when 'addresses'
          hash_separator = ' = '
          raise 'Please specify as Hash' unless value.is_a?(Hash)
        when 'include_exclude_item', 'runscript', 'hash'
          raise 'Please specify as Hash' unless value.is_a?(Hash)
        when 'backup_level'
          value_in_array = %w[full incremental differential virtualfull initcatalog catalog volumetocatalog disktocatalog]
        when 'io_direction'
          value_in_array = %w[in out both]
        when 'action_on_purge'
          value_in_array = %w[truncate]
        when 'encryption_cipher'
          value_in_array = %w[aes128 aes192 aes256 camellia128 camellia192 camellia256 aes128hmacsha1 aes256hmacsha1 blowfish]
        when 'auth_type'
          value_in_array = %w[clear md5]
        when 'auth_protocol_type', 'protocol_type'
          value_in_array = %w[native ndmp]
        when 'pooltype'
          value_in_array = %w[backup archive cloned migration copy save scratch]
        when 'label'
          value_in_array = %w[ansi ibm bareos]
        when 'migration_type'
          value_in_array = %w[smallestvolume oldestvolume client volume job sqlquery pooloccupancy pooltime pooluncopiedjobs]
        when 'job_type'
          value_in_array = %w[backup restore verify admin migrate copy consolidate]
        when 'replace_option'
          value_in_array = %w[always ifnewer ifolder never]
        when 'device_type'
          value_in_array = %w[tape file fifo gfapi rados]
        when 'compression_algorithm'
          value_in_array = %w[gzip lzo lzfast lz4 lz4hc]
        else
          raise "Invalid setting type '#{type}'"
        end

        unless value_in_array.nil?
          raise "Value '#{value}' needs to be one of #{value_in_array.inspect}" unless value_in_array.include? value.to_s.downcase
        end
        unless regex.nil?
          raise "Value '#{value}' does not match regex #{regex}" unless value =~ Regexp.compile(regex)
        end

        if value.is_a?(Hash)
          final_settings.push "#{indent}#{directive}#{hash_separator}{"
          value.each do |k, v|
            type_n = 'string_noquote'
            type_n = "#{type_n}_list" if v.is_a?(Array)
            # use same type again:
            type_n = type if v.is_a?(Hash)
            final_settings.push function_bareos_settings([[v, k, type_n, false, "#{indent}  "]])
          end
          final_settings.push "#{indent}}"
        else
          if quote
            # value = value.gsub(/(")/, '\"')
            value = "\"#{value}\""
          end
          final_settings.push "#{indent}#{directive} = #{value}"
        end
      end
    rescue => error
      raise Puppet::ParseError, "bareos_parse_settings(): #{setting.inspect}: #{error}."
    end
  end

  return final_settings.join "\n"
end