你能在弹性beanstalk环境中运行rails控制台或rake命令吗?

git*_*itb 43 ruby-on-rails amazon-ec2 amazon-web-services amazon-elastic-beanstalk

我在AWS'弹性beanstalk上建立了一个RoR环境.我能够进入我的EC2实例.我的主目录是/ home/ec2-user,它实际上是空的.如果我向上移动目录,还有一个我无法访问的/ home/webapp目录.

有没有办法在弹性beanstalk实例上运行rake命令或rails控制台

如果我键入rails console我得到Usage: rails new APP_PATH [options] 如果我键入RAILS_ENV =生产包exec rails console,我得到" Could not locate Gemfile"

kro*_*oss 52

对于rails,跳转到/var/app/current@juanpastas说,然后运行RAILS_ENV=production bundle exec rails c

  • `cd $ EB_CONFIG_APP_CURRENT`进入/ var/app/current.`sudo RAILS_ENV =生产包exec rails c`给`bundler:command not found:rails`.如果我指定rails的路径,`sudo RAILS_ENV =生产包exec/usr/local/bin/rails c`,我得到`无法在任何来源中找到可寻址的2.3.6.关于Beanstalk和/或Passenger捆绑宝石的方式有些奇怪,可能与[本答案](http://stackoverflow.com/a/13657473/550712)中描述的共享宝石有关. (3认同)
  • 不工作它说`bundle:command not found` (2认同)

eve*_*man 41

不知道为什么,但是因为EBS以root身份运行所有东西,这对我有用:

sudo su
bundle exec rails c production
Run Code Online (Sandbox Code Playgroud)

  • 是的,这对我也有用.```cd/var/app current; sudo su; 捆绑exec rails c```不确定为什么但不需要生产. (3认同)

wrd*_*vos 14

这里提到的这些解决方案都没有为我工作,所以我编写了一个脚本/ aws-console中的一个小脚本.

您可以以root身份从/ var/app/current目录运行它:

eb ssh
cd /var/app/current
sudo script/aws-console
Run Code Online (Sandbox Code Playgroud)

我的脚本可以在这里找到.


小智 8

其他答案都不为我工作,所以我去寻找 - 这是现在的工作对我来说在弹性魔豆64位Linux的亚马逊2016.03 V2.1.2红宝石2.2(PUMA)堆栈

cd /var/app/current
sudo su
rake rails:update:bin
bundle exec rails console
Run Code Online (Sandbox Code Playgroud)

返回给我预期的控制台

Loading production environment (Rails 4.2.6)
irb(main):001:0>
Run Code Online (Sandbox Code Playgroud)


use*_*467 8

对于红宝石 2.7:

如果不需要环境变量:

BUNDLE_PATH=/var/app/current/vendor/bundle/ bundle exec rails c
Run Code Online (Sandbox Code Playgroud)

看起来环境变量不再自动加载,这可能会阻止 Rails 控制台启动。我通过创建这个 .ebextensions 文件解决了这个问题:

# Simply call `sudo /var/app/scripts/rails_c`

commands:
  create_script_dir:
    command: "mkdir -p /var/app/scripts"
    ignoreErrors: true
files:
  "/var/app/scripts/export_envvars":
    mode: "000755"
    owner: root
    group: root
    content: |
      #!/opt/elasticbeanstalk/.rbenv/shims/ruby

      if __FILE__ == $0
          require 'json'
          env_file = '/var/app/scripts/envvars'
          env_vars = env_vars = JSON.parse(`/opt/elasticbeanstalk/bin/get-config environment`)

          str = ''
          env_vars.each do |key, value|
              new_key = key.gsub(/\s/, '_')
              str << "export #{new_key}=\"#{value}\"\n"
          end

          File.open(env_file, 'w') { |f| f.write(str) }
      end
  "/var/app/scripts/rails_c":
    mode: "000755"
    owner: root
    group: root
    content: |
      . ~/.bashrc
      /var/app/scripts/export_envvars
      . /var/app/scripts/envvars
      cd /var/app/current
      /opt/elasticbeanstalk/.rbenv/shims/bundle exec rails c
Run Code Online (Sandbox Code Playgroud)


Ahm*_*ama 6

创建一个名为setvars.config的 .ebextension 文件并将这些行添加到其中

commands:
  setvars:
    command: /opt/elasticbeanstalk/bin/get-config environment | jq -r 'to_entries | .[] | "export \(.key)=\"\(.value)\""' > /etc/profile.d/sh.local
packages:
  yum:
    jq: []
Run Code Online (Sandbox Code Playgroud)

然后再次部署您的代码,它应该可以工作。参考: https: //aws.amazon.com/ar/premiumsupport/knowledge-center/elastic-beanstalk-env-variables-shell/