Module: Grifork::Executable

Includes:
Configured, Loggable
Included in:
Grifork::Executor::Carrier, Grifork::Executor::Grifork, Grifork::Executor::Local
Defined in:
lib/grifork/mixin/executable.rb

Defined Under Namespace

Classes: CommandFailure, SSHCommandFailure

Instance Method Summary collapse

Methods included from Loggable

#logger

Methods included from Configured

#config

Instance Method Details

#rsync(host, from, to = nil) ⇒ Object

Shorthand for rsync command. Sync contents to target host

Parameters:

  • host (String)

    Target hostname

  • from (String)

    Path to source file or directory

  • to (String) (defaults to: nil)

    Path to destination at remote host. If you omit this param, it will be the same with from param



68
69
70
71
# 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) ⇒ Object

Shorthand for rsync command run by ssh to source host Sync contents from source host to target host

Parameters:

  • src (String)

    Source hostname to login by ssh

  • dst (String)

    Target hostname

  • from (String)

    Path to source file or directory

  • to (String) (defaults to: nil)

    Path to destination at remote host. If you omit this param, it will be the same with from param



80
81
82
83
# 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 = []) ⇒ Object

Execute shell command at localhost

Parameters:

  • cmd (String)

    command

  • args (Array) (defaults to: [])

    arguments



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# 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 = []) ⇒ Object

Execute ssh with command to execute at remote host

Parameters:

  • host (String)

    hostname

  • cmd (String)

    command

  • args (Array) (defaults to: [])

    arguments



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# 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