Sha256: 363c0aa769edbeef6da007ae8ce31e1c82c2a6f80c5b4d247e23f4788addc618

Contents?: true

Size: 1.47 KB

Versions: 8

Compression:

Stored size: 1.47 KB

Contents

require 'serverspec/backend/exec'

module Serverspec
  module Backend
    class Ssh < Exec
      def do_check(cmd, opt={})
        cmd = "sudo #{cmd}" if not RSpec.configuration.ssh.options[:user] == 'root'
        ssh_exec!(cmd)
      end

      private
      def ssh_exec!(command)
        stdout_data = ''
        stderr_data = ''
        exit_status   = nil
        exit_signal = nil

        ssh = RSpec.configuration.ssh
        ssh.open_channel do |channel|
          channel.request_pty do |ch, success|
            abort "Could not obtain pty " if !success
          end
          channel.exec("#{command}") do |ch, success|
            abort "FAILED: couldn't execute command (ssh.channel.exec)" if !success
            channel.on_data do |ch, data|
              if data =~ /^\[sudo\] password for/
                channel.send_data "#{RSpec.configuration.sudo_password}\n"
              else
                stdout_data += data
              end
            end

            channel.on_extended_data do |ch, type, data|
              stderr_data += data
            end

            channel.on_request("exit-status") do |ch, data|
              exit_status = data.read_long
            end

            channel.on_request("exit-signal") do |ch, data|
              exit_signal = data.read_long
            end
          end
        end
        ssh.loop
        { :stdout => stdout_data, :stderr => stderr_data, :exit_status => exit_status, :exit_signal => exit_signal }
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
serverspec-0.2.19 lib/serverspec/backend/ssh.rb
serverspec-0.2.18 lib/serverspec/backend/ssh.rb
serverspec-0.2.17 lib/serverspec/backend/ssh.rb
serverspec-0.2.16 lib/serverspec/backend/ssh.rb
serverspec-0.2.15 lib/serverspec/backend/ssh.rb
serverspec-0.2.14 lib/serverspec/backend/ssh.rb
serverspec-0.2.13 lib/serverspec/backend/ssh.rb
serverspec-0.2.12 lib/serverspec/backend/ssh.rb