Module: CCSH::Utils
- Defined in:
- lib/ccsh/utils.rb
Class Method Summary collapse
-
.clear_console ⇒ Object
clear the system console when run clear.
-
.debug(msg) ⇒ Object
Display a message, if debug mode is enabled.
-
.display_hosts(hosts) ⇒ Object
Pretty print the filtered hosts.
-
.display_hosts_verbosity(hosts) ⇒ Object
Display host display_hosts_verbosity.
-
.exit_console(code, msg = nil) ⇒ Object
Exit the console message.
-
.get_options(item, object, default) ⇒ Object
Given an item, hash and wanted default return, return the item if exists on array object or return the default value if the key doesn’t exist.
-
.handle_signals ⇒ Object
Handle terminal signals.
-
.merge_defaults(defaults) ⇒ Object
Responsible to merge the defaults settings with the each hosts parameters.
-
.reset_console ⇒ Object
reset the console.
-
.show_groups(hosts) ⇒ Object
Show at least 10 groups names.
-
.valid_ssh(options) ⇒ Object
validate the ssh connection before start the ccsh console.
-
.verbose(msg) ⇒ Object
Verbosely display a message, if verbose mode is enabled.
Class Method Details
.clear_console ⇒ Object
clear the system console when run clear
120 121 122 |
# File 'lib/ccsh/utils.rb', line 120 def self.clear_console printf "\e[H\e[2J" end |
.debug(msg) ⇒ Object
Display a message, if debug mode is enabled
87 88 89 |
# File 'lib/ccsh/utils.rb', line 87 def self.debug(msg) puts "[DEBUG] #{msg}" if ENV['CCSH_DEBUG'] == "true" end |
.display_hosts(hosts) ⇒ Object
Pretty print the filtered hosts
45 46 47 48 49 50 51 |
# File 'lib/ccsh/utils.rb', line 45 def self.display_hosts(hosts) puts "Selected Hosts (option: --show-hosts):" hosts.each do |host| puts "+ Server #{host.name}: #{host.user}@#{host.hostname} => Groups: #{host.groups}" end puts end |
.display_hosts_verbosity(hosts) ⇒ Object
Display host display_hosts_verbosity
55 56 57 58 59 60 |
# File 'lib/ccsh/utils.rb', line 55 def self.display_hosts_verbosity(hosts) hosts.each do |host| CCSH::Utils::verbose("sudo mode enable to host: #{host.hostname}") if host.sudo_enabled end puts end |
.exit_console(code, msg = nil) ⇒ Object
Exit the console message
111 112 113 114 115 116 |
# File 'lib/ccsh/utils.rb', line 111 def self.exit_console(code, msg = nil) puts msg if msg != nil puts "\nBye..." exit code || 0 end |
.get_options(item, object, default) ⇒ Object
Given an item, hash and wanted default return, return the item if exists on array object or return the default value if the key doesn’t exist.
35 36 37 38 39 40 41 |
# File 'lib/ccsh/utils.rb', line 35 def self.(item, object, default) if object.key? item return object[item] end return default end |
.handle_signals ⇒ Object
Handle terminal signals
99 100 101 102 103 104 105 106 107 |
# File 'lib/ccsh/utils.rb', line 99 def self.handle_signals Signal.trap('INT') do self.exit_console 0 end Signal.trap('TERM') do self.exit_console 0 end end |
.merge_defaults(defaults) ⇒ Object
Responsible to merge the defaults settings with the each hosts parameters.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/ccsh/utils.rb', line 7 def self.merge_defaults(defaults) defaultsValues = { 'user' => 'root', 'port' => '22', 'sudo_enabled' => false, 'sudo_password' => nil, 'private_key' => '~/.ssh/id_rsa', } defaultsValues.merge!(defaults) if defaults != nil = { 'timeout' => 720, 'ssh-rsa' => 'ssh-rsa', } if defaults['ssh_options'] != nil defaultsValues['ssh_options'] = .merge!(defaults['ssh_options']) else defaultsValues['ssh_options'] = end return defaultsValues end |
.reset_console ⇒ Object
reset the console
126 127 128 |
# File 'lib/ccsh/utils.rb', line 126 def self.reset_console printf("\033c"); end |
.show_groups(hosts) ⇒ Object
Show at least 10 groups names
64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/ccsh/utils.rb', line 64 def self.show_groups(hosts) hosts_groups = [] hosts.each do |host| if host.groups != nil host.groups.each do |gname| hosts_groups.push(gname) unless hosts_groups.include?(gname) end end end hosts_groups.slice(0, 10).each { |gname| puts " - #{gname}" } puts "Example: ccsh #{hosts_groups[0]}" unless hosts_groups[0] == nil puts "Example: ccsh #{hosts_groups.slice(0,2).join(' ')}" unless hosts_groups.slice(0,2) == nil end |
.valid_ssh(options) ⇒ Object
validate the ssh connection before start the ccsh console
81 82 83 |
# File 'lib/ccsh/utils.rb', line 81 def self.valid_ssh() return true end |
.verbose(msg) ⇒ Object
Verbosely display a message, if verbose mode is enabled
93 94 95 |
# File 'lib/ccsh/utils.rb', line 93 def self.verbose(msg) puts "[verbose] #{msg}" if ENV['CCSH_VERBOSE'] == "true" end |