Sha256: 087a21c818c8ade53bd3d6ecc086458dc0811002ef694a8196658cf269144a67

Contents?: true

Size: 1.94 KB

Versions: 29

Compression:

Stored size: 1.94 KB

Contents

require_relative 'shell'

class Indocker::Rsync
  def self.local_sync(from, to, create_path: nil, raise_on_error: false, exclude: nil)
    @session ||= Indocker::SshSession.new(
      host: 'localhost',
      user: nil,
      port: nil,
      logger: Indocker.logger
    )

    sync(@session, from, to, create_path: create_path, raise_on_error: raise_on_error, exclude: exclude)
  end

  def self.sync(session, from, to, create_path: nil, raise_on_error: false, exclude: nil)
    if create_path
      session.exec!("mkdir -p #{create_path}")
    end

    if session.local?
      return if File.expand_path(to) == File.expand_path(from)

      Indocker::Shell.command("rm -rf #{to}", Indocker.logger, raise_on_error: raise_on_error)

      if Indocker::Shell.command_exist?("rsync")
        sync_local_rsync(from, to, raise_on_error: raise_on_error, exclude: exclude)
      else
        Indocker.logger.debug("WARNING: exclude option skipped due to fallback to CP command")
        sync_local_cp(from, to, raise_on_error: raise_on_error)
      end
    else
      command = "rsync --delete-after -a -e 'ssh -p #{session.port}' #{from} #{session.user}@#{session.host}:#{to}"
      Indocker.logger.debug("sync #{from} #{session.user}@#{session.host}:#{to}")
      Indocker::Shell.command(command, Indocker.logger, raise_on_error: raise_on_error)
    end
  end

  private
    def self.sync_local_cp(from, to, raise_on_error:)
      Indocker::Shell.command("cp -r #{from} #{to}", Indocker.logger, raise_on_error: raise_on_error)
    end

    def self.sync_local_rsync(from, to, raise_on_error:, exclude: nil)
      options = []
      options << " --exclude=#{exclude}" if exclude
      # Add a trailing slash to directory to have behavior similar to CP command
      if File.directory?(from) && !from.end_with?("/")
        from = "#{from}/"
      end
      Indocker::Shell.command("rsync -a #{options.join(' ')} #{from} #{to}", Indocker.logger, raise_on_error: raise_on_error)
    end
end

Version data entries

29 entries across 29 versions & 1 rubygems

Version Path
indocker-0.3.9 lib/indocker/rsync.rb
indocker-0.3.8 lib/indocker/rsync.rb
indocker-0.3.7 lib/indocker/rsync.rb
indocker-0.3.6 lib/indocker/rsync.rb
indocker-0.3.5 lib/indocker/rsync.rb
indocker-0.3.4 lib/indocker/rsync.rb
indocker-0.3.3 lib/indocker/rsync.rb
indocker-0.3.2 lib/indocker/rsync.rb
indocker-0.1.18 lib/indocker/rsync.rb
indocker-0.3.1 lib/indocker/rsync.rb
indocker-0.3.0 lib/indocker/rsync.rb
indocker-0.1.17 lib/indocker/rsync.rb
indocker-0.1.16 lib/indocker/rsync.rb
indocker-0.1.15 lib/indocker/rsync.rb
indocker-0.1.14 lib/indocker/rsync.rb
indocker-0.1.13 lib/indocker/rsync.rb
indocker-0.1.12 lib/indocker/rsync.rb
indocker-0.1.11 lib/indocker/rsync.rb
indocker-0.1.10 lib/indocker/rsync.rb
indocker-0.1.9 lib/indocker/rsync.rb