diff options
author | luke <luke@980ebf18-57e1-0310-9a29-db15c13687c0> | 2005-09-02 19:26:31 +0000 |
---|---|---|
committer | luke <luke@980ebf18-57e1-0310-9a29-db15c13687c0> | 2005-09-02 19:26:31 +0000 |
commit | 75ec0543d27f3c552a778adae3255a3cb68f918b (patch) | |
tree | d86c7845c7012820d1b89ae50283598fc46607d2 | |
parent | 0d01770499d94c0d0140449569efe9126d97ea86 (diff) | |
download | puppet-75ec0543d27f3c552a778adae3255a3cb68f918b.tar.gz puppet-75ec0543d27f3c552a778adae3255a3cb68f918b.tar.xz puppet-75ec0543d27f3c552a778adae3255a3cb68f918b.zip |
committing initial tidy stuff, so i can move to the laptop
git-svn-id: https://reductivelabs.com/svn/puppet/trunk@622 980ebf18-57e1-0310-9a29-db15c13687c0
-rw-r--r-- | lib/puppet/type.rb | 1 | ||||
-rwxr-xr-x | lib/puppet/type/tidy.rb | 134 | ||||
-rwxr-xr-x | test/types/tc_tidy.rb | 78 |
3 files changed, 213 insertions, 0 deletions
diff --git a/lib/puppet/type.rb b/lib/puppet/type.rb index a15f41edb..1cb377eb2 100644 --- a/lib/puppet/type.rb +++ b/lib/puppet/type.rb @@ -1210,6 +1210,7 @@ require 'puppet/type/pfile' require 'puppet/type/pfilebucket' require 'puppet/type/service' require 'puppet/type/symlink' +require 'puppet/type/tidy' #require 'puppet/type/typegen' #require 'puppet/type/typegen/filetype' #require 'puppet/type/typegen/filerecord' diff --git a/lib/puppet/type/tidy.rb b/lib/puppet/type/tidy.rb new file mode 100755 index 000000000..cf9b3c939 --- /dev/null +++ b/lib/puppet/type/tidy.rb @@ -0,0 +1,134 @@ +#!/usr/local/bin/ruby -w + +# $Id$ + +require 'etc' +require 'puppet/type/state' +require 'puppet/type/pfile' + +module Puppet + # okay, how do we deal with parameters that don't have operations + # associated with them? + class State + class TidyUp < Puppet::State + require 'etc' + attr_accessor :file + + @doc = "Create a link to another file. Currently only symlinks + are supported, and attempts to replace normal files with + links will currently fail, while existing but incorrect symlinks + will be removed." + @name = :target + + def create + begin + debug("Creating symlink '%s' to '%s'" % + [self.parent[:path],self.should]) + unless File.symlink(self.should,self.parent[:path]) + raise TypeError.new("Could not create symlink '%s'" % + self.parent[:path]) + end + rescue => detail + raise TypeError.new("Cannot create symlink '%s': %s" % + [self.parent[:path],detail]) + end + end + + def remove + if FileTest.symlink?(self.parent[:path]) + debug("Removing symlink '%s'" % self.parent[:path]) + begin + File.unlink(self.parent[:path]) + rescue + raise TypeError.new("Failed to remove symlink '%s'" % + self.parent[:path]) + end + elsif FileTest.exists?(self.parent[:path]) + raise TypeError.new("Cannot remove normal file '%s'" % + self.parent[:path]) + else + debug("Symlink '%s' does not exist" % + self.parent[:path]) + end + end + + def retrieve + stat = nil + + if FileTest.symlink?(self.parent[:path]) + self.is = File.readlink(self.parent[:path]) + debug("link value is '%s'" % self.is) + return + else + self.is = nil + return + end + end + + # this is somewhat complicated, because it could exist and be + # a file + def sync + if self.should.nil? + self.remove() + else # it should exist and be a symlink + if FileTest.symlink?(self.parent[:path]) + path = File.readlink(self.parent[:path]) + if path != self.should + self.remove() + self.create() + end + elsif FileTest.exists?(self.parent[:path]) + raise TypeError.new("Cannot replace normal file '%s'" % + self.parent[:path]) + else + self.create() + end + end + + #self.parent.newevent(:event => :inode_changed) + end + end + end + + class Type + class Tidy < PFile + + # class instance variable + @states = [ + Puppet::State::TidyUp + ] + + @parameters = [ + :age, + :size, + :type, + :backup, + :recurse + ] + + @paramdoc[:age] = "Tidy files whose age is equal to or greater than + the specified number of days." + @paramdoc[:size] = "Tidy files whose size is equal to or greater than + the specified size. Unqualified values are in kilobytes, but + *b*, *k*, and *m* can be appended to specify *bytes*, *kilobytes*, + and *megabytes*, respectively. Only the first character is + significant, so the full word can also be used." + @paramdoc[:type] = "Set the mechanism for determining age. Access + time is the default mechanism, but modification." + @paramdoc[:recurse] = "If target is a directory, recursively descend + into the directory looking for files to tidy." + @doc = "Remove unwanted files based on specific criteria." + @name = :tidy + @namevar = :path + + def initialize(hash) + super + end + + def paramage=(age) + @parameters[:age] = age + end + + end # Puppet::Type::Symlink + end # Puppet::Type +end diff --git a/test/types/tc_tidy.rb b/test/types/tc_tidy.rb new file mode 100755 index 000000000..0e6bcd562 --- /dev/null +++ b/test/types/tc_tidy.rb @@ -0,0 +1,78 @@ +if __FILE__ == $0 + $:.unshift '..' + $:.unshift '../../lib' + $puppetbase = "../../../../language/trunk" +end + +require 'puppet' +require 'puppettest' +require 'test/unit' + +# $Id$ + +class TestTidy < TestPuppet + include FileTesting + # hmmm + # this is complicated, because we store references to the created + # objects in a central store + def setup + Puppet[:loglevel] = :debug if __FILE__ == $0 + super + end + + def mktmpfile + # because luke's home directory is on nfs, it can't be used for testing + # as root + tmpfile = tempfile() + File.open(tmpfile, "w") { |f| f.puts rand(100) } + @@tmpfiles.push tmpfile + return tmpfile + end + + def mktmpdir + dir = File.join(tmpdir(), "puppetlinkdir") + unless FileTest.exists?(dir) + Dir.mkdir(dir) + end + @@tmpfiles.push dir + return dir + end + + def test_simpletidy + dir = mktmpdir + file = File.join(dir, "tidytesting") + File.open(file, "w") { |f| + f.puts rand(100) + } + + Puppet::Type::Tidy.new( + :name => file + ) + end + + def test_recursion + source = mktmpdir() + FileUtils.cd(source) { + mkranddirsandfiles() + } + + link = nil + assert_nothing_raised { + link = newlink(:target => source, :recurse => true) + } + comp = newcomp("linktest",link) + cycle(comp) + + path = link.name + list = file_list(path) + FileUtils.cd(path) { + list.each { |file| + unless FileTest.directory?(file) + assert(FileTest.symlink?(file)) + target = File.readlink(file) + assert_equal(target,File.join(source,file.sub(/^\.\//,''))) + end + } + } + end +end |