/usr/share/puppet/modules.available/openstacklib/lib/puppet/util/openstackconfig/section.rb is in puppet-module-openstacklib 9.4.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 | #
# Author: Martin Magr <mmagr@redhat.com>
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#
# Forked from https://github.com/puppetlabs/puppetlabs-inifile .
module Puppet
module Util
class OpenStackConfig
class Section
@@SETTING_REGEX = /^(\s*)([^#;\s]|[^#;\s].*?[^\s=])(\s*=[ \t]*)(.*)\s*$/
@@COMMENTED_SETTING_REGEX = /^(\s*)[#;]+(\s*)(.*?[^\s=])(\s*=[ \t]*)(.*)\s*$/
def initialize(name, lines=nil)
@name = name
@lines = lines.nil? ? [] : lines
# parse lines
@indentation = nil
@settings = {}
@lines.each do |line|
if match = @@SETTING_REGEX.match(line)
indent = match[1].length
@indentation = [indent, @indentation || indent].min
if @settings.include?(match[2])
if not @settings[match[2]].kind_of?(Array)
@settings[match[2]] = [@settings[match[2]]]
end
@settings[match[2]].push(match[4])
else
@settings[match[2]] = match[4]
end
end
end
end
attr_reader :name, :indentation
def settings
Marshal.load(Marshal.dump(@settings))
end
def lines
@lines.clone
end
def is_global?
@name == ''
end
def is_new_section?
@lines.empty?
end
def setting_names
@settings.keys
end
def add_setting(setting_name, value)
@settings[setting_name] = value
add_lines(setting_name, value)
end
def update_setting(setting_name, value)
old_value = @settings[setting_name]
@settings[setting_name] = value
if value.kind_of?(Array) or old_value.kind_of?(Array)
# ---- update lines for multi setting ----
old_value = old_value.kind_of?(Array) ? old_value : [old_value]
new_value = value.kind_of?(Array) ? value : [value]
if useless = old_value - new_value
remove_lines(setting_name, useless)
end
if missing = new_value - old_value
add_lines(setting_name, missing)
end
else
# ---- update lines for single setting ----
@lines.each_with_index do |line, index|
if match = @@SETTING_REGEX.match(line)
if (match[2] == setting_name)
@lines[index] = "#{match[1]}#{match[2]}#{match[3]}#{value}\n"
end
end
end
end
end
def remove_setting(setting_name, value=nil)
if value.nil? or @settings[setting_name] == value
@settings.delete(setting_name)
else
value.each do |val|
@settings[setting_name].delete(val)
end
end
remove_lines(setting_name, value)
end
private
def find_commented_setting(setting_name)
@lines.each_with_index do |line, index|
if match = @@COMMENTED_SETTING_REGEX.match(line)
if match[3] == setting_name
return index
end
end
end
nil
end
def find_last_setting(setting_name)
result = nil
@lines.each_with_index do |line, index|
if match = @@SETTING_REGEX.match(line)
if match[2] == setting_name
result = index
end
end
end
result
end
def remove_lines(setting_name, value=nil)
if value.kind_of?(Array)
val_arr = value
else
val_arr = [value]
end
val_arr.each do |val|
@lines.each_with_index do |line, index|
if (match = @@SETTING_REGEX.match(line))
if match[2] == setting_name
if val.nil? or val_arr.include?(match[4])
@lines.delete_at(index)
break
end
end
end
end
end
end
def add_lines(setting_name, value)
indent_str = ' ' * (indentation || 0)
if current = find_last_setting(setting_name)
offset = current
elsif comment = find_commented_setting(setting_name)
offset = comment + 1
else
offset = @lines.length
end
if value.kind_of?(Array)
value.each do |val|
@lines.insert(offset, "#{indent_str}#{setting_name}=#{val}\n")
offset += 1
end
else
@lines.insert(offset, "#{indent_str}#{setting_name}=#{value}\n")
end
end
end
end
end
end
|