ruby.rb 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. require File.expand_path('../../../util/ini_file', __FILE__)
  2. Puppet::Type.type(:ini_setting).provide(:ruby) do
  3. def self.instances
  4. # this code is here to support purging and the query-all functionality of the
  5. # 'puppet resource' command, on a per-file basis. Users
  6. # can create a type for a specific config file with a provider that uses
  7. # this as its parent and implements the method
  8. # 'self.file_path', and that will provide the value for the path to the
  9. # ini file (rather than needing to specify it on each ini setting
  10. # declaration). This allows 'purging' to be used to clear out
  11. # all settings from a particular ini file except those included in
  12. # the catalog.
  13. if self.respond_to?(:file_path)
  14. # figure out what to do about the seperator
  15. ini_file = Puppet::Util::IniFile.new(file_path, '=')
  16. resources = []
  17. ini_file.section_names.each do |section_name|
  18. ini_file.get_settings(section_name).each do |setting, value|
  19. resources.push(
  20. new(
  21. :name => namevar(section_name, setting),
  22. :value => value,
  23. :ensure => :present
  24. )
  25. )
  26. end
  27. end
  28. resources
  29. else
  30. raise(Puppet::Error, 'Ini_settings only support collecting instances when a file path is hard coded')
  31. end
  32. end
  33. def self.namevar(section_name, setting)
  34. "#{section_name}/#{setting}"
  35. end
  36. def exists?
  37. !ini_file.get_value(section, setting).nil?
  38. end
  39. def create
  40. ini_file.set_value(section, setting, resource[:value])
  41. ini_file.save
  42. @ini_file = nil
  43. end
  44. def destroy
  45. ini_file.remove_setting(section, setting)
  46. ini_file.save
  47. @ini_file = nil
  48. end
  49. def value
  50. ini_file.get_value(section, setting)
  51. end
  52. def value=(value)
  53. ini_file.set_value(section, setting, resource[:value])
  54. ini_file.save
  55. end
  56. def section
  57. # this method is here so that it can be overridden by a child provider
  58. resource[:section]
  59. end
  60. def setting
  61. # this method is here so that it can be overridden by a child provider
  62. resource[:setting]
  63. end
  64. def file_path
  65. # this method is here to support purging and sub-classing.
  66. # if a user creates a type and subclasses our provider and provides a
  67. # 'file_path' method, then they don't have to specify the
  68. # path as a parameter for every ini_setting declaration.
  69. # This implementation allows us to support that while still
  70. # falling back to the parameter value when necessary.
  71. if self.class.respond_to?(:file_path)
  72. self.class.file_path
  73. else
  74. resource[:path]
  75. end
  76. end
  77. def separator
  78. if resource.class.validattr?(:key_val_separator)
  79. resource[:key_val_separator] || '='
  80. else
  81. '='
  82. end
  83. end
  84. def section_prefix
  85. if resource.class.validattr?(:section_prefix)
  86. resource[:section_prefix] || '['
  87. else
  88. '['
  89. end
  90. end
  91. def section_suffix
  92. if resource.class.validattr?(:section_suffix)
  93. resource[:section_suffix] || ']'
  94. else
  95. ']'
  96. end
  97. end
  98. private
  99. def ini_file
  100. @ini_file ||= Puppet::Util::IniFile.new(file_path, separator, section_prefix, section_suffix)
  101. end
  102. end