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

Includes:
Environment, Ssh, Token
Included in:
AppOrDatabase
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, #exit_with_ssh_portal, #with_ssh_cmd

Methods included from ConfigPath

#aptible_config_path

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



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

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



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

Raises:

  • (Thor::Error)


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_credential(database, type = nil) ⇒ Object

Raises:

  • (Thor::Error)


115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/aptible/cli/helpers/database.rb', line 115

def find_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)

  # On v1, we fallback to the DB. We make sure to make --type work, to
  # avoid a confusing experience for customers.
  if database..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

#find_database_image(type, version) ⇒ Object

Raises:

  • (Thor::Error)


143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/aptible/cli/helpers/database.rb', line 143

def find_database_image(type, version)
  available_versions = []

  Aptible::Api::DatabaseImage.all(token: fetch_token).each do |i|
    next unless i.type == type
    return i if i.version == version
    available_versions << i.version
  end

  err = "No Database Image of type #{type} with version #{version}"
  err = "#{err}, valid versions: #{available_versions.join(' ')}"
  raise Thor::Error, err
end

#local_url(credential, local_port) ⇒ Object



106
107
108
109
110
111
112
113
# File 'lib/aptible/cli/helpers/database.rb', line 106

def local_url(credential, local_port)
  remote_url = credential.connection_url

  uri = URI.parse(remote_url)
  domain = credential.database..stack.internal_domain
  "#{uri.scheme}://#{uri.user}:#{uri.password}@" \
  "localhost.#{domain}:#{local_port}#{uri.path}"
end

#render_database(database, account) ⇒ Object



170
171
172
173
174
175
176
# File 'lib/aptible/cli/helpers/database.rb', line 170

def render_database(database, )
  Formatter.render(Renderer.current) do |root|
    root.keyed_object('connection_url') do |node|
      ResourceFormatter.inject_database(node, database, )
    end
  end
end

#replicate_database(source, dest_handle, options) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/aptible/cli/helpers/database.rb', line 50

def replicate_database(source, dest_handle, options)
  replication_params = {
    handle: dest_handle,
    container_size: options[:container_size],
    disk_size: options[:size],
    key_arn: options[:key_arn]
  }.reject { |_, v| v.nil? }

  if options[:logical]
    replication_params[:type] = 'replicate_logical'
    replication_params[:docker_ref] =
      options[:database_image].docker_repo
  else
    replication_params[:type] = 'replicate'
  end

  op = source.create_operation!(replication_params)
  attach_to_operation_logs(op)

  replica = databases_from_handle(dest_handle, source.).first
  attach_to_operation_logs(replica.operations.last)
  replica
end

#validate_image_type(type) ⇒ Object

Raises:

  • (Thor::Error)


157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/aptible/cli/helpers/database.rb', line 157

def validate_image_type(type)
  available_types = []

  Aptible::Api::DatabaseImage.all(token: fetch_token).each do |i|
    return true if i.type == type
    available_types << i.type
  end

  err = "No Database Image of type \"#{type}\""
  err = "#{err}, valid types: #{available_types.uniq.join(', ')}"
  raise Thor::Error, err
end

#with_local_tunnel(credential, port = 0) ⇒ Object

Creates a local tunnel and yields the helper



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/aptible/cli/helpers/database.rb', line 76

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



94
95
96
97
98
99
100
101
102
103
104
# File 'lib/aptible/cli/helpers/database.rb', line 94

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

  credential = find_credential(database)

  with_local_tunnel(credential) do |tunnel_helper|
    yield local_url(credential, tunnel_helper.port)
  end
end