From 0b9e33433d06288dadfa9409d48f7cc0723c6d3c Mon Sep 17 00:00:00 2001 From: Dan Bode Date: Thu, 11 Mar 2010 22:10:09 -0600 Subject: still developing --- lib/puppet/provider/sudoers/parsed.rb | 105 +++++++++++++++++++++++++++------- lib/puppet/type/sudoers.rb | 3 - 2 files changed, 84 insertions(+), 24 deletions(-) (limited to 'lib/puppet') diff --git a/lib/puppet/provider/sudoers/parsed.rb b/lib/puppet/provider/sudoers/parsed.rb index 520eef5..e7f6cbe 100644 --- a/lib/puppet/provider/sudoers/parsed.rb +++ b/lib/puppet/provider/sudoers/parsed.rb @@ -18,18 +18,18 @@ Puppet::Type.type(:sudoers).provide( commands :visudo => 'visudo' # this is just copied from hosts - text_line :comment, :match => %r{^#}, :post_parse => proc { |record| - # we determine the name from the comment above user spec lines - if record[:line] =~ /Puppet Name: (.+)\s*$/ -# ok, we set record name, but how is this applied to the next line? - record[:name] = $1 - end - } +# text_line :comment, :match => %r{^#}, :post_parse => proc { |record| +# # we determine the name from the comment above user spec lines +# if record[:line] =~ /Puppet Name: (.+)\s*$/ +## ok, we set record name, but how is this applied to the next line? +# record[:name] = $1 +# end +# } text_line :blank, :match => /^\s*$/; # ignore for now, I will support this line later - text_line :defaults, :match => /^Defaults/ +# text_line :defaults, :match => /^Defaults/ # I need to parse the , delim list into an array # I am pretty sure that I would do this with a block statement @@ -49,52 +49,115 @@ Puppet::Type.type(:sudoers).provide( :post_parse => proc { |hash| puts "\npost_parse" puts hash[:line] + # create records for aliases if (hash[:line] =~ /^\s*(User_Alias|Runas_Alias|Host_Alias|Cmnd_Alias)\s+(\S+)\s*=\s*(.+)$/) + hash[:type] = 'alias' hash[:sudo_alias] = $1 hash[:name] = $2 - hash[:items] = $3 - hash[:items]=hash[:items].gsub(/\s/, '').split(',') + hash[:items] = $3.gsub(/\s/, '').split(',') #puts hash.to_yaml + # create records for user specs elsif (hash[:line] =~ /^(.*)?=(.*)$/) +puts hash[:line] # should name already be set when get get here? - hash = parse_user_spec($1, $2) + #hash = Puppet::Provider::Sudoers::Parsed.parse_user_spec($1, $2) + lhs = $1 + rhs = $2 + hash[:type] = 'spec' + hash[:commands] = rhs.gsub(/\s/, '').split(',') + lhs_array = lhs.split(',') + # every element will be a user until the hit the delim + currentsymbol = :users + hash[:users] = Array.new + hash[:hosts] = Array.new + lhs_array.each do |element| +puts "!! #{element}" + # all elements will be a single string, except the one that splits users and hosts + if element =~ /^\s*(\S+)\s+(\S+)\s*$/ + user, host = $1, $2 + raise Exception, 'found more than one whitespace delin when parsing left hand side of user spec' if currentsymbol==:hosts + # sweet we found the delim between user and host + hash[currentsymbol] << user.gsub(/\s/, '') + # now everything else will be a host + currentsymbol=:hosts + hash[currentsymbol] << host.gsub(/\s/, '') + elsif element =~ /\s*\S+\s*/ + + hash[currentsymbol] << element.gsub(/\s/, '') + end + end +puts hash.to_yaml else raise ArgumentError, "unexpected line #{hash[:line]}" end } - def self.parse_user_spec(lhs, rhs) - lhs_array = lhs.split(',') - end +# def self.parse_user_spec(lhs, rhs) +# hash[:type] = 'spec' +# hash[:commands] = rhs.gsub(/\s/, '').split(',') +# lhs_array = lhs.split(',') +# # every element will be a user until the hit the delim +# currentsymbol = :users +# lhs_array do |element| +# # all elements will be a single string, except the one that splits users and hosts +# if element =~ /(\S+)\s*(\S+)/ +# # sweet we found the delim between user and host +# hash[currentsymbol]=element +# # now everything else will be a host +# currentsymbol=:hosts +# hash[currentsymbol]=element +# else +# hash[currentsymbol]=element +# end +# end +# end def self.to_line(hash) puts "\nEntering self.to_line for #{hash[:name]}" - puts hash.to_yaml + #puts hash.to_yaml # dynamically call a function based on the value of hash[:type] if(hash[:type] == 'alias') self.alias_to_line(hash) elsif(hash[:type] == 'spec') - spec_to_line(hash) + self.spec_to_line(hash) elsif(hash[:type] == 'default') default_to_line(hash) end end def self.spec_to_line(hash) - "spec" +puts hash.to_yaml + #required + users=self.array_convert(hash[:users]) + # required + hosts=self.array_convert(hash[:hosts]) + # required + commands=self.array_convert(hash[:commands]) + puts "adding line: #{users} #{hosts}=#{commands}" + "#{users} #{hosts}=#{commands}" end def self.alias_to_line(hash) # do I need to ensure that the required elements are here? # shouldnt the type do that? check file, its similar # since different attributes make sense based on ensure value (dir/file/symlink) - items=hash[:items] - items=items.join(',') if items.class == Array + items=self.array_convert(hash[:items]) + #puts "adding line: #{hash[:sudo_alias]} #{hash[:name]}=#{items}" "#{hash[:sudo_alias]} #{hash[:name]}=#{items}" end - def default_to_line(hash) - "default" + def self.default_to_line(hash) + users=hash[:users] + # optional? + hosts=hash[:hosts] + end + + def self.array_convert(list) + if list.class == Array + list.join(',') + else + list + end end def self.flush(record) diff --git a/lib/puppet/type/sudoers.rb b/lib/puppet/type/sudoers.rb index 6c94269..f55473d 100644 --- a/lib/puppet/type/sudoers.rb +++ b/lib/puppet/type/sudoers.rb @@ -83,9 +83,6 @@ order matters!! desc "list of hosts for user spec" end - newproperty(:runas, :array_matching => :all) do - desc "user to run commands as" - end newproperty(:commands, :array_matching => :all) do desc "commands to run" end -- cgit v1.2.3