Sha256: 4f2533fb596eb80602b4193600ddd240a2483601ef2831f9a9fe6c2ad388189e

Contents?: true

Size: 1.27 KB

Versions: 3

Compression:

Stored size: 1.27 KB

Contents

require 'rsync'

module Rbfs
  class Rsync
    def initialize(config = {}, host = nil)
      @config = config
      @host = host
    end

    def logger
      @config[:logger]
    end

    def remote_url
      "#{@host.ip}:#{@config[:remote_root]}"
    end

    def local_root
      if File.directory?(@config[:root])
        @config[:root] + "/"
      else
        @config[:root]
      end
    end

    def mkdir
      args = [@host.ip, "mkdir", "-p", @config[:remote_root]]
      command("ssh", args)
    end

    def rsync
      args = ["-a", "--delete"]
      args << "-e #{@config[:shell]}" if @config[:shell]
      args << "-v" if @config[:verbose]
      args << "-n" if @config[:dry]
      args << "--timeout=#{@config[:timeout]}" if @config[:timeout]
      ::Rsync.run(local_root, remote_url, args)
    end

    def sync
      if File.directory?(@config[:root])
        mkdir
      end
      rsync
    end

    def command(cmd, options = [], &block)
      cmd_line = "#{cmd} "
      cmd_line += options.join(' ')
      output = run_command(cmd_line, &block)
      exitcode = $?
      {:output => output, :exitcode => exitcode}
    end

    def run_command(cmd, &block)
      if block_given?
        IO.popen("#{cmd} 2>&1", &block)
      else
        `#{cmd} 2>&1`
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rbfs-1.0.16 lib/rbfs/rsync.rb
rbfs-0.0.15 lib/rbfs/rsync.rb
rbfs-0.0.14 lib/rbfs/rsync.rb