Class: Puppet::Settings::BaseSetting

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet/settings/base_setting.rb

Overview

The base setting type

Constant Summary collapse

HOOK_TYPES =

Hooks are called during different parts of the settings lifecycle:

  • :on_write_only - This is the default hook type. The hook will be called if its value is set in ‘main` or programmatically. If its value is set in a section that doesn’t match the application’s run mode, it will be ignored entirely. If the section does match the run mode, the value will be used, but the hook will not be called!

  • :on_define_and_write - The hook behaves the same as above, except it is also called immediately when the setting is defined in Puppet::Settings.define_settings. In that case, the hook receives the default value as specified.

  • :on_initialize_and_write - The hook will be called if the value is set in ‘main`, the section that matches the run mode, or programmatically.

Set.new([:on_define_and_write, :on_initialize_and_write, :on_write_only]).freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ BaseSetting

Create the new element. Pretty much just sets the name.



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
# File 'lib/puppet/settings/base_setting.rb', line 87

def initialize(args = {})
  @settings = args.delete(:settings)
  unless @settings
    raise ArgumentError.new("You must refer to a settings object")
  end

  # explicitly set name prior to calling other param= methods to provide meaningful feedback during
  # other warnings
  @name = args[:name] if args.include? :name

  #set the default value for call_hook
  @call_hook = :on_write_only if args[:hook] and not args[:call_hook]
  @has_hook = false

  if args[:call_hook] and not args[:hook]
    #TRANSLATORS ':call_hook' and ':hook' are specific setting names and should not be translated
    raise ArgumentError, _("Cannot reference :call_hook for :%{name} if no :hook is defined") % { name: @name }
  end

  args.each do |param, value|
    method = param.to_s + "="
    unless self.respond_to? method
      raise ArgumentError, _("%{class_name} (setting '%{setting}') does not accept %{parameter}") %
          { class_name: self.class, setting: args[:name], parameter: param }
    end

    self.send(method, value)
  end
  unless self.desc
    raise ArgumentError, _("You must provide a description for the %{class_name} config option") % { class_name: self.name }
  end
end

Instance Attribute Details

#call_hookObject

Returns the value of attribute call_hook.



9
10
11
# File 'lib/puppet/settings/base_setting.rb', line 9

def call_hook
  @call_hook
end

#default(check_application_defaults_first = false) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/puppet/settings/base_setting.rb', line 134

def default(check_application_defaults_first = false)
  if @default.is_a? Proc
    # Give unit tests a chance to reevaluate the call by removing the instance variable
    unless instance_variable_defined?(:@evaluated_default)
      @evaluated_default = @default.call
    end
    default_value = @evaluated_default
  else
    default_value = @default
  end
  return default_value unless check_application_defaults_first
  return @settings.value(name, :application_defaults, true) || default_value
end

#deprecatedObject

Returns the value of attribute deprecated.



9
10
11
# File 'lib/puppet/settings/base_setting.rb', line 9

def deprecated
  @deprecated
end

#descObject

Returns the value of attribute desc.



8
9
10
# File 'lib/puppet/settings/base_setting.rb', line 8

def desc
  @desc
end

#nameObject

Returns the value of attribute name.



8
9
10
# File 'lib/puppet/settings/base_setting.rb', line 8

def name
  @name
end

#sectionObject

Returns the value of attribute section.



8
9
10
# File 'lib/puppet/settings/base_setting.rb', line 8

def section
  @section
end

#shortObject

Returns the value of attribute short.



9
10
11
# File 'lib/puppet/settings/base_setting.rb', line 9

def short
  @short
end

Class Method Details

.available_call_hook_valuesObject



29
30
31
# File 'lib/puppet/settings/base_setting.rb', line 29

def self.available_call_hook_values
  HOOK_TYPES.to_a
end

Instance Method Details

#allowed_on_commandline?Boolean

True if we should raise a deprecation_warning if the setting is found in puppet.conf, but not if the user sets it on the commandline

Returns:

  • (Boolean)


217
218
219
# File 'lib/puppet/settings/base_setting.rb', line 217

def allowed_on_commandline?
  @deprecated == :allowed_on_commandline
end

#call_hook_on_define?Boolean

Returns:

  • (Boolean)

See Also:

  • {HOOK_TYPES}


50
51
52
# File 'lib/puppet/settings/base_setting.rb', line 50

def call_hook_on_define?
  call_hook == :on_define_and_write
end

#call_hook_on_initialize?Boolean

Returns:

  • (Boolean)

See Also:

  • {HOOK_TYPES}


55
56
57
# File 'lib/puppet/settings/base_setting.rb', line 55

def call_hook_on_initialize?
  call_hook == :on_initialize_and_write
end

#completely_deprecated?Boolean

True if we should raise a deprecation_warning if the setting is submitted on the commandline or is set in puppet.conf.

Returns:

  • (Boolean)


211
212
213
# File 'lib/puppet/settings/base_setting.rb', line 211

def completely_deprecated?
  @deprecated == :completely
end

#deprecated?Boolean

Returns:

  • (Boolean)


205
206
207
# File 'lib/puppet/settings/base_setting.rb', line 205

def deprecated?
  !!@deprecated
end

#getopt_argsObject

get the arguments in getopt format



60
61
62
63
64
65
66
# File 'lib/puppet/settings/base_setting.rb', line 60

def getopt_args
  if short
    [["--#{name}", "-#{short}", GetoptLong::REQUIRED_ARGUMENT]]
  else
    [["--#{name}", GetoptLong::REQUIRED_ARGUMENT]]
  end
end

#has_hook?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/puppet/settings/base_setting.rb', line 82

def has_hook?
  @has_hook
end

#hook=(block) ⇒ Object



77
78
79
80
# File 'lib/puppet/settings/base_setting.rb', line 77

def hook=(block)
  @has_hook = true
  meta_def :handle, &block
end

#inspectObject



221
222
223
# File 'lib/puppet/settings/base_setting.rb', line 221

def inspect
  %Q{<#{self.class}:#{self.object_id} @name="#{@name}" @section="#{@section}" @default="#{@default}" @call_hook="#{@call_hook}">}
end

#iscreatedObject



120
121
122
# File 'lib/puppet/settings/base_setting.rb', line 120

def iscreated
  @iscreated = true
end

#iscreated?Boolean

Returns:

  • (Boolean)


124
125
126
# File 'lib/puppet/settings/base_setting.rb', line 124

def iscreated?
  @iscreated
end

#munge(value) ⇒ Object

Modify the value when it is first evaluated



183
184
185
# File 'lib/puppet/settings/base_setting.rb', line 183

def munge(value)
  value
end

#optparse_argsObject

get the arguments in OptionParser format



69
70
71
72
73
74
75
# File 'lib/puppet/settings/base_setting.rb', line 69

def optparse_args
  if short
    ["--#{name}", "-#{short}", desc, :REQUIRED]
  else
    ["--#{name}", desc, :REQUIRED]
  end
end

Print the value for the user in a config compatible format



188
189
190
# File 'lib/puppet/settings/base_setting.rb', line 188

def print(value)
  munge(value)
end

#set_meta(meta) ⇒ Object



192
193
194
# File 'lib/puppet/settings/base_setting.rb', line 192

def set_meta(meta)
  Puppet.notice("#{name} does not support meta data. Ignoring.")
end

#to_configObject

Convert the object to a config statement.



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/puppet/settings/base_setting.rb', line 149

def to_config
  require_relative '../../puppet/util/docs'
  # Scrub any funky indentation; comment out description.
  str = Puppet::Util::Docs.scrub(@desc).gsub(/^/, "# ") + "\n"

  # Add in a statement about the default.
  str << "# The default value is '#{default(true)}'.\n" if default(true)

  # If the value has not been overridden, then print it out commented
  # and unconverted, so it's clear that that's the default and how it
  # works.
  value = @settings.value(self.name)

  if value != @default
    line = "#{@name} = #{value}"
  else
    line = "# #{@name} = #{@default}"
  end

  str << (line + "\n")

  # Indent
  str.gsub(/^/, "    ")
end

#value(bypass_interpolation = false) ⇒ String

Returns Retrieves the value, or if it’s not set, retrieves the default.

Parameters:

  • bypass_interpolation (Boolean) (defaults to: false)

    Set this true to skip the interpolation step, returning the raw setting value. Defaults to false.

Returns:

  • (String)

    Retrieves the value, or if it’s not set, retrieves the default.



178
179
180
# File 'lib/puppet/settings/base_setting.rb', line 178

def value(bypass_interpolation = false)
  @settings.value(self.name, nil, bypass_interpolation)
end