【问题标题】:Can you run a rails console or rake command in the elastic beanstalk environment?您可以在弹性 beantalk 环境中运行 rails 控制台或 rake 命令吗?
【发布时间】:2013-11-06 09:43:03
【问题描述】:

我已经在 AWS 的弹性 beanstalk 上设置了一个 RoR 环境。我能够通过 ssh 连接到我的 EC2 实例。 我的主目录是 /home/ec2-user,实际上是空的。 如果我向上移动一个目录,还有一个我无权访问的 /home/webapp 目录。

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

如果我输入 rails console 我会得到 Usage: rails new APP_PATH [options] 如果我输入 RAILS_ENV=production bundle exec rails console,我会得到“Could not locate Gemfile

【问题讨论】:

标签: ruby-on-rails amazon-web-services amazon-ec2 amazon-elastic-beanstalk


【解决方案1】:

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

【讨论】:

  • cd $EB_CONFIG_APP_CURRENT 转到 /var/app/current。 sudo RAILS_ENV=production bundle exec rails cbundler: command not found: rails。如果我指定rails 的路径sudo RAILS_ENV=production bundle exec /usr/local/bin/rails c,我会得到Could not find addressable-2.3.6 in any of the sources。 Beanstalk 和/或Passenger 捆绑gem 的方式有些奇怪,可能与this answer 中描述的共享gem 有关。
  • 看我的回答:应该可以解决所有问题:stackoverflow.com/a/60585463/3090068
【解决方案2】:

不知道为什么,但由于 EBS 以 root 身份运行所有内容,这对我有用:

sudo su
bundle exec rails c production

【讨论】:

  • 是的,这对我也有用。 cd /var/app current; sudo su; bundle exec rails c 不知道为什么,但最终不需要生产。
  • 这实际上在root用户中运行你的rails;但是当前的 Elastic Beanstalk 平台假定您在 webapp 用户中运行 rails 应用程序。所以,我认为我们应该使用webapp 来处理我们正在使用的任何命令,这样做在我的回答中进行了解释,请参阅我的回答:它应该解决所有问题:stackoverflow.com/a/60585463/3090068
  • sudo su 使用 2 个命令。您可以使用运行登录 shell 的sudo -i
【解决方案3】:

这里提到的这些解决方案都不适合我,所以我编写了一个小脚本,放在 script/aws-console 中。

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

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

我的脚本可以通过 Gist here 找到。

【讨论】:

    【解决方案4】:

    其他答案都不适合我,所以我去寻找 - 现在这对我来说在弹性 beanstalk 64bit amazon linux 2016.03 V2.1.2 ruby​​ 2.2 (puma) 堆栈上工作

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

    返回我预期的控制台

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

    【讨论】:

    • 你在哪里输入cd/var/app/current?它在你的本地机器上吗?它对我说 -bash: cd: /var/app/current: No such file or directory
    • @KaMok - 不,您需要打开一个到您的 EB 实例的 ssh 连接 - 类似于 eb ssh myinstancename - 输入“是”作为身份验证密钥,然后您将被转储到控制台您可以输入以上内容。
    • 如果您是cd,则不需要myinstancename 进入目录。
    【解决方案5】:

    对于 Ruby 2.7:

    如果您不需要环境变量:

    BUNDLE_PATH=/var/app/current/vendor/bundle/ bundle exec rails c
    

    环境变量似乎不再自动加载,这可能会阻止 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
    

    【讨论】:

    • 对于那些使用 dotenv 的人来说,这更简单:sudo cp -f /opt/elasticbeanstalk/deployment/env /var/app/current/.envcd /var/app/current &amp;&amp; rails console
    【解决方案6】:

    我喜欢在我的 rails 应用程序的根目录下创建一个 eb_console 文件,然后是 chmod u+x 它。它包含以下内容:

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

    这样,我只需要运行:

    ./eb_console
    

    就像我会运行 heroku run bundle exec rails c

    【讨论】:

      【解决方案7】:

      对于 Ruby 2.7:

      正如有人所说,如果您不需要环境变量,请运行以下命令

      BUNDLE_PATH=/var/app/current/vendor/bundle/ bundle exec rails c
      

      但是,如果您需要 ENV,我建议您按照 AWS 文档执行此操作: https://aws.amazon.com/premiumsupport/knowledge-center/elastic-beanstalk-env-variables-linux2/

      tl;博士

      在 Amazon Linux 2 上,所有环境属性都集中在一个名为 /opt/elasticbeanstalk/deployment/env 的文件中。任何用户都无法在应用程序之外访问这些内容。因此,他们建议在部署后添加一些钩子脚本以基本上创建副本。

      #!/bin/bash
      
      #Create a copy of the environment variable file.
      cp /opt/elasticbeanstalk/deployment/env /opt/elasticbeanstalk/deployment/custom_env_var
      
      #Set permissions to the custom_env_var file so this file can be accessed by any user on the instance. You can restrict permissions as per your requirements.
      chmod 644 /opt/elasticbeanstalk/deployment/custom_env_var
      
      #Remove duplicate files upon deployment.
      rm -f /opt/elasticbeanstalk/deployment/*.bak
      

      如果由于某种原因您不想以 root 身份运行,请执行以下操作以将 env vars 从 root 传递到新用户环境:

      sudo -u <user> -E env "PATH=$PATH" bash -c 'cd /var/app/current/ && <wtv you want to run>'
      

      【讨论】:

      • 对于那些使用 dotenv 的人来说,这更简单:sudo cp -f /opt/elasticbeanstalk/deployment/env /var/app/current/.envcd /var/app/current &amp;&amp; rails console
      【解决方案8】:
      #!/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 "$@")"
      

      将上述文件放在源代码中的某个位置,将eb ssh 部署到eb 实例cd /var/app/current,然后执行path/to/above/script bin/rails whatever argumeents you usually use

      我写上面脚本的原因是:

      1. 使用sudo 时,它会丢弃一些您的rails 应用程序可能实际需要的环境变量;所以手动加载 Elastic Beanstalk 平台提供的配置文件。
      2. 当前的 Beanstalk ruby​​ 平台假定您在用户 webapp(一个无法登录的用户)上运行 rails 应用程序,因此最好在该用户中运行您的命令。

      【讨论】:

      • 如何使用它来将我的 rake 任务作为 webapp 运行?事实上,它似乎以 root 身份运行。
      • @ptoinson 我忘了添加以指定su 命令的用户。刚刚编辑并将webapp 添加到su 参数。
      【解决方案9】:

      最新的ruby版本,请使用以下命令:

      BUNDLE_PATH=/opt/rubies/ruby-2.6.3/lib/ruby/gems/2.6.0/ bundle exec rails c production

      不需要使用 sudo 运行它。

      【讨论】:

        【解决方案10】:

        添加一个eb扩展快捷方式:

        # .ebextensions/irb.config
        files:
          "/home/ec2-user/irb":
            mode: "000777"
            owner: root
            group: root
            content: |
              sudo su - -c 'cd /var/app/current; bundle exec rails c'
        

        然后:

        $ eb ssh
        $ ./irb
        irb(main):001:0>
        

        【讨论】:

          【解决方案11】:

          创建一个名为 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: []
          

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

          【讨论】:

            【解决方案12】:

            这些都不适合我,包括 aws-console 脚本。我最终在/var/app/current 中创建了一个script 目录,然后在该目录中创建了一个rails 文件作为this answer on another SO question 的大纲。

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

            将此添加到文件并保存:

            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'
            

            然后让它可执行并运行它:

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

            它奏效了。

            【讨论】:

              【解决方案13】:

              你必须找到你的 Gemfile 所在的文件夹:p。

              为此,我会查看您的网络服务器配置,应该有一个配置告诉您应用程序目录在哪里。

              也许你知道你的应用在哪里。

              但如果你不知道,我会尝试一下:

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

              在 Apache 配置中搜索包含 your_app_name 的文件。

              或者如果您使用的是 nginx,请将上面的 apache 替换为 nginx

              找到应用程序文件夹后,cd进入并运行RAILS_ENV=production bundle exec rails c

              确保您的应用程序配置为在 Apache 或 nginx 配置中在生产环境中运行。

              【讨论】:

              • 没有目录/etc/apache或/etc/nginx。据我所知,弹性豆茎使用乘客。也没有 /etc/passenger 目录。
              • 通常,我在 nginx 或 Apache 之上使用乘客...所以 Apache 或 nginx 应该在那里...奇怪...
              • 在 Beanstalk 中,gemfile 位于 /var/app/current$EB_CONFIG_APP_CURRENT 环境变量指向那里。
              猜你喜欢
              • 1970-01-01
              • 2013-02-15
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2012-06-21
              • 2022-01-13
              • 1970-01-01
              相关资源
              最近更新 更多