ccsh 0.0.3 → 0.0.4

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e18fe57923e83f6f801b3c17cffab2d0928df142
4
- data.tar.gz: 8fa3fb86f5626fa09a4f657379626bcc1e4c4d72
3
+ metadata.gz: '0061963e9d2650efa04cdc165c330639b30198b7'
4
+ data.tar.gz: 370fe5f65c0948e3887ef91b6d654bb5a6513b89
5
5
  SHA512:
6
- metadata.gz: a5b6525d2f5d690e88d2cb9322a42400d5909e625d797d24b7a7c64bf31607c7026bc34de478936bae625ec89dee37b936b8d1075e35b17ab21b5f8c9f72fede
7
- data.tar.gz: f40ee7dcf33c3071a3ff83c5b105ac66ae3b07cfc2d79109aecc96d864331d4dd389daa932c74faecb90b5a21440989b603f6e004cb169c44b4c0a779c947b10
6
+ metadata.gz: f19431222c8aabc93a1742bc7e4e671177938c388283cabf7fe312878da30a13a1d78ed076b06065a3f5567f5c7feff55c21ad3bf4b25d74f6c5c5ca27ab3311
7
+ data.tar.gz: c39dfa8bf6dac2f3f4986e05c3efcf8932ec7e71aad20e9fc220fe6c88468e1fc7ccfa222bc41ed55a53caa8c28c20b9197ba045b11b3f361521632f4fa461c4
@@ -9,20 +9,36 @@ module CCSH
9
9
  require 'ccsh/ssh.rb'
10
10
 
11
11
  def self.execute!
12
- options = CCSH::Options.parse_options ARGV
12
+ begin
13
+ options = CCSH::Options.parse_options ARGV
13
14
 
14
- ENV['CCSH_DEBUG'] = "true" if options[:debug]
15
- ENV['CCSH_VERBOSE'] = "true" if options[:verbose]
15
+ ENV['CCSH_DEBUG'] = "true" if options[:debug]
16
+ ENV['CCSH_VERBOSE'] = "true" if options[:verbose]
16
17
 
17
- raise "You must to specified a hostname or group" if options[:targets].empty?
18
+ raise "You must to specified a hostname or group" if options[:targets].empty?
18
19
 
19
- filename = options[:hosts]
20
- targets = options[:targets]
20
+ filename = options[:hosts]
21
+ targets = options[:targets]
21
22
 
22
- self.start_cli CCSH::Hosts.new.parser!(filename).filter_by(targets)
23
+ hosts = CCSH::Hosts.new.parser!(filename).filter_by(targets)
24
+ self.start_cli(hosts, options)
25
+
26
+ rescue Exception => e
27
+ if not e.message == 'exit'
28
+ CCSH::Utils.debug "Backtrace:\n\t#{e.backtrace.join("\n\t")}\n\n"
29
+ CCSH::Utils.verbose "Backtrace:\n\t#{e.backtrace.join("\n\t")}\n\n"
30
+
31
+ puts "An error occur and system exit with the following message: "
32
+ puts " #{e.message}"
33
+
34
+ exit!
35
+ end
36
+
37
+ exit
38
+ end
23
39
  end
24
40
 
25
- def self.with_info
41
+ def self.with_info(options)
26
42
  cmd_start = Time.now
27
43
  cmd = yield
28
44
  cmd_end = Time.now
@@ -32,13 +48,32 @@ module CCSH
32
48
  'time' => cmd_end - cmd_start,
33
49
  }.inspect
34
50
 
35
- puts ">>> #{cmd.hostname} #{info}"
51
+ prompt = ">>> #{cmd.hostname} #{info}"
52
+
53
+ puts prompt
36
54
  STDERR.puts cmd.stderr if cmd.stderr != nil && cmd.stderr != ''
37
55
  puts cmd.stdout if cmd.stdout != nil && cmd.stdout != ''
38
56
  puts
57
+
58
+ if options[:output] != nil
59
+ begin
60
+ file_handler = File.new(options[:output], "a+")
61
+
62
+ file_handler.write("#{prompt}\n")
63
+ file_handler.write("ERROR: #{cmd.stderr}") if cmd.stderr != nil && cmd.stderr != ''
64
+ file_handler.write("#{cmd.stdout}\n") if cmd.stdout != nil && cmd.stdout != ''
65
+ rescue Exception => e
66
+ puts "WARNING: An error occur when trying to write the output to file: #{options[:output]}. "
67
+ puts "WARNING: Thefore, the output is not being stored"
68
+ puts "WARNING: Error message: #{e.message}"
69
+ end
70
+ end
39
71
  end
40
72
 
41
- def self.start_cli(hosts)
73
+
74
+ def self.start_cli(hosts, options)
75
+ CCSH::Utils.display_hosts(hosts) if options[:show_hosts]
76
+
42
77
  quit = false
43
78
  loop do
44
79
  begin
@@ -55,17 +90,26 @@ module CCSH
55
90
  elsif command == 'reset'
56
91
  CCSH::Utils.reset_console
57
92
  else
58
- threads = []
59
- hosts.each do |host|
60
- threads << Thread.new do
93
+ if ((options.max_threads == 0) || (options.max_threads > hosts.length))
94
+ options.max_threads = hosts.length
95
+ end
61
96
 
62
- with_info do
63
- host.run command
97
+ CCSH::Utils.debug "Using #{options.max_threads} maximum of threads"
98
+
99
+ hosts.each_slice(options.max_threads) do |batch_hosts|
100
+ threads = []
101
+
102
+ batch_hosts.each do |host|
103
+ threads << Thread.new do
104
+
105
+ info = with_info(options) do
106
+ host.run command
107
+ end
64
108
  end
65
109
  end
66
- end
67
110
 
68
- threads.each(&:join)
111
+ threads.each(&:join)
112
+ end
69
113
  end
70
114
  end
71
115
  rescue Exception => exception
@@ -1,5 +1,5 @@
1
1
  module CCSH
2
- class Host
2
+ class Host
3
3
  attr_accessor :name
4
4
  attr_accessor :port
5
5
  attr_accessor :user
@@ -22,7 +22,7 @@ module CCSH
22
22
  end
23
23
 
24
24
  def run(command)
25
- CCSH::Utils.verbose("Running command '#{@command}' on #{@hostname}")
25
+ CCSH::Utils.debug("Running command '#{command}' on #{@hostname}")
26
26
  command = CCSH::SSH.start do |ssh|
27
27
  ssh.user = @user
28
28
  ssh.port = @port
@@ -42,4 +42,4 @@ module CCSH
42
42
  return @groups.include? target
43
43
  end
44
44
  end
45
- end
45
+ end
@@ -9,12 +9,15 @@ module CCSH
9
9
  # default values
10
10
  user_home = File.expand_path('~')
11
11
 
12
- options.config = "#{user_home}/.ccsh/config.yaml"
13
- options.hosts = "#{user_home}/.ccsh/hosts.yaml"
14
- options.output = ""
15
- options.debug = false
16
- options.verbose = false
17
- options.check = false
12
+ options.config = "#{user_home}/.ccsh/config.yaml"
13
+ options.hosts = "#{user_home}/.ccsh/hosts.yaml"
14
+ options.output = ""
15
+ options.debug = false
16
+ options.verbose = false
17
+ options.check = false
18
+ options.output = nil
19
+ options.show_hosts = false
20
+ options.max_threads = 0
18
21
 
19
22
  # open parser
20
23
  opt_parser = OptionParser.new do |opts|
@@ -27,6 +30,7 @@ module CCSH
27
30
  end
28
31
 
29
32
  opts.on("-d", "--[no-]debug", "Run Debug mode") do |d|
33
+ options.verbose = d
30
34
  options.debug = d
31
35
  end
32
36
 
@@ -38,10 +42,38 @@ module CCSH
38
42
  options.check = k
39
43
  end
40
44
 
45
+ opts.on("-o", "--output FILEPATH", "Write all the interaction into an output file") do |out|
46
+ options.output = out
47
+ end
48
+
41
49
  opts.on("-v", "--[no-]verbose", "Run verbosely") do |v|
50
+ options.show_hosts = v
42
51
  options.verbose = v
43
52
  end
44
53
 
54
+ opts.on("--show-hosts", "Output the filtered hosts") do |show_hosts|
55
+ options.show_hosts = show_hosts
56
+ end
57
+
58
+ opts.on("-t", "--max-threads MAX_THREADS",
59
+ "Define the maxinum number of threads (by default: the ammount of selected hosts)") do |max_threads|
60
+
61
+ begin
62
+ if not max_threads =~ /\d+/
63
+ raise "The value '#{max_threads}' is not a valid value to max_threads options"
64
+ end
65
+
66
+ max_threads = max_threads.to_i
67
+ if max_threads < 0
68
+ raise "max_threads value should be a positive number instead of '#{max_threads}'"
69
+ end
70
+
71
+ options.max_threads = max_threads
72
+ rescue Exception => e
73
+ raise e.message
74
+ end
75
+ end
76
+
45
77
  opts.on("--version", "Display version") do |v|
46
78
  build = "build #{CCSH::BUILD_NUMBER}" if CCSH::BUILD_NUMBER != nil
47
79
  puts "CCSH version #{CCSH::VERSION} #{build}"
@@ -1,5 +1,9 @@
1
1
  module CCSH
2
2
  module Utils
3
+
4
+ ##
5
+ # Responsible to merge the defaults settings with the each hosts
6
+ # parameters.
3
7
  def self.merge_defaults(defaults)
4
8
  defaultsValues = {
5
9
  'user' => 'root',
@@ -22,6 +26,10 @@ module CCSH
22
26
  return defaultsValues
23
27
  end
24
28
 
29
+ ##
30
+ # Given an item, hash and wanted default return, return the
31
+ # item if exists on array object or return the default
32
+ # value if the key doesn't exist.
25
33
  def self.get_options(item, object, default)
26
34
  if object.key? item
27
35
  return object[item]
@@ -30,18 +38,37 @@ module CCSH
30
38
  return default
31
39
  end
32
40
 
41
+ ##
42
+ # Pretty print the filtered hosts
43
+ def self.display_hosts(hosts)
44
+ puts "Selected Hosts (option: --show-hosts):"
45
+ hosts.each do |host|
46
+ puts "+ Server #{host.name}: #{host.user}@#{host.hostname} => Groups: #{host.groups}"
47
+ end
48
+
49
+ puts
50
+ end
51
+
52
+ ##
53
+ #
33
54
  def self.valid_ssh(options)
34
55
  return true
35
56
  end
36
57
 
58
+ ##
59
+ # Display a message, if debug mode is enabled
37
60
  def self.debug(msg)
38
- puts "DEBUG", msg if ENV['CCSH_DEBUG'] == "true"
61
+ puts "[DEBUG] #{msg}" if ENV['CCSH_DEBUG'] == "true"
39
62
  end
40
63
 
64
+ ##
65
+ # Verbosely display a message, if verbose mode is enabled
41
66
  def self.verbose(msg)
42
67
  puts msg if ENV['CCSH_VERBOSE'] == "true"
43
68
  end
44
69
 
70
+ ##
71
+ # Handle terminal signals
45
72
  def self.handle_signals
46
73
  Signal.trap('INT') do
47
74
  self.exit_console 0
@@ -52,6 +79,8 @@ module CCSH
52
79
  end
53
80
  end
54
81
 
82
+ ##
83
+ # Exit the console message
55
84
  def self.exit_console(code, msg = nil)
56
85
  puts msg if msg != nil
57
86
 
@@ -59,11 +88,15 @@ module CCSH
59
88
  exit code || 0
60
89
  end
61
90
 
91
+ ##
92
+ # clear the system console when run clear
62
93
  def self.clear_console
63
94
  printf "\e[H\e[2J"
64
95
  end
65
96
 
66
- def self.clear_console
97
+ ##
98
+ # reset the console
99
+ def self.reset_console
67
100
  printf("\033c");
68
101
  end
69
102
  end
@@ -1,7 +1,7 @@
1
1
  module CCSH
2
2
 
3
3
  # ccsh relased version
4
- VERSION = "0.0.3"
4
+ VERSION = "0.0.4"
5
5
 
6
6
  # the git commit tag number that generate this build
7
7
  BUILD_NUMBER = "dev-#{VERSION}"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ccsh
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
- - Rafael Silva <raffs>
7
+ - Rafael Silva - raffs
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-11-03 00:00:00.000000000 Z
11
+ date: 2018-01-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: net-ssh