Class: Chekku::Definition
- Inherits:
-
Object
- Object
- Chekku::Definition
- Defined in:
- lib/chekku/definition.rb
Instance Attribute Summary collapse
-
#executable ⇒ Object
Returns the value of attribute executable.
-
#name ⇒ Object
Returns the value of attribute name.
Class Method Summary collapse
-
.load(definitions_hash = {}) ⇒ Array
Convert a formatted hash to an array of definitions.
Instance Method Summary collapse
-
#check_version(version) ⇒ Boolean
Verify version.
-
#chekku(version = nil, args = {}) ⇒ Boolean
Check if version and other params are valid for current Definition instance and return false if errors.
-
#chekku!(version = nil, args = {}) ⇒ Boolean
Check if version and other params are valid for current Definition instance and raise errors.
-
#exists? ⇒ Boolean
Verify existence of dependency.
-
#found? ⇒ Boolean
Actual method which verifies if defined software is present.
-
#initialize(attributes = {}) ⇒ Definition
constructor
A new instance of Definition.
-
#installed_version ⇒ String
Get the current installed version on the host.
-
#is_running? ⇒ Boolean
Verify that current executable is running or not on host.
-
#sane_executable? ⇒ Boolean
Verify that the executable is harmless for the machine.
-
#sanitized_executable ⇒ String
Get the sanitized name of the executable.
-
#validates(version = nil, args = {}) ⇒ Boolean
Validates the params provided are the one of the local dependency.
-
#verify_executable! ⇒ Boolean
Verify if the executable of the Definition is sane and correct.
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
#executable ⇒ Object
Returns the value of attribute executable.
18 19 20 |
# File 'lib/chekku/definition.rb', line 18 def executable @executable end |
#name ⇒ Object
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
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
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
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
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
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
90 91 92 |
# File 'lib/chekku/definition.rb', line 90 def found? system "which #{executable} > /dev/null 2>&1" end |
#installed_version ⇒ String
Get the current installed version on the host
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
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
97 98 99 |
# File 'lib/chekku/definition.rb', line 97 def sane_executable? executable == sanitized_executable end |
#sanitized_executable ⇒ String
Get the sanitized name of the 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
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
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 |