Sha256: 75afeb89608505634ea3ec7b7944c564904fd91cabb1cfd147c961a7e45d62fc

Contents?: true

Size: 1.78 KB

Versions: 1

Compression:

Stored size: 1.78 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}..."
              opts = " -o 'SendEnv=*' -o StrictHostKeyChecking=no " \
                     '-o UserKnownHostsFile=/dev/null'
              Kernel.exec "ssh #{opts} #{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

1 entries across 1 versions & 1 rubygems

Version Path
aptible-cli-0.3.7 lib/aptible/cli/subcommands/tunnel.rb