Class: Puppet::Settings
- Includes:
- Enumerable
- Defined in:
- lib/puppet/settings.rb,
lib/puppet/settings/errors.rb
Overview
The class for handling configuration files.
Defined Under Namespace
Classes: ArraySetting, AutosignSetting, BaseSetting, BooleanSetting, ChainedValues, ConfigFile, DirectorySetting, DurationSetting, EnumSetting, EnvironmentConf, FileOrDirectorySetting, FileSetting, IniFile, InterpolationError, ParseError, PathSetting, PrioritySetting, SettingsError, StringSetting, TTLSetting, TerminusSetting, ValidationError, ValueTranslator, Values, ValuesFromEnvironmentConf, ValuesFromSection
Constant Summary collapse
- PuppetOptionParser =
local reference for convenience
Puppet::Util::CommandLine::PuppetOptionParser
- REQUIRED_APP_SETTINGS =
These are the settings that every app is required to specify; there are reasonable defaults defined in application.rb.
[:logdir, :confdir, :vardir]
- SETTING_TYPES =
{ :string => StringSetting, :file => FileSetting, :directory => DirectorySetting, :file_or_directory => FileOrDirectorySetting, :path => PathSetting, :boolean => BooleanSetting, :terminus => TerminusSetting, :duration => DurationSetting, :ttl => TTLSetting, :array => ArraySetting, :enum => EnumSetting, :priority => PrioritySetting, :autosign => AutosignSetting, }
Instance Attribute Summary collapse
- #files ⇒ Object writeonly
- #timer ⇒ Object readonly
Class Method Summary collapse
-
.app_defaults_for_run_mode(run_mode) ⇒ Object
This method is intended for puppet internal use only; it is a convenience method that returns reasonable application default settings values for a given run_mode.
-
.clean_opt(opt, val) ⇒ Object
A utility method (public, is used by application.rb and perhaps elsewhere) that munges a command-line option string into the format that Puppet.settings expects.
- .default_certname ⇒ Object
- .default_config_file_name ⇒ Object
- .domain_fact ⇒ Object
- .hostname_fact ⇒ Object
Instance Method Summary collapse
-
#[](param) ⇒ Object
private
Retrieve a config value.
-
#[]=(param, value) ⇒ Object
private
Set a config value.
-
#addargs(options) ⇒ Object
Generate the list of valid arguments, in a format that GetoptLong can understand, and add them to the passed option list.
- #app_defaults_initialized? ⇒ Boolean
- #apply_metadata_from_section(section) ⇒ Object
-
#boolean?(param) ⇒ Boolean
Is our setting a boolean setting?.
-
#clear ⇒ Object
Remove all set values, potentially skipping cli values.
- #clearused ⇒ Object
-
#define_settings(section, defs) ⇒ Object
Define a group of settings.
-
#description(name) ⇒ Object
Return a value’s description.
- #each ⇒ Object
-
#eachsection ⇒ Object
Iterate over each section name.
-
#flush_cache ⇒ Object
Clear @cache, @used and the Environment.
- #generate_config ⇒ Object
- #generate_manifest ⇒ Object
- #global_defaults_initialized? ⇒ Boolean
-
#handlearg(opt, value = nil) ⇒ Object
Handle a command-line argument.
- #include?(name) ⇒ Boolean
-
#initialize ⇒ Settings
constructor
Create a new collection of config settings.
- #initialize_app_defaults(app_defaults) ⇒ Object
- #initialize_global_settings(args = []) ⇒ Object
-
#metadata(param) ⇒ Object
Return a given object’s file metadata.
-
#mkdir(default) ⇒ Object
Make a directory with the appropriate user, group, and mode.
-
#optparse_addargs(options) ⇒ Object
Generate the list of valid arguments, in a format that OptionParser can understand, and add them to the passed option list.
-
#override_default(param, value) ⇒ Object
private
Create a new default value for the given setting.
-
#params(section = nil) ⇒ Object
Return all of the settings associated with a given section.
- #parse_config(text, file = "text") ⇒ Object
-
#parse_file(file) ⇒ Puppet::Settings::ConfigFile::Conf
private
This method just turns a file into a new ConfigFile::Conf instance.
-
#persection(section) ⇒ Object
Iterate across all of the objects in a given section.
-
#preferred_run_mode ⇒ Object
The currently configured run mode that is preferred for constructing the application configuration.
-
#preferred_run_mode=(mode) ⇒ Object
private
PRIVATE! This only exists because we need a hook to validate the run mode when it’s being set, and it should never, ever, ever, ever be called from outside of this file.
-
#print_config_options ⇒ Object
Prints the contents of a config file with the available config settings, or it prints a single value of a config setting.
- #print_configs ⇒ Object
- #print_configs? ⇒ Boolean
-
#reparse_config_files ⇒ Object
Reparse our config file, if necessary.
- #reuse ⇒ Object
-
#searchpath(environment = nil) ⇒ Object
The order in which to search for values.
-
#sectionlist ⇒ Object
Get a list of objects per section.
- #service_group_available? ⇒ Boolean
- #service_user_available? ⇒ Boolean
-
#set_by_cli?(param) ⇒ Boolean
Allow later inspection to determine if the setting was set on the command line, or through some other code path.
- #set_value(param, value, type, options = {}) ⇒ Object
-
#setdefaults(section, defs) ⇒ Object
Deprecated; use #define_settings instead.
-
#setting(param) ⇒ Object
Return an object by name.
-
#shortinclude?(short) ⇒ Boolean
check to see if a short name is already defined.
-
#to_catalog(*sections) ⇒ Object
Convert the settings we manage into a catalog full of resources that model those settings.
-
#to_config ⇒ Object
Convert our list of config settings into a configuration file.
-
#to_manifest ⇒ Object
Convert to a parseable manifest.
- #uninterpolated_value(param, environment = nil) ⇒ Object
-
#use(*sections) ⇒ Object
Create the necessary objects to use a section.
- #valid?(param) ⇒ Boolean
-
#value(param, environment = nil, bypass_interpolation = false) ⇒ Object
Find the correct value using our search path.
-
#values(environment, section) ⇒ Puppet::Settings::ChainedValues
Retrieve an object that can be used for looking up values of configuration settings.
-
#which_configuration_file ⇒ Object
private
(#15337) All of the logic to determine the configuration file to use should be centralized into this method.
Constructor Details
#initialize ⇒ Settings
Create a new collection of config settings.
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 |
# File 'lib/puppet/settings.rb', line 75 def initialize @config = {} @shortnames = {} @created = [] # Keep track of set values. @value_sets = { :cli => Values.new(:cli, @config), :memory => Values.new(:memory, @config), :application_defaults => Values.new(:application_defaults, @config), :overridden_defaults => Values.new(:overridden_defaults, @config), } @configuration_file = nil # And keep a per-environment cache @cache = Hash.new { |hash, key| hash[key] = {} } # The list of sections we've used. @used = [] @hooks_to_call_on_application_initialization = [] @deprecated_setting_names = [] @deprecated_settings_that_have_been_configured = [] @translate = Puppet::Settings::ValueTranslator.new @config_file_parser = Puppet::Settings::ConfigFile.new(@translate) end |
Instance Attribute Details
#files=(value) ⇒ Object
32 33 34 |
# File 'lib/puppet/settings.rb', line 32 def files=(value) @files = value end |
Class Method Details
.app_defaults_for_run_mode(run_mode) ⇒ Object
This method is intended for puppet internal use only; it is a convenience method that returns reasonable application default settings values for a given run_mode.
40 41 42 43 44 45 46 47 48 49 |
# File 'lib/puppet/settings.rb', line 40 def self.app_defaults_for_run_mode(run_mode) { :name => run_mode.to_s, :run_mode => run_mode.name, :confdir => run_mode.conf_dir, :vardir => run_mode.var_dir, :rundir => run_mode.run_dir, :logdir => run_mode.log_dir, } end |
.clean_opt(opt, val) ⇒ Object
A utility method (public, is used by application.rb and perhaps elsewhere) that munges a command-line option string into the format that Puppet.settings expects. (This mostly has to deal with handling the “no-” prefix on flag/boolean options).
295 296 297 298 299 300 301 302 303 |
# File 'lib/puppet/settings.rb', line 295 def self.clean_opt(opt, val) # rewrite --[no-]option to --no-option if that's what was given if opt =~ /\[no-\]/ and !val opt = opt.gsub(/\[no-\]/,'no-') end # otherwise remove the [no-] prefix to not confuse everybody opt = opt.gsub(/\[no-\]/, '') [opt, val] end |
.default_certname ⇒ Object
51 52 53 54 55 56 57 58 59 60 |
# File 'lib/puppet/settings.rb', line 51 def self.default_certname() hostname = hostname_fact domain = domain_fact if domain and domain != "" fqdn = [hostname, domain].join(".") else fqdn = hostname end fqdn.to_s.gsub(/\.$/, '') end |
.default_config_file_name ⇒ Object
70 71 72 |
# File 'lib/puppet/settings.rb', line 70 def self.default_config_file_name "puppet.conf" end |
.domain_fact ⇒ Object
66 67 68 |
# File 'lib/puppet/settings.rb', line 66 def self.domain_fact() Facter["domain"].value end |
.hostname_fact ⇒ Object
62 63 64 |
# File 'lib/puppet/settings.rb', line 62 def self.hostname_fact() Facter["hostname"].value end |
Instance Method Details
#[](param) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Retrieve a config value
114 115 116 117 118 119 |
# File 'lib/puppet/settings.rb', line 114 def [](param) if @deprecated_setting_names.include?(param) issue_deprecation_warning(setting(param), "Accessing '#{param}' as a setting is deprecated.") end value(param) end |
#[]=(param, value) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Set a config value. This doesn’t set the defaults, it sets the value itself.
125 126 127 128 129 130 131 |
# File 'lib/puppet/settings.rb', line 125 def []=(param, value) if @deprecated_setting_names.include?(param) issue_deprecation_warning(setting(param), "Modifying '#{param}' as a setting is deprecated.") end @value_sets[:memory].set(param, value) unsafe_flush_cache end |
#addargs(options) ⇒ Object
Generate the list of valid arguments, in a format that GetoptLong can understand, and add them to the passed option list.
149 150 151 152 153 154 155 156 |
# File 'lib/puppet/settings.rb', line 149 def addargs() # Add all of the settings as valid options. self.each { |name, setting| setting.getopt_args.each { |args| << args } } end |
#app_defaults_initialized? ⇒ Boolean
306 307 308 |
# File 'lib/puppet/settings.rb', line 306 def app_defaults_initialized? @app_defaults_initialized end |
#apply_metadata_from_section(section) ⇒ Object
649 650 651 652 653 654 655 |
# File 'lib/puppet/settings.rb', line 649 def (section) section.settings.each do |setting| if setting. && type = @config[setting.name] type.(setting.) end end end |
#boolean?(param) ⇒ Boolean
Is our setting a boolean setting?
170 171 172 173 |
# File 'lib/puppet/settings.rb', line 170 def boolean?(param) param = param.to_sym @config.include?(param) and @config[param].kind_of?(BooleanSetting) end |
#clear ⇒ Object
Remove all set values, potentially skipping cli values.
176 177 178 |
# File 'lib/puppet/settings.rb', line 176 def clear unsafe_clear end |
#clearused ⇒ Object
229 230 231 232 |
# File 'lib/puppet/settings.rb', line 229 def clearused @cache.clear @used = [] end |
#define_settings(section, defs) ⇒ Object
Define a group of settings.
846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 |
# File 'lib/puppet/settings.rb', line 846 def define_settings(section, defs) section = section.to_sym call = [] defs.each do |name, hash| raise ArgumentError, "setting definition for '#{name}' is not a hash!" unless hash.is_a? Hash name = name.to_sym hash[:name] = name hash[:section] = section raise ArgumentError, "Setting #{name} is already defined" if @config.include?(name) tryconfig = newsetting(hash) if short = tryconfig.short if other = @shortnames[short] raise ArgumentError, "Setting #{other.name} is already using short name '#{short}'" end @shortnames[short] = tryconfig end @config[name] = tryconfig # Collect the settings that need to have their hooks called immediately. # We have to collect them so that we can be sure we're fully initialized before # the hook is called. if tryconfig.has_hook? if tryconfig.call_hook_on_define? call << tryconfig elsif tryconfig.call_hook_on_initialize? @hooks_to_call_on_application_initialization << tryconfig end end @deprecated_setting_names << name if tryconfig.deprecated? end call.each do |setting| setting.handle(self.value(setting.name)) end end |
#description(name) ⇒ Object
Return a value’s description.
344 345 346 347 348 349 350 |
# File 'lib/puppet/settings.rb', line 344 def description(name) if obj = @config[name.to_sym] obj.desc else nil end end |
#each ⇒ Object
352 353 354 355 356 |
# File 'lib/puppet/settings.rb', line 352 def each @config.each { |name, object| yield name, object } end |
#eachsection ⇒ Object
Iterate over each section name.
359 360 361 362 363 364 365 366 367 368 |
# File 'lib/puppet/settings.rb', line 359 def eachsection yielded = [] @config.each do |name, object| section = object.section unless yielded.include? section yield section yielded << section end end end |
#flush_cache ⇒ Object
Clear @cache, @used and the Environment.
Whenever an object is returned by Settings, a copy is stored in @cache. As long as Setting attributes that determine the content of returned objects remain unchanged, Settings can keep returning objects from @cache without re-fetching or re-generating them.
Whenever a Settings attribute changes, such as @values or @preferred_run_mode, this method must be called to clear out the caches so that updated objects will be returned.
213 214 215 |
# File 'lib/puppet/settings.rb', line 213 def flush_cache unsafe_flush_cache end |
#generate_config ⇒ Object
455 456 457 458 |
# File 'lib/puppet/settings.rb', line 455 def generate_config puts to_config true end |
#generate_manifest ⇒ Object
460 461 462 463 |
# File 'lib/puppet/settings.rb', line 460 def generate_manifest puts to_manifest true end |
#global_defaults_initialized? ⇒ Boolean
234 235 236 |
# File 'lib/puppet/settings.rb', line 234 def global_defaults_initialized?() @global_defaults_initialized end |
#handlearg(opt, value = nil) ⇒ Object
Handle a command-line argument.
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 |
# File 'lib/puppet/settings.rb', line 377 def handlearg(opt, value = nil) @cache.clear if value.is_a?(FalseClass) value = "false" elsif value.is_a?(TrueClass) value = "true" end value &&= @translate[value] str = opt.sub(/^--/,'') bool = true newstr = str.sub(/^no-/, '') if newstr != str str = newstr bool = false end str = str.intern if @config[str].is_a?(Puppet::Settings::BooleanSetting) if value == "" or value.nil? value = bool end end if s = @config[str] @deprecated_settings_that_have_been_configured << s if s.completely_deprecated? end @value_sets[:cli].set(str, value) unsafe_flush_cache end |
#include?(name) ⇒ Boolean
411 412 413 414 |
# File 'lib/puppet/settings.rb', line 411 def include?(name) name = name.intern if name.is_a? String @config.include?(name) end |
#initialize_app_defaults(app_defaults) ⇒ Object
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 |
# File 'lib/puppet/settings.rb', line 310 def initialize_app_defaults(app_defaults) REQUIRED_APP_SETTINGS.each do |key| raise SettingsError, "missing required app default setting '#{key}'" unless app_defaults.has_key?(key) end app_defaults.each do |key, value| if key == :run_mode self.preferred_run_mode = value else @value_sets[:application_defaults].set(key, value) unsafe_flush_cache end end call_hooks_deferred_to_application_initialization issue_deprecations @app_defaults_initialized = true end |
#initialize_global_settings(args = []) ⇒ Object
238 239 240 241 242 243 244 245 246 247 248 249 250 |
# File 'lib/puppet/settings.rb', line 238 def initialize_global_settings(args = []) raise Puppet::DevError, "Attempting to initialize global default settings more than once!" if global_defaults_initialized? # The first two phases of the lifecycle of a puppet application are: # 1) Parse the command line options and handle any of them that are # registered, defined "global" puppet settings (mostly from defaults.rb). # 2) Parse the puppet config file(s). (args) parse_config_files @global_defaults_initialized = true end |
#metadata(param) ⇒ Object
Return a given object’s file metadata.
476 477 478 479 480 481 482 483 484 485 486 |
# File 'lib/puppet/settings.rb', line 476 def (param) if obj = @config[param.to_sym] and obj.is_a?(FileSetting) { :owner => obj.owner, :group => obj.group, :mode => obj.mode }.delete_if { |key, value| value.nil? } else nil end end |
#mkdir(default) ⇒ Object
Make a directory with the appropriate user, group, and mode
489 490 491 492 493 494 495 496 |
# File 'lib/puppet/settings.rb', line 489 def mkdir(default) obj = get_config_file_default(default) Puppet::Util::SUIDManager.asuser(obj.owner, obj.group) do mode = obj.mode || 0750 Dir.mkdir(obj.value, mode) end end |
#optparse_addargs(options) ⇒ Object
Generate the list of valid arguments, in a format that OptionParser can understand, and add them to the passed option list.
160 161 162 163 164 165 166 167 |
# File 'lib/puppet/settings.rb', line 160 def optparse_addargs() # Add all of the settings as valid options. self.each { |name, setting| << setting.optparse_args } end |
#override_default(param, value) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Create a new default value for the given setting. The default overrides are higher precedence than the defaults given in defaults.rb, but lower precedence than any other values for the setting. This allows one setting a to change the default of setting b, but still allow a user to provide a value for setting b.
142 143 144 145 |
# File 'lib/puppet/settings.rb', line 142 def override_default(param, value) @value_sets[:overridden_defaults].set(param, value) unsafe_flush_cache end |
#params(section = nil) ⇒ Object
Return all of the settings associated with a given section.
520 521 522 523 524 525 526 527 528 529 530 531 |
# File 'lib/puppet/settings.rb', line 520 def params(section = nil) if section section = section.intern if section.is_a? String @config.find_all { |name, obj| obj.section == section }.collect { |name, obj| name } else @config.keys end end |
#parse_config(text, file = "text") ⇒ Object
533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 |
# File 'lib/puppet/settings.rb', line 533 def parse_config(text, file = "text") begin data = @config_file_parser.parse_file(file, text) rescue => detail Puppet.log_exception(detail, "Could not parse #{file}: #{detail}") return end # If we get here and don't have any data, we just return and don't muck with the current state of the world. return if data.nil? record_deprecations_from_puppet_conf(data) # If we get here then we have some data, so we need to clear out any previous settings that may have come from # config files. unsafe_clear(false, false) # And now we can repopulate with the values from our last parsing of the config files. @configuration_file = data # Determine our environment, if we have one. if @config[:environment] env = self.value(:environment).to_sym else env = "none" end # Call any hooks we should be calling. @config.values.select(&:has_hook?).each do |setting| value_sets_for(env, self.preferred_run_mode).each do |source| if source.include?(setting.name) # We still have to use value to retrieve the value, since # we want the fully interpolated value, not $vardir/lib or whatever. # This results in extra work, but so few of the settings # will have associated hooks that it ends up being less work this # way overall. if setting.call_hook_on_initialize? @hooks_to_call_on_application_initialization << setting else setting.handle(self.value(setting.name, env)) end break end end end call_hooks_deferred_to_application_initialization :ignore_interpolation_dependency_errors => true end |
#parse_file(file) ⇒ Puppet::Settings::ConfigFile::Conf
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method just turns a file into a new ConfigFile::Conf instance
1061 1062 1063 |
# File 'lib/puppet/settings.rb', line 1061 def parse_file(file) @config_file_parser.parse_file(file, read_file(file)) end |
#persection(section) ⇒ Object
Iterate across all of the objects in a given section.
703 704 705 706 707 708 709 710 |
# File 'lib/puppet/settings.rb', line 703 def persection(section) section = section.to_sym self.each { |name, obj| if obj.section == section yield obj end } end |
#preferred_run_mode ⇒ Object
The currently configured run mode that is preferred for constructing the application configuration.
499 500 501 |
# File 'lib/puppet/settings.rb', line 499 def preferred_run_mode @preferred_run_mode_name || :user end |
#preferred_run_mode=(mode) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
PRIVATE! This only exists because we need a hook to validate the run mode when it’s being set, and
it should never, ever, ever, ever be called from outside of this file.
This method is also called when –run_mode MODE is used on the command line to set the default
509 510 511 512 513 514 515 516 517 |
# File 'lib/puppet/settings.rb', line 509 def preferred_run_mode=(mode) mode = mode.to_s.downcase.intern raise ValidationError, "Invalid run mode '#{mode}'" unless [:master, :agent, :user].include?(mode) @preferred_run_mode_name = mode # Changing the run mode has far-reaching consequences. Flush any cached # settings so they will be re-generated. flush_cache mode end |
#print_config_options ⇒ Object
Prints the contents of a config file with the available config settings, or it prints a single value of a config setting.
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 |
# File 'lib/puppet/settings.rb', line 424 def env = value(:environment) val = value(:configprint) if val == "all" hash = {} each do |name, obj| val = value(name,env) val = val.inspect if val == "" hash[name] = val end hash.sort { |a,b| a[0].to_s <=> b[0].to_s }.each do |name, val| puts "#{name} = #{val}" end else val.split(/\s*,\s*/).sort.each do |v| if include?(v) #if there is only one value, just print it for back compatibility if v == val puts value(val,env) break end puts "#{v} = #{value(v,env)}" else puts "invalid setting: #{v}" return false end end end true end |
#print_configs ⇒ Object
465 466 467 468 469 |
# File 'lib/puppet/settings.rb', line 465 def print_configs return if value(:configprint) != "" return generate_config if value(:genconfig) generate_manifest if value(:genmanifest) end |
#print_configs? ⇒ Boolean
471 472 473 |
# File 'lib/puppet/settings.rb', line 471 def print_configs? (value(:configprint) != "" || value(:genconfig) || value(:genmanifest)) && true end |
#reparse_config_files ⇒ Object
Reparse our config file, if necessary.
713 714 715 716 717 718 719 720 721 |
# File 'lib/puppet/settings.rb', line 713 def reparse_config_files if files if filename = any_files_changed? Puppet.notice "Config file #{filename} changed; triggering re-parse of all config files." parse_config_files reuse end end end |
#reuse ⇒ Object
746 747 748 749 750 751 |
# File 'lib/puppet/settings.rb', line 746 def reuse return unless defined?(@used) new = @used @used = [] self.use(*new) end |
#searchpath(environment = nil) ⇒ Object
The order in which to search for values.
754 755 756 |
# File 'lib/puppet/settings.rb', line 754 def searchpath(environment = nil) [:memory, :cli, environment, :run_mode, :main, :application_defaults, :overridden_defaults].compact end |
#sectionlist ⇒ Object
Get a list of objects per section
759 760 761 762 763 764 765 766 767 768 769 |
# File 'lib/puppet/settings.rb', line 759 def sectionlist sectionlist = [] self.each { |name, obj| section = obj.section || "puppet" sections[section] ||= [] sectionlist << section unless sectionlist.include?(section) sections[section] << obj } return sectionlist, sections end |
#service_group_available? ⇒ Boolean
783 784 785 786 787 788 789 790 791 792 793 |
# File 'lib/puppet/settings.rb', line 783 def service_group_available? return @service_group_available if defined?(@service_group_available) if self[:group] group = Puppet::Type.type(:group).new :name => self[:group], :audit => :ensure @service_group_available = group.exists? else @service_group_available = false end end |
#service_user_available? ⇒ Boolean
771 772 773 774 775 776 777 778 779 780 781 |
# File 'lib/puppet/settings.rb', line 771 def service_user_available? return @service_user_available if defined?(@service_user_available) if self[:user] user = Puppet::Type.type(:user).new :name => self[:user], :audit => :ensure @service_user_available = user.exists? else @service_user_available = false end end |
#set_by_cli?(param) ⇒ Boolean
Allow later inspection to determine if the setting was set on the command line, or through some other code path. Used for the dns_alt_names option during cert generate. –daniel 2011-10-18
798 799 800 801 |
# File 'lib/puppet/settings.rb', line 798 def set_by_cli?(param) param = param.to_sym !@value_sets[:cli].lookup(param).nil? end |
#set_value(param, value, type, options = {}) ⇒ Object
803 804 805 806 807 808 809 |
# File 'lib/puppet/settings.rb', line 803 def set_value(param, value, type, = {}) Puppet.deprecation_warning("Puppet.settings.set_value is deprecated. Use Puppet[]= instead.") if @value_sets[type] @value_sets[type].set(param, value) unsafe_flush_cache end end |
#setdefaults(section, defs) ⇒ Object
Deprecated; use #define_settings instead
812 813 814 815 |
# File 'lib/puppet/settings.rb', line 812 def setdefaults(section, defs) Puppet.deprecation_warning("'setdefaults' is deprecated and will be removed; please call 'define_settings' instead") define_settings(section, defs) end |
#setting(param) ⇒ Object
Return an object by name.
106 107 108 |
# File 'lib/puppet/settings.rb', line 106 def setting(name) @config[name] end |
#shortinclude?(short) ⇒ Boolean
check to see if a short name is already defined
417 418 419 420 |
# File 'lib/puppet/settings.rb', line 417 def shortinclude?(short) short = short.intern if name.is_a? String @shortnames.include?(short) end |
#to_catalog(*sections) ⇒ Object
Convert the settings we manage into a catalog full of resources that model those settings.
885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 |
# File 'lib/puppet/settings.rb', line 885 def to_catalog(*sections) sections = nil if sections.empty? catalog = Puppet::Resource::Catalog.new("Settings", Puppet::Node::Environment::NONE) @config.keys.find_all { |key| @config[key].is_a?(FileSetting) }.each do |key| file = @config[key] next unless (sections.nil? or sections.include?(file.section)) next unless resource = file.to_resource next if catalog.resource(resource.ref) Puppet.debug("Using settings: adding file resource '#{key}': '#{resource.inspect}'") catalog.add_resource(resource) end add_user_resources(catalog, sections) catalog end |
#to_config ⇒ Object
Convert our list of config settings into a configuration file.
907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 |
# File 'lib/puppet/settings.rb', line 907 def to_config str = %{The configuration file for #{Puppet.run_mode.name}. Note that this file is likely to have unused settings in it; any setting that's valid anywhere in Puppet can be in any config file, even if it's not used. Every section can specify three special parameters: owner, group, and mode. These parameters affect the required permissions of any files specified after their specification. Puppet will sometimes use these parameters to check its own configured state, so they can be used to make Puppet a bit more self-managing. The file format supports octothorpe-commented lines, but not partial-line comments. Generated on #{Time.now}. }.gsub(/^/, "# ") # Add a section heading that matches our name. str += "[#{preferred_run_mode}]\n" eachsection do |section| persection(section) do |obj| str += obj.to_config + "\n" unless obj.name == :genconfig end end return str end |
#to_manifest ⇒ Object
Convert to a parseable manifest
935 936 937 938 939 940 |
# File 'lib/puppet/settings.rb', line 935 def to_manifest catalog = to_catalog catalog.resource_refs.collect do |ref| catalog.resource(ref).to_manifest end.join("\n\n") end |
#uninterpolated_value(param, environment = nil) ⇒ Object
980 981 982 983 984 985 986 |
# File 'lib/puppet/settings.rb', line 980 def uninterpolated_value(param, environment = nil) Puppet.deprecation_warning("Puppet.settings.uninterpolated_value is deprecated. Use Puppet.settings.value instead") param = param.to_sym environment &&= environment.to_sym values(environment, self.preferred_run_mode).lookup(param) end |
#use(*sections) ⇒ Object
Create the necessary objects to use a section. This is idempotent; you can ‘use’ a section as many times as you want.
944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 |
# File 'lib/puppet/settings.rb', line 944 def use(*sections) sections = sections.collect { |s| s.to_sym } sections = sections.reject { |s| @used.include?(s) } return if sections.empty? begin catalog = to_catalog(*sections).to_ral rescue => detail Puppet.log_and_raise(detail, "Could not create resources for managing Puppet's files and directories in sections #{sections.inspect}: #{detail}") end catalog.host_config = false catalog.apply do |transaction| if transaction.any_failed? report = transaction.report status_failures = report.resource_statuses.values.select { |r| r.failed? } status_fail_msg = status_failures. collect(&:events). flatten. select { |event| event.status == 'failure' }. collect { |event| "#{event.resource}: #{event.message}" }.join("; ") raise "Got #{status_failures.length} failure(s) while initializing: #{status_fail_msg}" end end sections.each { |s| @used << s } @used.uniq! end |
#valid?(param) ⇒ Boolean
975 976 977 978 |
# File 'lib/puppet/settings.rb', line 975 def valid?(param) param = param.to_sym @config.has_key?(param) end |
#value(param, environment = nil, bypass_interpolation = false) ⇒ Object
Find the correct value using our search path.
1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 |
# File 'lib/puppet/settings.rb', line 1012 def value(param, environment = nil, bypass_interpolation = false) param = param.to_sym environment &&= environment.to_sym setting = @config[param] # Short circuit to nil for undefined settings. return nil if setting.nil? # Check the cache first. It needs to be a per-environment # cache so that we don't spread values from one env # to another. if @cache[environment||"none"].has_key?(param) return @cache[environment||"none"][param] elsif bypass_interpolation val = values(environment, self.preferred_run_mode).lookup(param) else val = values(environment, self.preferred_run_mode).interpolate(param) end @cache[environment||"none"][param] = val val end |
#values(environment, section) ⇒ Puppet::Settings::ChainedValues
Retrieve an object that can be used for looking up values of configuration settings.
995 996 997 998 999 1000 1001 |
# File 'lib/puppet/settings.rb', line 995 def values(environment, section) ChainedValues.new( section, environment, value_sets_for(environment, section), @config) end |
#which_configuration_file ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
this code duplicates Util::RunMode#which_dir as described in #16637
(#15337) All of the logic to determine the configuration file to use
should be centralized into this method. The simplified approach is:
-
If there is an explicit configuration file, use that. (–confdir or –config)
-
If we’re running as a root process, use the system puppet.conf (usually /etc/puppet/puppet.conf)
-
Otherwise, use the user puppet.conf (usually ~/.puppet/puppet.conf)
1049 1050 1051 1052 1053 1054 1055 |
# File 'lib/puppet/settings.rb', line 1049 def which_configuration_file if explicit_config_file? or Puppet.features.root? then return main_config_file else return user_config_file end end |