Class: Rbfs::Rsync

Inherits:
Object
  • Object
show all
Defined in:
lib/rbfs/rsync.rb

Instance Method Summary collapse

Constructor Details

#initialize(config = {}, host = nil) ⇒ Rsync

Returns a new instance of Rsync.



5
6
7
8
# File 'lib/rbfs/rsync.rb', line 5

def initialize(config = {}, host = nil)
  @config = config
  @host = host
end

Instance Method Details

#command(cmd, options = [], &block) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/rbfs/rsync.rb', line 47

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

#local_rootObject



18
19
20
21
22
23
24
# File 'lib/rbfs/rsync.rb', line 18

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

#loggerObject



10
11
12
# File 'lib/rbfs/rsync.rb', line 10

def logger
  @config[:logger]
end

#mkdirObject



26
27
28
29
# File 'lib/rbfs/rsync.rb', line 26

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

#remote_urlObject



14
15
16
# File 'lib/rbfs/rsync.rb', line 14

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

#rsyncObject



31
32
33
34
35
36
37
38
# File 'lib/rbfs/rsync.rb', line 31

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

#run_command(cmd, &block) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/rbfs/rsync.rb', line 55

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

#syncObject



40
41
42
43
44
45
# File 'lib/rbfs/rsync.rb', line 40

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