簡體   English   中英

您可以在彈性 beantalk 環境中運行 rails 控制台或 rake 命令嗎?

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

我已經在 AWS 的彈性豆莖上建立了一個 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

對於 rails,跳轉到/var/app/current然后正如@juanpastas 所說,運行RAILS_ENV=production bundle exec rails c

不知道為什么,但由於 EBS 以 root 身份運行所有內容,這對我有用:

sudo su
bundle exec rails c production

這里提到的這些解決方案都不適合我,所以我編寫了一個小腳本,放在 script/aws-console 中。

您可以以 root 身份從 /var/app/current 目錄運行它:

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

我的腳本可以在這里作為Gist 找到

其他答案都不適合我,所以我去尋找 - 這對我現在在彈性 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

返回給我預期的控制台

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

對於 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

我喜歡在我的 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

對於最新的 ruby​​ 版本,請使用以下命令:

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

不需要用 sudo 運行它。

對於 Ruby 2.7:

正如有人所說,如果您不需要 env vars,請運行以下命令

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>'

添加 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>
#!/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 應用程序,一個不可登錄的用戶,所以在這個用戶中運行您的命令是明智的。

創建一個名為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/

這些都不適合我,包括 aws-console 腳本。 我最終在/var/app/current創建了一個script目錄,然后在該目錄中創建了一個rails文件作為另一個 SO 問題的答案的大綱。

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

它奏效了。

您必須找到帶有 Gemfile 的文件夾:p。

為此,我會查看您的 Web 服務器配置,應該有一個配置告訴您您的應用程序目錄在哪里。

也許您知道您的應用程序在哪里。

但如果你不知道,我會嘗試:

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 配置中在生產中運行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM