blufin-lib 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,108 @@
1
+ require 'rbconfig'
2
+
3
+ module Blufin
4
+
5
+ class Tools
6
+
7
+ OS_WINDOWS = 'windows'
8
+ OS_MAC = 'mac'
9
+ OS_LINUX = 'linux'
10
+ OS_UNIX = 'unix'
11
+ OS_OTHER = 'other'
12
+
13
+ # Get PATH to assets, scripts, etc.
14
+ # @return String
15
+ def self.get_base_path
16
+ base_path = File.dirname(File.expand_path(__FILE__))
17
+ base_path = base_path.gsub(/\/\w+\/\w+\z/i, '')
18
+ base_path
19
+ end
20
+
21
+ # Check that remote host is reachable.
22
+ # @return void
23
+ def self.check_remote_is_reachable(host_address)
24
+ if ping(host_address) != 0
25
+ 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.'])
26
+ end
27
+ end
28
+
29
+ # Ping a URL or IP and returns the exit status. 0 = success, anything else means it failed.
30
+ # @return Integer
31
+ def self.ping(host_address, verbose = true)
32
+ Tools::Terminal::output("Checking if #{Tools::Terminal::format_highlight(host_address)} is reachable...") if verbose == true
33
+ `ping -t 1 -c 1 #{host_address}`
34
+ $?.exitstatus
35
+ end
36
+
37
+ # Check that SSHPASS is installed.
38
+ # @return void
39
+ def self.check_sshpass_is_installed
40
+ if @sshpass_installed.nil?
41
+ sshpass_result = Tools::Terminal::command_capture('sshpass -h', nil, false, false)
42
+ sshpass_result = sshpass_result[0].split(' ')
43
+ unless sshpass_result[0].downcase == 'usage:'
44
+ if this_is_a_mac
45
+ error_message = "Find how to install it at: #{Tools::Terminal::format_highlight('https://www.google.co.uk/search?q=install+sshpass+on+mac')}"
46
+ else
47
+ error_message = "Install it using: #{Tools::Terminal::format_command('sudo apt-get install sshpass')}"
48
+ end
49
+ Tools::Terminal::error("#{Tools::Terminal::format_highlight('sshpass')} is not installed", error_message, true)
50
+ end
51
+ @sshpass_installed = true
52
+ end
53
+ end
54
+
55
+ # Returns TRUE if Mac, FALSE if Linux (or anything else for that matter)
56
+ # @return boolean
57
+ def self.this_is_a_mac
58
+ return os == OS_MAC
59
+ end
60
+
61
+ # Get the operating system.
62
+ # @return String
63
+ def self.os
64
+ @os ||= (
65
+ host_os = RbConfig::CONFIG['host_os']
66
+ case host_os
67
+ when /mswin|msys|mingw|cygwin|bccwin|wince|emc/
68
+ OS_WINDOWS
69
+ when /darwin|mac os/
70
+ OS_MAC
71
+ when /linux/
72
+ OS_LINUX
73
+ when /solaris|bsd/
74
+ OS_UNIX
75
+ else
76
+ OS_OTHER
77
+ end
78
+ )
79
+ end
80
+
81
+ # Checks if a String or Integer is a whole number,
82
+ # @return boolean
83
+ def self.is_whole_number(value)
84
+ if value =~ /^\s*\d+\s*$/
85
+ true
86
+ elsif value.is_a? Integer
87
+ true
88
+ else
89
+ false
90
+ end
91
+ end
92
+
93
+ # Get the character at a specific index in a string.
94
+ # @return string
95
+ def self.get_char_at(char_at, string)
96
+ if is_whole_number(char_at)
97
+ char_at = char_at.to_i
98
+ char_at = (char_at - 1)
99
+ string[char_at]
100
+ else
101
+ raise(RuntimeError, "The value for CharAt must be a whole number. The script received (#{char_at.class}) #{char_at}.")
102
+ end
103
+ end
104
+
105
+
106
+ end
107
+
108
+ end
@@ -0,0 +1,98 @@
1
+ module Blufin
2
+
3
+ module Test
4
+
5
+ class EnvironmentValidator
6
+
7
+ CHECK_API = 'check-api'
8
+ CHECK_CRON = 'check-cron'
9
+ CHECK_WORKER = 'check-worker'
10
+ CHECK_MYSQL = 'check-mysql'
11
+ CHECK_RABBIT_MQ = 'check-rabbit-mq'
12
+
13
+ def initialize(opts, base_prefix)
14
+
15
+ raise RuntimeError, "Opts must be an array, you passed: #{opts.class}" unless opts.is_a?(Hash)
16
+
17
+ @base_prefix = base_prefix
18
+
19
+ valid_opts = [CHECK_API, CHECK_CRON, CHECK_WORKER, CHECK_MYSQL, CHECK_RABBIT_MQ]
20
+
21
+ opts.keys.each { |opt| raise RuntimeError, "#{opt} is not a valid value" unless valid_opts.include?(opt) }
22
+
23
+ Blufin::Terminal::info('Checking for necessary components...') if opts.length > 0
24
+
25
+ validation_results = []
26
+ validation_results << check_api(opts[CHECK_API]) if opts.keys.include?(CHECK_API)
27
+ validation_results << check_cron(opts[CHECK_CRON]) if opts.keys.include?(CHECK_CRON)
28
+ validation_results << check_worker(opts[CHECK_WORKER]) if opts.keys.include?(CHECK_WORKER)
29
+ validation_results << check_mysql if opts.keys.include?(CHECK_MYSQL)
30
+ validation_results << check_rabbit_mq if opts.keys.include?(CHECK_RABBIT_MQ)
31
+
32
+ Blufin::Terminal::error('Your environment is not setup correctly. Please check the above output.', nil, true) if validation_results.include?(false)
33
+
34
+ puts
35
+
36
+ end
37
+
38
+ private
39
+
40
+ # Checks that the API is running.
41
+ # @return void
42
+ def check_api(uri)
43
+ check_service_common(uri, 'api')
44
+ end
45
+
46
+ # Checks that the WORKER is running.
47
+ # @return void
48
+ def check_cron(uri)
49
+ check_service_common(uri, 'cron')
50
+ end
51
+
52
+ # Checks that the WORKER is running.
53
+ # @return void
54
+ def check_worker(uri)
55
+ check_service_common(uri, 'worker')
56
+ end
57
+
58
+ # Checks that the MySQL is running.
59
+ # @return void
60
+ def check_mysql
61
+ if system('mysqladmin version >& /dev/null')
62
+ Blufin::Terminal::output(Blufin::Terminal::format_highlight('mysql'), Blufin::Terminal::MSG_CUSTOM_AUTO_PAD, 'Pass', 22)
63
+ true
64
+ else
65
+ Blufin::Terminal::output(Blufin::Terminal::format_invalid('mysql'), Blufin::Terminal::MSG_CUSTOM_AUTO_PAD, 'Fail', 196)
66
+ false
67
+ end
68
+ end
69
+
70
+ # Checks that the RabbitMQ is running.
71
+ # @return void
72
+ def check_rabbit_mq
73
+ if system('rabbitmqadmin --help >& /dev/null')
74
+ Blufin::Terminal::output(Blufin::Terminal::format_highlight('rabbit-mq'), Blufin::Terminal::MSG_CUSTOM_AUTO_PAD, 'Pass', 22)
75
+ return true
76
+ else
77
+ Blufin::Terminal::output(Blufin::Terminal::format_invalid('rabbit-mq'), Blufin::Terminal::MSG_CUSTOM_AUTO_PAD, 'Fail', 196)
78
+ end
79
+ false
80
+ end
81
+
82
+ # Common method for checking Java services are running.
83
+ def check_service_common(uri, service_name)
84
+ service_name = "#{@base_prefix}-#{service_name}"
85
+ if system("curl -I #{uri} -k >& /dev/null")
86
+ Blufin::Terminal::output(Blufin::Terminal::format_highlight(service_name), Blufin::Terminal::MSG_CUSTOM_AUTO_PAD, 'Pass', 22)
87
+ return true
88
+ else
89
+ Blufin::Terminal::output(Blufin::Terminal::format_invalid(service_name), Blufin::Terminal::MSG_CUSTOM_AUTO_PAD, 'Fail', 196)
90
+ end
91
+ false
92
+ end
93
+
94
+ end
95
+
96
+ end
97
+
98
+ end
data/lib/version.rb ADDED
@@ -0,0 +1 @@
1
+ BLUFIN_LIB_VERSION = '1.4.0'
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: blufin-lib
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.4.0
5
+ platform: ruby
6
+ authors:
7
+ - Albert Rannetsperger
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-05-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: json
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '='
18
+ - !ruby/object:Gem::Version
19
+ version: 2.1.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '='
25
+ - !ruby/object:Gem::Version
26
+ version: 2.1.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: openssl
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '='
32
+ - !ruby/object:Gem::Version
33
+ version: 2.0.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '='
39
+ - !ruby/object:Gem::Version
40
+ version: 2.0.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: tty-spinner
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '='
46
+ - !ruby/object:Gem::Version
47
+ version: 0.9.0
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '='
53
+ - !ruby/object:Gem::Version
54
+ version: 0.9.0
55
+ description: Common functionality shared between ruby gems.
56
+ email: alb3rtuk@hotmail.com
57
+ executables: []
58
+ extensions: []
59
+ extra_rdoc_files: []
60
+ files:
61
+ - lib/blufin-lib.rb
62
+ - lib/blufin-lib/core/arrays.rb
63
+ - lib/blufin-lib/core/browser.rb
64
+ - lib/blufin-lib/core/datetime_utils.rb
65
+ - lib/blufin-lib/core/encryptor.rb
66
+ - lib/blufin-lib/core/files.rb
67
+ - lib/blufin-lib/core/network.rb
68
+ - lib/blufin-lib/core/numbers.rb
69
+ - lib/blufin-lib/core/routes.rb
70
+ - lib/blufin-lib/core/ssh.rb
71
+ - lib/blufin-lib/core/strings.rb
72
+ - lib/blufin-lib/core/terminal.rb
73
+ - lib/blufin-lib/core/tools.rb
74
+ - lib/blufin-lib/test/TestEnvironmentValidator.rb
75
+ - lib/version.rb
76
+ homepage: https://github.com/alb3rtuk
77
+ licenses:
78
+ - MIT
79
+ metadata: {}
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ requirements: []
95
+ rubyforge_project:
96
+ rubygems_version: 2.6.12
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: Blufin Lib
100
+ test_files: []