Sha256: 81ca76b022530d9a64367b737bf76724d1d7db184ba0e6996c4610073a2736df

Contents?: true

Size: 1.86 KB

Versions: 1

Compression:

Stored size: 1.86 KB

Contents

require 'etc'

module Serverspec
  module SshHelper
    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_code   = 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|
            stdout_data += data
          end

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

          channel.on_request("exit-status") do |ch,data|
            exit_code = 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_code => exit_code, :exit_signal => exit_signal }
    end
  end

  module ExecHelper
    def do_check(cmd, opts={})
      stdout = `#{cmd} 2>&1`
      # In ruby 1.9, it is possible to use Open3.capture3, but not in 1.8
      #stdout, stderr, status = Open3.capture3(cmd)
      { :stdout => stdout, :stderr => nil,
        :exit_code => $?, :exit_signal => nil }
    end
  end

  module RedHatHelper
    def commands
      Serverspec::Commands::RedHat.new
    end
  end

  module DebianHelper
    def commands
      Serverspec::Commands::Debian.new
    end
  end

  module GentooHelper
    def commands
      Serverspec::Commands::Gentoo.new
    end
  end

  module SolarisHelper
    def commands
      Serverspec::Commands::Solaris.new
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
serverspec-0.1.7 lib/serverspec/helper.rb