Can you run a rails console or rake command in the elastic beanstalk environment?

后端 未结 11 1024
一整个雨季
一整个雨季 2020-12-12 21:03

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 effective

相关标签:
11条回答
  • 2020-12-12 21:24

    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.

    0 讨论(0)
  • 2020-12-12 21:28
    #!/bin/sh
    
    shell_join () {
      ruby -r shellwords -e 'puts Shellwords.join(ARGV)' "$@"
    }
    
    
    command_str () {
      printf 'set -e; . /etc/profile.d/eb_envvars.sh; . /etc/profile.d/use-app-ruby.sh; set -x; exec %s\n' "$(shell_join "$@")"
    }
    
    exec sudo su webapp -c "$(command_str "$@")"
    

    Put above file somewhere in your source code, deploy, eb ssh into the eb instance, cd /var/app/current, and then execute path/to/above/script bin/rails whatever argumeents you usually use.

    Reason why I have written above script is:

    1. When using sudo, it drops some environment variables which might actually be needed for your rails app; so manually load the profiles which the Elastic Beanstalk platform provides.
    2. Current Beanstalk ruby platform assumes you run rails application on user webapp, a non-login-able user, so it would be wise to run your command in this user.
    0 讨论(0)
  • 2020-12-12 21:30

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

    0 讨论(0)
  • 2020-12-12 21:30

    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.

    0 讨论(0)
  • 2020-12-12 21:33

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

    sudo su
    bundle exec rails c production
    
    0 讨论(0)
提交回复
热议问题