diff options
author | luke <luke@980ebf18-57e1-0310-9a29-db15c13687c0> | 2006-04-02 20:42:46 +0000 |
---|---|---|
committer | luke <luke@980ebf18-57e1-0310-9a29-db15c13687c0> | 2006-04-02 20:42:46 +0000 |
commit | e6f9163c543c19756887b7be04e01fb51789070e (patch) | |
tree | 91aea078c8cc97d3ae2ba0e11a709f859c67f8b7 | |
parent | 0f15e8c8097aed6ed34aa65b247b9ede28146574 (diff) | |
download | puppet-e6f9163c543c19756887b7be04e01fb51789070e.tar.gz puppet-e6f9163c543c19756887b7be04e01fb51789070e.tar.xz puppet-e6f9163c543c19756887b7be04e01fb51789070e.zip |
Adding a "write" method to config objects, so that files can be easily written with the correct owner, group, and modes
git-svn-id: https://reductivelabs.com/svn/puppet/trunk@1051 980ebf18-57e1-0310-9a29-db15c13687c0
-rw-r--r-- | lib/puppet/config.rb | 28 | ||||
-rwxr-xr-x | test/other/config.rb | 34 | ||||
-rw-r--r-- | test/puppettest.rb | 5 |
3 files changed, 66 insertions, 1 deletions
diff --git a/lib/puppet/config.rb b/lib/puppet/config.rb index 4fb2840b7..18bd21693 100644 --- a/lib/puppet/config.rb +++ b/lib/puppet/config.rb @@ -512,6 +512,32 @@ Generated on #{Time.now}. @config.has_key?(param) end + # Open a file with the appropriate user, group, and mode + def write(default, *args) + obj = nil + unless obj = @config[default] + raise ArgumentError, "Unknown default %s" % default + end + + unless obj.is_a? CFile + raise ArgumentError, "Default %s is not a file" % default + end + + Puppet::Util.asuser(obj.owner, obj.group) do + mode = obj.mode || 0640 + + if args.empty? + args << "w" + end + + args << mode + + File.open(obj.value, *args) do |file| + yield file + end + end + end + # The base element type. class CElement attr_accessor :name, :section, :default, :parent, :setbycli @@ -695,7 +721,7 @@ Generated on #{Time.now}. end [:mode].each { |var| if value = self.send(var) - obj[var] = value + obj[var] = "%o" % value end } diff --git a/test/other/config.rb b/test/other/config.rb index 62a308040..2714eab17 100755 --- a/test/other/config.rb +++ b/test/other/config.rb @@ -517,6 +517,40 @@ yay = /a/path assert_equal(group, config[:group], "Group did not take") end + + # provide a method to modify and create files w/out specifying the info + # already stored in a config + def test_writingfiles + path = tempfile() + mode = 0644 + + config = mkconfig + + args = { :default => path, :mode => mode } + + user = nonrootuser() + group = nonrootgroup() + + if Process.uid == 0 + args[:owner] = user.name + args[:group] = group.name + end + + config.setdefaults(:testing, :myfile => args) + + assert_nothing_raised { + config.write(:myfile) do |file| + file.puts "yay" + end + } + + assert_equal(mode, filemode(path), "Modes are not equal") + + if Process.uid == 0 + assert_equal(user.uid, File.stat(path).uid, "UIDS are not equal") + assert_equal(group.gid, File.stat(path).gid, "GIDS are not equal") + end + end end # $Id$ diff --git a/test/puppettest.rb b/test/puppettest.rb index 0524cf769..bcc51cb8a 100644 --- a/test/puppettest.rb +++ b/test/puppettest.rb @@ -294,6 +294,11 @@ module TestPuppet return files end + + # wrap how to retrieve the masked mode + def filemode(file) + File.stat(file).mode & 007777 + end end |