How to run seed.rb file on Amazon ec2 - ruby-on-rails

Recently I hosted my Ruby on Rails application on Amazon EC2 using Elastic Beanstalk. Everything works fine except my seeds.rb file. My seeds.rb file is not executed at the time of hosting. I am using ActiveAdmin also and I define first admin on my seeds.rb file.
How can I create first admin user on Amazon by rails console? Is there any way to open Rails Console on Amazon EC2 ? I am trying to do this using putty but don't know how to do this. Please give me some pointers..

Are you not supposed to do something like this?
# .ebextensions/bundles_container.config
container_commands:
01-bundle-install:
command: "bundle install"
leader_only: true
02-bundle-db-migrate:
command: "bundle exec rake db:migrate"
leader_only: true
03-bundle-db-seed:
command: "bundle exec rake db:seed RAILS_ENV='staging'"
leader_only: true
You can also pass parameters if needed, or combine all those commands with "cmd1 && cmd2".

You need to create keypair to access the amazon instance(which i think you already have). Make sure that ssh access is enabled in the current selected security group.
You can connect to the amazon instance using
ssh -i path/to/keypair.pub ec2-user#ec2-an-ip-address.compute-1.amazonaws.com
Then cd into the app directory and run bundle exec rake db:seed RAILS_ENV='staging' assuming that you're running the app in staging environment.

In case you're here and the above solutions didn't work for you.
Apart from using the command provided in this answer above by benchwarmer:
https://stackoverflow.com/a/17232607/1216245
I had to run the seed command providing env vars for the master key and all rds settings.
bundle exec rake db:seed RAILS_ENV=production RAILS_MASTER_KEY=<your master key> RDS_HOSTNAME=<your rds hostname> RDS_PASSWORD=<...> RDS_USERNAME=<...> RDS_DB_NAME=<...> RDS_PORT=<...>
And it worked, finally :)
You can check all this in the Configuration panel for your environment in the AWS console (dashboard).

Related

Is there a way to connect to a Rails app's console on a linux server?

I have deployed a rails application using capistrano on a linux server and it is running without any problems. When I connect to my remote server via ssh, the folder structure of my app is quite different from what I have on my local machine. I would like to go to project root and say rails console so that I can have access to the console of my application. Is there a way to achieve this?
When I go to ~MyApp/ folder and run rails console it says command rails not found. I think that is probably because the app is running in another folder.
bundle exec to the rescue inside of project folded:
$ RAILS_ENV=production bundle exec rails console
If you've installed with rbenv it could be that you are defaulting to the wrong Ruby version. So you can run rbenv exec to get it to run on the right version, along with bundle exec to run the right version of Rails
So try running this:
rbenv exec bundle exec rails console
To sum up for anyone having the same problem in the future,
I went to MyApp/releases/2020331231231231 which is the latest release of my app and there I used this command RAILS_ENV=production bundle exec rails console and I was able to do whatever I wanted to do in the console.
First you need to move inside the folder where the code is.
Check Capistrano config if deploy_to is defined. (If it's not it should be set to /var/www/#{application}/ by default read here)
Since Capistrano keep older versions of your app go to the current folder which is a symlink of the latest deployed version.
And run ENVIRONMENT=production bundle exec rails c or ENVIRONMENT=production bin/rails c. If doesn't work try with rbenv ENVIRONMENT=production rbenv exec bundle exec rails c

How to run rake in ruby-on-rails application in production?

I am trying to figure out how to deploy my ruby-on-rails application in production.
The operating system is ubuntu and the application uses Postgres. I have managed to deploy the application and am able to login etc... but there are certain functionality that is not working (I am new to the application and rails).
The application uses elastic search, which I have installed and the service is running (I can access the page via http://localhost:9200). But when the application tries to access components of elastic search i get an error.
There is a rake file in the directory /app/lib/tasks, which has several files and one of the them is elasticsearch.rake
namespace :app do
desc "Bootstraping Elasticsearch index"
task(:elasticsearch => :environment) do
Image.__elasticsearch__.create_index! force:true
Image.import
CaseStudy.__elasticsearch__.create_index! force:true
CaseStudy.import
end
end
As the description in the file says this bootstraps the elastic search index. What I don't know is how do I run all this in a production environment? There are other rake files as well and I want to know how to run them.
You should go to the root path of your project directory in production and type the following command:
RAILS_ENV=production bundle exec rake app:elasticsearch
Note:
To check the available list of rake tasks, you can type rake -T
rake task-name RAILS_ENV=production

How to run 'rake db:drop' in Elastic Beanstalk

I have a rails app running on the Elastic Beanstalk service.
When I deploy my app, some pages didn't work. I think the problem is that after I delete a model and create a new model with the same name but different schema, the database didn't reset.
On local, I can run rake db:drop, rake db:create, rake db:migrate to reset the database. However, how to reset the database on EB?
to run it manually, you can simply eb ssh your environment and issue the rake commands. Alternatively if you don't need that db, simply recreate the environment from the beanstalk web console, which will recreate the db.
i think you have to delete also structure.rb file manual from your rails application and let it create again by using rake db:migrate

Running a heroku database command from pgAdmin III? (Harry's Prelaunchr)

so i've successfully installed Harrys Prelauncher on Heroku (https://github.com/harrystech/prelaunchr)
and to export my collected emails into a csv i need to run this command (bundle exec rake prelaunchr:create_winner_csvs)
is there any way to run that command through pgadmin or some other program?
or is the only way for me to download my heroku database and run the command locally? also how and what would i need to do this?
i'm pretty new to rails and postgresql and would really appreciate if someone could help me out!
Because the rake task creates files locally you can't just run it on heroku via heroku run rake. You can however set up your local database.yml to connect to your heroku postgresql instance and run the rake task locally.
Run heroku pg:credentials to get the required database values.
Fill in the production environment of config/database.yml with the values you obtained from step 1 (for the value of 'database' in the yml file, use dbnmae from step 1)
Test your connection with RAILS_ENV=production rails db. This should drop you into a psql console.
Run the rake task. RAILS_ENV=production bundle exec rake prelaunchr:create_winner_csvs
The files will save locally in lib/assets as indicated by the documentation.
From within the directory of the project you can use
heroku run rake prelaunchr:create_winner_csvs
You should probably create a UI form in to your application.
On click on export CSV, it should run background job on heroku (Using delayed jobs).
heroku run rake prelaunchr:create_winner_csvs
Use send_data ruby method. To send your generated and dumped data file on to your browser.
Download the file on to your local system from running heroku instance.
Hope this will resolve your problem.
Cheers!!!
I ran into this problem recently while developing the Prelaunchr campaign for a client. Assuming you have a local version of your app and are using Postgres Copper in Heroku, you can "pull" your Heroku database down to your local machine, set that as your development database in database.yml, and run the rake task from your local app, which should now have the same database as your heroku version. Here is the command to pull the db (subbing out name_for_database & heroku_app_name with your own):
heroku pg:pull HEROKU_POSTGRESQL_COPPER_URL name_for_database --app heroku_app_name
Make sure to restart your local server to see the new database info populated.

running rake task on 'production' and specifying environment?

I have a host at Linode and am trying to run a Rake task on it, but I get a mySQL error saying it can't connect. It looks like it thinks it is on dev. I did some Googling and saw that I can do something like this:
bundle exec rails c
It loads the dev environment and I can't run User.all giving me an access denied error.
If I run bundle exec rails c RAILS_ENV=production I get the error:
Rails.env=production database is not configured (ActiveRecord::AdapterNotSpecified)
However, if I access it via the web, everything is OK. I was able to run rake db:seed before so I know that there's some way around this.
Accessing mySQL with the production credentials works fine.
Any ideas?
Try this:
rails c production
or, at the beginning:
RAILS_ENV=production rails c
It thinks you're passing RAILS_ENV=production as an argument when you put it at the end.
If you want to run your console in the context of the current bundle in your Gemfile and ensure you're using your Gemset use:
bundle exec rails c production
This works for me. It depends on how your server and all its dependencies are set up:
RAILS_ENV=production bundle exec rails console

Resources