Module: Aptible::CLI::Subcommands::Tunnel
- Included in:
- Agent
- Defined in:
- lib/aptible/cli/subcommands/tunnel.rb
Class Method Summary collapse
-
.included(thor) ⇒ Object
rubocop:disable MethodLength.
Class Method Details
.included(thor) ⇒ Object
rubocop:disable MethodLength
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/aptible/cli/subcommands/tunnel.rb', line 6 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 |