Class: Indocker::Rsync
- Inherits:
-
Object
- Object
- Indocker::Rsync
- Defined in:
- lib/indocker/rsync.rb
Class Method Summary collapse
- .local_sync(from, to, create_path: nil, raise_on_error: false, exclude: nil) ⇒ Object
- .sync(session, from, to, create_path: nil, raise_on_error: false, exclude: nil) ⇒ Object
- .sync_local_cp(from, to, raise_on_error:) ⇒ Object
- .sync_local_rsync(from, to, raise_on_error:, exclude: nil) ⇒ Object
Class Method Details
.local_sync(from, to, create_path: nil, raise_on_error: false, exclude: nil) ⇒ Object
4 5 6 7 8 9 10 11 12 13 |
# File 'lib/indocker/rsync.rb', line 4 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 |
.sync(session, from, to, create_path: nil, raise_on_error: false, exclude: nil) ⇒ Object
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 |
# File 'lib/indocker/rsync.rb', line 15 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.(to) == File.(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 # BatchMode/ConnectTimeout: an unreachable server must fail in seconds with # a clear rsync error instead of hanging silently (or prompting for a password) # until the deployment-wide remote operation timeout kills the whole deploy. # ServerAlive*: a path that accepts the connection and then stops delivering # packets (seen when a cloud NAT address half-works) would otherwise hang # just as long — keepalives tear such a session down in ~30s. ssh_command = [ "ssh -p #{session.port}", "-o BatchMode=yes", "-o ConnectTimeout=#{Indocker.ssh_connect_timeout}", "-o ServerAliveInterval=10", "-o ServerAliveCountMax=3", ] # Tunnel through the jump host the server is defined with, so that a server # reachable only over its internal address syncs like any other one. ProxyCommand # rather than -J: it is the same tunnel every other remote operation uses, including # the shared connection that keeps a parallel sync from exhausting the jump host's # MaxStartups. if session.jump_host ssh_command << %Q{-o ProxyCommand="#{Indocker::SshSession.jump_proxy_command(session.jump_host)}"} end ssh_command = ssh_command.join(' ') command = "rsync --delete-after -a -e '#{ssh_command}' #{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 |
.sync_local_cp(from, to, raise_on_error:) ⇒ Object
63 64 65 |
# File 'lib/indocker/rsync.rb', line 63 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 |
.sync_local_rsync(from, to, raise_on_error:, exclude: nil) ⇒ Object
67 68 69 70 71 72 73 74 75 |
# File 'lib/indocker/rsync.rb', line 67 def self.sync_local_rsync(from, to, raise_on_error:, exclude: nil) = [] << " --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 #{.join(' ')} #{from} #{to}", Indocker.logger, raise_on_error: raise_on_error) end |