Class: Puppetserver::Ca::Utils::FileSystem
- Inherits:
-
Object
- Object
- Puppetserver::Ca::Utils::FileSystem
- Defined in:
- lib/puppetserver/ca/utils/file_system.rb
Constant Summary collapse
- DIR_MODES =
{ :ssldir => 0771, :cadir => 0755, :certdir => 0755, :privatekeydir => 0750, :publickeydir => 0755, :signeddir => 0755 }
Class Method Summary collapse
- .check_for_existing_files(one_or_more_paths) ⇒ Object
- .ensure_dirs(one_or_more_dirs) ⇒ Object
- .forcibly_symlink(source, link_target) ⇒ Object
- .instance ⇒ Object
- .validate_file_paths(one_or_more_paths) ⇒ Object
- .write_file(*args) ⇒ Object
Instance Method Summary collapse
-
#ensure_dir(directory) ⇒ Object
Warning: directory mode should be specified in DIR_MODES above.
- #find_user_and_group ⇒ Object
-
#initialize ⇒ FileSystem
constructor
A new instance of FileSystem.
- #pe_puppet_exists? ⇒ Boolean
- #running_as_root? ⇒ Boolean
- #write_file(path, one_or_more_objects, mode) ⇒ Object
Constructor Details
#initialize ⇒ FileSystem
Returns a new instance of FileSystem.
58 59 60 |
# File 'lib/puppetserver/ca/utils/file_system.rb', line 58 def initialize @user, @group = find_user_and_group end |
Class Method Details
.check_for_existing_files(one_or_more_paths) ⇒ Object
43 44 45 46 47 48 49 50 51 |
# 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) ⇒ Object
26 27 28 29 30 |
# 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 |
.forcibly_symlink(source, link_target) ⇒ Object
53 54 55 56 |
# File 'lib/puppetserver/ca/utils/file_system.rb', line 53 def self.forcibly_symlink(source, link_target) FileUtils.remove_dir(link_target, true) FileUtils.symlink(source, link_target) end |
.instance ⇒ Object
18 19 20 |
# File 'lib/puppetserver/ca/utils/file_system.rb', line 18 def self.instance @instance ||= new end |
.validate_file_paths(one_or_more_paths) ⇒ Object
32 33 34 35 36 37 38 39 40 41 |
# 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) ⇒ Object
22 23 24 |
# File 'lib/puppetserver/ca/utils/file_system.rb', line 22 def self.write_file(*args) instance.write_file(*args) end |
Instance Method Details
#ensure_dir(directory) ⇒ Object
Warning: directory mode should be specified in DIR_MODES above
92 93 94 95 96 97 |
# File 'lib/puppetserver/ca/utils/file_system.rb', line 92 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 ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/puppetserver/ca/utils/file_system.rb', line 62 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? ⇒ Boolean
78 79 80 |
# File 'lib/puppetserver/ca/utils/file_system.rb', line 78 def pe_puppet_exists? !!(Etc.getpwnam('pe-puppet') rescue nil) end |
#running_as_root? ⇒ Boolean
74 75 76 |
# File 'lib/puppetserver/ca/utils/file_system.rb', line 74 def running_as_root? !Gem.win_platform? && Process.euid == 0 end |
#write_file(path, one_or_more_objects, mode) ⇒ Object
82 83 84 85 86 87 88 89 |
# File 'lib/puppetserver/ca/utils/file_system.rb', line 82 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 |