Class: IsItWorking::DirectoryCheck

Inherits:
Object
  • Object
show all
Defined in:
lib/is_it_working/checks/directory_check.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ DirectoryCheck

Check if a file system directory exists and has the correct access. This can be very useful to check if the application relies on a shared file sytem being mounted. The :path options must be supplied to the initializer. You may also supply an :permission option with the values :read, :write, or [:read, :write] to check the permission on the directory as well.

Example

IsItWorking::Handler.new do |h|
  h.check :directory, :path => "/var/shared/myapp", :permission => [:read, :write]
end

Raises:

  • (ArgumentError)


14
15
16
17
18
19
# File 'lib/is_it_working/checks/directory_check.rb', line 14

def initialize (options={})
  raise ArgumentError.new(":path not specified") unless options[:path]
  @path = File.expand_path(options[:path])
  @permission = options[:permission]
  @permission = [@permission] if @permission && !@permission.is_a?(Array)
end

Instance Method Details

#call(status) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/is_it_working/checks/directory_check.rb', line 21

def call(status)
  stat = File.stat(@path) if File.exist?(@path)
  if stat
    if stat.directory?
      if @permission
        if @permission.include?(:read) && !stat.readable?
          status.fail("#{@path} is not readable by #{ENV['USER']}")
        elsif @permission.include?(:write) && !stat.writable?
          status.fail("#{@path} is not writable by #{ENV['USER']}")
        else
          status.ok("#{@path} exists with #{@permission.collect{|a| a.to_s}.join('/')} permission")
        end
      else
        status.ok("#{@path} exists")
      end
    else
      status.fail("#{@path} is not a directory")
    end
  else
    status.fail("#{@path} does not exist")
  end
end