ruby-on-rails - Elastic Beanstalk:找不到带有可执行包的 gem bundler (>= 0.a) (Gem::GemNotFoundException)

标签 ruby-on-rails ruby amazon-web-services rubygems amazon-elastic-beanstalk

此错误消息是众所周知的错误消息。 (例如,参见 https://bundler.io/blog/2019/01/04/an-update-on-the-bundler-2-release.html。)虽然我是通过一个新的 Elastic Beanstalk 应用程序获得它的,该应用程序具有 Ruby 2.6.1 和 bundler 2.0.1。错误是:

  /opt/rubies/ruby-2.6.1/lib/ruby/site_ruby/2.6.0/rubygems.rb:289:in `find_spec_for_exe': can't find gem bundler (>= 0.a) with executable bundle (Gem::GemNotFoundException)
from /opt/rubies/ruby-2.6.1/lib/ruby/site_ruby/2.6.0/rubygems.rb:308:in `activate_bin_path'
from /opt/rubies/ruby-2.6.1/bin/bundle:23:in `<main>' (ElasticBeanstalk::ExternalInvocationError)

我尝试将以下文件:01_install_bundler.config 放入 .ebextensions 文件夹中:

container_commands:
  01_install_bundler:
    command: "gem install bundler —-version 2.0.1"

虽然这永远不会运行,因为如果我查看上面的错误,我可以看到它是在部署过程的这一点发生的:

.../AppDeployStage0/AppDeployPreHook/10_bundle_install.sh] : Activity failed.

(即在 AppDeployPreHook 脚本的 bundle install 命令期间)。参见 https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/custom-platform-hooks.html供 PlatformHooks 引用。

我很确定,如果我能确保使用的 bundler 版本至少是 2.0.0 版本,那么就不会有问题。虽然我不知道如何轻松指定。目前,我正在通过 SSH 连接到服务器以 /opt/elasticbeanstalk/hooks/appdeploy/pre/ 编辑和摆弄脚本。尽管我显然需要一种自动化的、可重复的方式来完成它。

令人沮丧的是,ruby 2.6.1 没有默认选择 bundler 版本 2.0.0。有什么想法吗?

==============================

更新:

如果我编辑文件 /opt/elasticbeanstalk/hooks/appdeploy/pre/10_bundle_install.sh

if [ -f Gemfile ]; then
  echo "running 'bundle install' with Gemfile:"
  cat Gemfile

  +++ gem install bundler +++
  if [ -d $EB_APP_STAGING_DIR/vendor/cache ]; then
    bundle install --local
  else
    bundle install
  fi
else
  echo "no Gemfile found! Skipping bundle install stage!"
fi

并添加 gem install bundler(没有加号),这就解决了问题,因为它安装了最新的 bundler ,即 2.0.1。对于那些想了解黑客的人,命令是:

eb ssh

sudo -i

cd/opt/elasticbeanstalk/hooks/appdeploy/pre

vim 10_bundle_install.sh

这个解决方案的问题在于它感觉有点像 hack,因为它不使用 .ebextensions。有没有更合适的方法来解决这个问题?

最佳答案

下面是上述问题的编程解决方案。在 .ebextensions/gem_install_bundler.config 下创建以下文件:

files:
  # Runs before `./10_bundle_install.sh`:
  "/opt/elasticbeanstalk/hooks/appdeploy/pre/09_gem_install_bundler.sh" :
    mode: "000775"
    owner: root
    group: users
    content: |
      #!/usr/bin/env bash

      EB_APP_STAGING_DIR=$(/opt/elasticbeanstalk/bin/get-config container -k app_staging_dir)
      EB_SCRIPT_DIR=$(/opt/elasticbeanstalk/bin/get-config container -k script_dir)
      # Source the application's ruby, i.e. 2.6. Otherwise it will be 2.3, which will give this error: `bundler requires Ruby version >= 2.3.0` 
      . $EB_SCRIPT_DIR/use-app-ruby.sh

      cd $EB_APP_STAGING_DIR
      echo "Installing compatible bundler"
      gem install bundler -v 2.0.1

那么当你下一次eb deploy时,bundler已经更新到2.0.1版本了,你就不会再出现上面的错误了。

此处文档中的更多信息:

https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/custom-platform-hooks.html

这里:

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

最后一点:确保您在运行 eb deploy 之前提交这些更改,或者暂存它们并运行 eb deploy --staged。请参阅:https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/eb3-cli-git.html .我通过艰难的方式学到了这一点!

关于ruby-on-rails - Elastic Beanstalk:找不到带有可执行包的 gem bundler (>= 0.a) (Gem::GemNotFoundException),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55360450/

相关文章:

ruby-on-rails - rails 4 中有序的 has_many 关联

ruby - 您的 bundle 已锁定为 ffi (1.11.0),但在您的 Gemfile 中列出的任何来源中都找不到该版本

amazon-web-services - 为什么 API 网关会损坏我的二进制文件?

ruby-on-rails - 到 View 的路由在生产中中断

ruby-on-rails - Rails 首先或通过一个值初始化,但如果创建则添加更多值

ruby-on-rails - ruby on rails 我应该从 Controller 查询数据库吗

ruby - 产生多个参数的链接枚举器

ruby-on-rails - 如何根据日期作为数据库中的列的确切年份过滤用户?

amazon-web-services - ECR 存储库策略与 IAM 策略有何不同?

amazon-web-services - 无法将 SQL Server 连接到 AWS RDS,错误 258?