Sha256: 1257f8efde5f9f0673cf90e9eef13a81a9ee09842ea5734d7faf875f7c1a9f98

Contents?: true

Size: 1.08 KB

Versions: 1

Compression:

Stored size: 1.08 KB

Contents

require 'shellwords'

module Pec2
  class Pssh

    PSSH_PATH = File.expand_path('../../../exe/bin/pssh', __FILE__)

    def initialize(options, hosts_file, parallel = 1)
      @pssh_command = "#{PSSH_PATH} -t 0 -x '-tt' -h #{hosts_file} -O StrictHostKeyChecking=no"
      if options[:print]
        @pssh_command = "#{@pssh_command} -P"
      end

      if options[:user]
        @pssh_command = "#{@pssh_command} -l #{options[:user]}"
      end

      if options[:log]
        @pssh_command = "#{@pssh_command} -o #{options[:log]}"
      end

      @pssh_command = "#{@pssh_command} -p #{options[:parallel] || parallel}"
      @sudo_password = options[:sudo_password]
    end

    def build_pssh_command(command)
      if @sudo_password
        %Q{(echo #{@sudo_password}) | #{@pssh_command} -I #{Shellwords.escape(command)}}
      else
        %Q{#{@pssh_command} -i #{Shellwords.escape(command)}}
      end
    end

    def exec_pssh_command(command)
      return false if command.nil? || command.empty?
      build_command = build_pssh_command(command)
      system(build_command)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pec2-0.5.1 lib/pec2/pssh.rb