LoginSignup
2
1

More than 5 years have passed since last update.

Elastic BeanstlkにComposerで入れたツールにパスを通す(Drush)

Posted at

Elastic Beanstlk を最近使い始めました。設定はできるだけコード化しておきたい。

$ eb --version
EB CLI 3.6.1 (Python 2.7.1)
$ tree -L 2 -a
.
├── .ebextensions
│   ├── ec2-phpini.config
│   ├── ec2.config
│   ├── elb.config
│   ├── environment.config
│   └── rds.config
├── .elasticbeanstalk
│   └── config.yml
├── .gitignore
├── composer.json
├── composer.lock
└── drupal(本体中身省略)

Drush自体はComposer経由でインストールする。composer.lockを含めてデプロイすれば自動的に/var/www/htmlvendor以下にインストールされる。

composer.json はプロジェクトルートに配置し、ドキュメントルートはdrupalを置いたディレクトリに変更した。

composer.json
{
    "name": "snize/プロジェクト名",
    "require": {
        "drush/drush": "8.0.*"
    },
    "authors": [
        {
            "name": "snize",
            "email": "メールアドレス"
        }
    ],
    "minimum-stability": "stable"
}

eb deployでアップロードされ、.ebextensions以下の設定ファイルが読み込まれ環境が更新される。ここまでは良いのだが、実際にDrushを利用するためにはPATHを通す必要がある。eb sshでログインして設定する方法もあるのだけども、環境ごとにその作業しなければならないしそんなことはしたくない。

解決策

以下の設定ファイルを用意した。

ebextensions/ec2.config
container_commands:
  02-set_path:
    command: echo 'export PATH=/var/www/html/vendor/bin:$PATH' >> /home/ec2-user/.bash_profile
    cwd: /home/ec2-user
    test: test ! -f .semaphore
  99-signal-startup-complete:
    command: touch .semaphore
    cwd: /home/ec2-user

以上

ハマりポイントは2つ(以下おまけ)

ec2-userで環境変数をexportできない

container_commands:
  02-set_path:
    command: export PATH=/var/www/html/vendor/bin:$PATH

最初に思いついたのがこれで、楽勝と思ってたけどいくらやってもパスが通せない。考えてみれば簡単で、デプロイ時のユーザはrootで実行されたのだから、ec2-userで環境変数が変更されないのは当たり前。

.bash_profileが徐々に太る

ec2-userの環境変数をデプロイ時に直接設定できないから、.bash_profileを修正することにしたわけだけどeb deployのたびに一行づつパスが追加されていくことに気づいた。問題ないといえば問題ないんだけど...
そこでワークアラウンドではあるけど、フラグを立てる方法を見つけたので採用した。

configuration - Where to put Elastic Beanstalk config commands that are only run once on spin-up? - Stack Overflow

Commands can be run conditionally using the test: modifier. You specify a test to be done. If the test returns 0, the command is run, otherwise it is not.

If the last command in your config file touches a file, and the commands above that you only want to run once check for the existence of that file, then those commands will only run the first time.

commands:
  01-do-always:
    command: run_my_script
  02-do-on-boot:
    command: script_to_run_once
    test: test ! -f .semaphore
  99-signal-startup-complete:
    command: touch .semaphore

Linux サーバーでのソフトウェアのカスタマイズ - Elastic Beanstalk

test
オプション。Elastic Beanstalk が command キーに含まれるコマンド(bash スクリプトなど)を処理するために、値 true (終了コード 0)を返す必要があるコマンドです。

.semaphore ファイルが存在すれば .bash_profileへの書き込みのコマンドは実行されない

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1