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, line 131
def self.clear
  @@tabs.clear
end
filetype(type) click to toggle source
# File lib/puppet/util/filetype.rb, line 70
def self.filetype(type)
  @filetypes[type]
end
new(path) click to toggle source
# File lib/puppet/util/filetype.rb, line 79
def initialize(path)
  raise ArgumentError.new("Path is nil") if path.nil?
  @path = path
end
newfiletype(name, &block) click to toggle source

Create a new filetype.

# File lib/puppet/util/filetype.rb, line 19
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
      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
      end
    end
  end
end

Public Instance Methods

backup() click to toggle source

Back the file up before replacing it.

# File lib/puppet/util/filetype.rb, line 98
def backup
  bucket.backup(@path) if File.exists?(@path)
end
bucket() click to toggle source

Pick or create a filebucket to use.

# File lib/puppet/util/filetype.rb, line 75
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 203
def cmdbase
  cmd = nil
  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 87
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 165
def path=(user)
  begin
    @uid = Puppet::Util.uid(user)
  rescue Puppet::Error => detail
    raise Puppet::Error, "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 103
def read
  if File.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 112
def remove
  File.unlink(@path) if File.exist?(@path)
end
write(text) click to toggle source

Overwrite the file.

# File lib/puppet/util/filetype.rb, line 117
def write(text)
  tf = Tempfile.new("puppet")
  tf.print text; tf.flush
  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