ini_subsetting.rb 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. require 'digest/md5'
  2. Puppet::Type.newtype(:ini_subsetting) 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. end
  30. newparam(:subsetting) do
  31. desc 'The name of the subsetting to be defined.'
  32. end
  33. newparam(:subsetting_separator) do
  34. desc 'The separator string between subsettings. Defaults to " "'
  35. defaultto(" ")
  36. end
  37. newparam(:subsetting_key_val_separator) do
  38. desc 'The separator string between the subsetting name and its value. Defaults to the empty string.'
  39. defaultto('')
  40. end
  41. newparam(:path) do
  42. desc 'The ini file Puppet will ensure contains the specified setting.'
  43. validate do |value|
  44. unless (Puppet.features.posix? and value =~ /^\//) or (Puppet.features.microsoft_windows? and (value =~ /^.:\// or value =~ /^\/\/[^\/]+\/[^\/]+/))
  45. raise(Puppet::Error, "File paths must be fully qualified, not '#{value}'")
  46. end
  47. end
  48. end
  49. newparam(:show_diff) do
  50. desc 'Whether to display differences when the setting changes.'
  51. defaultto :true
  52. newvalues(:true, :md5, :false)
  53. munge do |value|
  54. @resource.munge_boolean_md5(value)
  55. end
  56. end
  57. newparam(:key_val_separator) do
  58. desc 'The separator string to use between each setting name and value. ' +
  59. 'Defaults to " = ", but you could use this to override e.g. ": ", or' +
  60. 'whether or not the separator should include whitespace.'
  61. defaultto(" = ")
  62. end
  63. newparam(:quote_char) do
  64. desc 'The character used to quote the entire value of the setting. ' +
  65. %q{Valid values are '', '"' and "'". Defaults to ''.}
  66. defaultto('')
  67. validate do |value|
  68. unless value =~ /^["']?$/
  69. raise Puppet::Error, %q{:quote_char valid values are '', '"' and "'"}
  70. end
  71. end
  72. end
  73. newparam(:use_exact_match) do
  74. desc 'Set to true if your subsettings don\'t have values and you want to use exact matches to determine if the subsetting exists. See MODULES-2212'
  75. newvalues(:true, :false)
  76. defaultto(:false)
  77. end
  78. newproperty(:value) do
  79. desc 'The value of the subsetting to be defined.'
  80. def should_to_s(newvalue)
  81. if (@resource[:show_diff] == :true && Puppet[:show_diff]) then
  82. return newvalue
  83. elsif (@resource[:show_diff] == :md5 && Puppet[:show_diff]) then
  84. return '{md5}' + Digest::MD5.hexdigest(newvalue.to_s)
  85. else
  86. return '[redacted sensitive information]'
  87. end
  88. end
  89. def is_to_s(value)
  90. should_to_s(value)
  91. end
  92. def is_to_s(value)
  93. should_to_s(value)
  94. end
  95. end
  96. newparam(:insert_type) do
  97. desc <<-eof
  98. Where the new subsetting item should be inserted?
  99. * :start - insert at the beginning of the line.
  100. * :end - insert at the end of the line (default).
  101. * :before - insert before the specified element if possible.
  102. * :after - insert after the specified element if possible.
  103. * :index - insert at the specified index number.
  104. eof
  105. newvalues(:start, :end, :before, :after, :index)
  106. defaultto(:end)
  107. end
  108. newparam(:insert_value) do
  109. desc 'The value for the insert types which require one.'
  110. end
  111. end