Skip to content

Instantly share code, notes, and snippets.

@rgaufman
Created April 3, 2019 15:07
Show Gist options
  • Select an option

  • Save rgaufman/6c36fd46ff3e692c3fe5fb208cfcd1a4 to your computer and use it in GitHub Desktop.

Select an option

Save rgaufman/6c36fd46ff3e692c3fe5fb208cfcd1a4 to your computer and use it in GitHub Desktop.

Revisions

  1. rgaufman created this gist Apr 3, 2019.
    78 changes: 78 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,78 @@
    #!/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