Module: Aptible::CLI::Helpers::Database
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
#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
49
50
51
52
53
54
|
# File 'lib/aptible/cli/helpers/database.rb', line 49
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.account).first
end
|
#databases_from_handle(handle, environment) ⇒ Object
34
35
36
37
38
39
40
41
|
# File 'lib/aptible/cli/helpers/database.rb', line 34
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
32
|
# File 'lib/aptible/cli/helpers/database.rb', line 11
def ensure_database(options = {})
db_handle = options[:db]
environment_handle = options[:environment]
raise Thor::Error, 'Database handle not specified' unless db_handle
environment = environment_from_handle(environment_handle)
if environment_handle && !environment
raise 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
raise Thor::Error, "Could not find database #{db_handle}"
else
err = 'Multiple databases exist, please specify with --environment'
raise Thor::Error, err
end
end
|
#find_tunnel_credential(database, type = nil) ⇒ Object
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
# File 'lib/aptible/cli/helpers/database.rb', line 96
def find_tunnel_credential(database, type = nil)
unless database.provisioned?
raise Thor::Error, "Database #{database.handle} is not provisioned"
end
finder = proc { |c| c.default }
finder = proc { |c| c.type == type } if type
credential = database.database_credentials.find(&finder)
return credential if credential
types = database.database_credentials.map(&:type)
if database.account.stack.version == 'v1'
types << database.type
types.uniq!
return database if type.nil? || type == database.type
end
valid = types.join(', ')
err = 'No default credential for database'
err = "No credential with type #{type} for database" if type
raise Thor::Error, "#{err}, valid credential types: #{valid}"
end
|
#local_url(credential, local_port) ⇒ Object
88
89
90
91
92
93
94
|
# File 'lib/aptible/cli/helpers/database.rb', line 88
def local_url(credential, local_port)
remote_url = credential.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
43
44
45
46
47
|
# File 'lib/aptible/cli/helpers/database.rb', line 43
def present_environment_databases(environment)
say "=== #{environment.handle}"
environment.databases.each { |db| say db.handle }
say ''
end
|
#with_local_tunnel(credential, port = 0) ⇒ Object
Creates a local tunnel and yields the helper
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
# File 'lib/aptible/cli/helpers/database.rb', line 58
def with_local_tunnel(credential, port = 0)
op = credential.create_operation!(type: 'tunnel', status: 'succeeded')
with_ssh_cmd(op) do |base_ssh_cmd, ssh_credential|
ssh_cmd = base_ssh_cmd + ['-o', 'SendEnv=ACCESS_TOKEN']
ssh_env = { 'ACCESS_TOKEN' => fetch_token }
socket_path = ssh_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
76
77
78
79
80
81
82
83
84
85
86
|
# File 'lib/aptible/cli/helpers/database.rb', line 76
def with_postgres_tunnel(database)
if database.type != 'postgresql'
raise Thor::Error, 'This command only works for PostgreSQL'
end
credential = find_tunnel_credential(database)
with_local_tunnel(credential) do |tunnel_helper|
yield local_url(credential, tunnel_helper.port)
end
end
|