Skip to content

Instantly share code, notes, and snippets.

@halkeye
Created June 7, 2014 05:05
Show Gist options
  • Save halkeye/f061872eb4b47dc0928e to your computer and use it in GitHub Desktop.
Save halkeye/f061872eb4b47dc0928e to your computer and use it in GitHub Desktop.
For production Capistrano deploys, we can't use our own internal git server because the one off amazon box doesn't have access to the internal network. Hacked in a solution to create a port forward to talk back to our network. Works in this case so sharing.
require 'net/ssh'
#set :ssh_options, :verbose => :debug
set :user, 'labuser'
set :repo_url, "ssh://eti@localhost:9000/#{fetch(:application)}.git"
$gateway_ssh = {}
namespace :ssh do
task :git_start do
on roles(:all) do
hostname = host.hostname.to_s
port = (host.port || 22).to_i
user = host.user.to_s
password = host.password.to_s
run_locally do
if !$gateway_ssh[hostname]
$gateway_ssh[hostname] = OpenStruct.new
puts "Connecting to #{user}@#{hostname}:#{port} to open gitserver:22 on port server"
ssh = $gateway_ssh[hostname].ssh = Net::SSH.start(hostname, user, :password => password, :port => port)
$gateway_ssh[hostname].remote = ssh.forward.remote(22, "gitserver", 9000)
$gateway_ssh[hostname].thread = Thread.new {
while true
ssh.loop { true }
end
}
end
end
end
end
task :git_stop do
$gateway_ssh.each do |key, val|
val.thread.exit
$gateway_ssh.delete key
end
end
before :deploy, "ssh:git_start"
after :deploy, "ssh:git_stop"
end
server 'amazonserver.amazonaws.com', roles: %w{app web db}, user: 'ec2-user'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment