class Capistrano::Deploy::Strategy::RemoteCacheFromLocal

A deployment strategy which has the app server pull the code from the deploying machine via an SSH tunnel. The strategy behaves like :remote_cache otherwise.

Private Instance Methods

execute(description, &block) click to toggle source

Local execution methods copied from the copy strategy

# File lib/capistrano/recipes/deploy/strategy/remote_cache_from_local.rb, line 61
def execute(description, &block)
  logger.debug description
  handle_system_errors(&block)
end
git_daemon_pid_file() click to toggle source
# File lib/capistrano/recipes/deploy/strategy/remote_cache_from_local.rb, line 55
def git_daemon_pid_file
  fetch(:git_daemon_pid_file, 'tmp/git-daemon.pid')
end
git_tunnel_local_port() click to toggle source
# File lib/capistrano/recipes/deploy/strategy/remote_cache_from_local.rb, line 47
def git_tunnel_local_port
  fetch(:git_tunnel_local_port, 50123)
end
git_tunnel_remote_port() click to toggle source
# File lib/capistrano/recipes/deploy/strategy/remote_cache_from_local.rb, line 51
def git_tunnel_remote_port
  fetch(:git_tunnel_remote_port, 50123)
end
handle_system_errors(&block) click to toggle source
# File lib/capistrano/recipes/deploy/strategy/remote_cache_from_local.rb, line 66
def handle_system_errors(&block)
  block.call
  raise_command_failed if last_command_failed?
end
last_command_failed?() click to toggle source
# File lib/capistrano/recipes/deploy/strategy/remote_cache_from_local.rb, line 75
def last_command_failed?
  $? != 0
end
raise_command_failed() click to toggle source
# File lib/capistrano/recipes/deploy/strategy/remote_cache_from_local.rb, line 71
def raise_command_failed
  raise Capistrano::Error, "shell command failed with return code #{$?}"
end
update_repository_cache() click to toggle source
Calls superclass method
# File lib/capistrano/recipes/deploy/strategy/remote_cache_from_local.rb, line 11
def update_repository_cache
  configuration.set(:repository, "git://127.0.0.1:#{git_tunnel_remote_port}/.git")
  with_tunneled_local_daemon { super }
end
with_tunneled_local_daemon() { || ... } click to toggle source
# File lib/capistrano/recipes/deploy/strategy/remote_cache_from_local.rb, line 16
def with_tunneled_local_daemon
  execute_on_servers do |servers|
    servers.each do |s|
      begin
        # Run a git daemon locally (listening on 127.0.0.1 only)
        execute "Run local git daemon" do
          system "git daemon --verbose --listen=127.0.0.1 --port=#{git_tunnel_local_port} --reuseaddr --export-all --base-path='#{local_repository}' --detach --pid-file='#{git_daemon_pid_file}'"
        end

        # Forward remote connections to local git daemon via capistrano's SSH session
        session = sessions[s]
        logger.debug "Open git tunnel from local #{git_tunnel_local_port} to remote #{git_tunnel_remote_port}"
        session.forward.remote(git_tunnel_local_port, '127.0.0.1', git_tunnel_remote_port)
        keep_looping = true
        Thread.new { session.loop { sleep(0.1); keep_looping } }

        yield
      ensure
        keep_looping = false
        if session.forward.active_remotes.include?([git_tunnel_remote_port, '127.0.0.1'])
          logger.debug "Close git tunnel from local #{git_tunnel_local_port} to remote #{git_tunnel_remote_port}"
          session.forward.cancel_remote(git_tunnel_remote_port, '127.0.0.1')
        end
        execute "Kill local git daemon if running" do
          system "if [ -e #{git_daemon_pid_file} ]; then kill `cat '#{git_daemon_pid_file}'`; rm '#{git_daemon_pid_file}'; fi"
        end
      end
    end
  end
end