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/html
のvendor
以下にインストールされる。
composer.json
はプロジェクトルートに配置し、ドキュメントルートはdrupalを置いたディレクトリに変更した。
{
"name": "snize/プロジェクト名",
"require": {
"drush/drush": "8.0.*"
},
"authors": [
{
"name": "snize",
"email": "メールアドレス"
}
],
"minimum-stability": "stable"
}
eb deploy
でアップロードされ、.ebextensions
以下の設定ファイルが読み込まれ環境が更新される。ここまでは良いのだが、実際にDrushを利用するためにはPATHを通す必要がある。eb ssh
でログインして設定する方法もあるのだけども、環境ごとにその作業しなければならないしそんなことはしたくない。
解決策
以下の設定ファイルを用意した。
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
のたびに一行づつパスが追加されていくことに気づいた。問題ないといえば問題ないんだけど...
そこでワークアラウンドではあるけど、フラグを立てる方法を見つけたので採用した。
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
への書き込みのコマンドは実行されない