From 0eae739fd34ff297b7468cfa8b729c213568c30f Mon Sep 17 00:00:00 2001 From: luke Date: Sat, 1 Apr 2006 20:16:22 +0000 Subject: renaming filesystem to mount git-svn-id: https://reductivelabs.com/svn/puppet/trunk@1049 980ebf18-57e1-0310-9a29-db15c13687c0 --- lib/puppet/type/parsedtype/filesystem.rb | 186 ---------------------- lib/puppet/type/parsedtype/mount.rb | 186 ++++++++++++++++++++++ test/types/filesystem.rb | 256 ------------------------------- test/types/mount.rb | 256 +++++++++++++++++++++++++++++++ 4 files changed, 442 insertions(+), 442 deletions(-) delete mode 100755 lib/puppet/type/parsedtype/filesystem.rb create mode 100755 lib/puppet/type/parsedtype/mount.rb delete mode 100755 test/types/filesystem.rb create mode 100755 test/types/mount.rb diff --git a/lib/puppet/type/parsedtype/filesystem.rb b/lib/puppet/type/parsedtype/filesystem.rb deleted file mode 100755 index 65b1832d3..000000000 --- a/lib/puppet/type/parsedtype/filesystem.rb +++ /dev/null @@ -1,186 +0,0 @@ -require 'etc' -require 'facter' -require 'puppet/type/parsedtype' -require 'puppet/type/state' - -module Puppet - newtype(:filesystem, Puppet::Type::ParsedType) do - - ensurable do - newvalue(:present) do - @parent.create() - end - - newvalue(:absent) do - @parent.destroy() - - if @parent.mounted? - @parent.unmount - end - - :filesystem_removed - end - - newvalue(:mounted) do - if @is == :absent - set_present - end - - @parent.mount - - :filesystem_mounted - end - end - - newstate(:device) do - desc "The device providing the filesystem. This can be whatever - device is supporting by the filesystem, including network - devices or devices specified by UUID rather than device - path, depending on the operating system." - end - - # Solaris specifies two devices, not just one. - newstate(:blockdevice) do - desc "The the device to fsck. This is state is only valid - on Solaris, and in most cases will default to the correct - value." - - # Default to the device but with "dsk" replaced with "rdsk". - defaultto do - if Facter["operatingsystem"].value == "Solaris" - device = @parent.value(:device) - if device =~ %r{/dsk/} - device.sub(%r{/dsk/}, "/rdsk/") - else - nil - end - else - nil - end - end - end - - newstate(:fstype) do - desc "The filesystem type. Valid values depend on the - operating system." - end - - newstate(:options) do - desc "Mount options for the filesystems, as they would - appear in the fstab." - end - - newstate(:pass) do - desc "The pass in which the filesystem is checked." - end - - newstate(:atboot) do - desc "Whether to mount the filesystem at boot. Not all platforms - support this." - end - - newstate(:dump) do - desc "Whether to dump the filesystem. Not all platforms - support this." - end - - newparam(:path) do - desc "The mount path for the filesystem." - - isnamevar - end - - @doc = "Manages mounted filesystems, including putting mount - information into the filesystem table." - - @instances = [] - - case Facter["operatingsystem"].value - when "Solaris": - @path = "/etc/vfstab" - @fields = [:device, :blockdevice, :path, :fstype, :pass, :atboot, - :options] - else - @path = "/etc/fstab" - @fields = [:device, :path, :fstype, :options, :dump, :pass] - end - - @filetype = Puppet::FileType.filetype(:flat) - - # Parse a filesystem tab. - # - # This method also stores existing comments, and it stores all - # filesystems in order, mostly so that comments are retained in the - # order they were written and in proximity to the same fses. - def self.parse(text) - count = 0 - hash = {} - text.chomp.split("\n").each { |line| - case line - when /^#/, /^\s*$/: - # add comments and blank lines to the list as they are - comment(line) - else - values = line.split(/\s+/) - unless @fields.length == values.length - raise Puppet::Error, "Could not parse line %s" % line - end - - @fields.zip(values).each do |field, value| - hash[field] = value - end - - hash2obj(hash) - - hash.clear - count += 1 - end - } - end - - # This only works when the mount point is synced to the fstab. - def mount - output = %x{mount #{self[:path]} 2>&1} - - unless $? == 0 - raise Puppet::Error, "Could not mount %s: %s" % [self[:path], output] - end - end - - # This only works when the mount point is synced to the fstab. - def unmount - output = %x{umount #{self[:path]}} - - unless $? == 0 - raise Puppet::Error, "Could not mount %s" % self[:path] - end - end - - # Is the filesystem currently mounted? - def mounted? - %x{df}.split("\n").find do |line| - fs = line.split(/\s+/)[-1] - fs == self[:path] - end - end - - # Convert the current object into an fstab-style string. - def to_record - self.class.fields.collect do |field| - if value = self.value(field) - value - else - if @states.include? field - self.warning @states[field].inspect - else - self.warning field.inspect - end - raise Puppet::Error, - "Could not retrieve value for %s" % field - end - end.join("\t") - end - end -end - -# $Id$ diff --git a/lib/puppet/type/parsedtype/mount.rb b/lib/puppet/type/parsedtype/mount.rb new file mode 100755 index 000000000..08f797f72 --- /dev/null +++ b/lib/puppet/type/parsedtype/mount.rb @@ -0,0 +1,186 @@ +require 'etc' +require 'facter' +require 'puppet/type/parsedtype' +require 'puppet/type/state' + +module Puppet + newtype(:mount, Puppet::Type::ParsedType) do + + ensurable do + newvalue(:present) do + @parent.create() + end + + newvalue(:absent) do + @parent.destroy() + + if @parent.mounted? + @parent.unmount + end + + :mount_removed + end + + newvalue(:mounted) do + if @is == :absent + set_present + end + + @parent.mount + + :mount_mounted + end + end + + newstate(:device) do + desc "The device providing the mount. This can be whatever + device is supporting by the mount, including network + devices or devices specified by UUID rather than device + path, depending on the operating system." + end + + # Solaris specifies two devices, not just one. + newstate(:blockdevice) do + desc "The the device to fsck. This is state is only valid + on Solaris, and in most cases will default to the correct + value." + + # Default to the device but with "dsk" replaced with "rdsk". + defaultto do + if Facter["operatingsystem"].value == "Solaris" + device = @parent.value(:device) + if device =~ %r{/dsk/} + device.sub(%r{/dsk/}, "/rdsk/") + else + nil + end + else + nil + end + end + end + + newstate(:fstype) do + desc "The mount type. Valid values depend on the + operating system." + end + + newstate(:options) do + desc "Mount options for the mounts, as they would + appear in the fstab." + end + + newstate(:pass) do + desc "The pass in which the mount is checked." + end + + newstate(:atboot) do + desc "Whether to mount the mount at boot. Not all platforms + support this." + end + + newstate(:dump) do + desc "Whether to dump the mount. Not all platforms + support this." + end + + newparam(:path) do + desc "The mount path for the mount." + + isnamevar + end + + @doc = "Manages mounted mounts, including putting mount + information into the mount table." + + @instances = [] + + case Facter["operatingsystem"].value + when "Solaris": + @path = "/etc/vfstab" + @fields = [:device, :blockdevice, :path, :fstype, :pass, :atboot, + :options] + else + @path = "/etc/fstab" + @fields = [:device, :path, :fstype, :options, :dump, :pass] + end + + @filetype = Puppet::FileType.filetype(:flat) + + # Parse a mount tab. + # + # This method also stores existing comments, and it stores all + # mounts in order, mostly so that comments are retained in the + # order they were written and in proximity to the same fses. + def self.parse(text) + count = 0 + hash = {} + text.chomp.split("\n").each { |line| + case line + when /^#/, /^\s*$/: + # add comments and blank lines to the list as they are + comment(line) + else + values = line.split(/\s+/) + unless @fields.length == values.length + raise Puppet::Error, "Could not parse line %s" % line + end + + @fields.zip(values).each do |field, value| + hash[field] = value + end + + hash2obj(hash) + + hash.clear + count += 1 + end + } + end + + # This only works when the mount point is synced to the fstab. + def mount + output = %x{mount #{self[:path]} 2>&1} + + unless $? == 0 + raise Puppet::Error, "Could not mount %s: %s" % [self[:path], output] + end + end + + # This only works when the mount point is synced to the fstab. + def unmount + output = %x{umount #{self[:path]}} + + unless $? == 0 + raise Puppet::Error, "Could not mount %s" % self[:path] + end + end + + # Is the mount currently mounted? + def mounted? + %x{df}.split("\n").find do |line| + fs = line.split(/\s+/)[-1] + fs == self[:path] + end + end + + # Convert the current object into an fstab-style string. + def to_record + self.class.fields.collect do |field| + if value = self.value(field) + value + else + if @states.include? field + self.warning @states[field].inspect + else + self.warning field.inspect + end + raise Puppet::Error, + "Could not retrieve value for %s" % field + end + end.join("\t") + end + end +end + +# $Id$ diff --git a/test/types/filesystem.rb b/test/types/filesystem.rb deleted file mode 100755 index 189430784..000000000 --- a/test/types/filesystem.rb +++ /dev/null @@ -1,256 +0,0 @@ -# Test host job creation, modification, and destruction - -if __FILE__ == $0 - $:.unshift '..' - $:.unshift '../../lib' - $puppetbase = "../.." -end - -require 'puppettest' -require 'puppet' -require 'puppet/type/parsedtype/filesystem' -require 'test/unit' -require 'facter' - -class TestFilesystem < Test::Unit::TestCase - include TestPuppet - def setup - super - @filesystemtype = Puppet.type(:filesystem) - @oldfiletype = @filesystemtype.filetype - end - - def teardown - @filesystemtype.filetype = @oldfiletype - Puppet.type(:file).clear - super - end - - # Here we just create a fake host type that answers to all of the methods - # but does not modify our actual system. - def mkfaketype - pfile = tempfile() - old = @filesystemtype.path - @filesystemtype.path = pfile - - cleanup do - @filesystemtype.path = old - @filesystemtype.fileobj = nil - end - - # Reset this, just in case - @filesystemtype.fileobj = nil - end - - def mkfilesystem - filesystem = nil - - if defined? @pcount - @pcount += 1 - else - @pcount = 1 - end - args = { - :path => "/fspuppet%s" % @pcount, - :device => "/dev/dsk%s" % @pcount, - } - - Puppet.type(:filesystem).fields.each do |field| - unless args.include? field - args[field] = "fake%s" % @pcount - end - end - - assert_nothing_raised { - filesystem = Puppet.type(:filesystem).create(args) - } - - return filesystem - end - - def test_simplefilesystem - mkfaketype - host = nil - assert_nothing_raised { - assert_nil(Puppet.type(:filesystem).retrieve) - } - - filesystem = mkfilesystem - - assert_nothing_raised { - Puppet.type(:filesystem).store - } - - assert_nothing_raised { - assert( - Puppet.type(:filesystem).to_file.include?( - Puppet.type(:filesystem).fileobj.read - ), - "File does not include all of our objects" - ) - } - end - - def test_filesystemsparse - assert_nothing_raised { - @filesystemtype.retrieve - } - - # Now just make we've got some filesystems we know will be there - root = @filesystemtype["/"] - assert(root, "Could not retrieve root filesystem") - end - - def test_rootfs - fs = nil - assert_nothing_raised { - Puppet.type(:filesystem).retrieve - } - - assert_nothing_raised { - fs = Puppet.type(:filesystem)["/"] - } - assert(fs, "Could not retrieve root fs") - - assert_nothing_raised { - assert(fs.mounted?, "Root is considered not mounted") - } - end - - # Make sure it reads and writes correctly. - def test_readwrite - assert_nothing_raised { - Puppet::Type.type(:filesystem).retrieve - } - - # Now switch to storing in ram - mkfaketype - - fs = mkfilesystem - - assert(Puppet::Type.type(:filesystem).path != "/etc/fstab") - - assert_events([:filesystem_created], fs) - - text = Puppet::Type.type(:filesystem).fileobj.read - - assert(text =~ /#{fs[:path]}/, "Text did not include new fs") - - fs[:ensure] = :absent - - assert_events([:filesystem_removed], fs) - text = Puppet::Type.type(:filesystem).fileobj.read - - assert(text !~ /#{fs[:path]}/, "Text still includes new fs") - - fs[:ensure] = :present - - assert_events([:filesystem_created], fs) - - text = Puppet::Type.type(:filesystem).fileobj.read - - assert(text =~ /#{fs[:path]}/, "Text did not include new fs") - end - - if Process.uid == 0 - def test_mountfs - fs = nil - case Facter["hostname"].value - when "culain": fs = "/ubuntu" - else - $stderr.puts "No filesystem for mount testing; skipping" - return - end - - backup = tempfile() - - FileUtils.cp(Puppet::Type.type(:filesystem).path, backup) - - # Make sure the original gets reinstalled. - cleanup do - FileUtils.cp(backup, Puppet::Type.type(:filesystem).path) - end - - Puppet.type(:filesystem).retrieve - - obj = Puppet.type(:filesystem)[fs] - - assert(obj, "Could not retrieve %s object" % fs) - - current = nil - - assert_nothing_raised { - current = obj.mounted? - } - - if current - # Make sure the original gets reinstalled. - cleanup do - unless obj.mounted? - obj.mount - end - end - end - - unless current - assert_nothing_raised { - obj.mount - } - end - - # Now copy all of the states' "is" values to the "should" values - obj.each do |state| - state.should = state.is - end - - # Verify we can remove the filesystem - assert_nothing_raised { - obj[:ensure] = :absent - } - - assert_events([:filesystem_removed], obj) - - # And verify it's gone - assert(!obj.mounted?, "Object is mounted after being removed") - - text = Puppet.type(:filesystem).fileobj.read - - assert(text !~ /#{fs}/, - "Fstab still contains %s" % fs) - - assert_raise(Puppet::Error, "Removed filesystem did not throw an error") { - obj.mount - } - - assert_nothing_raised { - obj[:ensure] = :present - } - - assert_events([:filesystem_created], obj) - - assert(File.read(Puppet.type(:filesystem).path) =~ /#{fs}/, - "Fstab does not contain %s" % fs) - - assert(! obj.mounted?, "Object is mounted incorrectly") - - assert_nothing_raised { - obj[:ensure] = :mounted - } - - assert_events([:filesystem_mounted], obj) - - assert(File.read(Puppet.type(:filesystem).path) =~ /#{fs}/, - "Fstab does not contain %s" % fs) - - assert(obj.mounted?, "Object is not mounted") - - unless current - assert_nothing_raised { - obj.unmount - } - end - end - end -end - -# $Id$ diff --git a/test/types/mount.rb b/test/types/mount.rb new file mode 100755 index 000000000..7641fd2cb --- /dev/null +++ b/test/types/mount.rb @@ -0,0 +1,256 @@ +# Test host job creation, modification, and destruction + +if __FILE__ == $0 + $:.unshift '..' + $:.unshift '../../lib' + $puppetbase = "../.." +end + +require 'puppettest' +require 'puppet' +require 'puppet/type/parsedtype/mount' +require 'test/unit' +require 'facter' + +class TestMounts < Test::Unit::TestCase + include TestPuppet + def setup + super + @mounttype = Puppet.type(:mount) + @oldfiletype = @mounttype.filetype + end + + def teardown + @mounttype.filetype = @oldfiletype + Puppet.type(:file).clear + super + end + + # Here we just create a fake host type that answers to all of the methods + # but does not modify our actual system. + def mkfaketype + pfile = tempfile() + old = @mounttype.path + @mounttype.path = pfile + + cleanup do + @mounttype.path = old + @mounttype.fileobj = nil + end + + # Reset this, just in case + @mounttype.fileobj = nil + end + + def mkmount + mount = nil + + if defined? @pcount + @pcount += 1 + else + @pcount = 1 + end + args = { + :path => "/fspuppet%s" % @pcount, + :device => "/dev/dsk%s" % @pcount, + } + + Puppet.type(:mount).fields.each do |field| + unless args.include? field + args[field] = "fake%s" % @pcount + end + end + + assert_nothing_raised { + mount = Puppet.type(:mount).create(args) + } + + return mount + end + + def test_simplemount + mkfaketype + host = nil + assert_nothing_raised { + assert_nil(Puppet.type(:mount).retrieve) + } + + mount = mkmount + + assert_nothing_raised { + Puppet.type(:mount).store + } + + assert_nothing_raised { + assert( + Puppet.type(:mount).to_file.include?( + Puppet.type(:mount).fileobj.read + ), + "File does not include all of our objects" + ) + } + end + + def test_mountsparse + assert_nothing_raised { + @mounttype.retrieve + } + + # Now just make we've got some mounts we know will be there + root = @mounttype["/"] + assert(root, "Could not retrieve root mount") + end + + def test_rootfs + fs = nil + assert_nothing_raised { + Puppet.type(:mount).retrieve + } + + assert_nothing_raised { + fs = Puppet.type(:mount)["/"] + } + assert(fs, "Could not retrieve root fs") + + assert_nothing_raised { + assert(fs.mounted?, "Root is considered not mounted") + } + end + + # Make sure it reads and writes correctly. + def test_readwrite + assert_nothing_raised { + Puppet::Type.type(:mount).retrieve + } + + # Now switch to storing in ram + mkfaketype + + fs = mkmount + + assert(Puppet::Type.type(:mount).path != "/etc/fstab") + + assert_events([:mount_created], fs) + + text = Puppet::Type.type(:mount).fileobj.read + + assert(text =~ /#{fs[:path]}/, "Text did not include new fs") + + fs[:ensure] = :absent + + assert_events([:mount_removed], fs) + text = Puppet::Type.type(:mount).fileobj.read + + assert(text !~ /#{fs[:path]}/, "Text still includes new fs") + + fs[:ensure] = :present + + assert_events([:mount_created], fs) + + text = Puppet::Type.type(:mount).fileobj.read + + assert(text =~ /#{fs[:path]}/, "Text did not include new fs") + end + + if Process.uid == 0 + def test_mountfs + fs = nil + case Facter["hostname"].value + when "culain": fs = "/ubuntu" + else + $stderr.puts "No mount for mount testing; skipping" + return + end + + backup = tempfile() + + FileUtils.cp(Puppet::Type.type(:mount).path, backup) + + # Make sure the original gets reinstalled. + cleanup do + FileUtils.cp(backup, Puppet::Type.type(:mount).path) + end + + Puppet.type(:mount).retrieve + + obj = Puppet.type(:mount)[fs] + + assert(obj, "Could not retrieve %s object" % fs) + + current = nil + + assert_nothing_raised { + current = obj.mounted? + } + + if current + # Make sure the original gets reinstalled. + cleanup do + unless obj.mounted? + obj.mount + end + end + end + + unless current + assert_nothing_raised { + obj.mount + } + end + + # Now copy all of the states' "is" values to the "should" values + obj.each do |state| + state.should = state.is + end + + # Verify we can remove the mount + assert_nothing_raised { + obj[:ensure] = :absent + } + + assert_events([:mount_removed], obj) + + # And verify it's gone + assert(!obj.mounted?, "Object is mounted after being removed") + + text = Puppet.type(:mount).fileobj.read + + assert(text !~ /#{fs}/, + "Fstab still contains %s" % fs) + + assert_raise(Puppet::Error, "Removed mount did not throw an error") { + obj.mount + } + + assert_nothing_raised { + obj[:ensure] = :present + } + + assert_events([:mount_created], obj) + + assert(File.read(Puppet.type(:mount).path) =~ /#{fs}/, + "Fstab does not contain %s" % fs) + + assert(! obj.mounted?, "Object is mounted incorrectly") + + assert_nothing_raised { + obj[:ensure] = :mounted + } + + assert_events([:mount_mounted], obj) + + assert(File.read(Puppet.type(:mount).path) =~ /#{fs}/, + "Fstab does not contain %s" % fs) + + assert(obj.mounted?, "Object is not mounted") + + unless current + assert_nothing_raised { + obj.unmount + } + end + end + end +end + +# $Id$ -- cgit