因使用rb-readline而导致使用Elastic Beanstalk将rails 5应用程序部署到AWS

我正在尝试使用弹性beanstalk将我的代码部署到AWS,但在部署时仍然遇到错误。

我添加了一个gem’rb-readline’,当我检查日志时,我发现这个gem在部署时出错了。 我不记得添加gem’rb-readline’修复了什么错误,但我不知道删除gem会不会解决这个问题。 以下是gem的链接: https : //github.com/ConnorAtherton/rb-readline

这是部署中的错误日志。

Application update failed at 2017-10-24T19:27:36Z with exit status 1 and error: Hook /opt/elasticbeanstalk/hooks/appdeploy/pre/11_asset_compilation.sh failed. ++ /opt/elasticbeanstalk/bin/get-config container -k script_dir + EB_SCRIPT_DIR=/opt/elasticbeanstalk/support/scripts ++ /opt/elasticbeanstalk/bin/get-config container -k app_staging_dir + EB_APP_STAGING_DIR=/var/app/ondeck ++ /opt/elasticbeanstalk/bin/get-config container -k app_user + EB_APP_USER=webapp ++ /opt/elasticbeanstalk/bin/get-config container -k support_dir + EB_SUPPORT_DIR=/opt/elasticbeanstalk/support + . /opt/elasticbeanstalk/support/envvars ++ export RACK_ENV=production ++ RACK_ENV=production ++ export RAILS_SKIP_ASSET_COMPILATION=false ++ RAILS_SKIP_ASSET_COMPILATION=false ++ export RAILS_SKIP_MIGRATIONS=false ++ RAILS_SKIP_MIGRATIONS=false ++ export BUNDLE_WITHOUT=test:development ++ BUNDLE_WITHOUT=test:development ++ export RDS_PORT=5432 ++ RDS_PORT=5432 ++ export RDS_HOSTNAME=aa16lalntiz6vr0.cghvpjf0tzo5.us-east-1.rds.amazonaws.com ++ RDS_HOSTNAME=aa16lalntiz6vr0.cghvpjf0tzo5.us-east-1.rds.amazonaws.com ++ export RDS_USERNAME=theasteve ++ RDS_USERNAME=theasteve ++ export RDS_DB_NAME=ebdb ++ RDS_DB_NAME=ebdb ++ export RDS_PASSWORD=Colombia1 ++ RDS_PASSWORD=Colombia1 + RAKE_TASK=assets:precompile + . /opt/elasticbeanstalk/support/scripts/use-app-ruby.sh ++ . /usr/local/share/chruby/chruby.sh +++ CHRUBY_VERSION=0.3.9 +++ RUBIES=() +++ for dir in '"$PREFIX/opt/rubies"' '"$HOME/.rubies"' +++ [[ -d /opt/rubies ]] ++++ ls -A /opt/rubies +++ [[ -n ruby-1.9.3-p551 ruby-2.0.0-p648 ruby-2.1.10 ruby-2.2.8 ruby-2.3.5 ruby-2.4.2 ruby-current ]] +++ RUBIES+=("$dir"/*) +++ for dir in '"$PREFIX/opt/rubies"' '"$HOME/.rubies"' +++ [[ -d /.rubies ]] +++ unset dir +++ cat /etc/elasticbeanstalk/.ruby_version ++ chruby 2.3.5 ++ case "$1" in ++ local dir match ++ for dir in '"${RUBIES[@]}"' ++ dir=/opt/rubies/ruby-1.9.3-p551 ++ case "${dir##*/}" in ++ for dir in '"${RUBIES[@]}"' ++ dir=/opt/rubies/ruby-2.0.0-p648 ++ case "${dir##*/}" in ++ for dir in '"${RUBIES[@]}"' ++ dir=/opt/rubies/ruby-2.1.10 ++ case "${dir##*/}" in ++ for dir in '"${RUBIES[@]}"' ++ dir=/opt/rubies/ruby-2.2.8 ++ case "${dir##*/}" in ++ for dir in '"${RUBIES[@]}"' ++ dir=/opt/rubies/ruby-2.3.5 ++ case "${dir##*/}" in ++ match=/opt/rubies/ruby-2.3.5 ++ for dir in '"${RUBIES[@]}"' ++ dir=/opt/rubies/ruby-2.4.2 ++ case "${dir##*/}" in ++ for dir in '"${RUBIES[@]}"' ++ dir=/opt/rubies/ruby-current ++ case "${dir##*/}" in ++ [[ -z /opt/rubies/ruby-2.3.5 ]] ++ shift ++ chruby_use /opt/rubies/ruby-2.3.5 '' ++ [[ ! -x /opt/rubies/ruby-2.3.5/bin/ruby ]] ++ [[ -n '' ]] ++ export RUBY_ROOT=/opt/rubies/ruby-2.3.5 ++ RUBY_ROOT=/opt/rubies/ruby-2.3.5 ++ export RUBYOPT= ++ RUBYOPT= ++ export PATH=/opt/rubies/ruby-2.3.5/bin:/opt/elasticbeanstalk/lib/ruby/bin:/usr/local/sbin:/usr/local/bin:/usr/bin:/usr/sbin:/sbin:/bin ++ PATH=/opt/rubies/ruby-2.3.5/bin:/opt/elasticbeanstalk/lib/ruby/bin:/usr/local/sbin:/usr/local/bin:/usr/bin:/usr/sbin:/sbin:/bin +++ /opt/rubies/ruby-2.3.5/bin/ruby - ++ eval 'export RUBY_ENGINE=ruby; export RUBY_VERSION=2.3.5; export GEM_ROOT="/opt/rubies/ruby-2.3.5/lib/ruby/gems/2.3.0";' +++ export RUBY_ENGINE=ruby +++ RUBY_ENGINE=ruby +++ export RUBY_VERSION=2.3.5 +++ RUBY_VERSION=2.3.5 +++ export GEM_ROOT=/opt/rubies/ruby-2.3.5/lib/ruby/gems/2.3.0 +++ GEM_ROOT=/opt/rubies/ruby-2.3.5/lib/ruby/gems/2.3.0 ++ (( 0 != 0 )) + cd /var/app/ondeck + su -s /bin/bash -c 'bundle exec /opt/elasticbeanstalk/support/scripts/check-for-rake-task.rb assets:precompile' webapp `/home/webapp` is not a directory. Bundler will use `/tmp/bundler/home/webapp' as your home directory temporarily. + '[' false == true ']' + su -s /bin/bash -c 'bundle exec rake assets:precompile' webapp `/home/webapp` is not a directory. Bundler will use `/tmp/bundler/home/webapp' as your home directory temporarily. rake aborted! Bundler::GemRequireError: There was an error while trying to load the gem 'rb-readline'. Gem Load Error is: HOME environment variable (or HOMEDRIVE and HOMEPATH) must be set and point to a directory Backtrace for gem load error is: /opt/rubies/ruby-2.3.5/lib/ruby/gems/2.3.0/gems/rb-readline-0.5.4/lib/rbreadline.rb:1097:in `' /opt/rubies/ruby-2.3.5/lib/ruby/gems/2.3.0/gems/rb-readline-0.5.4/lib/rbreadline.rb:17:in `' /opt/rubies/ruby-2.3.5/lib/ruby/gems/2.3.0/gems/rb-readline-0.5.4/lib/readline.rb:10:in `require' /opt/rubies/ruby-2.3.5/lib/ruby/gems/2.3.0/gems/rb-readline-0.5.4/lib/readline.rb:10:in `' /opt/rubies/ruby-2.3.5/lib/ruby/gems/2.3.0/gems/rb-readline-0.5.4/lib/readline.rb:8:in `' /opt/rubies/ruby-2.3.5/lib/ruby/gems/2.3.0/gems/rb-readline-0.5.4/lib/rb-readline.rb:16:in `require' /opt/rubies/ruby-2.3.5/lib/ruby/gems/2.3.0/gems/rb-readline-0.5.4/lib/rb-readline.rb:16:in `' /opt/rubies/ruby-2.3.5/lib/ruby/gems/2.3.0/gems/bundler-1.15.0/lib/bundler/runtime.rb:82:in `require' /opt/rubies/ruby-2.3.5/lib/ruby/gems/2.3.0/gems/bundler-1.15.0/lib/bundler/runtime.rb:82:in `block (2 levels) in require' /opt/rubies/ruby-2.3.5/lib/ruby/gems/2.3.0/gems/bundler-1.15.0/lib/bundler/runtime.rb:77:in `each' /opt/rubies/ruby-2.3.5/lib/ruby/gems/2.3.0/gems/bundler-1.15.0/lib/bundler/runtime.rb:77:in `block in require' /opt/rubies/ruby-2.3.5/lib/ruby/gems/2.3.0/gems/bundler-1.15.0/lib/bundler/runtime.rb:66:in `each' /opt/rubies/ruby-2.3.5/lib/ruby/gems/2.3.0/gems/bundler-1.15.0/lib/bundler/runtime.rb:66:in `require' /opt/rubies/ruby-2.3.5/lib/ruby/gems/2.3.0/gems/bundler-1.15.0/lib/bundler.rb:108:in `require' /var/app/ondeck/config/application.rb:17:in `' /var/app/ondeck/Rakefile:4:in `require_relative' /var/app/ondeck/Rakefile:4:in `' /opt/rubies/ruby-2.3.5/lib/ruby/gems/2.3.0/gems/rake-12.0.0/lib/rake/rake_module.rb:28:in `load' /opt/rubies/ruby-2.3.5/lib/ruby/gems/2.3.0/gems/rake-12.0.0/lib/rake/rake_module.rb:28:in `load_rakefile' /opt/rubies/ruby-2.3.5/lib/ruby/gems/2.3.0/gems/rake-12.0.0/lib/rake/application.rb:687:in `raw_load_rakefile' /opt/rubies/ruby-2.3.5/lib/ruby/gems/2.3.0/gems/rake-12.0.0/lib/rake/application.rb:96:in `block in load_rakefile' /opt/rubies/ruby-2.3.5/lib/ruby/gems/2.3.0/gems/rake-12.0.0/lib/rake/application.rb:178:in `standard_exception_handling' /opt/rubies/ruby-2.3.5/lib/ruby/gems/2.3.0/gems/rake-12.0.0/lib/rake/application.rb:95:in `load_rakefile' /opt/rubies/ruby-2.3.5/lib/ruby/gems/2.3.0/gems/rake-12.0.0/lib/rake/application.rb:79:in `block in run' /opt/rubies/ruby-2.3.5/lib/ruby/gems/2.3.0/gems/rake-12.0.0/lib/rake/application.rb:178:in `standard_exception_handling' /opt/rubies/ruby-2.3.5/lib/ruby/gems/2.3.0/gems/rake-12.0.0/lib/rake/application.rb:77:in `run' /opt/rubies/ruby-2.3.5/lib/ruby/gems/2.3.0/gems/rake-12.0.0/exe/rake:27:in `' /opt/rubies/ruby-2.3.5/bin/rake:23:in `load' /opt/rubies/ruby-2.3.5/bin/rake:23:in `' /opt/rubies/ruby-2.3.5/lib/ruby/gems/2.3.0/gems/bundler-1.15.0/lib/bundler/cli/exec.rb:74:in `load' /opt/rubies/ruby-2.3.5/lib/ruby/gems/2.3.0/gems/bundler-1.15.0/lib/bundler/cli/exec.rb:74:in `kernel_load' /opt/rubies/ruby-2.3.5/lib/ruby/gems/2.3.0/gems/bundler-1.15.0/lib/bundler/cli/exec.rb:27:in `run' /opt/rubies/ruby-2.3.5/lib/ruby/gems/2.3.0/gems/bundler-1.15.0/lib/bundler/cli.rb:360:in `exec' /opt/rubies/ruby-2.3.5/lib/ruby/gems/2.3.0/gems/bundler-1.15.0/lib/bundler/vendor/thor/lib/thor/command.rb:27:in `run' /opt/rubies/ruby-2.3.5/lib/ruby/gems/2.3.0/gems/bundler-1.15.0/lib/bundler/vendor/thor/lib/thor/invocation.rb:126:in `invoke_command' /opt/rubies/ruby-2.3.5/lib/ruby/gems/2.3.0/gems/bundler-1.15.0/lib/bundler/vendor/thor/lib/thor.rb:369:in `dispatch' /opt/rubies/ruby-2.3.5/lib/ruby/gems/2.3.0/gems/bundler-1.15.0/lib/bundler/cli.rb:20:in `dispatch' /opt/rubies/ruby-2.3.5/lib/ruby/gems/2.3.0/gems/bundler-1.15.0/lib/bundler/vendor/thor/lib/thor/base.rb:444:in `start' /opt/rubies/ruby-2.3.5/lib/ruby/gems/2.3.0/gems/bundler-1.15.0/lib/bundler/cli.rb:10:in `start' /opt/rubies/ruby-2.3.5/lib/ruby/gems/2.3.0/gems/bundler-1.15.0/exe/bundle:35:in `block in ' /opt/rubies/ruby-2.3.5/lib/ruby/gems/2.3.0/gems/bundler-1.15.0/lib/bundler/friendly_errors.rb:121:in `with_friendly_errors' /opt/rubies/ruby-2.3.5/lib/ruby/gems/2.3.0/gems/bundler-1.15.0/exe/bundle:27:in `' /opt/rubies/ruby-2.3.5/bin/bundle:23:in `load' /opt/rubies/ruby-2.3.5/bin/bundle:23:in `' Bundler Error Backtrace: /var/app/ondeck/config/application.rb:17:in `' /var/app/ondeck/Rakefile:4:in `require_relative' /var/app/ondeck/Rakefile:4:in `' /opt/rubies/ruby-2.3.5/bin/bundle:23:in `load' /opt/rubies/ruby-2.3.5/bin/bundle:23:in `' HOME environment variable (or HOMEDRIVE and HOMEPATH) must be set and point to a directory /var/app/ondeck/config/application.rb:17:in `' /var/app/ondeck/Rakefile:4:in `require_relative' /var/app/ondeck/Rakefile:4:in `' /opt/rubies/ruby-2.3.5/bin/bundle:23:in `load' /opt/rubies/ruby-2.3.5/bin/bundle:23:in `' (See full trace by running task with --trace). Incorrect application version "app-cd09-171024_152653" (deployment 3). Expected version "app-cd09-171024_150253" (deployment 2). 100 % of CPU is in use. 

我有类似的错误,但使用dot-env rails gem。 它看起来可能是相同的根本原因。

这似乎是因为在部署期间AWS未设置$HOME ,并且在未设置时对File.expand_path的调用将死亡。

可能的修复:

  1. 删除rbreadline
  2. HOME环境变量添加到EB配置或eb setenv (并重新部署)
  3. 添加一个覆盖现有部署脚本的配置文件,并在运行bundle install命令之前添加$ HOME变量

我成功地尝试了第一个和最后一个选项,并使用Gem的分叉版本正确处理了错误的$HOME

我怀疑第二个版本是更正确的方法,但说实话,目前还不清楚为什么亚马逊没有设置一个$HOME开始,它应该是什么。

该应用程序将存在于/var/app/ondeck/var/app/current因此这两个文件夹中的任何一个或/home/webapp (根据错误)都可能是一个很好的值。

这可能不是100%的根本原因,因为我在使用不同版本的Ruby本地复制它时有不同程度的运气。 但是, expand_path文档确实说必须正确设置HOME ,因此这可能是原因。

https://ruby-doc.org/core-2.4.2/File.html#method-c-expand_path

有关工作方案的更多细节:

我发现两个有效的解决方案(尝试其中一个)

有一次,我确实用dotenv提交了PR来解决这个问题。 https://github.com/bkeepers/dotenv/pull/314 。 据我所知,这可以修复它,并且不会破坏其他任何东西。 该问题被自动机器人标记为wontfix ,但最近由新的维护者重新打开

在我很清楚我的PR不会合并之后,我最终切换回ebextensions方法,但最后我记得它按预期运行(我认为更好的方法)

我使用的另一个解决方案是创建一个新的配置文件,替换/opt/elasticbeanstalk/hooks/appdeploy/pre/10_bundle_install.sh文件。

  • 从现有应用程序服务器获取该文件的当前版本
  • 创建一个创建该文件的ebextension配置(它将覆盖原始文件),但在运行bundle install命令之前包含一行来设置$HOME变量。
  • $HOME值设置为默认用户的值

使用ebextensions创建此类文件的文档如下:

https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/customize-containers-ec2.html#linux-files