简体   繁体   English

您可以在弹性 beantalk 环境中运行 rails 控制台或 rake 命令吗?

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

I have set up a RoR environement on AWS' elastic beanstalk.我已经在 AWS 的弹性豆茎上建立了一个 RoR 环境。 I am able to ssh into my EC2 instance.我可以 ssh 进入我的 EC2 实例。 My home directory is / home/ec2-user , which is effectively empty.我的主目录是 / home/ec2-user ,实际上是空的。 If I move up a directory, there is also a /home/webapp directory that i do not have access to.如果我向上移动一个目录,还有一个我无权访问的/home/webapp目录。

Is there a way to run a rake command or rails console on my elastic beanstalk instance?有没有办法在我的弹性 beanstalk 实例上运行rake命令或rails 控制台

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

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

Don't know why, but since EBS runs everything as root, this worked for me:不知道为什么,但由于 EBS 以 root 身份运行所有内容,这对我有用:

sudo su
bundle exec rails c production

None of these solutions mentioned here worked for me, so I cooked up a little script that I put in script/aws-console.这里提到的这些解决方案都不适合我,所以我编写了一个小脚本,放在 script/aws-console 中。

You can run it from the /var/app/current directory as root:您可以以 root 身份从 /var/app/current 目录运行它:

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

My script can be found as a Gist here .我的脚本可以在这里作为Gist 找到

None of the other answers worked for me so I went looking - this is working for me now on an elastic beanstalk 64bit amazon linux 2016.03 V2.1.2 ruby 2.2 (puma) stack其他答案都不适合我,所以我去寻找 - 这对我现在在弹性 beantalk 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

that returns me the expected console返回给我预期的控制台

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

For Ruby 2.7:对于 Ruby 2.7:

if you don't need environment variables:如果您不需要环境变量:

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

It looks like environment variables are not loaded automatically anymore, which might prevent rails console from starting.看起来环境变量不再自动加载,这可能会阻止 rails 控制台启动。 I solved it by creating this .ebextensions file:我通过创建这个 .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

I like to create an eb_console file at the root of my rails app, then chmod u+x it.我喜欢在我的 rails 应用程序的根目录创建一个eb_console文件,然后chmod u+x它。 It contains the following:它包含以下内容:

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

This way, I just have to run:这样,我只需要运行:

./eb_console

like I would have run heroku run bundle exec rails c .就像我会运行heroku run bundle exec rails c

For the latest ruby version, please use the following command:对于最新的 ruby​​ 版本,请使用以下命令:

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

Running it with sudo is not needed.不需要用 sudo 运行它。

For Ruby 2.7:对于 Ruby 2.7:

As someone said, if you don't need env vars, run the following正如有人所说,如果您不需要 env vars,请运行以下命令

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

However, if you need ENV, I recommend doing this as per AWS doc: https://aws.amazon.com/premiumsupport/knowledge-center/elastic-beanstalk-env-variables-linux2/但是,如果您需要 ENV,我建议按照 AWS 文档执行此操作: https : //aws.amazon.com/premiumsupport/knowledge-center/elastic-beanstalk-env-variables-linux2/

tl;dr tl;博士

On Amazon Linux 2, all environment properties are centralised into a single file called /opt/elasticbeanstalk/deployment/env .在 Amazon Linux 2 上,所有环境属性都集中在一个名为/opt/elasticbeanstalk/deployment/env No user can access these outside the app.没有用户可以在应用程序之外访问这些。 So, they recommend to add some hook scripts after deploy to basically create a copy.因此,他们建议在部署后添加一些钩子脚本以创建副本。

#!/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

If because of some reason you don't want to run as root, do the following to pass env vars from root into new user environment:如果由于某种原因您不想以 root 身份运行,请执行以下操作以将 env vars 从 root 传递到新的用户环境:

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

add an eb extension shortcut:添加 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'

then:然后:

$ eb ssh
$ ./irb
irb(main):001:0>
#!/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 .将上述文件放在源代码中的某处,部署, eb ssh到 eb 实例, cd /var/app/current ,然后执行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;使用sudo ,它会删除一些 Rails 应用程序可能实际需要的环境变量; so manually load the profiles which the Elastic Beanstalk platform provides.所以手动加载 Elastic Beanstalk 平台提供的配置文件。
  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.当前的 Beanstalk ruby​​ 平台假设您在用户webapp上运行 rails 应用程序,一个不可登录的用户,所以在这个用户中运行您的命令是明智的。

Create a .ebextension file named setvars.config and add those lines to it创建一个名为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: []

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

None of these were working for me, including the aws-console script.这些都不适合我,包括 aws-console 脚本。 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 .我最终在/var/app/current创建了一个script目录,然后在该目录中创建了一个rails文件作为另一个 SO 问题的答案的大纲。

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.它奏效了。

You have to find the folder with your Gemfile :p.您必须找到带有 Gemfile 的文件夹:p。

To do that, I would take a look in you web server config there should be a config that tells you where your app directory is.为此,我会查看您的 Web 服务器配置,应该有一个配置告诉您您的应用程序目录在哪里。

Maybe you know where your app is.也许您知道您的应用程序在哪里。

But in case you don't know, I would give a try to:但如果你不知道,我会尝试:

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

To search files containing your_app_name in Apache config.在 Apache 配置中搜索包含 your_app_name 的文件。

Or if you are using nginx, replace apache above by nginx .或者,如果您使用的是 nginx,请将上面的apache替换为nginx

after you find application folder, cd into it and run RAILS_ENV=production bundle exec rails c .找到应用程序文件夹后, cd 进入其中并运行RAILS_ENV=production bundle exec rails c

Making sure that your application is configured to run in production in Apache or nginx configuration.确保您的应用程序配置为在 Apache 或 nginx 配置中在生产中运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM