Class: Puppet::Util::Settings
- Includes:
- Enumerable, Cacher
- Defined in:
- lib/puppet/util/settings.rb
Overview
The class for handling configuration files.
Defined Under Namespace
Classes: BooleanSetting, FileSetting, Setting
Constant Summary collapse
- ReadOnly =
[:run_mode, :name]
Instance Attribute Summary collapse
-
#file ⇒ Object
Returns the value of attribute file.
-
#timer ⇒ Object
readonly
Returns the value of attribute timer.
Attributes included from Cacher::Expirer
Instance Method Summary collapse
-
#[](param) ⇒ Object
Retrieve a config value.
-
#[]=(param, value) ⇒ Object
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.
-
#boolean?(param) ⇒ Boolean
Is our parameter a boolean parameter?.
-
#clear(exceptcli = false) ⇒ Object
Remove all set values, potentially skipping cli values.
-
#clearused ⇒ Object
This is mostly just used for testing.
-
#convert(value, environment = nil) ⇒ Object
Do variable interpolation on the value.
-
#description(name) ⇒ Object
Return a value’s description.
- #each ⇒ Object
-
#eachsection ⇒ Object
Iterate over each section name.
- #generate_config ⇒ Object
- #generate_manifest ⇒ Object
-
#handlearg(opt, value = nil) ⇒ Object
Handle a command-line argument.
- #include?(name) ⇒ Boolean
-
#initialize ⇒ Settings
constructor
Create a new collection of config settings.
- #legacy_to_mode(type, param) ⇒ 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.
-
#params(section = nil) ⇒ Object
Return all of the parameters associated with a given section.
-
#parse ⇒ Object
Parse the configuration file.
-
#persection(section) ⇒ Object
Iterate across all of the objects in a given section.
-
#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
- #readwritelock(default, *args, &bloc) ⇒ Object
-
#reparse ⇒ Object
Reparse our config file, if necessary.
- #reuse ⇒ Object
-
#run_mode ⇒ Object
Figure out the section name for the run_mode.
-
#searchpath(environment = nil) ⇒ Object
The order in which to search for values.
-
#sectionlist ⇒ Object
Get a list of objects per section.
- #service_user_available? ⇒ Boolean
-
#set_filetimeout_timer ⇒ Object
Create a timer to check whether the file should be reparsed.
- #set_value(param, value, type, options = {}) ⇒ Object
-
#setdefaults(section, defs) ⇒ Object
Set a bunch of defaults in a given section.
-
#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
-
#unsafe_clear(exceptcli = false) ⇒ Object
Remove all set values, potentially skipping cli values.
-
#unsafe_parse(file) ⇒ Object
Unsafely parse the file – this isn’t thread-safe and causes plenty of problems if used directly.
-
#use(*sections) ⇒ Object
Create the necessary objects to use a section.
- #valid?(param) ⇒ Boolean
-
#value(param, environment = nil) ⇒ Object
Find the correct value using our search path.
-
#write(default, *args, &bloc) ⇒ Object
Open a file with the appropriate user, group, and mode.
-
#writesub(default, file, *args, &bloc) ⇒ Object
Open a non-default file under a default dir with the appropriate user, group, and mode.
Methods included from Cacher
Methods included from Cacher::Expirer
#dependent_data_expired?, #expire
Constructor Details
#initialize ⇒ Settings
Create a new collection of config settings.
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/puppet/util/settings.rb', line 172 def initialize @config = {} @shortnames = {} @created = [] @searchpath = nil # Mutex-like thing to protect @values @sync = Sync.new # Keep track of set values. @values = Hash.new { |hash, key| hash[key] = {} } # And keep a per-environment cache @cache = Hash.new { |hash, key| hash[key] = {} } # The list of sections we've used. @used = [] end |
Instance Attribute Details
#file ⇒ Object
Returns the value of attribute file.
17 18 19 |
# File 'lib/puppet/util/settings.rb', line 17 def file @file end |
#timer ⇒ Object (readonly)
Returns the value of attribute timer.
18 19 20 |
# File 'lib/puppet/util/settings.rb', line 18 def timer @timer end |
Instance Method Details
#[](param) ⇒ Object
Retrieve a config value
23 24 25 |
# File 'lib/puppet/util/settings.rb', line 23 def [](param) value(param) end |
#[]=(param, value) ⇒ Object
Set a config value. This doesn’t set the defaults, it sets the value itself.
28 29 30 |
# File 'lib/puppet/util/settings.rb', line 28 def []=(param, value) set_value(param, value, :memory) 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.
34 35 36 37 38 39 40 41 |
# File 'lib/puppet/util/settings.rb', line 34 def addargs() # Add all of the config parameters as valid options. self.each { |name, setting| setting.getopt_args.each { |args| << args } } end |
#boolean?(param) ⇒ Boolean
Is our parameter a boolean parameter?
55 56 57 58 |
# File 'lib/puppet/util/settings.rb', line 55 def boolean?(param) param = param.to_sym !!(@config.include?(param) and @config[param].kind_of? BooleanSetting) end |
#clear(exceptcli = false) ⇒ Object
Remove all set values, potentially skipping cli values.
61 62 63 64 65 |
# File 'lib/puppet/util/settings.rb', line 61 def clear(exceptcli = false) @sync.synchronize do unsafe_clear(exceptcli) end end |
#clearused ⇒ Object
This is mostly just used for testing.
81 82 83 84 |
# File 'lib/puppet/util/settings.rb', line 81 def clearused @cache.clear @used = [] end |
#convert(value, environment = nil) ⇒ Object
Do variable interpolation on the value.
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/puppet/util/settings.rb', line 87 def convert(value, environment = nil) return value unless value return value unless value.is_a? String newval = value.gsub(/\$(\w+)|\$\{(\w+)\}/) do |value| varname = $2 || $1 if varname == "environment" and environment environment elsif pval = self.value(varname, environment) pval else raise Puppet::DevError, "Could not find value for #{value}" end end newval end |
#description(name) ⇒ Object
Return a value’s description.
105 106 107 108 109 110 111 |
# File 'lib/puppet/util/settings.rb', line 105 def description(name) if obj = @config[name.to_sym] obj.desc else nil end end |
#each ⇒ Object
113 114 115 116 117 |
# File 'lib/puppet/util/settings.rb', line 113 def each @config.each { |name, object| yield name, object } end |
#eachsection ⇒ Object
Iterate over each section name.
120 121 122 123 124 125 126 127 128 129 |
# File 'lib/puppet/util/settings.rb', line 120 def eachsection yielded = [] @config.each do |name, object| section = object.section unless yielded.include? section yield section yielded << section end end end |
#generate_config ⇒ Object
230 231 232 233 |
# File 'lib/puppet/util/settings.rb', line 230 def generate_config puts to_config true end |
#generate_manifest ⇒ Object
235 236 237 238 |
# File 'lib/puppet/util/settings.rb', line 235 def generate_manifest puts to_manifest true end |
#handlearg(opt, value = nil) ⇒ Object
Handle a command-line argument.
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/puppet/util/settings.rb', line 138 def handlearg(opt, value = nil) @cache.clear value &&= munge_value(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::Util::Settings::BooleanSetting) if value == "" or value.nil? value = bool end end set_value(str, value, :cli) end |
#include?(name) ⇒ Boolean
160 161 162 163 |
# File 'lib/puppet/util/settings.rb', line 160 def include?(name) name = name.intern if name.is_a? String @config.include?(name) end |
#legacy_to_mode(type, param) ⇒ Object
462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 |
# File 'lib/puppet/util/settings.rb', line 462 def legacy_to_mode(type, param) if not defined?(@app_names) require 'puppet/util/command_line' command_line = Puppet::Util::CommandLine.new @app_names = Puppet::Util::CommandLine::LegacyName.inject({}) do |hash, pair| app, legacy = pair command_line.require_application app hash[legacy.to_sym] = Puppet::Application.find(app).run_mode.name hash end end if new_type = @app_names[type] Puppet.warning "You have configuration parameter $#{param} specified in [#{type}], which is a deprecated section. I'm assuming you meant [#{new_type}]" return new_type end type end |
#metadata(param) ⇒ Object
Return a given object’s file metadata.
251 252 253 254 255 256 257 258 259 260 261 262 |
# File 'lib/puppet/util/settings.rb', line 251 def (param) if obj = @config[param.to_sym] and obj.is_a?(FileSetting) return [:owner, :group, :mode].inject({}) do |, p| if v = obj.send(p) [p] = v end end else nil end end |
#mkdir(default) ⇒ Object
Make a directory with the appropriate user, group, and mode
265 266 267 268 269 270 271 272 |
# File 'lib/puppet/util/settings.rb', line 265 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.
45 46 47 48 49 50 51 52 |
# File 'lib/puppet/util/settings.rb', line 45 def optparse_addargs() # Add all of the config parameters as valid options. self.each { |name, setting| << setting.optparse_args } end |
#params(section = nil) ⇒ Object
Return all of the parameters associated with a given section.
280 281 282 283 284 285 286 287 288 289 290 291 |
# File 'lib/puppet/util/settings.rb', line 280 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 ⇒ Object
Parse the configuration file. Just provides thread safety.
295 296 297 298 299 300 301 302 303 304 305 |
# File 'lib/puppet/util/settings.rb', line 295 def parse raise "No :config setting defined; cannot parse unknown config file" unless self[:config] @sync.synchronize do unsafe_parse(self[:config]) end # Create a timer so that this file will get checked automatically # and reparsed if necessary. set_filetimeout_timer end |
#persection(section) ⇒ Object
Iterate across all of the objects in a given section.
395 396 397 398 399 400 401 402 |
# File 'lib/puppet/util/settings.rb', line 395 def persection(section) section = section.to_sym self.each { |name, obj| if obj.section == section yield obj end } 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.
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 |
# File 'lib/puppet/util/settings.rb', line 199 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 parameter: #{v}" return false end end end true end |
#print_configs ⇒ Object
240 241 242 243 244 |
# File 'lib/puppet/util/settings.rb', line 240 def print_configs return if value(:configprint) != "" return generate_config if value(:genconfig) generate_manifest if value(:genmanifest) end |
#print_configs? ⇒ Boolean
246 247 248 |
# File 'lib/puppet/util/settings.rb', line 246 def print_configs? (value(:configprint) != "" || value(:genconfig) || value(:genmanifest)) && true end |
#readwritelock(default, *args, &bloc) ⇒ Object
738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 |
# File 'lib/puppet/util/settings.rb', line 738 def readwritelock(default, *args, &bloc) file = value(get_config_file_default(default).name) tmpfile = file + ".tmp" sync = Sync.new raise Puppet::DevError, "Cannot create #{file}; directory #{File.dirname(file)} does not exist" unless FileTest.directory?(File.dirname(tmpfile)) sync.synchronize(Sync::EX) do File.open(file, ::File::CREAT|::File::RDWR, 0600) do |rf| rf.lock_exclusive do if File.exist?(tmpfile) raise Puppet::Error, ".tmp file already exists for #{file}; Aborting locked write. Check the .tmp file and delete if appropriate" end # If there's a failure, remove our tmpfile begin writesub(default, tmpfile, *args, &bloc) rescue File.unlink(tmpfile) if FileTest.exist?(tmpfile) raise end begin File.rename(tmpfile, file) rescue => detail Puppet.err "Could not rename #{file} to #{tmpfile}: #{detail}" File.unlink(tmpfile) if FileTest.exist?(tmpfile) end end end end end |
#reparse ⇒ Object
Reparse our config file, if necessary.
413 414 415 416 417 418 419 |
# File 'lib/puppet/util/settings.rb', line 413 def reparse if file and file.changed? Puppet.notice "Reparsing #{file.file}" parse reuse end end |
#reuse ⇒ Object
421 422 423 424 425 426 427 428 |
# File 'lib/puppet/util/settings.rb', line 421 def reuse return unless defined?(@used) @sync.synchronize do # yay, thread-safe new = @used @used = [] self.use(*new) end end |
#run_mode ⇒ Object
Figure out the section name for the run_mode.
275 276 277 |
# File 'lib/puppet/util/settings.rb', line 275 def run_mode Puppet.run_mode.name end |
#searchpath(environment = nil) ⇒ Object
The order in which to search for values.
431 432 433 434 435 436 437 |
# File 'lib/puppet/util/settings.rb', line 431 def searchpath(environment = nil) if environment [:cli, :memory, environment, :run_mode, :main, :mutable_defaults] else [:cli, :memory, :run_mode, :main, :mutable_defaults] end end |
#sectionlist ⇒ Object
Get a list of objects per section
440 441 442 443 444 445 446 447 448 449 450 |
# File 'lib/puppet/util/settings.rb', line 440 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_user_available? ⇒ Boolean
452 453 454 455 456 457 458 459 460 |
# File 'lib/puppet/util/settings.rb', line 452 def service_user_available? return @service_user_available if defined?(@service_user_available) return @service_user_available = false unless user_name = self[:user] user = Puppet::Type.type(:user).new :name => self[:user], :audit => :ensure @service_user_available = user.exists? end |
#set_filetimeout_timer ⇒ Object
Create a timer to check whether the file should be reparsed.
551 552 553 554 |
# File 'lib/puppet/util/settings.rb', line 551 def set_filetimeout_timer return unless timeout = self[:filetimeout] and timeout = Integer(timeout) and timeout > 0 timer = EventLoop::Timer.new(:interval => timeout, :tolerance => 1, :start? => true) { self.reparse } end |
#set_value(param, value, type, options = {}) ⇒ Object
480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 |
# File 'lib/puppet/util/settings.rb', line 480 def set_value(param, value, type, = {}) param = param.to_sym unless setting = @config[param] if [:ignore_bad_settings] return else raise ArgumentError, "Attempt to assign a value to unknown configuration parameter #{param.inspect}" end end value = setting.munge(value) if setting.respond_to?(:munge) setting.handle(value) if setting.respond_to?(:handle) and not [:dont_trigger_handles] if ReadOnly.include? param and type != :mutable_defaults raise ArgumentError, "You're attempting to set configuration parameter $#{param}, which is read-only." end type = legacy_to_mode(type, param) @sync.synchronize do # yay, thread-safe @values[type][param] = value @cache.clear clearused # Clear the list of environments, because they cache, at least, the module path. # We *could* preferentially just clear them if the modulepath is changed, # but we don't really know if, say, the vardir is changed and the modulepath # is defined relative to it. We need the defined?(stuff) because of loading # order issues. Puppet::Node::Environment.clear if defined?(Puppet::Node) and defined?(Puppet::Node::Environment) end value end |
#setdefaults(section, defs) ⇒ Object
Set a bunch of defaults in a given section. The sections are actually pretty pointless, but they help break things up a bit, anyway.
516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 |
# File 'lib/puppet/util/settings.rb', line 516 def setdefaults(section, defs) section = section.to_sym call = [] defs.each { |name, hash| if hash.is_a? Array unless hash.length == 2 raise ArgumentError, "Defaults specified as an array must contain only the default value and the decription" end tmp = hash hash = {} [:default, :desc].zip(tmp).each { |p,v| hash[p] = v } end name = name.to_sym hash[:name] = name hash[:section] = section raise ArgumentError, "Parameter #{name} is already defined" if @config.include?(name) tryconfig = newsetting(hash) if short = tryconfig.short if other = @shortnames[short] raise ArgumentError, "Parameter #{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. call << tryconfig if tryconfig.call_on_define } call.each { |setting| setting.handle(self.value(setting.name)) } end |
#setting(param) ⇒ Object
Return an object by name.
132 133 134 135 |
# File 'lib/puppet/util/settings.rb', line 132 def setting(param) param = param.to_sym @config[param] end |
#shortinclude?(short) ⇒ Boolean
check to see if a short name is already defined
166 167 168 169 |
# File 'lib/puppet/util/settings.rb', line 166 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.
557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 |
# File 'lib/puppet/util/settings.rb', line 557 def to_catalog(*sections) sections = nil if sections.empty? catalog = Puppet::Resource::Catalog.new("Settings") @config.values.find_all { |value| value.is_a?(FileSetting) }.each do |file| next unless (sections.nil? or sections.include?(file.section)) next unless resource = file.to_resource next if catalog.resource(resource.ref) 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.
576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 |
# File 'lib/puppet/util/settings.rb', line 576 def to_config str = %{The configuration file for #{Puppet[:name]}. Note that this file is likely to have unused configuration parameters in it; any parameter 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. Generated on #{Time.now}. }.gsub(/^/, "# ") # Add a section heading that matches our name. if @config.include?(:run_mode) str += "[#{self[:run_mode]}]\n" end eachsection do |section| persection(section) do |obj| str += obj.to_config + "\n" unless ReadOnly.include? obj.name or obj.name == :genconfig end end return str end |
#to_manifest ⇒ Object
Convert to a parseable manifest
604 605 606 607 608 609 |
# File 'lib/puppet/util/settings.rb', line 604 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
650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 |
# File 'lib/puppet/util/settings.rb', line 650 def uninterpolated_value(param, environment = nil) param = param.to_sym environment &&= environment.to_sym # See if we can find it within our searchable list of values val = catch :foundval do each_source(environment) do |source| # Look for the value. We have to test the hash for whether # it exists, because the value might be false. @sync.synchronize do throw :foundval, @values[source][param] if @values[source].include?(param) end end throw :foundval, nil end # If we didn't get a value, use the default val = @config[param].default if val.nil? val end |
#unsafe_clear(exceptcli = false) ⇒ Object
Remove all set values, potentially skipping cli values.
68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/puppet/util/settings.rb', line 68 def unsafe_clear(exceptcli = false) @values.each do |name, values| @values.delete(name) unless exceptcli and name == :cli end # Don't clear the 'used' in this case, since it's a config file reparse, # and we want to retain this info. @used = [] unless exceptcli @cache.clear end |
#unsafe_parse(file) ⇒ Object
Unsafely parse the file – this isn’t thread-safe and causes plenty of problems if used directly.
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 |
# File 'lib/puppet/util/settings.rb', line 308 def unsafe_parse(file) return unless FileTest.exist?(file) begin data = parse_file(file) rescue => details puts details.backtrace if Puppet[:trace] Puppet.err "Could not parse #{file}: #{details}" return end unsafe_clear(true) = {} data.each do |area, values| [area] = values.delete(:_meta) values.each do |key,value| set_value(key, value, area, :dont_trigger_handles => true, :ignore_bad_settings => true ) end end # 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. settings_with_hooks.each do |setting| each_source(env) do |source| if value = @values[source][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. setting.handle(self.value(setting.name, env)) break end end end # We have to do it in the reverse of the search path, # because multiple sections could set the same value # and I'm too lazy to only set the metadata once. searchpath.reverse.each do |source| source = run_mode if source == :run_mode source = @name if (@name && source == :name) if = [source] () end end 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.
613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 |
# File 'lib/puppet/util/settings.rb', line 613 def use(*sections) sections = sections.collect { |s| s.to_sym } @sync.synchronize do # yay, thread-safe sections = sections.reject { |s| @used.include?(s) } return if sections.empty? begin catalog = to_catalog(*sections).to_ral rescue => detail puts detail.backtrace if Puppet[:trace] Puppet.err "Could not create resources for managing Puppet's files and directories in sections #{sections.inspect}: #{detail}" # We need some way to get rid of any resources created during the catalog creation # but not cleaned up. return end catalog.host_config = false catalog.apply do |transaction| if transaction.any_failed? report = transaction.report failures = report.logs.find_all { |log| log.level == :err } raise "Got #{failures.length} failure(s) while initializing: #{failures.collect { |l| l.to_s }.join("; ")}" end end sections.each { |s| @used << s } @used.uniq! end end |
#valid?(param) ⇒ Boolean
645 646 647 648 |
# File 'lib/puppet/util/settings.rb', line 645 def valid?(param) param = param.to_sym @config.has_key?(param) end |
#value(param, environment = nil) ⇒ Object
Find the correct value using our search path. Optionally accept an environment in which to search before the other configuration sections.
674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 |
# File 'lib/puppet/util/settings.rb', line 674 def value(param, environment = nil) param = param.to_sym environment &&= environment.to_sym # Short circuit to nil for undefined parameters. return nil unless @config.include?(param) # Yay, recursion. #self.reparse unless [:config, :filetimeout].include?(param) # 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 cached = @cache[environment||"none"][param] return cached end val = uninterpolated_value(param, environment) if param == :code # if we interpolate code, all hell breaks loose. return val end # Convert it if necessary val = convert(val, environment) # And cache it @cache[environment||"none"][param] = val val end |
#write(default, *args, &bloc) ⇒ Object
Open a file with the appropriate user, group, and mode
707 708 709 710 |
# File 'lib/puppet/util/settings.rb', line 707 def write(default, *args, &bloc) obj = get_config_file_default(default) writesub(default, value(obj.name), *args, &bloc) end |
#writesub(default, file, *args, &bloc) ⇒ Object
Open a non-default file under a default dir with the appropriate user, group, and mode
714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 |
# File 'lib/puppet/util/settings.rb', line 714 def writesub(default, file, *args, &bloc) obj = get_config_file_default(default) chown = nil if Puppet.features.root? chown = [obj.owner, obj.group] else chown = [nil, nil] end Puppet::Util::SUIDManager.asuser(*chown) do mode = obj.mode || 0640 args << "w" if args.empty? args << mode # Update the umask to make non-executable files Puppet::Util.withumask(File.umask ^ 0111) do File.open(file, *args) do |file| yield file end end end end |