Ruby on Rails + Michael Hartl tutorial + VPS = issues with AWS - ruby-on-rails

I have finished Michael Hartl tutorial and I'm trying to deploy it with https://www.digitalocean.com/community/tutorials/how-to-deploy-a-rails-app-with-puma-and-nginx-on-ubuntu-14-04. Unfortunately I'm stuck with RAILS_ENV=production rake db:migrate because it's generating following error:
ArgumentError: Missing required arguments: aws_access_key_id, aws_secret_access_key
/home/me/simpleapp/config/initializers/carrier_wave.rb:3:in `block in <top (required)>'
/home/me/simpleapp/config/initializers/carrier_wave.rb:2:in `<top (required)>'
/home/me/simpleapp/config/environment.rb:5:in `<top (required)>
'
I have even copied those files 1:1 and still this error persists. Any idea how could I solve it? I really am tired with trying to fix it with different solutions from web.
Thanks
edit: I am adding my carrier_wave file:
if Rails.env.production?
CarrierWave.configure do |config|
config.fog_credentials = {
# Configuration for Amazon S3
:provider => 'AWS',
:aws_access_key_id => ENV['S3_ACCESS_KEY'],
:aws_secret_access_key => ENV['S3_SECRET_KEY']
}
config.fog_directory = ENV['S3_BUCKET']
end
end

The root issue is that Carrier Wave is expecting your AWS environment variables to be populated but they are not set in your production environment.
I would recommend you look at something like the dotenv gem which can find here. Environment variable housekeeping in multiple environments can be a real pain so it helps to have a tool to facilitate. Dotenv (and others like it) provides more of a turn key approach to this.
Note that the recently introduced secrets.yml for Rails secret management is a nice step in the right direction, but still requires a bit of code/know how to utilize.
In any event, be very careful about how you manage any files with secrets. At a minimum, the file should be in your .gitignore so that you aren't potentially broadcasting your secrets.

Related

Paperclip config conflicting with AWS Elastic Beanstalk

I am using Paperclip in my Rails 5.1 app and have the standard config in my development.rb and production.rb:
config.paperclip_defaults = {
storage: :s3,
s3_credentials: {
bucket: ENV.fetch('S3_BUCKET'),
access_key_id: ENV.fetch('AWS_ACCESS_KEY_ID'),
secret_access_key: ENV.fetch('AWS_SECRET_ACCESS_KEY'),
s3_region: ENV.fetch('AWS_REGION'),
}
}
I'm storing the ENV variables in application.yml via Figaro gem.
This is fine, but I'm now trying to upload my app to a new AWS Elastic Beanstalk app and I'm having the build fail. Having spent the last 3 or so hours looking through all the logs and trying various changes, I've narrowed the failure down to the fact I think Beanstalk is reading the Paperclip ENVs and is getting confused.
From the logs:
rake aborted!
<<<
/var/app/ondeck/config/environments/production.rb:97:in `fetch'
/var/app/ondeck/config/environments/production.rb:97:in `block in <top (required)>'
/var/app/ondeck/config/environments/production.rb:1:in `<top (required)>'
/var/app/ondeck/config/environment.rb:5:in `<top (required)>'
/opt/rubies/ruby-2.4.3/bin/bundle:23:in `load'
/opt/rubies/ruby-2.4.3/bin/bundle:23:in `<main>'
Tasks: TOP => environment
and config/environments/production.rb:97 equates the below value from my Paperclip config:
access_key_id: ENV.fetch('AWS_ACCESS_KEY_ID')
I can't seem to find any items online that would help resolve this.
Any ideas?
Could I use the same IAM user for both so the ENV values match?
Add the paperclip S3 environment variables to your Elastic Beanstalk Environment
How?
Head over to your specific Environment
Select Configuration
In Configuration overview select Software tile
At the very bottom you'll see a form section Environment properties
Add key and value data for your variables
Save and wait for your environment to update.
Read more: Elastic Beanstalk Docs

Docker compose-build with rake assets:precompile

Im trying to set up my app to run in production mode and Ive hit a problem with building the assets folder specifically this line in my Dockerfile:
RUN bundle exec rake RAILS_ENV=production DATABASE_URL=postgresql://user:pass#127.0.0.1/dbname SECRET_TOKEN=dummytoken assets:precompile
The database is just a dummy line. The problem is when it runs the rake it seems to not see the env variables and I get the following errors when it goes to initialize carrierwave.rb
rake aborted!
ArgumentError: Missing required arguments: aws_access_key_id, aws_secret_access_key
/usr/local/bundle/gems/fog-core-1.42.0/lib/fog/core/service.rb:244:in `validate_options'
/usr/local/bundle/gems/fog-core-1.42.0/lib/fog/core/service.rb:268:in `handle_settings'
/usr/local/bundle/gems/fog-core-1.42.0/lib/fog/core/service.rb:98:in `new'
/usr/local/bundle/gems/fog-core-1.42.0/lib/fog/core/services_mixin.rb:16:in `new'
/usr/local/bundle/gems/fog-core-1.42.0/lib/fog/storage.rb:27:in `new'
/usr/local/bundle/gems/carrierwave-0.11.2/lib/carrierwave/uploader/configuration.rb:83:in `eager_load_fog'
/usr/local/bundle/gems/carrierwave-0.11.2/lib/carrierwave/uploader/configuration.rb:96:in `fog_credentials='
/mnt/hgfs/Projects/livingrecipe/config/initializers/carrierwave.rb:3:in `block in <top (required)>'
/usr/local/bundle/gems/carrierwave-0.11.2/lib/carrierwave/uploader/configuration.rb:118:in `configure'
/usr/local/bundle/gems/carrierwave-0.11.2/lib/carrierwave.rb:14:in `configure'
/mnt/hgfs/Projects/livingrecipe/config/initializers/carrierwave.rb:1:in `<top (required)>'
Theres a few more lines to the error but the first line says it all and the ENV variables for the aws_access_key_id and aws_secret_access_key dont seem to load up. If I run this without trying to precompile the assets everything works but I need to precompile the assets to make them visible to nginx
I did find that for a fix I could replace the ENV variables with what they are, this is the code that is having the trouble:
CarrierWave.configure do |config|
config.fog_credentials = {
provider: 'AWS', # required
aws_access_key_id: ENV['AWS_ACCESS_KEY_ID'], # required
aws_secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'], # required
region: 'us-west-2' # optional, defaults to 'us-east-1'
}
config.fog_directory = ENV['S3_BUCKET_NAME'] # required
#config.fog_host = 'https://assets.example.com' # optional, defaults to nil
#config.fog_public = false # optional, defaults to true
config.fog_attributes = {'Cache-Control'=>'max-age=315576000'} # optional, defaults to {}
end
So I got it to work with just typing in the keys...but obviously that isnt a good solution long term to have the keys programmed in.
Your question has shifted a bit, so I'm going to address your last sentence:
So I got it to work with just typing in the keys...but obviously that isn't a good solution long term to have the keys programmed in.
If you're sure you want to handle this during your build, as opposed to adding your rake task to your command entry, you can set build args in your docker-compose.yml config file.
# compose.yml
version: '2'
services:
app:
# ...
build:
context: .
args:
# This will make your variables available during the
# "build" phase.
# You can hardcode these values here, or better,
# add them to a .env file, whose contents
# Docker/Compose will make available during the build.
- AWS_ACCESS_KEY_ID
- AWS_SECRET_ACCESS_KEY
- DATABASE_URL
- SECRET_TOKEN
environment:
# You should also add these values to your application's
# environment.
# You can hardcode these values here, or better,
# add them to a .env file, whose contents
# Docker/Compose will make available to your running container.
- AWS_ACCESS_KEY_ID
- AWS_SECRET_ACCESS_KEY
- DATABASE_URL
- SECRET_TOKEN
You can then declare and use the build args in your Dockerfile:
# Dockerfile
# ...
ARG AWS_ACCESS_KEY_ID
ARG AWS_SECRET_ACCESS_KEY
ARG DATABASE_URL
ARG SECRET_TOKEN
# these values will now be available to your rake task
# in ENV['AWS_ACCESS_KEY_ID'], etc.
RUN bundle exec rake RAILS_ENV=production assets:precompile

Mongoid::Errors::NoSessionConfig in rails consolse

I clone rails app (our company project) and then install everything that related to mongo db and mongoid gem. Then I run the rake db:setup and then rails s. Its working fine, I can access all the sites and do everything. But I got problem in the rails console.
Whenever I do this:
rails c
then
User.first
I will get this error
Loading development environment (Rails 4.1.1) irb(main):001:0>
User.first Mongoid::Errors::NoSessionConfig: Problem: No
configuration could be found for a session named 'default'. Summary:
When attempting to create the new session, Mongoid could not find a
session configuration for the name: 'default'. This is necessary in
order to know the host, port, and options needed to connect.
Resolution: Double check your mongoid.yml to make sure under the
sessions key that a configuration exists for 'default'. If you have
set the configuration programatically, ensure that 'default' exists in
the configuration hash. from
/Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/mongoid-4.0.0.beta2/lib/mongoid/sessions/factory.rb:27:in
create' from
/Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/mongoid-4.0.0.beta2/lib/mongoid/sessions.rb:65:in
with_name' from
/Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/mongoid-4.0.0.beta2/lib/mongoid/sessions.rb:105:in
mongo_session' from
/Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/mongoid-4.0.0.beta2/lib/mongoid/sessions.rb:121:in
collection' from
/Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/mongoid-4.0.0.beta2/lib/mongoid/sessions/options.rb:161:in
method_missing' from
/Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/mongoid-4.0.0.beta2/lib/mongoid/contextual/mongo.rb:263:in
initialize' from
/Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/mongoid-4.0.0.beta2/lib/mongoid/contextual.rb:53:in
new' from
/Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/mongoid-4.0.0.beta2/lib/mongoid/contextual.rb:53:in
create_context' from
/Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/mongoid-4.0.0.beta2/lib/mongoid/contextual.rb:35:in
context' from
/Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/mongoid-4.0.0.beta2/lib/mongoid/contextual.rb:20:in
first' from
/Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/mongoid-4.0.0.beta2/lib/mongoid/findable.rb:122:in
first' from (irb):1 from
/Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/railties-4.1.1/lib/rails/commands/console.rb:90:in
start' from
/Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/railties-4.1.1/lib/rails/commands/console.rb:9:in
start' from
/Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/railties-4.1.1/lib/rails/commands/commands_tasks.rb:69:in
console' from
/Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/railties-4.1.1/lib/rails/commands/commands_tasks.rb:40:in
run_command!' from
/Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/railties-4.1.1/lib/rails/commands.rb:17:in
' from
/Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:247:in require' from
/Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:247:inblock in require' from
/Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:232:in load_dependency' from
/Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:247:inrequire' from
/Users/rizalmuthi/Documents/Sites/WORK/tapway/bin/rails:8:in <top
(required)>' from
/Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:241:inload' from
/Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:241:in block in load' from
/Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:232:inload_dependency' from
/Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:241:in load' from
/Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in
require' from
/Users/rizalmuthi/.rbenv/versions/2.1.1/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in
require' from -e:1:in'irb(main):002:0>
And FYI, this is the mongoid.yml that I generated by run
rails g mongoid:config
mongoid.yml
development:
sessions:
default:
database: mongo_demo_development
hosts:
- localhost:27017
options:
options:
test:
sessions:
default:
database: mongo_demo_test
hosts:
- localhost:27017
options:
consistency: :strong
max_retries: 1
retry_interval: 0
I have been looking all over google and some blogs, could not figure it out how to fix this.
Besides that, we dont use the Rspec tho.
change your config/application.rb file:
replace it Bundler.require(*Rails.groups)
to it Bundler.require(*Rails.groups(assets: %w(development test)))
In the latest mongoid -5.0.0 version you can simply solve by placing the below code in application.rb
config.generators do |g|
g.orm :mongoid
end
In config/application.rb change
Bundler.require(*Rails.groups)
to:
Bundler.require(:default, Rails.env)
Rails is trying to eager load the group of gems by environment and the stock config/mongoid.yml file you get when you run rails g mongoid:config only gives you a test and development namespace. Even though you're only running development, it's looking at all the groups. If you add the production namespace to the yml file it will load but my answer above works without you needing to do that.
Just make sure that on application initialization you do the following (in Rails console)
Mongoid.load!("path/to/your/mongoid.yml")
For more details visit http://mongoid.org/en/mongoid/docs/installation.html
With MongoId version 5.x, You will change config in Rails application follow:
File: application.rb
# Add line.
config.generators do |g|
g.orm :mongoid
end
I was set it and it working in rails console.
See more at:
https://docs.mongodb.com/ecosystem/tutorial/mongoid-installation/#rails-applications
may be The problem is in the gem file, you did't mention the version of mongoid gem.
in gem file change
gem "mongoid"
to
gem "mongoid" , '~> 4.0.2'
and run bundle install
restart server and console.

How do I set secrets

Long story short, I decided to use VM for development in addition to my local machine.
So when I pulled my source code inside that VM and ran rspec I received following output:
action#rails:~/workspace(master)$ rspec
/home/action/.rvm/gems/ruby-2.0.0-p451/gems/devise-3.2.3/lib/devise/rails/routes.rb:481:in `raise_no_secret_key': Devise.secret_key was not set. Please add the following to your Devise initializer: (RuntimeError)
config.secret_key = '...'
I've added the key, but now I have following errors in specs:
2) Password pages user views his passwords
Failure/Error: sign_in user
RuntimeError:
Missing `secret_key_base` for 'test' environment, set this value in `config/secrets.yml`
# ./spec/support/login_macros.rb:3:in `sign_in'
# ./spec/features/account_pages_spec.rb:7:in `block (2 levels) in <top (required)>'
What should be inside that file?
I just installed rails 4.1 and created a new project. The following is the default generated content of config/secrets.yml:
# Be sure to restart your server when you modify this file.
# Your secret key is used for verifying the integrity of signed cookies.
# If you change this key, all old signed cookies will become invalid!
# Make sure the secret is at least 30 characters and all random,
# no regular words or you'll be exposed to dictionary attacks.
# You can use `rake secret` to generate a secure secret key.
# Make sure the secrets in this file are kept private
# if you're sharing your code publicly.
development:
secret_key_base: 83aa0c7d6e2ed4574099514eb64bc3896fb8a71a344935fbd54705e0dd65adb897bc062fe477d03395a4d65675c833ba73ed340166be3874bfc01f43d6076385
test:
secret_key_base: 513fb7657945b56098db290394bf23f5e11463c473fb228719428a30fd34b8b899dff3f6173c32d7e6bc028dc3276f15dcba11b684d27983d8203fb5634ce8ae
# Do not keep production secrets in the repository,
# instead read values from the environment.
production:
secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>
You can generate a new key using rake secret then updating the value of config.secret_key.
$ rake secret
Use the output of the above command as the value for config.secret_key usually placed in config/initializers/devise.rb for devise. Restart rails server if you are using that as well.

Ruby on rails $ mysql configuration

I want to configure mysql instead of Sqlite3. I had already configured the database.yml file:
development:
adapter: mysql
database: test
username:false
password:neha
host :localhost
port:3306
and also installed the mysql gem using gem install mysql
still i stuck out in error...please help me out
:\rails\TestApp1>ruby script/server
Booting WEBrick
Rails 2.3.11 application starting on http://0.0.0.0:3000
:/Ruby192/lib/ruby/1.9.1/syck.rb:135:in load': syntax error on line 13, col 0:test:' (ArgumentError)
from C:/Ruby192/lib/ruby/1.9.1/syck.rb:135:in load'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/rails-2.3.11/lib/initializer.rb:926:indatabase_configuration'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/rails-2.3.11/lib/initializer.rb:437:in initialize_database'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/rails-2.3.11/lib/initializer.rb:141:inprocess'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/rails-2.3.11/lib/initializer.rb:113:in run'
from C:/rails/TestApp1/config/environment.rb:9:in'
from :29:in require'
from <internal:lib/rubygems/custom_require>:29:inrequire'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/activesupport-2.3.11/lib/active_support/dependencies.rb:182:in block in require'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/activesupport-2.3.11/lib/active_support/dependencies.rb:547:innew_constants_in'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/activesupport-2.3.11/lib/active_support/dependencies.rb:182:in require'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/rails-2.3.11/lib/commands/server.rb:84:in'
from :29:in require'
from <internal:lib/rubygems/custom_require>:29:inrequire'
from script/server:3:in `'
i got this error
#d11wtq:
here is my yml file for mysql configuration:
ActiveRecord::Base.establish_connection(
:adapter => "mysql",
:host => "localhost",
:username => "root",
:password => "neha",
:database => "ruby"
)
& where i have to put
require 'mysql'
Looking at that log output you've now posted, it looks like you have haven't formatted that .yml file correctly. The use of whitespace in YAML is absolutely critical. Indent with spaces, not tabs, and make sure your indentation is consistent within each level. It appears that the lines are all aligned differently in your file, which is causing syck (the YAML parser) to throw an Exception.
EDIT | You also want to make the spacing between the key, the semi-colon and the value consistent too. Take pride in the layout of your code and you'll enjoy coding more ;) But pride or not... YAML is (intentionally) picky about whitespace.
Have your yml files properly indented and you will not be getting syntax error anymore.
Also add mysql2 compatible version in your Gemfile, so that while you will be deploying it to any server it will take from Gemfile automatically.
Did you add "mysql" to your Gemfile and run "bundle install"?
If you're working on Windows, I know that I didn't have the MySQL C Driver installed on my machine. It's a different download than the MySQL Community Server. You also have to make sure that your PATH has the route to the C driver. Download the driver here.

Resources