Class: Blufin::Tools
- Inherits:
-
Object
- Object
- Blufin::Tools
- Defined in:
- lib/core/tools.rb
Constant Summary collapse
- OS_WINDOWS =
'windows'
- OS_MAC =
'mac'
- OS_LINUX =
'linux'
- OS_UNIX =
'unix'
- OS_OTHER =
'other'
Class Method Summary collapse
-
.check_remote_is_reachable(host_address) ⇒ Object
Check that remote host is reachable.
-
.check_sshpass_is_installed ⇒ Object
Check that SSHPASS is installed.
-
.get_base_path ⇒ Object
Get PATH to assets, scripts, etc.
-
.get_char_at(char_at, string) ⇒ Object
Get the character at a specific index in a string.
-
.is_whole_number(value) ⇒ Object
Checks if a String or Integer is a whole number,.
-
.os ⇒ Object
Get the operating system.
-
.os_not_supported(array_of_os) ⇒ Object
Throws uniform error for non-supported os’.
-
.ping(host_address, verbose = true) ⇒ Object
Ping a URL or IP and returns the exit status.
-
.this_is_a_mac ⇒ Object
Returns TRUE if Mac, FALSE if Linux (or anything else for that matter).
-
.value_based_on_os(mac: nil, windows: nil, linux: nil) ⇒ Object
Returns a value based on OS.
Class Method Details
.check_remote_is_reachable(host_address) ⇒ Object
Check that remote host is reachable.
23 24 25 26 27 |
# File 'lib/core/tools.rb', line 23 def self.check_remote_is_reachable(host_address) if ping(host_address) != 0 Tools::Terminal::error('Cannot reach remote host', ["#{Tools::Terminal::format_highlight(host_address)} cannot be reached.", 'Please make sure the host is online and/or configured correctly.']) end end |
.check_sshpass_is_installed ⇒ Object
Check that SSHPASS is installed.
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/core/tools.rb', line 39 def self.check_sshpass_is_installed if @sshpass_installed.nil? sshpass_result = Tools::Terminal::command_capture('sshpass -h', nil, false, false) sshpass_result = sshpass_result[0].split(' ') unless sshpass_result[0].downcase == 'usage:' if this_is_a_mac = "Find how to install it at: #{Tools::Terminal::format_highlight('https://www.google.co.uk/search?q=install+sshpass+on+mac')}" else = "Install it using: #{Tools::Terminal::format_command('sudo apt-get install sshpass')}" end Tools::Terminal::error("#{Tools::Terminal::format_highlight('sshpass')} is not installed", , true) end @sshpass_installed = true end end |
.get_base_path ⇒ Object
Get PATH to assets, scripts, etc.
15 16 17 18 19 |
# File 'lib/core/tools.rb', line 15 def self.get_base_path base_path = File.dirname(File.(__FILE__)) base_path = base_path.gsub(/\/\w+\/\w+\z/i, '') base_path end |
.get_char_at(char_at, string) ⇒ Object
Get the character at a specific index in a string.
95 96 97 98 99 100 101 102 103 |
# File 'lib/core/tools.rb', line 95 def self.get_char_at(char_at, string) if is_whole_number(char_at) char_at = char_at.to_i char_at = (char_at - 1) string[char_at] else raise(RuntimeError, "The value for CharAt must be a whole number. The script received (#{char_at.class}) #{char_at}.") end end |
.is_whole_number(value) ⇒ Object
Checks if a String or Integer is a whole number,
83 84 85 86 87 88 89 90 91 |
# File 'lib/core/tools.rb', line 83 def self.is_whole_number(value) if value =~ /^\s*\d+\s*$/ true elsif value.is_a? Integer true else false end end |
.os ⇒ Object
Get the operating system.
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/core/tools.rb', line 63 def self.os @os ||= ( host_os = RbConfig::CONFIG['host_os'] case host_os when /mswin|msys|mingw|cygwin|bccwin|wince|emc/ OS_WINDOWS when /darwin|mac os/ OS_MAC when /linux/ OS_LINUX when /solaris|bsd/ OS_UNIX else OS_OTHER end ) end |
.os_not_supported(array_of_os) ⇒ Object
Throws uniform error for non-supported os’.
133 134 135 136 137 138 139 140 |
# File 'lib/core/tools.rb', line 133 def self.os_not_supported(array_of_os) raise RuntimeError, "Expected Array, instead got: #{array_of_os.class}" unless array_of_os.is_a?(Array) array_of_os.each { |os| raise RuntimeError, "Unsupported OS value: #{Blufin::Terminal::format_invalid(os)}" unless [OS_MAC, OS_WINDOWS, OS_LINUX, OS_UNIX].include?(os) } current_os = os if array_of_os.include?(current_os) Blufin::Terminal::error("#{Blufin::Terminal::format_highlight(current_os.capitalize)} is not supported to run this command/operation.", 'Someone needs to program it and make it compatible.') end end |
.ping(host_address, verbose = true) ⇒ Object
Ping a URL or IP and returns the exit status. 0 = success, anything else means it failed.
31 32 33 34 35 |
# File 'lib/core/tools.rb', line 31 def self.ping(host_address, verbose = true) Tools::Terminal::output("Checking if #{Tools::Terminal::format_highlight(host_address)} is reachable...") if verbose == true `ping -t 1 -c 1 #{host_address}` $?.exitstatus end |
.this_is_a_mac ⇒ Object
Returns TRUE if Mac, FALSE if Linux (or anything else for that matter)
57 58 59 |
# File 'lib/core/tools.rb', line 57 def self.this_is_a_mac return os == OS_MAC end |
.value_based_on_os(mac: nil, windows: nil, linux: nil) ⇒ Object
Returns a value based on OS. If any of the values are nil, throws an Exception. Linux supports both LINUX and UNIX enums.
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/core/tools.rb', line 108 def self.value_based_on_os(mac: nil, windows: nil, linux: nil) begin case Blufin::Tools::os when Blufin::Tools::OS_WINDOWS raise RuntimeError if windows.nil? return windows when Blufin::Tools::OS_MAC raise RuntimeError if mac.nil? return mac when Blufin::Tools::OS_LINUX raise RuntimeError if linux.nil? return linux when Blufin::Tools::OS_UNIX raise RuntimeError if linux.nil? return linux else raise RuntimeError end rescue => e Blufin::Terminal::error("This command/operation is not yet supported on: #{Blufin::Terminal::format_highlight(Blufin::Tools::os)}", e.split("\n"), true) end end |