Capistrano的,防火墙和隧道(Capistrano, Firewalls and Tunne

2019-08-06 11:52发布

我们使用Capistrano的自动化推动PHP应用程序的新版本到生产服务器。 生产服务器(我们称之为生产)是公共的,而我们的资源库服务器(我们称之为回购)坐在我们的企业防火墙后面,用我们自己的机器一起。

Capistrano的,因为默认配置下,将无法正常工作,由于生产不能跟回购。

我在想,如果有好歹我可以安装Capistrano的使用SSH先回购,再SSH到生产的端口然后我就可以使用SSH从生产回到回购拉从SCM的变化上打开隧道。

我只是无法弄清楚如何设置这还是找出一个更好的解决方案。 想法?

编辑:

我已经试过这样:

role :web, "deploy.com"

namespace :deploy do
    task :remote_tunnel do
        run 'Creating SSH tunnel...' do |channel, stream, data|
            ssh = channel.connection
            ssh.forward.remote(22, 'server.com', 10000, '127.0.0.1')
            ssh.loop {!ssh.forward.active_remotes.include?([10000, '127.0.0.1'])}
        end
    end
end

before "deploy:update_code", "deploy:remote_tunnel"

不过,我不断收到此错误:

failed: "sh -c 'Creating SSH tunnel...'" on deploy.com

Answer 1:

下面的两种方法来完成它。

1路

不知道你是否已经看到了这个主题?

  • https://groups.google.com/forum/?fromgroups=#!topic/capistrano/RVwMim-qnMg

它利用的net-ssh-gateway库,但创建的本地转发方法副本,但他们面向远程访问。

class Net::SSH::Gateway 
  # Opens a SSH tunnel from a port on a remote host to a given host and port 
  # on the local side 
  # (equivalent to openssh -R parameter) 
  def open_remote(port, host, remote_port, remote_host = "127.0.0.1") 
    ensure_open! 

    @session_mutex.synchronize do 
      @session.forward.remote(port, host, remote_port, remote_host) 
    end 

    if block_given? 
      begin 
        yield [remote_port, remote_host] 
      ensure 
        close_remote(remote_port, remote_host) 
      end 
    else 
      return [remote_port, remote_host] 
    end 
  rescue Errno::EADDRINUSE 
    retry 
  end 


  # Cancels port-forwarding over an open port that was previously opened via 
  # open_remote. 
  def close_remote(port, host = "127.0.0.1") 
    ensure_open! 

    @session_mutex.synchronize do 
      @session.forward.cancel_remote(port, host) 
    end 
  end 
end

第二个方法

概述在回答这个问题的SO:

  • 是否有可能做的都做Capistrano的通过反向SSH隧道结账?

这种技术非常类似于第1方式。 首先,你需要创建2路到仓库:

# deploy.rb
set :local_repository, "ssh://git@serverbehindfirewall/path/to/project.git"
set :repository,  "ssh://git@localhost:9000/path/to/project.git"

然后你在部署之前,您需要设置远程前锋:

% ssh -R 9000:serverbehindfirewall:22 deploybot@deployserver.com
# CTRL + C + A (Screen) or ⌘ + T (Terminal.app) to open new tab

其次是你的部署:

% cap HOSTFILTER=deployserver.com deploy # HOSTFILTER reduces set to specified host. Only useful if you have multiple servers.

看到这个答案为更多的细节,SO问题:

  • https://stackoverflow.com/a/3953351/33204


Answer 2:

使用Capistrano的3.x中,对我下面的工作:

namespace :deploy do
  desc "Open SSH Tunnel to GitLab"
  task :open_tunnel do
    on roles(:app) do
      info "Opening SSH Remote Tunnel..."
      self.send(:with_ssh) do |ssh|
        # ssh -R 9000:192.168.1.123:22
        ssh.forward.remote(22, "192.168.1.123", 9000)
      end
    end
  end
  before "deploy:check", "deploy:open_tunnel"
end

请注意, ssh.forward.remote预计的不同的次序参数ssh -R ,上面的是相当于ssh -R 9000:192.168.1.123:22

该任务调用私有方法,如果有谁知道一个正式的方式来获得访问Capistrano酒店的ssh连接,请评论或编辑。

编辑:另见隧道等相关SSH主题 SSHKit的README的



文章来源: Capistrano, Firewalls and Tunnel