Sha256: 67cf3e7b7570e396f042df32c4f64f15d265621f01df8882ea4e33be0d7a9370

Contents?: true

Size: 1.92 KB

Versions: 4

Compression:

Stored size: 1.92 KB

Contents

require 'serverspec/backend/exec'

module Serverspec
  module Backend
    class Ssh < Exec
      def run_command(cmd, opt={})
        cmd = build_command(cmd)
        cmd = add_pre_command(cmd)
        ret = ssh_exec!(cmd)
        if ! @example.nil?
          @example.metadata[:command] = cmd
          @example.metadata[:stdout]  = ret[:stdout]
        end
        ret
      end

      def build_command(cmd)
        cmd = super(cmd)
        cmd = "sudo #{cmd}" if RSpec.configuration.ssh.options[:user] != 'root'
        cmd
      end

      def add_pre_command(cmd)
        cmd = super(cmd)
        cmd = "sudo #{cmd}" if RSpec.configuration.ssh.options[:user] != 'root'
        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

4 entries across 4 versions & 1 rubygems

Version Path
serverspec-0.5.3 lib/serverspec/backend/ssh.rb
serverspec-0.5.2 lib/serverspec/backend/ssh.rb
serverspec-0.5.1 lib/serverspec/backend/ssh.rb
serverspec-0.5.0 lib/serverspec/backend/ssh.rb