Class: Chekku::Definition

Inherits:
Object
  • Object
show all
Defined in:
lib/chekku/definition.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Definition

Returns a new instance of Definition.



32
33
34
35
36
# File 'lib/chekku/definition.rb', line 32

def initialize(attributes = {})
  attributes.each do |name, value|
    send("#{name}=", value)
  end
end

Instance Attribute Details

#executableObject

Returns the value of attribute executable.



18
19
20
# File 'lib/chekku/definition.rb', line 18

def executable
  @executable
end

#nameObject

Returns the value of attribute name.



18
19
20
# File 'lib/chekku/definition.rb', line 18

def name
  @name
end

Class Method Details

.load(definitions_hash = {}) ⇒ Array

Convert a formatted hash to an array of definitions

Parameters:

  • definitions_hash (Hash) (defaults to: {})

    a valid hash of definitions

Returns:

  • (Array)

    an Array of Definition



24
25
26
27
28
29
30
# File 'lib/chekku/definition.rb', line 24

def self.load(definitions_hash = {})
  [].tap do |definitions|
    definitions_hash.each_pair do |name, hash_definition|
      definitions << self.new(hash_definition.merge(name: name))
    end
  end
end

Instance Method Details

#check_version(version) ⇒ Boolean

Verify version

Parameters:

  • version (String)

    current version wanted

Returns:

  • (Boolean)

    good_version?



123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/chekku/definition.rb', line 123

def check_version(version)
  operator, version = version.split(' ')
  if version.nil?
    version = operator
    operator = '=='
  end
  if operator == '~>'
    installed_version >= Gem::Version.new(version) && installed_version <= Gem::Version.new(version).bump
  else
    installed_version.send(operator, Gem::Version.new(version))
  end
end

#chekku(version = nil, args = {}) ⇒ Boolean

Check if version and other params are valid for current Definition instance and return false if errors

Parameters:

  • version (String) (defaults to: nil)

    Version to check in the format ‘>= 4.0’ accepts ‘~> 3.0’

  • args (Hash) (defaults to: {})

    Possible checks that must be verified for current Definition

Returns:

  • (Boolean)

    valid version and args ?



55
56
57
58
59
# File 'lib/chekku/definition.rb', line 55

def chekku(version = nil, args = {})
  chekku!(version, args)
rescue
  false
end

#chekku!(version = nil, args = {}) ⇒ Boolean

Check if version and other params are valid for current Definition instance and raise errors

Parameters:

  • version (String) (defaults to: nil)

    Version to check in the format ‘>= 4.0’ accepts ‘~> 3.0’

  • args (Hash) (defaults to: {})

    Possible checks that must be verified for current Definition

Options Hash (args):

  • must_run (Boolean)

    Check if the software is running on the host

Returns:

  • (Boolean)

    true if valid, if not valid errors are raised

Raises:



47
48
49
50
# File 'lib/chekku/definition.rb', line 47

def chekku!(version = nil, args = {})
  raise(NotInstalledError, "not installed") unless exists?
  validates version, args
end

#exists?Boolean

Verify existence of dependency

Returns:

  • (Boolean)

    corresponding to the existence



64
65
66
# File 'lib/chekku/definition.rb', line 64

def exists?
  verify_executable! && found?
end

#found?Boolean

Actual method which verifies if defined software is present

Returns:

  • (Boolean)

    found?



90
91
92
# File 'lib/chekku/definition.rb', line 90

def found?
  system "which #{executable} > /dev/null 2>&1"
end

#installed_versionString

Get the current installed version on the host

Returns:

  • (String)

    string representation of version



139
140
141
142
143
144
145
146
# File 'lib/chekku/definition.rb', line 139

def installed_version
  version_matches = `#{executable} --version`.scan(/(\d+[\.\d+]*)/).flatten
  max_dot_number_version = ''
  version_matches.each do |version|
    max_dot_number_version = version if version.count('.') > max_dot_number_version.count('.')
  end
  Gem::Version.new(max_dot_number_version)
end

#is_running?Boolean

Verify that current executable is running or not on host

Returns:

  • (Boolean)

    running?



111
112
113
114
115
116
117
# File 'lib/chekku/definition.rb', line 111

def is_running?
  ps_result = `ps aux | grep #{executable}`
  ps_result_array = ps_result.split("\n")
  ps_result_array.any? do |ps_line|
    ps_line.include?(executable) && (executable == 'grep' || !ps_line.include?('grep'))
  end
end

#sane_executable?Boolean

Verify that the executable is harmless for the machine

Returns:

  • (Boolean)

    sane?



97
98
99
# File 'lib/chekku/definition.rb', line 97

def sane_executable?
  executable == sanitized_executable
end

#sanitized_executableString

Get the sanitized name of the executable

Returns:

  • (String)

    sane executable



104
105
106
# File 'lib/chekku/definition.rb', line 104

def sanitized_executable
  executable.gsub /&|"|'|;|\s/, ""
end

#validates(version = nil, args = {}) ⇒ Boolean

Validates the params provided are the one of the local dependency

Parameters:

  • version (String) (defaults to: nil)

    Version to check in the format ‘>= 4.0’ accepts ‘~> 3.0’

  • args (Hash) (defaults to: {})

    Possible checks that must be verified for current Definition

Returns:

  • (Boolean)

    valid?

Raises:



72
73
74
75
76
# File 'lib/chekku/definition.rb', line 72

def validates(version = nil, args = {})
  raise(DefinitionValidationError, "wrong version: wanted #{version}, got #{installed_version}") unless ( !version || check_version(version))
  raise(DefinitionValidationError, "installed but not running (must_run: true)") unless (!args[:must_run] || is_running?)
  true
end

#verify_executable!Boolean

Verify if the executable of the Definition is sane and correct

Returns:

  • (Boolean)

    valid?

Raises:



81
82
83
84
85
# File 'lib/chekku/definition.rb', line 81

def verify_executable!
  raise(AppNameNotStringError, 'You need to use strings for app names') unless executable.is_a?(String)
  raise(AppNameNotSaneError, "Sorry the app name '#{name}' is not sane") unless sane_executable?
  true
end