Module: Aptible::CLI::Helpers::Database

Includes:
Environment, Ssh, Token
Defined in:
lib/aptible/cli/helpers/database.rb

Constant Summary

Constants included from Token

Token::TOKEN_ENV_VAR

Instance Method Summary collapse

Methods included from Ssh

#connect_to_ssh_portal, #with_ssh_cmd

Methods included from Environment

#ensure_default_environment, #ensure_environment, #environment_from_handle, #scoped_environments

Methods included from Token

#current_token_hash, #fetch_token, #save_token, #token_file

Instance Method Details

#clone_database(source, dest_handle) ⇒ Object



48
49
50
51
52
53
# File 'lib/aptible/cli/helpers/database.rb', line 48

def clone_database(source, dest_handle)
  op = source.create_operation!(type: 'clone', handle: dest_handle)
  attach_to_operation_logs(op)

  databases_from_handle(dest_handle, source.).first
end

#databases_from_handle(handle, environment) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/aptible/cli/helpers/database.rb', line 33

def databases_from_handle(handle, environment)
  if environment
    databases = environment.databases
  else
    databases = Aptible::Api::Database.all(token: fetch_token)
  end
  databases.select { |a| a.handle == handle }
end

#ensure_database(options = {}) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/aptible/cli/helpers/database.rb', line 11

def ensure_database(options = {})
  db_handle = options[:db]
  environment_handle = options[:environment]

  fail Thor::Error, 'Database handle not specified' unless db_handle

  environment = environment_from_handle(environment_handle)
  if environment_handle && !environment
    fail Thor::Error, "Could not find environment #{environment_handle}"
  end
  databases = databases_from_handle(db_handle, environment)
  case databases.count
  when 1
    return databases.first
  when 0
    fail Thor::Error, "Could not find database #{db_handle}"
  else
    err = 'Multiple databases exist, please specify with --environment'
    fail Thor::Error, err
  end
end

#local_url(database, local_port) ⇒ Object



87
88
89
90
91
92
93
# File 'lib/aptible/cli/helpers/database.rb', line 87

def local_url(database, local_port)
  remote_url = database.connection_url
  uri = URI.parse(remote_url)

  "#{uri.scheme}://#{uri.user}:#{uri.password}@" \
  "localhost.aptible.in:#{local_port}#{uri.path}"
end

#present_environment_databases(environment) ⇒ Object



42
43
44
45
46
# File 'lib/aptible/cli/helpers/database.rb', line 42

def present_environment_databases(environment)
  say "=== #{environment.handle}"
  environment.databases.each { |db| say db.handle }
  say ''
end

#with_local_tunnel(database, port = 0) ⇒ Object

Creates a local tunnel and yields the helper



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/aptible/cli/helpers/database.rb', line 57

def with_local_tunnel(database, port = 0)
  op = database.create_operation!(type: 'tunnel', status: 'succeeded')

  with_ssh_cmd(op) do |base_ssh_cmd, credential|
    ssh_cmd = base_ssh_cmd + ['-o', 'SendEnv=ACCESS_TOKEN']
    ssh_env = { 'ACCESS_TOKEN' => fetch_token }

    socket_path = credential.ssh_port_forward_socket
    tunnel_helper = Helpers::Tunnel.new(ssh_env, ssh_cmd, socket_path)

    tunnel_helper.start(port)
    yield tunnel_helper if block_given?
    tunnel_helper.stop
  end
end

#with_postgres_tunnel(database) ⇒ Object

Creates a local PG tunnel and yields the url to it



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/aptible/cli/helpers/database.rb', line 75

def with_postgres_tunnel(database)
  if database.type != 'postgresql'
    fail Thor::Error, 'This command only works for PostgreSQL'
  end

  with_local_tunnel(database) do |tunnel_helper|
    auth = "aptible:#{database.passphrase}"
    host = "localhost.aptible.in:#{tunnel_helper.port}"
    yield "postgresql://#{auth}@#{host}/db"
  end
end