Skip to content

Instantly share code, notes, and snippets.

@rgaufman
Created April 3, 2019 15:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rgaufman/6c36fd46ff3e692c3fe5fb208cfcd1a4 to your computer and use it in GitHub Desktop.
Save rgaufman/6c36fd46ff3e692c3fe5fb208cfcd1a4 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# frozen_string_literal: true
require_relative '../lib/xanview_resolve'
# Class to rsync Xanview timeagent/deploy code to various Xanview hosts
class XanviewRsyncFrom
def initialize
end
def rsync(host, src_path)
@resolv = XanviewResolv.new(host)
@host = @resolv.resolv
exit 1 unless host
rsync_from(host, src_path)
end
private
def rsync_from(host, src_path)
dest_path = "#{Time.now.strftime('%Y%m%d')}-#{host}"
unless File.directory?(dest_path)
puts "*** Creating #{dest_path} for requested files"
Dir.mkdir(dest_path)
end
if @resolv.timebox?
rsync_through_cloud(@resolv, src_path, dest_path)
else
rsync_direct(host, src_path, dest_path)
end
`cd #{dest_path}`
end
def rsync_direct(host, src_path, dest_path)
cmd = "rsync -aP --rsync-path='sudo rsync' --relative " \
"deployer@#{@host}:#{src_path} #{dest_path}"
puts "*** Running: #{cmd}"
system cmd
end
def rsync_through_cloud(resolv, src_path, dest_path)
if src_path == 'logs'
src_path = [
'/var/log/syslog*', '/var/log/kern.log*', '/var/log/auth.log*',
'/home/deployer/timeagent/log', '/data/logs.txt'
].map { |p| ":#{p}" }.join(" ")[1..-1]
cmd = "ssh -t deployer@#{resolv.domain} ssh -o 'StrictHostKeyChecking=no' " \
"#{resolv.host} \"eval 'sudo journalctl -a > /data/logs.txt'\""
puts "*** Running: #{cmd}"
system cmd
else
":#{src_path}"
end
cmd = "rsync -azP --rsync-path='sudo rsync' --relative -e " \
"'ssh -t deployer@#{resolv.domain} ssh' " \
"deployer@#{resolv.host}:#{src_path} #{dest_path}"
puts "*** Running: #{cmd}"
system cmd
end
end
if $PROGRAM_NAME == __FILE__
host = ARGV[0]&.strip&.downcase
src_path = ARGV[1]&.strip&.downcase
unless host && src_path
puts "Usage: rsyncfrom timebox_serial /src_path"
puts " OR: rsyncfrom timebox_serial logs"
puts " OR: rsyncfrom staging /src_path"
exit 1
end
xanview_rsync = XanviewRsyncFrom.new
xanview_rsync.rsync(host, src_path)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment