Can you run a rails console or rake command in the

2019-01-31 06:50发布

问题:

I have set up a RoR environement on AWS' elastic beanstalk. I am able to ssh into my EC2 instance. My home directory is /home/ec2-user, which is effectively empty. If I move up a directory, there is also a /home/webapp directory that i do not have access to.

Is there a way to run a rake command or rails console on my elastic beanstalk instance?

If I type rails console I get Usage: rails new APP_PATH [options] If I type RAILS_ENV=production bundle exec rails console, I get "Could not locate Gemfile"

回答1:

For rails, jump to /var/app/current then as @juanpastas said, run RAILS_ENV=production bundle exec rails c



回答2:

Don't know why, but since EBS runs everything as root, this worked for me:

sudo su
bundle exec rails c production


回答3:

None of these solutions mentioned here worked for me, so I cooked up a little script that I put in script/aws-console.

You can run it from the /var/app/current directory as root:

eb ssh
cd /var/app/current
sudo script/aws-console

My script can be found as a Gist here.



回答4:

None of the other answers worked for me so I went looking - this is working for me now on an elastic beanstalk 64bit amazon linux 2016.03 V2.1.2 ruby 2.2 (puma) stack

cd /var/app/current
sudo su
rake rails:update:bin
bundle exec rails console

that returns me the expected console

Loading production environment (Rails 4.2.6)
irb(main):001:0>


回答5:

I like to create an eb_console file at the root of my rails app, then chmod u+x it. It contains the following:

ssh -t ec2-user@YOUR_EC2_STATION.compute.amazonaws.com  'cd /var/app/current && bin/rails c'

This way, I just have to run:

./eb_console

like I would have run heroku run bundle exec rails c.



回答6:

You have to find the folder with your Gemfile :p.

To do that, I would take a look in you web server config there should be a config that tells you where your app directory is.

Maybe you know where your app is.

But in case you don't know, I would give a try to:

grep -i your_app_name /etc/apache/*
grep -i your_app_name /etc/apache/sites-enabled/*

To search files containing your_app_name in Apache config.

Or if you are using nginx, replace apache above by nginx.

after you find application folder, cd into it and run RAILS_ENV=production bundle exec rails c.

Making sure that your application is configured to run in production in Apache or nginx configuration.



回答7:

None of these were working for me, including the aws-console script. I finally ended up creating a script directory in /var/app/current and then creating a rails file in that directory as outline by this answer on another SO question.

eb ssh myEnv
cd /var/app/current
sudo mkdir script
sudo vim script/rails

Add this to file and save:

echo #!/usr/bin/env ruby
# This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application.

APP_PATH = File.expand_path('../../config/application',  __FILE__)
require File.expand_path('../../config/boot',  __FILE__)
require 'rails/commands'

Then make it executable and run it:

sudo chmod +x script/rails
sudo script/rails console

And it worked.