From ac2d7693e02a11d80879ccd80dc33a4f213295e6 Mon Sep 17 00:00:00 2001 From: Feng Pan Date: Sun, 19 Feb 2017 15:14:40 -0500 Subject: Add support for inline config changes This patch changes VPP configuration from file template to inline change directly. This prevents issue with overwriting existing VPP config. Change-Id: I4133be8ebe689d9288e3a8e64cca43afd7e42e64 Signed-off-by: Feng Pan --- lib/puppet/provider/vpp_config/vpp.rb | 118 ++++++++++++++++++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 lib/puppet/provider/vpp_config/vpp.rb (limited to 'lib/puppet/provider/vpp_config/vpp.rb') diff --git a/lib/puppet/provider/vpp_config/vpp.rb b/lib/puppet/provider/vpp_config/vpp.rb new file mode 100644 index 0000000..2574d4c --- /dev/null +++ b/lib/puppet/provider/vpp_config/vpp.rb @@ -0,0 +1,118 @@ +Puppet::Type.type(:vpp_config).provide(:vpp) do + + def file_path + '/etc/vpp/startup.conf' + end + + def initialize(value={}) + super(value) + settings_arr = @resource[:setting].split('/') + @section = settings_arr[0] + @real_setting = settings_arr[1] + @dev = settings_arr[2] + + if @section.nil? || @real_setting.nil? + fail("#{@resource[:setting]} is not a valid setting string") + end + + if @dev + @search_regex = /^\s*dev\s+#{@dev}\s*({[^}]*})?/ + else + @search_regex = /^\s*#{@real_setting}\s+(\S+)?/ + end + + end + + def write_config(config) + if File.read(file_path) != config + File.open(file_path, 'w') do |fh| + fh.puts(config) + end + end + end + + def get_sections + vpp_config = File.read(file_path) + scanner = StringScanner.new vpp_config + + #first skip to section beginning + string = scanner.scan_until(/#{@section}\s*{\s*/) + + #if we can't find the section, add it to the end + return vpp_config+"\n#{@section} {", "", "}\n" unless string + + level = 1 + before = string + after = '' + section_config = '' + + while current_char = scanner.getch + case current_char + when '{' + level += 1 + section_config << current_char + when '}' + level -= 1 + if level == 0 + after = current_char + scanner.post_match + break + else + section_config << current_char + end + else + section_config << current_char + end + end + + fail("Failed to parse VPP config: #{vpp_config}") unless level == 0 + return before, section_config, after + end + + def add_setting(value) + before, section_config, after = get_sections + + if @dev + if value.to_s.empty? + setting_string = "#{@real_setting} #{@dev}" + else + setting_string = "#{@real_setting} #{@dev} {#{value}}" + end + else + setting_string = "#{@real_setting} #{value}" + end + + if section_config =~ @search_regex + section_config.sub!(@search_regex, " #{setting_string}") + else + section_config.rstrip! + section_config << "\n #{setting_string}\n" + end + + write_config(before+section_config+after) + end + + def create + add_setting(@resource[:value]) + end + + def destroy + before, section_config, after = get_sections + section_config.sub!(@search_regex, "") + write_config(before+section_config+after) + end + + def exists? + before, section_config, after = get_sections + @search_regex.match(section_config) + end + + def value + before, section_config, after = get_sections + @search_regex.match(section_config) { |m| m[1] } + end + + def value=(value) + add_setting(value) + end + +end -- cgit 1.2.3-korg