Class: Carioca::Services::Sanitycheck

Inherits:
Object
  • Object
show all
Defined in:
lib/carioca/services/sanitycheck.rb

Overview

Exiter namespace

Verifiers for application : FS and TCP/IP services collapse

Instance Method Summary collapse

Constructor Details

#initializeSanitycheck

Returns a new instance of Sanitycheck.



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/carioca/services/sanitycheck.rb', line 8

def initialize
  registry = Carioca::Registry.get
  @output = registry.get_service name: :output
  @i18n = registry.get_service name: :i18n
  @toolbox = registry.get_service name: :toolbox
  @configuration = registry.get_service name: :configuration
  @finisher = registry.get_service name: :finisher
  @schema = {}
  if @configuration.settings.include? :sanitycheck
    @schema = @configuration.settings.sanitycheck.include?(:rules) ? @configuration.settings.sanitycheck.rules : {}
  end
end

Instance Method Details

#runObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/carioca/services/sanitycheck.rb', line 21

def run
  unless @schema.empty?
    begin
      @output.info @i18n.t('sanitycheck.run.start')
      error_number = 0
      @schema.each do |item|
        testcase = item[:test]
        item.delete(:test)
        res = send(testcase, **item)
        if  res.empty?
          @output.ok @i18n.t('sanitycheck.run.ok', testcase:, name: item[:name].to_s)
        else
          pbm = res.map(&:to_s).join(',')
          @output.ko @i18n.t('sanitycheck.run.ko', testcase:, name: item[:name].to_s, pbm:)
          error_number = + 1
        end
      end
      if error_number.positive?
        @output.error @i18n.t('sanitycheck.failure')
      else
        @output.success @i18n.t('sanitycheck.success')
      end
    rescue StandardError
      @finisher.secure_raise message: @i18n.t('sanitychek.error'), error_case: :status_ko
    end
  end
end

#verify_file(name:, mode: '644', owner: nil, group: nil) ⇒ Array

check file

Parameters:

  • [String] (Hash)

    a customizable set of options

Returns:

  • (Array)

    of Symbol with error type : [:inexistant,:mode,:owner,:group]



88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/carioca/services/sanitycheck.rb', line 88

def verify_file(name:, mode: '644', owner: nil, group: nil)
  full_name = File.expand_path(name)
  res = []
  return [:inexistant] unless File.file?(full_name)

  stat = File.stat(full_name)
  if mode
    tested_mode = format('%o', stat.mode)
    res << :mode if tested_mode[-3..] != mode
  end
  res << :owner if owner && (Etc.getpwuid(stat.uid).name != owner)
  res << :group if group && (Etc.getgrgid(stat.gid).name != group)
  res
end

#verify_folder(name:, mode: '755', owner: nil, group: nil) ⇒ Array

check folder

Parameters:

  • [String] (Hash)

    a customizable set of options

Returns:

  • (Array)

    of Symbol with error type : [:inexistant,:mode,:owner,:group]



57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/carioca/services/sanitycheck.rb', line 57

def verify_folder(name:, mode: '755', owner: nil, group: nil)
  full_name = File.expand_path(name)
  res = []
  return [:inexistant] unless File.directory?(full_name)

  stat = File.stat(full_name)
  if mode
    tested_mode = format('%o', stat.mode)
    res << :mode if tested_mode[-3..] != mode
  end
  res << :owner if owner && (Etc.getpwuid(stat.uid).name != owner)
  res << :group if group && (Etc.getgrgid(stat.gid).name != group)
  res
end

check symlink

Parameters:

  • [String] (Hash)

    a customizable set of options

Returns:

  • (Boolean)


75
76
77
78
79
80
# File 'lib/carioca/services/sanitycheck.rb', line 75

def verify_link(name:)
  full_name = File.expand_path(name)
  res = []
  res.push :inexistant unless File.file?(full_name)
  res
end

#verify_service(url: nil, host: nil, port: nil) ⇒ Bool

TCP/IP service checker

Parameters:

  • [String] (Hash)

    a customizable set of options

Returns:

  • (Bool)

    status



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/carioca/services/sanitycheck.rb', line 108

def verify_service(url: nil, host: nil, port: nil)
  if url
    uri = URI.parse(url)
    host = uri.host
    port = uri.port
  end
  Timeout.timeout(1) do
    s = TCPSocket.new(host, port)
    s.close
    return true
  rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH
    return false
  end
rescue Timeout::Error
  false
end