Class: CCSH::SSH::RemoteCommand

Inherits:
Object
  • Object
show all
Defined in:
lib/ccsh/ssh.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRemoteCommand

Returns a new instance of RemoteCommand.



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/ccsh/ssh.rb', line 32

def initialize
    @command  = nil
    @hostname = nil
    @options  = []
    @stdout = ''
    @stderr = ''

    @enable_sudo      = false
    @sudo_user        = 'root'
    @sudo_password    = nil
end

Instance Attribute Details

#commandObject

Returns the value of attribute command.



16
17
18
# File 'lib/ccsh/ssh.rb', line 16

def command
  @command
end

#enable_sudoObject

Returns the value of attribute enable_sudo.



24
25
26
# File 'lib/ccsh/ssh.rb', line 24

def enable_sudo
  @enable_sudo
end

#hostnameObject

Returns the value of attribute hostname.



17
18
19
# File 'lib/ccsh/ssh.rb', line 17

def hostname
  @hostname
end

#optionsObject

Returns the value of attribute options.



18
19
20
# File 'lib/ccsh/ssh.rb', line 18

def options
  @options
end

#passwordObject

Returns the value of attribute password.



21
22
23
# File 'lib/ccsh/ssh.rb', line 21

def password
  @password
end

#portObject

Returns the value of attribute port.



20
21
22
# File 'lib/ccsh/ssh.rb', line 20

def port
  @port
end

#private_keyObject

Returns the value of attribute private_key.



22
23
24
# File 'lib/ccsh/ssh.rb', line 22

def private_key
  @private_key
end

#return_codeObject

Returns the value of attribute return_code.



29
30
31
# File 'lib/ccsh/ssh.rb', line 29

def return_code
  @return_code
end

#return_signalObject

Returns the value of attribute return_signal.



30
31
32
# File 'lib/ccsh/ssh.rb', line 30

def return_signal
  @return_signal
end

#stderrObject

Returns the value of attribute stderr.



28
29
30
# File 'lib/ccsh/ssh.rb', line 28

def stderr
  @stderr
end

#stdoutObject

Returns the value of attribute stdout.



27
28
29
# File 'lib/ccsh/ssh.rb', line 27

def stdout
  @stdout
end

#sudo_passwordObject

Returns the value of attribute sudo_password.



25
26
27
# File 'lib/ccsh/ssh.rb', line 25

def sudo_password
  @sudo_password
end

#userObject

Returns the value of attribute user.



19
20
21
# File 'lib/ccsh/ssh.rb', line 19

def user
  @user
end

Instance Method Details

#execute!Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/ccsh/ssh.rb', line 44

def execute!
    @options = {
        :password => @password,
        :keys     => [@private_key],
        :port     => @port,
        :host_key => @options['host_key'],
        :timeout  => @options['timeout'],
    }.select {|key,value| value != nil}

    raise "something" unless CCSH::Utils.valid_ssh(@options)

    Net::SSH.start(@hostname, @user, @options) do |ssh|
        ssh.open_channel do |ch|
            ch.on_data do |c, data|

                if data =~ /^\[sudo\] password for /

                    if @enable_sudo
                        if @sudo_password != nil
                            ch.send_data "#{@sudo_password}\n"
                        else
                            msg = """The server #{@hostname} asked for user password
                                by the 'password' or 'sudo_password' option was not defined.
                            """.gsub("\n", ' ').squeeze(' ')

                            STDERR.puts "[WARN] #{msg}"
                            ch.send_data "\x03"
                        end
                    else
                        msg = """The server #{@hostname} asked for user password
                            the sudo_enabled option was not allowed
                            skipping this hosts setup
                        """.gsub("\n", ' ').squeeze(' ')

                        STDERR.puts "[WARN] #{msg}"
                        ch.send_data "\x03"
                    end
                else
                    @stdout << data
                end
            end

            ch.on_extended_data do |c, type, data|
                @stderr << data
            end

            ch.on_request("exit-status") do |c,data|
                @return_code = data.read_long
            end

            ch.on_request("exit-signal") do |c, data|
                @return_signal = data.read_long
            end

            ch.request_pty do |ch, success|
                if success
                    CCSH::Utils.debug("pty successfully obtained for #{@hostname}")
                else
                    CCSH::Utils.debug("pty unsuccessfully obtained for #{@hostname}")
                end
            end

            ch.exec @command do |ch, success|
                raise "Could execute command #{command} on #{host}" unless success
            end
        end.wait
    end

    return self
end