class Puppet::Util::FileType
Attributes
name[RW]
loaded[RW]
path[RW]
synced[RW]
Public Class Methods
clear()
click to toggle source
# File lib/puppet/util/filetype.rb 140 def self.clear 141 @@tabs.clear 142 end
filetype(type)
click to toggle source
# File lib/puppet/util/filetype.rb 73 def self.filetype(type) 74 @filetypes[type] 75 end
new(path, default_mode = nil)
click to toggle source
# File lib/puppet/util/filetype.rb 82 def initialize(path, default_mode = nil) 83 raise ArgumentError.new(_("Path is nil")) if path.nil? 84 @path = path 85 @default_mode = default_mode 86 end
newfiletype(name, &block)
click to toggle source
Create a new filetype.
# File lib/puppet/util/filetype.rb 22 def self.newfiletype(name, &block) 23 @filetypes ||= {} 24 25 klass = genclass( 26 name, 27 :block => block, 28 :prefix => "FileType", 29 :hash => @filetypes 30 ) 31 32 # Rename the read and write methods, so that we're sure they 33 # maintain the stats. 34 klass.class_eval do 35 # Rename the read method 36 define_method(:real_read, instance_method(:read)) 37 define_method(:read) do 38 begin 39 val = real_read 40 @loaded = Time.now 41 if val 42 return val.gsub(/# HEADER.*\n/,'') 43 else 44 return "" 45 end 46 rescue Puppet::Error 47 raise 48 rescue => detail 49 message = _("%{klass} could not read %{path}: %{detail}") % { klass: self.class, path: @path, detail: detail } 50 Puppet.log_exception(detail, message) 51 raise Puppet::Error, message, detail.backtrace 52 end 53 end 54 55 # And then the write method 56 define_method(:real_write, instance_method(:write)) 57 define_method(:write) do |text| 58 begin 59 val = real_write(text) 60 @synced = Time.now 61 return val 62 rescue Puppet::Error 63 raise 64 rescue => detail 65 message = _("%{klass} could not write %{path}: %{detail}") % { klass: self.class, path: @path, detail: detail } 66 Puppet.log_exception(detail, message) 67 raise Puppet::Error, message, detail.backtrace 68 end 69 end 70 end 71 end
Public Instance Methods
backup()
click to toggle source
Back the file up before replacing it.
# File lib/puppet/util/filetype.rb 103 def backup 104 bucket.backup(@path) if Puppet::FileSystem.exist?(@path) 105 end
bucket()
click to toggle source
Pick or create a filebucket to use.
# File lib/puppet/util/filetype.rb 78 def bucket 79 @bucket ||= Puppet::Type.type(:filebucket).mkdefaultbucket.bucket 80 end
cmdbase()
click to toggle source
Only add the -u flag when the @path is different. Fedora apparently does not think I should be allowed to set the @path to my own user name
# File lib/puppet/util/filetype.rb 247 def cmdbase 248 if @uid == Puppet::Util::SUIDManager.uid || Puppet.runtime[:facter].value('os.name') == "HP-UX" 249 return "crontab" 250 else 251 return "crontab -u #{@path}" 252 end 253 end
cronargs()
click to toggle source
Arguments that will be passed to the execute method. Will set the uid to the target user if the target user and the current user are not the same
# File lib/puppet/util/filetype.rb 91 def cronargs 92 uid = Puppet::Util.uid(@path) 93 if uid && uid == Puppet::Util::SUIDManager.uid 94 {:failonfail => true, :combine => true} 95 else 96 {:failonfail => true, :combine => true, :uid => @path} 97 end 98 end
path=(user)
click to toggle source
# File lib/puppet/util/filetype.rb 182 def path=(user) 183 begin 184 @uid = Puppet::Util.uid(user) 185 rescue Puppet::Error => detail 186 raise FileReadError, _("Could not retrieve user %{user}: %{detail}") % { user: user, detail: detail }, detail.backtrace 187 end 188 189 # XXX We have to have the user name, not the uid, because some 190 # systems *cough*linux*cough* require it that way 191 @path = user 192 end
read()
click to toggle source
Read the file.
# File lib/puppet/util/filetype.rb 108 def read 109 if Puppet::FileSystem.exist?(@path) 110 # this code path is used by many callers so the original default is 111 # being explicitly preserved 112 Puppet::FileSystem.read(@path, :encoding => Encoding.default_external) 113 else 114 return nil 115 end 116 end
remove()
click to toggle source
Remove the file.
# File lib/puppet/util/filetype.rb 119 def remove 120 Puppet::FileSystem.unlink(@path) if Puppet::FileSystem.exist?(@path) 121 end
write(text)
click to toggle source
Overwrite the file.
# File lib/puppet/util/filetype.rb 124 def write(text) 125 # this file is managed by the OS and should be using system encoding 126 tf = Tempfile.new("puppet", :encoding => Encoding.default_external) 127 tf.print text; tf.flush 128 File.chmod(@default_mode, tf.path) if @default_mode 129 FileUtils.cp(tf.path, @path) 130 tf.close 131 # If SELinux is present, we need to ensure the file has its expected context 132 set_selinux_default_context(@path) 133 end