Module: Aptible::CLI::Helpers::Database
Constant Summary
Constants included
from Token
Token::TOKEN_ENV_VAR
Instance Method Summary
collapse
#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
47
48
49
50
51
52
|
# File 'lib/aptible/cli/helpers/database.rb', line 47
def clone_database(source, dest_handle)
op = source.create_operation!(type: 'clone', handle: dest_handle)
poll_for_success(op)
databases_from_handle(dest_handle, source.account).first
end
|
#databases_from_handle(handle, environment) ⇒ Object
32
33
34
35
36
37
38
39
|
# File 'lib/aptible/cli/helpers/database.rb', line 32
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
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
# File 'lib/aptible/cli/helpers/database.rb', line 10
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
83
84
85
86
87
88
89
|
# File 'lib/aptible/cli/helpers/database.rb', line 83
def local_url(database, local_port)
remote_url = database.connection_url
uri = URI.parse(remote_url)
"#{uri.scheme}://#{uri.user}:#{uri.password}@" \
"127.0.0.1:#{local_port}#{uri.path}"
end
|
#present_environment_databases(environment) ⇒ Object
41
42
43
44
45
|
# File 'lib/aptible/cli/helpers/database.rb', line 41
def present_environment_databases(environment)
say "=== #{environment.handle}"
environment.databases.each { |db| say db.handle }
say ''
end
|
#ssh_args(database) ⇒ Object
91
92
93
94
95
96
97
98
99
100
|
# File 'lib/aptible/cli/helpers/database.rb', line 91
def ssh_args(database)
host = database.account.bastion_host
port = database.account.bastion_port
['-o', 'SendEnv=APTIBLE_DATABASE',
'-o', 'SendEnv=ACCESS_TOKEN',
'-o', 'StrictHostKeyChecking=no',
'-o', 'UserKnownHostsFile=/dev/null',
'-p', port.to_s, "root@#{host}"]
end
|
#with_local_tunnel(database, port = 0) ⇒ Object
Creates a local tunnel and yields the helper
56
57
58
59
60
61
62
63
64
65
66
67
|
# File 'lib/aptible/cli/helpers/database.rb', line 56
def with_local_tunnel(database, port = 0)
env = {
'ACCESS_TOKEN' => fetch_token,
'APTIBLE_DATABASE' => database.href
}
command = ['ssh', '-q'] + ssh_args(database)
Helpers::Tunnel.new(env, command).tap do |tunnel_helper|
tunnel_helper.start(port)
yield tunnel_helper
tunnel_helper.stop
end
end
|
#with_postgres_tunnel(database) ⇒ Object
Creates a local PG tunnel and yields the url to it
71
72
73
74
75
76
77
78
79
80
81
|
# File 'lib/aptible/cli/helpers/database.rb', line 71
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:#{tunnel_helper.port}"
yield "postgresql://#{auth}@#{host}/db"
end
end
|