module Publisher class Rsync attr_accessor :remote_host attr_accessor :remote_user attr_accessor :ssh_key_file 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' result = '' 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 if not 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 if not options[:exclude].nil? [options[:exclude]].flatten.each do |current_exclude| if current_exclude > "" command_options << "--exclude #{current_exclude}" end 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 = %x"#{rsync} #{command_options.join(' ')} \"#{source}\" \"#{target}\"" $log.writer.debug stdout if not RUBY_VERSION < '1.9' raise 'rsync execution error' if $?.exitstatus != 0 end rescue Exception => 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 end end