Class: App::UtilsTools

Inherits:
Object
  • Object
show all
Defined in:
lib/core/utils_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/utils_tools.rb', line 23

def self.check_remote_is_reachable(host_address)
    if ping(host_address) != 0
        App::Terminal::error('Cannot reach remote host', ["#{App::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/utils_tools.rb', line 39

def self.check_sshpass_is_installed
    if @sshpass_installed.nil?
        sshpass_result = App::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: #{App::Terminal::format_highlight('https://www.google.co.uk/search?q=install+sshpass+on+mac')}"
            else
                error_message = "Install it using: #{App::Terminal::format_command('sudo apt-get install sshpass')}"
            end
            App::Terminal::error("#{App::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/utils_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

.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/utils_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

.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/utils_tools.rb', line 31

def self.ping(host_address, verbose = true)
    App::Terminal::output("Checking if #{App::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/utils_tools.rb', line 57

def self.this_is_a_mac
    return os == OS_MAC
end