Module: Grifork::Executable
- Includes:
- Configured, Loggable
- Defined in:
- lib/grifork/mixin/executable.rb
Defined Under Namespace
Classes: CommandFailure, SSHCommandFailure
Instance Method Summary collapse
-
#rsync(host, from, to = nil) ⇒ Object
Shorthand for
rsync
command. -
#rsync_remote(src, dst, from, to = nil) ⇒ Object
Shorthand for
rsync
command run byssh
to source host Sync contents from source host to target host. -
#sh(cmd, args = []) ⇒ Object
Execute shell command at localhost.
-
#ssh(host, cmd, args = []) ⇒ Object
Execute
ssh
with command to execute at remote host.
Methods included from Loggable
Methods included from Configured
Instance Method Details
#rsync(host, from, to = nil) ⇒ Object
Shorthand for rsync
command. Sync contents to target host
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
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
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
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.} | #{cmd} #{args}") return else logger.info("#ssh @#{host} #{config.ssh.} | #{cmd} #{args}") end Net::SSH.start(host, nil, config.ssh.) 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 |