2013-02-15 10:56:44 +01:00
|
|
|
module Puppet
|
2015-07-14 01:03:29 +02:00
|
|
|
module Util
|
2013-02-15 10:56:44 +01:00
|
|
|
|
2015-07-14 01:03:29 +02:00
|
|
|
class SettingValue
|
Add quote_char parameter to the ini_subsetting resource type
The quote_char is used to quote the entire setting when it is modified
as a result of changes to some subsettings.
For an example let's assume we have a setting of this form:
JAVA_ARGS=-Xmx256m
and we want to add the '-Xms' parameter to the setting, for that purpose
we define a resource like this:
ini_subsetting { '-Xms':
ensure => present,
path => '/some/config/file',
section => '',
setting => 'JAVA_ARGS',
subsetting => '-Xms'
value => '256m',
}
which results into the following setting:
JAVA_ARGS=-Xmx256m -Xms256m
But this is not what we intended - if this setting is read by the bash
shell the '-Xms256m' parameter is interpreted as a command to be
executed rather than a value to be assigned to the JAVA_ARGS variable.
To fix this problem the quote_char parameter was added to the
ini_subsetting resource type, and we'll take advantage of it to fix the
problem in the above example like so:
ini_subsetting { '-Xms':
ensure => present,
path => '/some/config/file',
section => '',
setting => 'JAVA_ARGS',
quote_char => '"',
subsetting => '-Xms'
value => '256m',
}
which will result into:
JAVA_ARGS="-Xmx256m -Xms256m"
which is what we intended.
2014-05-07 17:04:43 +02:00
|
|
|
|
2015-07-14 01:03:29 +02:00
|
|
|
def initialize(setting_value, subsetting_separator = ' ', default_quote_char = nil)
|
|
|
|
@setting_value = setting_value
|
|
|
|
@subsetting_separator = subsetting_separator
|
2013-05-28 23:56:57 +02:00
|
|
|
|
2015-07-14 01:03:29 +02:00
|
|
|
default_quote_char ||= ''
|
2013-05-28 23:56:57 +02:00
|
|
|
|
2015-07-14 01:03:29 +02:00
|
|
|
if @setting_value
|
|
|
|
unquoted, @quote_char = unquote_setting_value(setting_value)
|
|
|
|
@subsetting_items = unquoted.scan(Regexp.new("(?:(?:[^\\#{@subsetting_separator}]|\\.)+)")) # an item can contain escaped separator
|
|
|
|
@subsetting_items.map! { |item| item.strip }
|
|
|
|
@quote_char = default_quote_char if @quote_char.empty?
|
|
|
|
else
|
|
|
|
@subsetting_items = []
|
|
|
|
@quote_char = default_quote_char
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def unquote_setting_value(setting_value)
|
|
|
|
quote_char = ""
|
|
|
|
if (setting_value.start_with?('"') and setting_value.end_with?('"'))
|
|
|
|
quote_char = '"'
|
|
|
|
elsif (setting_value.start_with?("'") and setting_value.end_with?("'"))
|
|
|
|
quote_char = "'"
|
|
|
|
end
|
|
|
|
|
|
|
|
unquoted = setting_value
|
2013-02-15 10:56:44 +01:00
|
|
|
|
2015-07-14 01:03:29 +02:00
|
|
|
if (quote_char != "")
|
|
|
|
unquoted = setting_value[1, setting_value.length - 2]
|
|
|
|
end
|
|
|
|
|
|
|
|
[unquoted, quote_char]
|
2013-05-22 20:50:12 +02:00
|
|
|
end
|
|
|
|
|
2015-07-14 01:03:29 +02:00
|
|
|
def get_value
|
|
|
|
|
|
|
|
result = ""
|
|
|
|
first = true
|
|
|
|
|
|
|
|
@subsetting_items.each { |item|
|
|
|
|
result << @subsetting_separator unless first
|
|
|
|
result << item
|
|
|
|
first = false
|
|
|
|
}
|
2013-05-22 20:50:12 +02:00
|
|
|
|
2015-07-14 01:03:29 +02:00
|
|
|
@quote_char + result + @quote_char
|
2013-05-22 20:50:12 +02:00
|
|
|
end
|
|
|
|
|
2015-08-01 01:11:27 +02:00
|
|
|
def get_subsetting_value(subsetting, use_exact_match=:false)
|
2013-05-22 20:50:12 +02:00
|
|
|
|
2015-07-14 01:03:29 +02:00
|
|
|
value = nil
|
|
|
|
|
|
|
|
@subsetting_items.each { |item|
|
2015-08-01 01:11:27 +02:00
|
|
|
if(use_exact_match == :false and item.start_with?(subsetting))
|
2015-07-14 01:03:29 +02:00
|
|
|
value = item[subsetting.length, item.length - subsetting.length]
|
|
|
|
break
|
2015-08-01 01:11:27 +02:00
|
|
|
elsif(use_exact_match == :true and item.eql?(subsetting))
|
|
|
|
return true
|
2015-07-14 01:03:29 +02:00
|
|
|
end
|
|
|
|
}
|
2013-02-15 10:56:44 +01:00
|
|
|
|
|
|
|
value
|
|
|
|
end
|
|
|
|
|
2015-08-01 01:11:27 +02:00
|
|
|
def add_subsetting(subsetting, subsetting_value, use_exact_match=:false)
|
2015-07-14 01:03:29 +02:00
|
|
|
|
|
|
|
new_item = subsetting + (subsetting_value || '')
|
|
|
|
found = false
|
|
|
|
|
|
|
|
@subsetting_items.map! { |item|
|
2015-08-01 01:11:27 +02:00
|
|
|
if use_exact_match == :false and item.start_with?(subsetting)
|
|
|
|
value = new_item
|
|
|
|
found = true
|
|
|
|
elsif use_exact_match == :true and item.eql?(subsetting)
|
2015-07-14 01:03:29 +02:00
|
|
|
value = new_item
|
|
|
|
found = true
|
|
|
|
else
|
|
|
|
value = item
|
|
|
|
end
|
|
|
|
|
|
|
|
value
|
|
|
|
}
|
|
|
|
|
|
|
|
unless found
|
|
|
|
@subsetting_items.push(new_item)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-08-01 01:11:27 +02:00
|
|
|
def remove_subsetting(subsetting, use_exact_match=:false)
|
|
|
|
if use_exact_match == :false
|
|
|
|
@subsetting_items = @subsetting_items.map { |item| item.start_with?(subsetting) ? nil : item }.compact
|
|
|
|
else
|
|
|
|
@subsetting_items = @subsetting_items.map { |item| item.eql?(subsetting) ? nil : item }.compact
|
|
|
|
end
|
2015-07-14 01:03:29 +02:00
|
|
|
end
|
2013-02-15 10:56:44 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|