class Puppetserver::Ca::Utils::FileSystem

Constants

DIR_MODES

Public Class Methods

check_for_existing_files(one_or_more_paths) click to toggle source
# File lib/puppetserver/ca/utils/file_system.rb, line 43
def self.check_for_existing_files(one_or_more_paths)
  errors = []
  Array(one_or_more_paths).each do |path|
    if File.exist?(path)
      errors << "Existing file at '#{path}'"
    end
  end
  errors
end
ensure_dirs(one_or_more_dirs) click to toggle source
# File lib/puppetserver/ca/utils/file_system.rb, line 26
def self.ensure_dirs(one_or_more_dirs)
  Array(one_or_more_dirs).each do |directory|
    instance.ensure_dir(directory)
  end
end
instance() click to toggle source
# File lib/puppetserver/ca/utils/file_system.rb, line 18
def self.instance
  @instance ||= new
end
new() click to toggle source
# File lib/puppetserver/ca/utils/file_system.rb, line 66
def initialize
  @user, @group = find_user_and_group
end
validate_file_paths(one_or_more_paths) click to toggle source
# File lib/puppetserver/ca/utils/file_system.rb, line 32
def self.validate_file_paths(one_or_more_paths)
  errors = []
  Array(one_or_more_paths).each do |path|
    if !File.exist?(path) || !File.readable?(path)
      errors << "Could not read file '#{path}'"
    end
  end

  errors
end
write_file(*args) click to toggle source
# File lib/puppetserver/ca/utils/file_system.rb, line 22
def self.write_file(*args)
  instance.write_file(*args)
end

Public Instance Methods

ensure_dir(directory) click to toggle source

Warning: directory mode should be specified in DIR_MODES above

# File lib/puppetserver/ca/utils/file_system.rb, line 100
def ensure_dir(directory)
  if !File.exist?(directory)
    FileUtils.mkdir_p(directory, mode: DIR_MODES[directory])
    FileUtils.chown(@user, @group, directory)
  end
end
find_user_and_group() click to toggle source
# File lib/puppetserver/ca/utils/file_system.rb, line 70
def find_user_and_group
  if !running_as_root?
    return Process.euid, Process.egid
  else
    if pe_puppet_exists?
      return 'pe-puppet', 'pe-puppet'
    else
      return 'puppet', 'puppet'
    end
  end
end
pe_puppet_exists?() click to toggle source
# File lib/puppetserver/ca/utils/file_system.rb, line 86
def pe_puppet_exists?
  !!(Etc.getpwnam('pe-puppet') rescue nil)
end
running_as_root?() click to toggle source
# File lib/puppetserver/ca/utils/file_system.rb, line 82
def running_as_root?
  !Gem.win_platform? && Process.euid == 0
end
write_file(path, one_or_more_objects, mode) click to toggle source
# File lib/puppetserver/ca/utils/file_system.rb, line 90
def write_file(path, one_or_more_objects, mode)
  File.open(path, 'w', mode) do |f|
    Array(one_or_more_objects).each do |object|
      f.puts object.to_s
    end
  end
  FileUtils.chown(@user, @group, path)
end