Capistrano Rails deployment with Nginx and Passenger: database credentials being ignored - ruby-on-rails

I'm trying to deploy my first Rails 4 app on an EC2 instance with Capistrano 3. I've followed several tutorials on how to setup the web server with Nginx and Passenger on Ubuntu. I also have a RDS instance for the database with MySQL. But when I try to run cap deploy it gives me an error at the assets:precompile phase:
INFO [d5d05621] Running ~/.rbenv/bin/rbenv exec bundle exec rake assets:precompile as deploy#[EC2 PUBLIC IP]
(Backtrace restricted to imported tasks)
cap aborted!
SSHKit::Runner::ExecuteError: Exception while executing as deploy#[EC2 PUBLIC IP]: rake exit status: 1
rake stdout: Nothing written
rake stderr: rake aborted!
Mysql2::Error: Access denied for user 'root'#'[EC2 PRIVATE IP]' (using password: NO)
From what I understand, it's trying to connect to the database on localhost with the credentials from the development environment.
Here is what my database.yml file looks like:
development:
adapter: mysql2
encoding: utf8
pool: 5
socket: /tmp/mysql.sock
username: root
password:
database: Store_development
production:
adapter: mysql2
encoding: utf8
database: Store_production
username: <%= ENV['STORE_DATABASE_USERNAME'] %>
password: <%= ENV['STORE_DATABASE_PASSWORD'] %>
host: [RDS INSTANCE ENDPOINT]
port: 3306
pool: 5
timeout: 5000
I looked all over to search for all the places where I could specify the environment, and I ended up having something like this. In my deploy/production.rb I have:
set :stage, :production
set :rails_env, :production
I also have this in the http section of /etc/nginx/nginx.conf on my server:
passenger_app_env production;
And this in the virtual host configuration in /etc/nginx/sites-available
passenger_enabled on;
rails_env production;
And finally I have this in the .bashrc of the deploy user:
export RAILS_ENV=production
Am I missing something to make the deploy work for the production environment and connect to the RDS instance with the credentials from the database.yml file? Or is it something else?

Make sure that the STORE_DATABASE_USERNAME and STORE_DATABASE_PASSWORD environment variables are set on the server and visible to your Rails app. When you SSH into the EC2 instance and run the following do you get the expected values?
echo $STORE_DATABASE_USERNAME
echo $STORE_DATABASE_PASSWORD
If not, add exports for these to the .bashrc of the deploy user:
export STORE_DATABASE_USERNAME=< your username >
export STORE_DATABASE_PASSWORD=< your password >

Related

Migrating from SQLite to Postgres for deployment on Heroku. PG::ConnectionBad. Using Cloud9

I'm trying to migrate from SQLite to Postgres so the app deploys correctly on Heroku. But I'm not having any luck.
rake aborted!
PG::ConnectionBad: could not connect to server: Connection refused
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 5432?
I'm new to Postgres but I've tried every suggestion I've come across with no luck. Here's my database.yaml file:
default: &default
adapter: postgresql
encoding: unicode
pool: 5
timeout: 5000
host: localhost
username: postgres
password: postgres
development:
<<: *default
database: app_development
test:
<<: *default
database: app_test
production:
<<: *default
database: app_production
I believe I have the empty database setup correctly as it is in the Postgres list, but running rake db:migrate fails.
Any help is greatly appreciated.
If you haven't yet run:
sudo service postgresql start
in the cloud9 terminal
then switch to your postgres user and verify that you have the database you specified in your database.yaml created
sudo su - postgres
psql
then in the psql prompt enter the following command to list all available databases
\list
If you need to create a database you can do so by typing
CREATE DATABASE "database_name";
To set the password for the postgres user use
\password postgres
After going through the above steps make sure your database.yaml file has the correct database name, username, and password and try running
rake db:migrate
If the database is setup correctly and the database.yaml file is configured
Try restarting the postgres server
sudo /etc/init.d/postgresql restart
Then run rake db:migrate again
Sources
Cloud9 docs related to setting up PostgresSQL
Cloud9 Forum Post related to setting up PostgreSQL in a Rails app
Stackoverflow post related to changing psql password
Stackoverflow post related to restarting the PostgreSQL server
Alternative
Another way I've managed to push apps that I've started developing with SQLite to heroku, is to move the SQLite gem into the development group in my Gemfile, and add a production group with the heroku dependencies:
group :production do
gem 'rails12_factor'
gem 'pg'
end
Then in development bundle install without the production group:
bundle install --without production
And do normal bundle install when I push to heroku

rails is not using the configuration set on database.yaml

This is my database.yml
production:
adapter: mysql2
encoding: utf8
database: testing
pool: 5
username: ubuntu
host: 127.0.0.1
When I run rake db:setup I got this:
Couldn't create database for {"adapter"=>"mysql2", "encoding"=>"utf8", "database"=>"testing", "pool"=>5, "username"=>"ubuntu", "host"=>"127.0.0.1"}
-- create_table("documents", {:force=>:cascade})
rake aborted!
Mysql2::Error: Access denied for user ''#'localhost' to database 'testing'
Why it's not using my username and host? instead is showing '' # 'localhost'
I think, database.yml config for production environment, but you run it on development environment. You can use 'RAILS_ENV=development rake db:setup' or 'RAILS_ENV=test rake db:setup'
One more thing, database should be created by mysql user with root privileges

database configuration does not specify adapter when preparing asset precompile

I recently switched my test database to be postgrsql from sqlite3. Since doing so, when I follow the instruction here: https://devcenter.heroku.com/articles/rails-asset-pipeline on deploying my assets to heroku, I get the following error:
database configuration does not specify adapter
This happens after running this step
RAILS_ENV=production bundle exec rake assets:precompile
I tried following the instructions from this post and it did not work.
bundle exec rake assets:precompile - database configuration does not specify adapter
I'm guessing it has something to do with my database.yml file, which is here
development:
adapter: postgresql
database: playerpong_database
pool: 5
timeout: 5000
encoding: unicode
host: localhost
Any ideas?
I figured it out. I added a production section to my database.yml file.
production:
adapter: postgresql
database: playerpong_database
pool: 5
timeout: 5000
encoding: unicode
host: my_app_url
I didn't think this was needed.
Just pass a dummy database as mentioned in this article:
https://iprog.com/posting/2013/07/errors-when-precompiling-assets-in-rails-4-0
The command is: bundle exec rake RAILS_ENV=production DATABASE_URL=postgresql://user:pass#127.0.0.1/dbname assets:precompile
The problem is you are running
RAILS_ENV=production bundle exec rake assets:precompile
where as your file has development adapter , and so the error comes to be like the adapter is missing.
You need to either change the environment article to development
RAILS_ENV=development bundle exec rake assets:precompile
or change the development directive to production
production:
adapter: postgresql
database: playerpong_database
pool: 5
timeout: 5000
encoding: unicode
host: localhost

Specify a branch in Git for staging in Capistrano multistage extension in Rails

I am currently using multistage extension in Capistrano to deploy my code. The two stages I have are "production" and "staging".
Now I would like to be able to specify one of my branches in Git to be used for deployment in staging. Currently, both prod and staging are deployed off of master - but I would like to implement Gitflow and use my develop branch for my staging environment.
I've looked around and found this to be the answer in my staging.rb file:
set :rails_env, "staging"
set :deploy_to, "/path/to/project"
set :branch, "develop"
But once I include this and try to run cap staging deploy, I get this error:
* executing "cd -- /path/to/project/releases/201309131 && bundle exec rake RAILS_ENV=staging RAILS_GROUPS=assets assets:precompile && cp -- /path/to/project/shared/assets/manifest.yml /path/to/project/releases/201309131/assets_manifest.yml"
servers: \["10.1.171.106"\]
\[10.1.171.106\] executing command
** \[out :: 10.1.171.106\] rake aborted!
** \[out :: 10.1.171.106\] database configuration does not specify adapter
I see this question: Thin / Capistrano cannot connect to database
But when I look at my tasks they look just like this:
task :start do ; end
task :stop do ; end
I wasn't the one who set these environments up nor am I very familiar with Capistrano, so I'm not so sure whats happening here.
Any ideas about what is going on?
EDIT: Including my database.yml
# MySQL. Versions 4.1 and 5.0 are recommended.
#
# Install the MYSQL driver
# gem install mysql2
#
# Ensure the MySQL gem is defined in your Gemfile
# gem 'mysql2'
#
# And be sure to use new-style password hashing:
# http://dev.mysql.com/doc/refman/5.0/en/old-client.html
development:
adapter: mysql2
encoding: utf8
reconnect: false
database: project_development
pool: 5
username: user
password:
socket: /tmp/mysql.sock
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
adapter: mysql2
encoding: utf8
reconnect: false
database: project_test
pool: 5
username: user
password:
socket: /tmp/mysql.sock
production:
adapter: mysql2
encoding: utf8
reconnect: false
database: project_production
pool: 5
username: user
password:
socket: /tmp/mysql.sock
The problem is you have no section for staging in your database.yml. Maybe you had one in the other branch and you removed it when you moved over? Either way, if you add one back in, it should eliminate this error message.

Rails database.yml configuration with different sockets on dev, test and production

So I am developing locally using Rails 3.2 and mysql. My local machine is a Mac and my database.yml for development is:
development:
adapter: mysql2
database: dbname
encoding: utf8
host: localhost
port: 3306
timeout: 5000
socket: /tmp/mysql.sock
And for test it's
test:
adapter: mysql2
database: dbname
encoding: utf8
host: localhost
port: 3306
timeout: 5000
socket: /var/lib/mysql/mysql.sock
Test and production servers are on CentOS and the socket works correctly when deploying to them. However I just went to do a manual rake and got the
Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
My site works, but I'm curious if I'm should be handling database.yml separately for deployment since it's somehow looking at development when I run rake?
Looked for a suggestion and didn't see the same issue, apologize in advance if I missed it.
You can specify the Rails environment when you run the Rake task.
rake db:migrate RAILS_ENV=production
I can think of three alternatives:
Change your database.yml and just don't version in the changes
Use capistrano and its shared folder to handle different database.yml
Use an environment variable i.e. ENV['TEST_SOCKET']

Resources