Class: Publisher::Rsync

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#remote_hostObject

Returns the value of attribute remote_host.



3
4
5
# File 'lib/depengine/publisher/rsync.rb', line 3

def remote_host
  @remote_host
end

#remote_userObject

Returns the value of attribute remote_user.



4
5
6
# File 'lib/depengine/publisher/rsync.rb', line 4

def remote_user
  @remote_user
end

#ssh_key_fileObject

Returns the value of attribute ssh_key_file.



5
6
7
# File 'lib/depengine/publisher/rsync.rb', line 5

def ssh_key_file
  @ssh_key_file
end

Instance Method Details

#sync(source, target, options = {}) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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
61
62
63
# File 'lib/depengine/publisher/rsync.rb', line 7

def sync(source, target, options = {})
  # default options
  command_options = ['-a']
  if options[:local].nil? or options[:local] == false
    Helper.validates_presence_of remote_host, 'Remote Host not set'
    Helper.validates_presence_of remote_user, 'Remote User not set'
    Helper.validates_presence_of ssh_key_file, 'SSH-Keyfile not set'

    target = "#{remote_user}@#{remote_host}:" + target
    command_options << "-e \'ssh -c arcfour -o StrictHostKeyChecking=no -i #{ssh_key_file}\'"
  end

  rsync  = options[:rsync_bin] || 'rsync'

  if not options[:delete].nil? and options[:delete] == true
    command_options << '--delete'
  end
  if not options[:whole_file].nil? and options[:whole_file] == true
    command_options << '-W'
  end
  unless options[:rsync_path].nil?
    command_options << "--rsync-path=#{options[:rsync_path]}"
  end
  if options[:one_filesystem].nil? or options[:one_filesystem] == true
    command_options << '-x'
  end
  if options[:compression].nil? or options[:compression] == true
    command_options << '-z'
  end
  unless options[:exclude].nil?
    [options[:exclude]].flatten.each do |current_exclude|
      next unless current_exclude > ''
      command_options << "--exclude #{current_exclude}"
    end
  end

  # sync options
  # options[:delete] default false
  # options[:whole_file] default false
  # options[:one_filesystem] default true
  # options[:compression] default true
  # TODO: ssh-agent preload key with passphrase
  begin
    $log.writer.debug "#{rsync} #{command_options.join(' ')} #{source} #{target}"
    stdout = `#{rsync} #{command_options.join(' ')} \"#{source}\" \"#{target}\"`
    $log.writer.debug stdout

    unless RUBY_VERSION < '1.9'
      fail 'rsync execution error' if $CHILD_STATUS.exitstatus != 0
    end
  rescue => e
    $log.writer.error "Can not sync directory #{source} with #{target}"
    $log.writer.error e.message
    $log.writer.error e.backtrace.join("\n")
    exit 1
  end
end