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