How to set environment variables in Phoenix with Distillery library? - environment-variables

Follow this guide to use Distillery to release a elixir/phoenix project:
https://blog.polyscribe.io/a-complete-guide-to-deploying-elixir-phoenix-applications-on-kubernetes-part-1-setting-up-d88b35b64dcd
At the set config/prod.exs step, the author wrote:
config :myapp, Myapp.Repo,
adapter: Ecto.Adapters.Postgres,
hostname: "${DB_HOSTNAME}",
username: "${DB_USERNAME}",
password: "${DB_PASSWORD}",
database: "${DB_NAME}",
to config database. Here using ${DB_HOSTNAME} type to get environment variables, but not System.get_env("DB_HOSTNAME").
However, when I run MIX_ENV=prod mix release --env=prod and set environment variables local:
REPLACE_OS_VARS=true PORT=4000 HOST=0.0.0.0 SECRET_KEY_BASE=highlysecretkey DB_USERNAME=postgres DB_PASSWORD=postgres DB_NAME=myapp_dev DB_HOSTNAME=localhost ./_build/prod/rel/myapp/bin/myapp foreground
It loops:
12:05:13.671 [error] Postgrex.Protocol (#PID<0.1348.0>) failed to connect: ** (DBConnection.ConnectionError) tcp connect (${DB_HOSTNAME}:5432): non-existing domain - :nxdomain
12:05:13.671 [error] Postgrex.Protocol (#PID<0.1347.0>) failed to connect: ** (DBConnection.ConnectionError) tcp connect (${DB_HOSTNAME}:5432): non-existing domain - :nxdomain
12:05:13.672 [error] Postgrex.Protocol (#PID<0.1344.0>) failed to connect: ** (DBConnection.ConnectionError) tcp connect (${DB_HOSTNAME}:5432): non-existing domain - :nxdomain
12:05:13.672 [error] Postgrex.Protocol (#PID<0.1346.0>) failed to connect: ** (DBConnection.ConnectionError) tcp connect (${DB_HOSTNAME}:5432): non-existing domain - :nxdomain
...
It seems ${DB_HOSTNAME} didn't been known by elixir/phoenix.
I am using Elixir 1.5.2 and Phoenix 1.3 now. The version problem?

I never saw this approach and I am unsure how it is supposed to work. The common way to getting the environment variables at runtime would be:
{:system, "VAR"}
For your case it’d be:
config :myapp, Myapp.Repo,
adapter: Ecto.Adapters.Postgres,
hostname: {:system, "DB_HOSTNAME"},
username: {:system, "DB_USERNAME"},
password: {:system, "DB_PASSWORD"},
database: {:system, "DB_NAME"}

Related

Can't connect Rails app to Postgres using PG gem

I'm trying to connect a rails app to a remote Postgres database (using the PG gem) and getting issues.
I set my database.yaml file
development:
adapter: postgresql
encoding: unicode
database: testdb
username: testuser
password: "*******"
host: **.***.***.***
port: 5432
Then in my rails controller I have this line to connect to the db
conn = PG.connect( :dbname => 'testdb' )
And I get this error in response
"status": "error",
"message": "could not connect to server: No such file or directory\n\tIs the server running locally and accepting\n\tconnections on Unix domain socket \"/var/run/postgresql/.s.PGSQL.5432\"?\n"
I was under the impression that PG would pull from the database connection loaded in rails. If I do an inspect on the db configuration I see that it's pulling the database.yaml parameters
#<ActiveRecord::DatabaseConfigurations::HashConfig:0x00007fd5ae0562d0 #env_name="development", #name="primary", #configuration_hash={:adapter=>"postgresql", :encoding=>"unicode", :database=>"testdb", :username=>"testuser", :password=>"*****", :host=>"**.***.***.***", :port=>5432}>
I'm able to successfully connect though if I pass in the necessary variables or connection string to PG instead
conn = PG::Connection.new( "postgresql://testuser:*******#**.***.***.***:5432/testdb" )

Rails CQL cannot connect to AWS Keyspaces (AWS Cassandra)

I am trying to connect from a Ruby on Rails application to AWS Keyspaces (AWS Cassandra), but I cannot manage to do it. I use the cequel gem and generated the config/cequel.yml which contains a similar thing to the following:
development:
host: "CONTACT_POINT"
username: "USER"
password: "PASS"
port: 9142
keyspace: key_development
max_retries: 3
retry_delay: 0.5
newrelic: true
ssl: true
server_cert: 'config/certs/AmazonRootCA1.pem'
replication:
class: NetworkTopologyStrategy
datacenter1: 3
datacenter2: 2
durable_writes: false
(Credentials where used in another app and they work which is working as expected.)
when I try to run:
rake cequel:keyspace:create
I get the following errors:
Cassandra::Errors::NoHostsAvailable: All attempted hosts failed: x.xxx.xxx.xxx (Cassandra::Errors::ServerError: Internal Server Error)
Set the dc to us-east-1 . drop the replication definition.

How to properly connect ROR with Oracle database with Ruby-OCI8 gem?

I'm trying to use Oracle Database Xe on my ruby on rails app
but I'm having a lot of trouble with my database connection I'm currently not sure what the problem is, but according to what I have read I may have a problem with my TNS setup the error message that I'am having is
OCIError: ORA-12514: TNS:listener does not currently know of service requested in connect descriptor
The error appears every time I try to run rake db:migrate
on my rails console I try to run OCI8.new and it gives me this error
OCIError: ORA-12545: Connect failed because target host or object does not exist
I'm pretty much stuck and I'm really not sure what to do here.
TNS:
METRO=
(description=
(address_list=
(address = (protocol = TCP)(host = 127.0.0.1)(port = 1521))
)
(connect_data =
(service_name=METRO)
)
)
Database.yml :
development:
adapter: oracle_enhanced
database: metro
host: 192.168.18.55
username: metro
password: imperium
Looks like you are missing the port in database.yml file.
development:
adapter: oracle_enhanced
host: localhost
port: 1521
database: xe
username: user
password: secret

Ruby on rails - "cannot load Java class com.ibm.db2.jcc.DB2Driver"

I am using:
Ubuntu 12.04 LTC
JRuby 1.7.2
Rails 3.2.12
DB2 C express 10.1
My database.yml file looks like this:
development:
adapter: jdbc
driver: com.ibm.db2.jcc.DB2Driver
url: localhost:50001/devdb
host: localhost
port: 50001
database: devdb
username: db2inst1
password: mypass
But in when I stats my server and open the home page "localhost:3000/" and click on "About your application’s environment" I get the following error:
cannot load Java class com.ibm.db2.jcc.DB2Driver
Could you advice?
The only thing that I have found as suggestion was to add the driver in my JRUBY_HOME/bin/ folder but this do not solve my issue.
Copy db2jcc.jar and db2jcc_license_cu.jar to jruby/lib directory.
You are getting that error because you need the DB2 Universal JDBC driver jars in your classpath

how to set Seamless database pool read slave database as default?

I have problem with seamless gem
development:
adapter: jdbcmysql
database: mydb_development
username: read_user
password: abc123
pool_adapter: jdbcmysql
port: 3306
master:
host: master-db.example.com
port: 6000
username: master_user
password: 567pass
read_pool:
- host: read-db-1.example.com
pool_weight: 2
- host: read-db-2.example.com
it should read for slave right [read-db-1.example.com] ? but it was weird.. it always read to master database [mydb_development] .
do you have any suggestion, how should i do to configure this gem for default read to slave database?
Thank you
Specify pool_weight=0 in the master configuration
By default, the master connection will be included in the read pool. If you would like to dedicate this connection only for write operations, you should set the pool weight to zero.
seam_leass_database_pool plugin

Resources