Class: PlanetScale::Proxy

Inherits:
Object
  • Object
show all
Extended by:
FFI::Library
Defined in:
lib/planetscale.rb

Defined Under Namespace

Classes: ProxyError, ProxyReturn

Constant Summary collapse

AUTH_SERVICE_TOKEN =

Use Service Tokens for Auth

1
AUTH_PLANETSCALE =

Use externally configured ‘pscale` auth & org config

2
AUTH_STATIC =

Use a locally provided certificate

3
AUTH_AUTO =

Default. Let the Gem figure it out

4
PLANETSCALE_FILE =
'.pscale.yml'

Instance Method Summary collapse

Constructor Details

#initialize(auth_method: AUTH_AUTO, **kwargs) ⇒ Proxy

Returns a new instance of Proxy.

Raises:

  • (ArgumentError)


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# 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

Instance Method Details

#startObject

Raises:



79
80
81
82
83
84
85
86
87
88
89
90
91
92
# 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