From 2718b638d1df7fe37941952e396d84d1eff1efc9 Mon Sep 17 00:00:00 2001 From: Luke Kanies Date: Thu, 18 Oct 2007 11:56:45 -0500 Subject: This is the first mostly functional commit of the new file serving structure. The next step is to hook it up to the indirection so we can start writing integration tests to see if we can actually serve up files. --- lib/puppet/file_serving/configuration/parser.rb | 123 ++++++++++++++++++++++++ 1 file changed, 123 insertions(+) create mode 100644 lib/puppet/file_serving/configuration/parser.rb (limited to 'lib/puppet/file_serving/configuration/parser.rb') diff --git a/lib/puppet/file_serving/configuration/parser.rb b/lib/puppet/file_serving/configuration/parser.rb new file mode 100644 index 000000000..5b17d9801 --- /dev/null +++ b/lib/puppet/file_serving/configuration/parser.rb @@ -0,0 +1,123 @@ +require 'puppet/file_serving/configuration' +require 'puppet/util/loadedfile' + +class Puppet::FileServing::Configuration::Parser < Puppet::Util::LoadedFile + Mount = Puppet::FileServing::Mount + + # Parse our configuration file. + def parse + raise("File server configuration %s does not exist" % self.file) unless FileTest.exists?(self.file) + raise("Cannot read file server configuration %s" % self.file) unless FileTest.readable?(self.file) + + @mounts = {} + @count = 0 + + File.open(self.file) { |f| + mount = nil + f.each { |line| + # Have the count increment at the top, in case we throw exceptions. + @count += 1 + + case line + when /^\s*#/: next # skip comments + when /^\s*$/: next # skip blank lines + when /\[([-\w]+)\]/: + mount = newmount($1) + when /^\s*(\w+)\s+(.+)$/: + var = $1 + value = $2 + raise(ArgumentError, "Fileserver configuration file does not use '=' as a separator") if value =~ /^=/ + case var + when "path": + path(mount, value) + when "allow": + allow(mount, value) + when "deny": + deny(mount, value) + else + raise ArgumentError.new("Invalid argument '%s'" % var, + @count, file) + end + else + raise ArgumentError.new("Invalid line '%s'" % line.chomp, + @count, file) + end + } + } + + return @mounts + end + + private + + # Add the mount for getting files from modules. + def add_module_mount + unless @mounts[Mount::MODULES] + mount = Mount.new(Mount::MODULES) + mount.allow("*") + @mounts[Mount::MODULES] = mount + end + end + + # Allow a given pattern access to a mount. + def allow(mount, value) + value.split(/\s*,\s*/).each { |val| + begin + mount.info "allowing %s access" % val + mount.allow(val) + rescue AuthStoreError => detail + raise ArgumentError.new(detail.to_s, + @count, file) + end + } + end + + # Deny a given pattern access to a mount. + def deny(mount, value) + value.split(/\s*,\s*/).each { |val| + begin + mount.info "denying %s access" % val + mount.deny(val) + rescue AuthStoreError => detail + raise ArgumentError.new(detail.to_s, + @count, file) + end + } + end + + # Create a new mount. + def newmount(name) + if @mounts.include?(name) + raise ArgumentError, "%s is already mounted at %s" % + [@mounts[name], name], @count, file + end + mount = Mount.new(name) + @mounts[name] = mount + return mount + end + + # Set the path for a mount. + def path(mount, value) + if mount.name == Mount::MODULES + Puppet.warning "The '#{Mount::MODULES}' module can not have a path. Ignoring attempt to set it" + else + begin + mount.path = value + rescue ArgumentError => detail + Puppet.err "Removing mount %s: %s" % + [mount.name, detail] + @mounts.delete(mount.name) + end + end + end + + # Make sure all of our mounts are valid. We have to do this after the fact + # because details are added over time as the file is parsed. + def validate + @mounts.each { |name, mount| + unless mount.valid? + raise ArgumentError, "No path specified for mount %s" % name + end + } + end +end -- cgit From 08099b7a383987e292357f285e05933e10205660 Mon Sep 17 00:00:00 2001 From: Luke Kanies Date: Fri, 19 Oct 2007 17:35:40 -0500 Subject: File serving now works. I've tested a couple of ways to use it, and added integration tests at the most important hook points. This provides the final class structure for all of these classes, but a lot of the class names are pretty bad, so I'm planning on going through all of them (especially the file_server stuff) and renaming. The functionality is all here for finding files, though (finally). Once the classes are renamed, I'll be adding searching ability (which will enable the recursive file copies) and then adding the link management and enabling ignoring files. --- lib/puppet/file_serving/configuration/parser.rb | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'lib/puppet/file_serving/configuration/parser.rb') diff --git a/lib/puppet/file_serving/configuration/parser.rb b/lib/puppet/file_serving/configuration/parser.rb index 5b17d9801..707c3f9b1 100644 --- a/lib/puppet/file_serving/configuration/parser.rb +++ b/lib/puppet/file_serving/configuration/parser.rb @@ -3,6 +3,7 @@ require 'puppet/util/loadedfile' class Puppet::FileServing::Configuration::Parser < Puppet::Util::LoadedFile Mount = Puppet::FileServing::Mount + MODULES = 'modules' # Parse our configuration file. def parse @@ -52,10 +53,10 @@ class Puppet::FileServing::Configuration::Parser < Puppet::Util::LoadedFile # Add the mount for getting files from modules. def add_module_mount - unless @mounts[Mount::MODULES] - mount = Mount.new(Mount::MODULES) + unless @mounts[MODULES] + mount = Mount.new(MODULES) mount.allow("*") - @mounts[Mount::MODULES] = mount + @mounts[MODULES] = mount end end @@ -98,8 +99,8 @@ class Puppet::FileServing::Configuration::Parser < Puppet::Util::LoadedFile # Set the path for a mount. def path(mount, value) - if mount.name == Mount::MODULES - Puppet.warning "The '#{Mount::MODULES}' module can not have a path. Ignoring attempt to set it" + if mount.name == MODULES + Puppet.warning "The '#{MODULES}' module can not have a path. Ignoring attempt to set it" else begin mount.path = value -- cgit