Class: Blufin::Tools

Inherits:
Object
  • Object
show all
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

Class Method Details

.check_remote_is_reachable(host_address) ⇒ Object

Check that remote host is reachable.

Returns:

  • void



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_installedObject

Check that SSHPASS is installed.

Returns:

  • void



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
                error_message = "Find how to install it at: #{Tools::Terminal::format_highlight('https://www.google.co.uk/search?q=install+sshpass+on+mac')}"
            else
                error_message = "Install it using: #{Tools::Terminal::format_command('sudo apt-get install sshpass')}"
            end
            Tools::Terminal::error("#{Tools::Terminal::format_highlight('sshpass')} is not installed", error_message, true)
        end
        @sshpass_installed = true
    end
end

.get_base_pathObject

Get PATH to assets, scripts, etc.

Returns:

  • String



15
16
17
18
19
# File 'lib/core/tools.rb', line 15

def self.get_base_path
    base_path = File.dirname(File.expand_path(__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.

Returns:

  • 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,

Returns:

  • boolean



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

.osObject

Get the operating system.

Returns:

  • String



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’.

Returns:

  • void

Raises:

  • (RuntimeError)


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.

Returns:

  • Integer



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_macObject

Returns TRUE if Mac, FALSE if Linux (or anything else for that matter)

Returns:

  • boolean



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.

Returns:

  • string



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