Sha256: 1f9ea8e23386556b619d088bb4b1bccb089398776a82e4264d2f99c93a9c3399

Contents?: true

Size: 1.65 KB

Versions: 3

Compression:

Stored size: 1.65 KB

Contents

module Aptible
  module CLI
    module Subcommands
      module Tunnel
        # rubocop:disable MethodLength
        def self.included(thor)
          thor.class_eval do
            include Helpers::Operation
            include Helpers::Token

            desc 'tunnel DATABASE', 'Create a local tunnel to a database'
            def tunnel(handle)
              database = database_from_handle(handle)
              unless database
                fail Thor::Error, "Could not find database #{handle}"
              end
              host = database.account.bastion_host
              port = database.account.bastion_port

              ENV['ACCESS_TOKEN'] = fetch_token
              ENV['APTIBLE_DATABASE'] = handle
              tunnel_args = "-L #{local_port}:localhost:#{remote_port}"
              connection_args = "-o 'SendEnv=*' -p #{port} root@#{host}"
              puts "Creating tunnel at localhost:#{local_port}..."
              Kernel.exec "ssh #{tunnel_args} #{connection_args}"
            end

            private

            def database_from_handle(handle)
              Aptible::Api::Database.all(token: fetch_token).find do |a|
                a.handle == handle
              end
            end

            def local_port
              return @local_port if @local_port

              # Allocate a dummy server to discover an available port
              dummy = TCPServer.new('127.0.0.1', 0)
              port = dummy.addr[1]
              dummy.close
              @local_port = port
            end

            def remote_port
              8080
            end
          end
        end
        # rubocop:enable MethodLength
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
aptible-cli-0.3.6 lib/aptible/cli/subcommands/tunnel.rb
aptible-cli-0.3.5 lib/aptible/cli/subcommands/tunnel.rb
aptible-cli-0.3.4 lib/aptible/cli/subcommands/tunnel.rb