ini_setting.rb 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. require 'digest/md5'
  2. Puppet::Type.newtype(:ini_setting) do
  3. ensurable do
  4. defaultvalues
  5. defaultto :present
  6. end
  7. def munge_boolean_md5(value)
  8. case value
  9. when true, :true, 'true', :yes, 'yes'
  10. :true
  11. when false, :false, 'false', :no, 'no'
  12. :false
  13. when :md5, 'md5'
  14. :md5
  15. else
  16. fail('expected a boolean value or :md5')
  17. end
  18. end
  19. newparam(:name, :namevar => true) do
  20. desc 'An arbitrary name used as the identity of the resource.'
  21. end
  22. newparam(:section) do
  23. desc 'The name of the section in the ini file in which the setting should be defined.' +
  24. 'If not provided, defaults to global, top of file, sections.'
  25. defaultto("")
  26. end
  27. newparam(:setting) do
  28. desc 'The name of the setting to be defined.'
  29. munge do |value|
  30. if value =~ /(^\s|\s$)/
  31. Puppet.warn("Settings should not have spaces in the value, we are going to strip the whitespace")
  32. end
  33. value.lstrip.rstrip
  34. end
  35. end
  36. newparam(:path) do
  37. desc 'The ini file Puppet will ensure contains the specified setting.'
  38. validate do |value|
  39. unless (Puppet.features.posix? and value =~ /^\//) or (Puppet.features.microsoft_windows? and (value =~ /^.:\// or value =~ /^\/\/[^\/]+\/[^\/]+/))
  40. raise(Puppet::Error, "File paths must be fully qualified, not '#{value}'")
  41. end
  42. end
  43. end
  44. newparam(:show_diff) do
  45. desc 'Whether to display differences when the setting changes.'
  46. defaultto :true
  47. newvalues(:true, :md5, :false)
  48. munge do |value|
  49. @resource.munge_boolean_md5(value)
  50. end
  51. end
  52. newparam(:key_val_separator) do
  53. desc 'The separator string to use between each setting name and value. ' +
  54. 'Defaults to " = ", but you could use this to override e.g. ": ", or' +
  55. 'whether or not the separator should include whitespace.'
  56. defaultto(" = ")
  57. end
  58. newproperty(:value) do
  59. desc 'The value of the setting to be defined.'
  60. munge do |value|
  61. value.to_s
  62. end
  63. def should_to_s(newvalue)
  64. if (@resource[:show_diff] == :true && Puppet[:show_diff]) then
  65. return newvalue
  66. elsif (@resource[:show_diff] == :md5 && Puppet[:show_diff]) then
  67. return '{md5}' + Digest::MD5.hexdigest(newvalue.to_s)
  68. else
  69. return '[redacted sensitive information]'
  70. end
  71. end
  72. def is_to_s(value)
  73. should_to_s(value)
  74. end
  75. def insync?(current)
  76. if (@resource[:refreshonly]) then
  77. true
  78. else
  79. current == should
  80. end
  81. end
  82. end
  83. newparam(:section_prefix) do
  84. desc 'The prefix to the section name\'s header.' +
  85. 'Defaults to \'[\'.'
  86. defaultto('[')
  87. end
  88. newparam(:section_suffix) do
  89. desc 'The suffix to the section name\'s header.' +
  90. 'Defaults to \']\'.'
  91. defaultto(']')
  92. end
  93. newparam(:refreshonly) do
  94. desc 'A flag indicating whether or not the ini_setting should be updated '+
  95. 'only when called as part of a refresh event'
  96. defaultto false
  97. newvalues(true,false)
  98. end
  99. def refresh
  100. if self[:refreshonly] then
  101. # update the value in the provider, which will save the value to the ini file
  102. provider.value = self[:value]
  103. end
  104. end
  105. end