class Puppet::Util::FileType

Attributes

name[RW]
loaded[RW]
path[RW]
synced[RW]

Public Instance Methods

backup() click to toggle source

Back the file up before replacing it.

# File lib/puppet/util/filetype.rb, line 101
def backup
  bucket.backup(@path) if Puppet::FileSystem.exist?(@path)
end
bucket() click to toggle source

Pick or create a filebucket to use.

# File lib/puppet/util/filetype.rb, line 77
def bucket
  @bucket ||= Puppet::Type.type(:filebucket).mkdefaultbucket.bucket
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, line 209
def cmdbase
  if @uid == Puppet::Util::SUIDManager.uid || Facter.value(:operatingsystem) == "HP-UX"
    return "crontab"
  else
    return "crontab -u #{@path}"
  end
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, line 90
def cronargs
  if uid = Puppet::Util.uid(@path) and uid == Puppet::Util::SUIDManager.uid
    {:failonfail => true, :combine => true}
  else
    {:failonfail => true, :combine => true, :uid => @path}
  end
end
path=(user) click to toggle source
# File lib/puppet/util/filetype.rb, line 171
def path=(user)
  begin
    @uid = Puppet::Util.uid(user)
  rescue Puppet::Error => detail
    raise FileReadError, "Could not retrieve user #{user}: #{detail}", detail.backtrace
  end

  # XXX We have to have the user name, not the uid, because some
  # systems *cough*linux*cough* require it that way
  @path = user
end
read() click to toggle source

Read the file.

# File lib/puppet/util/filetype.rb, line 106
def read
  if Puppet::FileSystem.exist?(@path)
    File.read(@path)
  else
    return nil
  end
end
remove() click to toggle source

Remove the file.

# File lib/puppet/util/filetype.rb, line 115
def remove
  Puppet::FileSystem.unlink(@path) if Puppet::FileSystem.exist?(@path)
end
write(text) click to toggle source

Overwrite the file.

# File lib/puppet/util/filetype.rb, line 120
def write(text)
  tf = Tempfile.new("puppet")
  tf.print text; tf.flush
  File.chmod(@default_mode, tf.path) if @default_mode
  FileUtils.cp(tf.path, @path)
  tf.close
  # If SELinux is present, we need to ensure the file has its expected context
  set_selinux_default_context(@path)
end

Public Class Methods

clear() click to toggle source
# File lib/puppet/util/filetype.rb, line 135
def self.clear
  @@tabs.clear
end
filetype(type) click to toggle source
# File lib/puppet/util/filetype.rb, line 72
def self.filetype(type)
  @filetypes[type]
end
new(path, default_mode = nil) click to toggle source
# File lib/puppet/util/filetype.rb, line 81
def initialize(path, default_mode = nil)
  raise ArgumentError.new("Path is nil") if path.nil?
  @path = path
  @default_mode = default_mode
end
newfiletype(name, &block) click to toggle source

Create a new filetype.

# File lib/puppet/util/filetype.rb, line 21
def self.newfiletype(name, &block)
  @filetypes ||= {}

  klass = genclass(
    name,
    :block => block,
    :prefix => "FileType",
    :hash => @filetypes
  )

  # Rename the read and write methods, so that we're sure they
  # maintain the stats.
  klass.class_eval do
    # Rename the read method
    define_method(:real_read, instance_method(:read))
    define_method(:read) do
      begin
        val = real_read
        @loaded = Time.now
        if val
          return val.gsub(/# HEADER.*\n/,'')
        else
          return ""
        end
      rescue Puppet::Error => detail
        raise
      rescue => detail
        message = "#{self.class} could not read #{@path}: #{detail}"
        Puppet.log_exception(detail, message)
        raise Puppet::Error, message, detail.backtrace
      end
    end

    # And then the write method
    define_method(:real_write, instance_method(:write))
    define_method(:write) do |text|
      begin
        val = real_write(text)
        @synced = Time.now
        return val
      rescue Puppet::Error => detail
        raise
      rescue => detail
        message = "#{self.class} could not write #{@path}: #{detail}"
        Puppet.log_exception(detail, message)
        raise Puppet::Error, message, detail.backtrace
      end
    end
  end
end