Module: CCSH

Defined in:
lib/ccsh.rb,
lib/ccsh/ssh.rb,
lib/ccsh/host.rb,
lib/ccsh/hosts.rb,
lib/ccsh/utils.rb,
lib/ccsh/options.rb,
lib/ccsh/version.rb

Defined Under Namespace

Modules: Options, SSH, Utils Classes: Host, Hosts

Constant Summary collapse

VERSION =
"0.0.5"

Class Method Summary collapse

Class Method Details

.execute!Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/ccsh.rb', line 11

def self.execute!
    begin
        options = CCSH::Options.parse_options ARGV

        ENV['CCSH_DEBUG']   = "true" if options[:debug]
        ENV['CCSH_VERBOSE'] = "true" if options[:verbose]

        if options[:targets].empty?
            error_msg = "You must specify a target hostname or group name."

            puts error_msg
            puts "Example:"
            puts "    'ccsh databases' - select all servers from the database group"
            puts "    'ccsh all'       - select all defined servers"
            puts "Please run 'ccsh --help' for details"
            puts ""

            raise error_msg
        end

        filename = options[:hosts]
        targets = options[:targets]
        hosts = CCSH::Hosts.new.parser!(filename).filter_by(targets)

        # validate and display pretty message if no hosts was found to the
        # given target.
        if hosts.empty?
            error_msg = "Could not found any hosts that matches the given target(s): '#{targets.join(', ')}'"
            puts error_msg

            puts "Here are some groups found on the file: '#{filename}'"
            CCSH::Utils.show_groups(CCSH::Hosts.new.parser!(filename).filter_by('all'))

            puts ""
            raise error_msg
        end

        self.start_cli(hosts, options)

    rescue Exception => e
        if not e.message == 'exit'
            CCSH::Utils.debug "Backtrace:\n\t#{e.backtrace.join("\n\t")}\n\n"
            CCSH::Utils.verbose "Backtrace:\n\t#{e.backtrace.join("\n\t")}\n\n"

            STDERR.puts "An error occur and system exit with the following message: "
            STDERR.puts "  #{e.message.gsub("\n", ' ').squeeze(' ')}"

            exit!
        end

        exit
    end
end

.start_cli(hosts, options) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/ccsh.rb', line 98

def self.start_cli(hosts, options)
    CCSH::Utils.display_hosts(hosts) if options[:show_hosts]
    CCSH::Utils.display_hosts_verbosity(hosts) if options[:verbose]

    quit = false
    loop do
        begin
            CCSH::Utils.handle_signals
            printf "ccsh> "
            command = STDIN.gets.chomp

            if command != ''
                CCSH::Utils.exit_console 0 if command == 'quit'
                CCSH::Utils.exit_console 0 if command == 'exit'

                if command == 'clear'
                    CCSH::Utils.clear_console
                elsif command == 'reset'
                    CCSH::Utils.reset_console
                else
                    if ((options.max_threads == 0) || (options.max_threads > hosts.length))
                        options.max_threads = hosts.length
                    end

                    CCSH::Utils.debug "Using #{options.max_threads} maximum of threads"

                    hosts.each_slice(options.max_threads) do |batch_hosts|
                        threads = []

                        batch_hosts.each do |host|
                            if command =~ /^([ ]*)sudo/
                                if host.sudo_enabled != true
                                    STDERR.puts "WARN: You cannot run sudo on the host #{host.hostname}, please enable sudo mode for this hosts"
                                    next
                                end
                            end

                            threads << Thread.new do
                                info = with_info(options) do
                                    host.run command
                                end
                            end
                        end

                        threads.each(&:join)
                    end
                end
            end
        rescue Exception => exception
            STDERR.puts exception.message
            STDERR.puts
            raise exception
        end
    end
end

.with_info(options) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/ccsh.rb', line 65

def self.with_info(options)
    cmd_start = Time.now
    cmd = yield
    cmd_end = Time.now

    info = {
        'rc'   => cmd.return_code,
        'time' => cmd_end - cmd_start,
    }.inspect

    prompt = ">>> #{cmd.hostname} #{info}"

    puts prompt
    STDERR.puts cmd.stderr if cmd.stderr != nil && cmd.stderr != ''
    puts cmd.stdout if cmd.stdout != nil && cmd.stdout != ''
    puts

    if options[:output] != nil
        begin
            file_handler = File.new(options[:output], "a+")

            file_handler.write("#{prompt}\n")
            file_handler.write("ERROR: #{cmd.stderr}") if cmd.stderr != nil && cmd.stderr != ''
            file_handler.write("#{cmd.stdout}\n") if cmd.stdout != nil && cmd.stdout != ''
        rescue Exception => e
            puts "WARNING: An error occur when trying to write the output to file: #{options[:output]}. "
            puts "WARNING: Thefore, the output is not being stored"
            puts "WARNING: Error message: #{e.message}"
        end
    end
end