module Buffet
  class Slave
    attr_reader :user, :host

    def initialize user, host, project
      @user = user
      @host = host
      @project = project
    end

    def rsync src, dest
      Buffet.run! 'rsync', '-aqz', '--delete', rsync_exclude_flags,
                  '-e', 'ssh', src, "#{user_at_host}:#{dest}"
    end

    def scp src, dest, options = {}
      args = [src, "#{user_at_host}:#{dest}"]
      args.unshift '-r' if options[:recurse]
      Buffet.run! 'scp', *args
    end

    def execute_in_project command
      execute "cd #{@project.directory_on_slave} && #{command}"
    end

    def execute command
      Buffet.run! 'ssh', "#{user_at_host}", command
    end

    def name
      user_at_host
    end

    def user_at_host
      "#{@user}@#{@host}"
    end

    private

    def rsync_exclude_flags
      if Settings.has_exclude_filter_file?
        exclude_flags = "--exclude-from=#{Settings.exclude_filter_file}"
      end
      exclude_flags || ''
    end
  end
end

module Buffet
  class Slave
    attr_reader :user, :host

    def initialize user, host, project
      @user = user
      @host = host
      @project = project
    end

    def rsync src, dest
      Buffet.run! 'rsync', '-aqz', '--delete',
                  '--delete-excluded', rsync_exclude_flags,
                  '-e', 'ssh', src, "#{user_at_host}:#{dest}"
    end

    def scp src, dest, options = {}
      args = [src, "#{user_at_host}:#{dest}"]
      args.unshift '-r' if options[:recurse]
      Buffet.run! 'scp', *args
    end

    def execute_in_project command
      execute "cd #{@project.directory_on_slave} && #{command}"
    end

    def execute command
      Buffet.run! 'ssh', "#{user_at_host}", command
    end

    def name
      user_at_host
    end

    def user_at_host
      "#{@user}@#{@host}"
    end

    private

    def rsync_exclude_flags
      if Settings.has_exclude_filter_file?
        exclude_flags = "--exclude-from=#{Settings.exclude_filter_file}"
      end
      exclude_flags || ''
    end
  end
end

Delete excluded project files on remote
Delete excluded project files on remote

While we weren't syncing files specified in the filter file, we were
also not deleting any old copies of these files if they already existed
on the remote. This could potentially have been bad if the log files
grew to exceptionally large sizes over time.

Adding the --delete-excluded flag to the rsync call takes care of this
problem for us.

Change-Id: I7e5026e8c485b65299b24726bcc390c70b1ed375
Reviewed-on: https://gerrit.causes.com/1854
Tested-by: Shane da Silva <6f6e68d1df92f30cb4b3ce35260ddf94a402f33d@causes.com>
Reviewed-by: Greg Hurrell <79e2475f81a6317276bf7cbb3958b20d289b78df@causes.com>
