diff options
| author | Luke Kanies <luke@madstop.com> | 2005-08-27 20:19:45 +0000 |
|---|---|---|
| committer | Luke Kanies <luke@madstop.com> | 2005-08-27 20:19:45 +0000 |
| commit | 814a9b0288b74407b3ec0aa58310c9972e0ca347 (patch) | |
| tree | 958a8245427fb24782d56dc9478a1e54ba66a2ce | |
| parent | 9ec321edd0883b1ddb53d1cd97bb0ad42352b5a3 (diff) | |
adding config file parsing
git-svn-id: https://reductivelabs.com/svn/puppet/trunk@592 980ebf18-57e1-0310-9a29-db15c13687c0
| -rwxr-xr-x | lib/puppet/server/fileserver.rb | 140 | ||||
| -rwxr-xr-x | test/server/tc_fileserver.rb | 124 |
2 files changed, 221 insertions, 43 deletions
diff --git a/lib/puppet/server/fileserver.rb b/lib/puppet/server/fileserver.rb index 96080920d..179459f42 100755 --- a/lib/puppet/server/fileserver.rb +++ b/lib/puppet/server/fileserver.rb @@ -7,6 +7,8 @@ class Server class FileServer < Handler attr_accessor :local + Puppet.setdefault(:fileserverconfig, [:puppetconf, "fileserver.conf"]) + #CHECKPARAMS = %w{checksum type mode owner group} CHECKPARAMS = [:mode, :type, :owner, :group, :checksum] @@ -83,7 +85,17 @@ class Server @local = false end + if hash[:Config] == false + @noreadconfig = true + else + @config = hash[:Config] || Puppet[:fileserverconfig] + @noreadconfig = false + end + + @configstamp = nil + if hash.include?(:Mount) + @passedconfig = true unless hash[:Mount].is_a?(Hash) raise Puppet::DevError, "Invalid mount hash %s" % hash[:Mount].inspect @@ -94,6 +106,9 @@ class Server self.mount(dir, name) end } + else + @passedconfig = false + readconfig end end @@ -127,34 +142,79 @@ class Server }.join("\n") end - def mount(dir, name) + def mount(path, name) if @mounts.include?(name) - if @mounts[name] != dir + if @mounts[name] != path raise FileServerError, "%s is already mounted at %s" % - [@mounts[name], name] + [@mounts[name].path, name] else # it's already mounted; no problem return end end - unless name =~ %r{^\w+$} - raise FileServerError, "Invalid name format '%s'" % name - end - - unless FileTest.exists?(dir) - raise FileServerError, "%s does not exist" % dir - end - - if FileTest.directory?(dir) - if FileTest.readable?(dir) - Puppet.info "Mounting %s at %s" % [dir, name] - @mounts[name] = dir + if FileTest.directory?(path) + if FileTest.readable?(path) + @mounts[name] = Mount.new(name, path) + Puppet.info "Mounted %s at %s" % [path, name] else - raise FileServerError, "%s is not readable" % dir + raise FileServerError, "%s is not readable" % path end else - raise FileServerError, "%s is not a directory" % dir + raise FileServerError, "%s is not a directory" % path + end + end + + def readconfig + return if @noreadconfig + + @mounts.clear + + begin + File.open(@config) { |f| + mount = nil + count = 1 + f.each { |line| + case line + when /^\s*#/: next # skip comments + when /^\s*$/: next # skip blank lines + when /\[(\w+)\]/: + name = $1 + if @mounts.include?(name) + raise FileServerError, "%s is already mounted at %s" % + [@mounts[name], name] + end + mount = Mount.new(name) + @mounts[name] = mount + when /\s*(\w+)\s+(.+)$/: + var = $1 + value = $2 + case var + when "path": + mount.path = value + when "allow": + value.split(/\s*,\s*/).each { |val| + mount.allow(val) + } + when "deny": + value.split(/\s*,\s*/).each { |val| + mount.deny(val) + } + else + raise Puppet::Error, + "Invalid argument %s at line %s" % [var, count] + end + else + raise Puppet::Error, + "Invalid line %s: %s" % [count, line] + end + count += 1 + } + } + rescue Errno::EACCES => detail + raise Puppet::Error, "Cannot read %s" % @config + rescue Errno::ENOENT => detail + raise Puppet::Error, "%s does not exit" % @config end end @@ -168,9 +228,9 @@ class Server fpath = nil if path - fpath = File.join(@mounts[mount], path) + fpath = File.join(@mounts[mount].path, path) else - fpath = @mounts[mount] + fpath = @mounts[mount].path end unless FileTest.exists?(fpath) @@ -189,7 +249,7 @@ class Server private def nameswap(name, mount) - name.sub(/\/#{mount}/, @mounts[mount]).gsub(%r{//}, '/').sub( + name.sub(/\/#{mount}/, @mounts[mount].path).gsub(%r{//}, '/').sub( %r{/$}, '' ) #Puppet.info "Swapped %s to %s" % [name, newname] @@ -255,7 +315,7 @@ class Server end def subdir(mount, dir) - basedir = @mounts[mount] + basedir = @mounts[mount].path dirname = nil if dir @@ -266,6 +326,44 @@ class Server return dirname end + + class Mount + attr_reader :path, :name + + def allow(pattern) + end + + def allowed?(host) + end + + def deny(pattern) + end + + def initialize(name, path = nil) + unless name =~ %r{^\w+$} + raise FileServerError, "Invalid name format '%s'" % name + end + @name = name + + @allow = [] + @deny = [] + + if path + self.path = path + end + end + + def path=(path) + unless FileTest.exists?(path) + raise FileServerError, "%s does not exist" % path + end + @path = path + end + + def to_s + @path + end + end end end end diff --git a/test/server/tc_fileserver.rb b/test/server/tc_fileserver.rb index a06e993d4..7369ef5ab 100755 --- a/test/server/tc_fileserver.rb +++ b/test/server/tc_fileserver.rb @@ -30,18 +30,30 @@ class TestFileServer < TestPuppet } end - def mkrandomdirs(dir, depth, width) + def mktestfiles(testdir) + @@tmpfiles << testdir assert_nothing_raised { - Dir.mkdir(dir) - } + Dir.mkdir(testdir) + @@tmpfiles << testdir + files = %w{a b c d e}.collect { |l| + name = File.join(testdir, "file%s" % l) + File.open(name, "w") { |f| + f.puts rand(100) + } + + name + } + return files + } end def test_namefailures server = nil assert_nothing_raised { server = Puppet::Server::FileServer.new( - :Local => true + :Local => true, + :Config => false ) } @@ -79,7 +91,8 @@ class TestFileServer < TestPuppet assert_nothing_raised { server = Puppet::Server::FileServer.new( - :Local => true + :Local => true, + :Config => false ) } @@ -121,7 +134,8 @@ class TestFileServer < TestPuppet assert_nothing_raised { server = Puppet::Server::FileServer.new( - :Local => true + :Local => true, + :Config => false ) } @@ -175,7 +189,8 @@ class TestFileServer < TestPuppet assert_nothing_raised { server = Puppet::Server::FileServer.new( - :Local => true + :Local => true, + :Config => false ) } @@ -207,7 +222,8 @@ class TestFileServer < TestPuppet server = nil assert_nothing_raised { server = Puppet::Server::FileServer.new( - :Local => true + :Local => true, + :Config => false ) } @@ -237,7 +253,8 @@ class TestFileServer < TestPuppet server = nil assert_nothing_raised { server = Puppet::Server::FileServer.new( - :Local => true + :Local => true, + :Config => false ) } @@ -282,7 +299,8 @@ class TestFileServer < TestPuppet server = nil assert_nothing_raised { server = Puppet::Server::FileServer.new( - :Local => true + :Local => true, + :Config => false ) } @@ -319,7 +337,8 @@ class TestFileServer < TestPuppet server = nil assert_nothing_raised { server = Puppet::Server::FileServer.new( - :Local => true + :Local => true, + :Config => false ) } @@ -348,23 +367,15 @@ class TestFileServer < TestPuppet def test_describe server = nil testdir = "/tmp/remotefilecopying" - assert_nothing_raised { - Dir.mkdir(testdir) - @@tmpfiles << testdir - %w{a b c d e}.each { |l| - name = File.join(testdir, "file%s" % name) - File.open(name, "w") { |f| - f.puts rand(100) - } - } - } + files = mktestfiles(testdir) file = nil checks = Puppet::Server::FileServer::CHECKPARAMS assert_nothing_raised { server = Puppet::Server::FileServer.new( - :Local => true + :Local => true, + :Config => false ) } @@ -385,6 +396,75 @@ class TestFileServer < TestPuppet desc = server.describe(sfile + file) } } + + files.each { |file| + file = File.basename(file) + assert_nothing_raised { + desc = server.describe(sfile + file) + assert(desc, "Got no description for %s" % file) + assert(desc != "", "Got no description for %s" % file) + assert_match(/^\d+/, desc, "Got invalid description %s" % desc) + } + } + end + + def test_configfile + server = nil + basedir = "/tmp/configfiletesting" + + conftext = "# a test config file\n \n" + + @@tmpfiles << basedir + + Dir.mkdir(basedir) + mounts = {} + %w{thing thus ahna the}.each { |dir| + path = File.join(basedir, dir) + conftext << "[#{dir}] + path #{path} +" + mounts[dir] = mktestfiles(path) + + } + + conffile = "/tmp/fileservertestingfile" + @@tmpfiles << conffile + + File.open(conffile, "w") { |f| + f.print conftext + } + + + assert_nothing_raised { + server = Puppet::Server::FileServer.new( + :Local => true, + :Config => conffile + ) + } + + list = nil + mounts.each { |mount, files| + mount = "/#{mount}/" + assert_nothing_raised { + list = server.list(mount, true) + } + + assert_nothing_raised { + list.split("\n").each { |line| + file, type = line.split("\t") + + desc = server.describe(mount + file) + } + } + + files.each { |f| + file = File.basename(f) + desc = server.describe(mount + file) + assert(desc, "Got no description for %s" % f) + assert(desc != "", "Got no description for %s" % f) + assert_match(/^\d+/, desc, "Got invalid description %s" % f) + } + } end end |
