module Grifork::Executable

Public Instance Methods

rsync(host, from, to = nil) click to toggle source

Shorthand for rsync command. Sync contents to target host @param host [String] Target hostname @param from [String] Path to source file or directory @param to [String] Path to destination at remote host.

If you omit this param, it will be the same with +from+ param
# File lib/grifork/mixin/executable.rb, line 68
def rsync(host, from, to = nil)
  to ||= from
  sh :rsync, [*config.rsync_opts, from, "#{host}:#{to}"]
end
rsync_remote(src, dst, from, to = nil) click to toggle source

Shorthand for rsync command run by ssh to source host Sync contents from source host to target host @param src [String] Source hostname to login by ssh @param dst [String] Target hostname @param from [String] Path to source file or directory @param to [String] Path to destination at remote host.

If you omit this param, it will be the same with +from+ param
# File lib/grifork/mixin/executable.rb, line 80
def rsync_remote(src, dst, from, to = nil)
  to ||= from
  ssh src, :rsync, [*config.rsync_opts, from, "#{dst}:#{to}"]
end
sh(cmd, args = []) click to toggle source

Execute shell command at localhost @param cmd [String] command @param args [Array] arguments

# File lib/grifork/mixin/executable.rb, line 11
def sh(cmd, args = [])
  if config.dry_run?
    logger.info("[Dry-run] #sh | #{cmd} #{args}")
    return
  else
    logger.info("#sh | #{cmd} #{args}")
  end
  stat = Open3.popen3(cmd.to_s, *args) do |stdin, stdout, stderr, wait_thr|
    stdin.close
    stdout.each { |l| logger.info("#sh [out] #{l.chomp}") }
    stderr.each { |l| logger.warn("#sh [err] #{l.chomp}") }
    wait_thr.value
  end

  unless stat.success?
    raise CommandFailure, "Failed to exec command! #{cmd} #{args}"
  end
end
ssh(host, cmd, args = []) click to toggle source

Execute ssh with command to execute at remote host @param host [String] hostname @param cmd [String] command @param args [Array] arguments

# File lib/grifork/mixin/executable.rb, line 34
def ssh(host, cmd, args = [])
  command = "#{cmd} #{args.shelljoin}"
  if config.dry_run?
    logger.info("[Dry-run] #ssh @#{host} #{config.ssh.options} | #{cmd} #{args}")
    return
  else
    logger.info("#ssh @#{host} #{config.ssh.options} | #{cmd} #{args}")
  end
  Net::SSH.start(host, nil, config.ssh.options) do |ssh|
    channel = ssh.open_channel do |ch|
      ch.exec(command) do |ch, success|
        unless success
          raise SSHCommandFailure, "Failed to exec ssh command! - #{user}@#{host} | #{cmd} #{args}"
        end

        ch.on_data do |c, d|
          d.each_line { |l| logger.info("#ssh @#{host} [out] #{l.chomp}") }
        end
        ch.on_extended_data do |c, t, d|
          d.each_line { |l| logger.warn("#ssh @#{host} [err] #{l.chomp}") }
        end
        ch.on_close { logger.debug("#ssh @#{host} end.") }
      end
    end
    channel.wait
  end
end