class PlanetScale::Proxy

Constants

AUTH_AUTO
AUTH_PLANETSCALE
AUTH_SERVICE_TOKEN
AUTH_STATIC
PLANETSCALE_FILE

Public Class Methods

new(auth_method: AUTH_AUTO, **kwargs) click to toggle source
# File lib/planetscale.rb, line 35
def initialize(auth_method: AUTH_AUTO, **kwargs)
  @auth_method = auth_method

  default_file = if defined?(Rails.root)
    File.join(Rails.root, PLANETSCALE_FILE) if defined?(Rails.root)
  else
    PLANETSCALE_FILE
  end

  @cfg_file = kwargs[:cfg_file] || ENV['PLANETSCALE_DB_CONFIG'] || default_file

  @branch_name = kwargs[:branch] || ENV['PLANETSCALE_DB_BRANCH']
  @branch = lookup_branch

  @db_name = kwargs[:db] || ENV['PLANETSCALE_DB']
  @db = lookup_database

  @org_name = kwargs[:org] || ENV['PLANETSCALE_ORG']
  @org = lookup_org

  raise ArgumentError, 'missing required configuration variables' if [@db, @branch, @org].any?(&:nil?)

  @token_name = kwargs[:token_id] || ENV['PLANETSCALE_TOKEN_NAME']
  @token = kwargs[:token] || ENV['PLANETSCALE_TOKEN']

  if @token && @token_name && auto_auth?
    @auth_method = AUTH_SERVICE_TOKEN
  end

  raise ArgumentError, 'missing configured service token auth' if token_auth? && [@token_name, @token].any?(&:nil?)

  @priv_key = kwargs[:private_key]
  @cert_chain = kwargs[:cert_chain]
  @remote_addr = kwargs[:remote_addr]
  @certificate = kwargs[:certificate]
  @port = kwargs[:port]

  if local_auth? && [@priv_key, @certificate, @cert_chain, @remote_addr, @port].any?(&:nil?)
    raise ArgumentError, 'missing configuration options for auth'
  end

  @listen_addr = kwargs[:listen_addr] || ENV['LISTEN_ADDR']
end

Public Instance Methods

start() click to toggle source
# File lib/planetscale.rb, line 79
def start
  ret = case @auth_method
  when AUTH_PLANETSCALE
    startfromenv(@org, @db, @branch, @listen_addr)
  when AUTH_AUTO
    startfromenv(@org, @db, @branch, @listen_addr)
  when AUTH_SERVICE_TOKEN
    startfromtoken(@token_name, @token, @org, @db, @branch, @listen_addr)
  when AUTH_STATIC
    startfromstatic(@org, @db, @branch, @priv_key, @certificate, @cert_chain, @remote_addr, @port, @listen_addr)
  end
  @err = ret[:r1].null? ? nil : ret[:r1].read_string
  raise(ProxyError, @err) if @err
end

Private Instance Methods

auto_auth?() click to toggle source
# File lib/planetscale.rb, line 133
def auto_auth?
  @auth_method == AUTH_AUTO
end
cfg_file() click to toggle source
# File lib/planetscale.rb, line 117
def cfg_file
  @cfg ||= YAML.safe_load(File.read(@cfg_file))
end
env_auth?() click to toggle source
# File lib/planetscale.rb, line 125
def env_auth?
  @auth_method == AUTH_PLANETSCALE
end
local_auth?() click to toggle source
# File lib/planetscale.rb, line 121
def local_auth?
  @auth_method == AUTH_STATIC
end
lookup_branch() click to toggle source
# File lib/planetscale.rb, line 96
def lookup_branch
  return @branch_name if @branch_name
  return nil unless File.exist?(@cfg_file)

  cfg_file['branch']
end
lookup_database() click to toggle source
# File lib/planetscale.rb, line 103
def lookup_database
  return @db_name if @db_name
  return nil unless File.exist?(@cfg_file)

  cfg_file['database']
end
lookup_org() click to toggle source
# File lib/planetscale.rb, line 110
def lookup_org
  return @org_name if @org_name
  return nil unless File.exist?(@cfg_file)

  cfg_file['org']
end
token_auth?() click to toggle source
# File lib/planetscale.rb, line 129
def token_auth?
  @auth_method == AUTH_SERVICE_TOKEN
end