OpenStackConfig provider

This patch add support for parsing configuration files of projects
which use oslo.config.cfg.MultiStrOpt (currently Neutron LBaaSv2).

For details see:
http://docs.openstack.org/developer/oslo.config/api/oslo.config.cfg.html#oslo.config.cfg.MultiStrOpt

Change-Id: If782aa4bf83712ac2a5f4d2f4e8ace782409182d
This commit is contained in:
Martin Mágr 2015-10-07 15:11:41 +02:00
parent 57475d93af
commit 029c6a74cc
4 changed files with 690 additions and 0 deletions

View File

@ -0,0 +1,79 @@
require File.expand_path('../../../util/openstackconfig', __FILE__)
Puppet::Type.type(:openstack_config).provide(:ruby) do
def self.instances
if self.respond_to?(:file_path)
config = Puppet::Util::OpenStackConfig.new(file_path)
resources = []
config.section_names.each do |section_name|
config.get_settings(section_name).each do |setting, value|
resources.push(
new(
:name => namevar(section_name, setting),
:value => value,
:ensure => :present
)
)
end
end
resources
else
raise(Puppet::Error,
'OpenStackConfig only support collecting instances when a file path ' +
'is hard coded'
)
end
end
def exists?
if resource[:value] == ensure_absent_val
resource[:ensure] = :absent
end
!config.get_value(section, setting).nil?
end
def create
config.set_value(section, setting, resource[:value])
config.save
@config = nil
end
def destroy
config.remove_setting(section, setting)
config.save
@config = nil
end
def value=(value)
config.set_value(section, setting, resource[:value])
config.save
end
def value
config.get_value(section, setting)
end
def section
resource[:name].split('/', 2).first
end
def setting
resource[:name].split('/', 2).last
end
def ensure_absent_val
resource[:ensure_absent_val]
end
def file_path
self.class.file_path
end
private
def config
@config ||= Puppet::Util::OpenStackConfig.new(file_path)
end
end

View File

@ -0,0 +1,117 @@
#
# 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 .
require File.expand_path('../openstackconfig/section', __FILE__)
module Puppet
module Util
class OpenStackConfig
@@SECTION_REGEX = /^\s*\[(.*)\]\s*$/
def initialize(path)
@path = path
@order = []
@sections = {}
parse_file
end
attr_reader :path
def section_names
@sections.keys
end
def get_settings(section_name)
@sections[section_name].settings
end
def get_value(section_name, setting_name)
if @sections.has_key?(section_name)
@sections[section_name].settings[setting_name]
end
end
def set_value(section_name, setting_name, value)
unless @sections.has_key?(section_name)
add_section(section_name)
end
if @sections[section_name].settings.has_key?(setting_name)
@sections[section_name].update_setting(setting_name, value)
else
@sections[section_name].add_setting(setting_name, value)
end
end
def remove_setting(section_name, setting_name, value=nil)
@sections[section_name].remove_setting(setting_name, value)
end
def save
File.open(@path, 'w') do |fh|
@order.each do |section_name|
if section_name.length > 0
fh.puts("[#{section_name}]")
end
unless @sections[section_name].lines.empty?
@sections[section_name].lines.each do |line|
fh.puts(line)
end
end
end
end
end
private
# This is mostly here because it makes testing easier
# --we don't have to try to stub any methods on File.
def self.readlines(path)
# If this type is ever used with very large files, we should
# write this in a different way, using a temp
# file; for now assuming that this type is only used on
# small-ish config files that can fit into memory without
# too much trouble.
File.file?(path) ? File.readlines(path) : []
end
def parse_file
# We always create a "global" section at the beginning of the file,
# for anything that appears before the first named section.
lines = []
current_section = ''
OpenStackConfig.readlines(@path).each do |line|
if match = @@SECTION_REGEX.match(line)
add_section(current_section, lines)
# start new section parsing
lines = []
current_section = match[1]
else
lines.push(line)
end
end
add_section(current_section, lines)
end
def add_section(section_name, lines=nil)
@order.push(section_name)
@sections[section_name] = Section.new(section_name, lines)
end
end
end
end

View File

@ -0,0 +1,172 @@
#
# 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.eafach 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)
@lines.each_with_index do |line, index|
if (match = @@SETTING_REGEX.match(line))
if match[2] == setting_name
if value.nil? or (
value.kind_of?(Array) and value.include?(match[4])
)
lines.delete_at(index)
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

View File

@ -0,0 +1,322 @@
#
# Forked from https://github.com/puppetlabs/puppetlabs-inifile .
#
require 'spec_helper'
require 'puppet/util/openstackconfig'
describe Puppet::Util::OpenStackConfig do
include PuppetlabsSpec::Files
let(:subject) do
Puppet::Util::OpenStackConfig.new("/my/config/path")
end
before :each do
Puppet::Util::OpenStackConfig.stubs(:readlines).returns(sample_content)
end
context "when parsing a file" do
let(:sample_content) {
template = <<-EOS
# This is a comment
[section1]
; This is also a comment
foo=foovalue
bar = barvalue
baz =
[section2]
foo= foovalue2
baz=bazvalue
; commented = out setting
#another comment
; yet another comment
zot = multi word value
xyzzy['thing1']['thing2']=xyzzyvalue
l=git log
[section3]
multi_setting = value1
multi_setting = value2
EOS
template.split("\n")
}
it "should parse the correct number of sections" do
# there is always a "global" section, so our count should be 3.
subject.section_names.length.should == 4
end
it "should parse the correct section_names" do
# there should always be a "global" section named "" at the beginning of the list
subject.section_names.should == ["", "section1", "section2", "section3"]
end
it "should expose settings for sections" do
subject.get_settings("section1").should == {
"bar" => "barvalue",
"baz" => "",
"foo" => "foovalue"
}
subject.get_settings("section2").should == {
"baz" => "bazvalue",
"foo" => "foovalue2",
"l" => "git log",
"xyzzy['thing1']['thing2']" => "xyzzyvalue",
"zot" => "multi word value"
}
subject.get_settings("section3").should == {
"multi_setting" => ["value1", "value2"]
}
end
end
context "when parsing a file whose first line is a section" do
let(:sample_content) {
template = <<-EOS
[section1]
; This is a comment
foo=foovalue
EOS
template.split("\n")
}
it "should parse the correct number of sections" do
# there is always a "global" section, so our count should be 2.
subject.section_names.length.should == 2
end
it "should parse the correct section_names" do
# there should always be a "global" section named "" at the beginning of the list
subject.section_names.should == ["", "section1"]
end
it "should expose settings for sections" do
subject.get_value("section1", "foo").should == "foovalue"
end
end
context "when parsing a file with a 'global' section" do
let(:sample_content) {
template = <<-EOS
foo = bar
[section1]
; This is a comment
foo=foovalue
EOS
template.split("\n")
}
it "should parse the correct number of sections" do
# there is always a "global" section, so our count should be 2.
subject.section_names.length.should == 2
end
it "should parse the correct section_names" do
# there should always be a "global" section named "" at the beginning of the list
subject.section_names.should == ["", "section1"]
end
it "should expose settings for sections" do
subject.get_value("", "foo").should == "bar"
subject.get_value("section1", "foo").should == "foovalue"
end
end
context "when updating a file with existing empty values" do
let(:sample_content) {
template = <<-EOS
[section1]
foo=
#bar=
#xyzzy['thing1']['thing2']='xyzzyvalue'
EOS
template.split("\n")
}
it "should properly update uncommented values" do
subject.get_value("section1", "far").should == nil
subject.set_value("section1", "foo", "foovalue")
subject.get_value("section1", "foo").should == "foovalue"
end
it "should properly update commented values" do
subject.get_value("section1", "bar").should == nil
subject.set_value("section1", "bar", "barvalue")
subject.get_value("section1", "bar").should == "barvalue"
subject.get_value("section1", "xyzzy['thing1']['thing2']").should == nil
subject.set_value("section1", "xyzzy['thing1']['thing2']", "xyzzyvalue")
subject.get_value("section1", "xyzzy['thing1']['thing2']").should == "xyzzyvalue"
end
it "should properly add new empty values" do
subject.get_value("section1", "baz").should == nil
end
end
context 'the file has quotation marks in its section names' do
let(:sample_content) do
template = <<-EOS
[branch "master"]
remote = origin
merge = refs/heads/master
[alias]
to-deploy = log --merges --grep='pull request' --format='%s (%cN)' origin/production..origin/master
[branch "production"]
remote = origin
merge = refs/heads/production
EOS
template.split("\n")
end
it 'should parse the sections' do
subject.section_names.should match_array ['',
'branch "master"',
'alias',
'branch "production"'
]
end
end
context 'Samba INI file with dollars in section names' do
let(:sample_content) do
template = <<-EOS
[global]
workgroup = FELLOWSHIP
; ...
idmap config * : backend = tdb
[printers]
comment = All Printers
; ...
browseable = No
[print$]
comment = Printer Drivers
path = /var/lib/samba/printers
[Shares]
path = /home/shares
read only = No
guest ok = Yes
EOS
template.split("\n")
end
it "should parse the correct section_names" do
subject.section_names.should match_array [
'',
'global',
'printers',
'print$',
'Shares'
]
end
end
context 'section names with forward slashes in them' do
let(:sample_content) do
template = <<-EOS
[monitor:///var/log/*.log]
disabled = test_value
EOS
template.split("\n")
end
it "should parse the correct section_names" do
subject.section_names.should match_array [
'',
'monitor:///var/log/*.log'
]
end
end
context 'KDE Configuration with braces in setting names' do
let(:sample_content) do
template = <<-EOS
[khotkeys]
_k_friendly_name=khotkeys
{5465e8c7-d608-4493-a48f-b99d99fdb508}=Print,none,PrintScreen
{d03619b6-9b3c-48cc-9d9c-a2aadb485550}=Search,none,Search
EOS
template.split("\n")
end
it "should expose settings for sections" do
subject.get_value("khotkeys", "{5465e8c7-d608-4493-a48f-b99d99fdb508}").should == "Print,none,PrintScreen"
subject.get_value("khotkeys", "{d03619b6-9b3c-48cc-9d9c-a2aadb485550}").should == "Search,none,Search"
end
end
context 'Configuration with colons in setting names' do
let(:sample_content) do
template = <<-EOS
[Drive names]
A:=5.25" Floppy
B:=3.5" Floppy
C:=Winchester
EOS
template.split("\n")
end
it "should expose settings for sections" do
subject.get_value("Drive names", "A:").should eq '5.25" Floppy'
subject.get_value("Drive names", "B:").should eq '3.5" Floppy'
subject.get_value("Drive names", "C:").should eq 'Winchester'
end
end
context 'Configuration with spaces in setting names' do
let(:sample_content) do
template = <<-EOS
[global]
# log files split per-machine:
log file = /var/log/samba/log.%m
kerberos method = system keytab
passdb backend = tdbsam
security = ads
EOS
template.split("\n")
end
it "should expose settings for sections" do
subject.get_value("global", "log file").should eq '/var/log/samba/log.%m'
subject.get_value("global", "kerberos method").should eq 'system keytab'
subject.get_value("global", "passdb backend").should eq 'tdbsam'
subject.get_value("global", "security").should eq 'ads'
end
end
context 'Multi settings' do
let(:sample_content) do
template = <<-EOS
[test]
# multi values
test = value1
test = value2
test = value3
EOS
template.split("\n")
end
it "should expose setting with array value" do
subject.get_value("test", "test").should eq ['value1', 'value2', 'value3']
end
it "should create setting with array value" do
subject.set_value("test", "test2", ['valueA', 'valueB', 'valueC'])
subject.get_value("test", "test2").should eq ['valueA', 'valueB', 'valueC']
end
end
end