Elastic Beanstalkにrailsのアプリ(Hello world)がデプロイできない


## 環境
ローカル環境は以下の通り。

```shell
$ rails --version                          
Warning: You're using Rubygems 2.0.14 with Spring. Upgrade to at least Rubygems 2.1.0 and run `gem pristine --all` for better startup performance.
Rails 4.2.0
```

AWSは、`ruby 2.0 / Passenger`を選択した。

## 出てきたエラー

まず確認したのは、EC2上のエラーログ

```cfn-init.log
2015-06-01 09:26:35,415 [ERROR] Command hooks (directoryHooksExecutor.py --path /opt/elasticbeanstalk/hooks/appdeploy/pre/) failed
2015-06-01 09:26:35,416 [DEBUG] Command hooks output: 
2015-06-01 09:26:35,417 [ERROR] Error encountered during build of Hook-PreAppDeploy: Command hooks failed
Traceback (most recent call last):
  File "/usr/lib/python2.6/site-packages/cfnbootstrap/construction.py", line 511, in run_config
    CloudFormationCarpenter(config, self._auth_config).build(worklog)
  File "/usr/lib/python2.6/site-packages/cfnbootstrap/construction.py", line 247, in build
    changes['commands'] = CommandTool().apply(self._config.commands)
  File "/usr/lib/python2.6/site-packages/cfnbootstrap/command_tool.py", line 113, in apply
    raise ToolError(u"Command %s failed" % name)
ToolError: Command hooks failed
2015-06-01 09:26:35,419 [ERROR] Unhandled exception during build: Command hooks failed
Traceback (most recent call last):
  File "/opt/aws/bin/cfn-init", line 122, in <module>
    worklog.build(detail.metadata, configSets)
  File "/usr/lib/python2.6/site-packages/cfnbootstrap/construction.py", line 117, in build
    Contractor(metadata).build(configSets, self)
  File "/usr/lib/python2.6/site-packages/cfnbootstrap/construction.py", line 502, in build
    self.run_config(config, worklog)
  File "/usr/lib/python2.6/site-packages/cfnbootstrap/construction.py", line 511, in run_config
    CloudFormationCarpenter(config, self._auth_config).build(worklog)
  File "/usr/lib/python2.6/site-packages/cfnbootstrap/construction.py", line 247, in build
    changes['commands'] = CommandTool().apply(self._config.commands)
  File "/usr/lib/python2.6/site-packages/cfnbootstrap/command_tool.py", line 113, in apply
    raise ToolError(u"Command %s failed" % name)
ToolError: Command hooks failed
```

何かしらエラーがおきているのはわかるけれど、何が起きているのかはわからない・・・。

次に見たのが、`$ eb logs`。実体は`directory-hooks-executor.log`。

```directory-hooks-executor.log
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

    /usr/bin/ruby2.0 extconf.rb 
checking if the C compiler accepts ... yes
Building nokogiri using packaged libraries.
checking for gzdopen() in -lz... yes
checking for iconv... yes
************************************************************************
IMPORTANT NOTICE:

Building Nokogiri with a packaged version of libxml2-2.9.2
with the following patches applied:
        - 0001-Revert-Missing-initialization-for-the-catalog-module.patch
        - 0002-Fix-missing-entities-after-CVE-2014-3660-fix.patch

Team Nokogiri will keep on doing their best to provide security
updates in a timely manner, but if this is a concern for you and want
to use the system library instead; abort this installation process and
reinstall nokogiri as follows:

    gem install nokogiri -- --use-system-libraries
        [--with-xml2-config=/path/to/xml2-config]
        [--with-xslt-config=/path/to/xslt-config]

If you are using Bundler, tell it to use the option:

    bundle config build.nokogiri --use-system-libraries
    bundle install

Note, however, that nokogiri is not fully compatible with arbitrary
versions of libxml2 provided by OS/package vendors.
************************************************************************
Extracting libxml2-2.9.2.tar.gz into tmp/x86_64-redhat-linux-gnu/ports/libxml2/2.9.2... OK
Running patch with /var/app/ondeck/vendor/bundle/ruby/2.0/gems/nokogiri-1.6.6.2/ports/patches/libxml2/0001-Revert-Missing-initialization-for-the-catalog-module.patch...
Running 'patch' for libxml2 2.9.2... ERROR, review '/var/app/ondeck/vendor/bundle/ruby/2.0/gems/nokogiri-1.6.6.2/ext/nokogiri/tmp/x86_64-redhat-linux-gnu/ports/libxml2/2.
9.2/patch.log' to see what happened.
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers.  Check the mkmf.log file for more details.  You may
need configuration options.

Provided configuration options:
        --with-opt-dir
        --without-opt-dir
        --with-opt-include
        --without-opt-include=${opt-dir}/include
        --with-opt-lib
        --without-opt-lib=${opt-dir}/lib64
        --with-make-prog
        --without-make-prog
        --srcdir=.
        --curdir
        --ruby=/usr/bin/ruby2.0
        --help
        --clean
        --use-system-libraries
        --enable-static
        --disable-static
        --with-zlib-dir
        --without-zlib-dir
        --with-zlib-include
        --without-zlib-include=${zlib-dir}/include
        --with-zlib-lib
        --without-zlib-lib=${zlib-dir}/lib
        --enable-cross-build
        --disable-cross-build
/var/app/ondeck/vendor/bundle/ruby/2.0/gems/mini_portile-0.6.2/lib/mini_portile.rb:279:in `block in execute': Failed to complete patch task (RuntimeError)
        from /var/app/ondeck/vendor/bundle/ruby/2.0/gems/mini_portile-0.6.2/lib/mini_portile.rb:271:in `chdir'
        from /var/app/ondeck/vendor/bundle/ruby/2.0/gems/mini_portile-0.6.2/lib/mini_portile.rb:271:in `execute'
        from extconf.rb:311:in `block in patch'
        from extconf.rb:308:in `each'
        from extconf.rb:308:in `patch'
        from /var/app/ondeck/vendor/bundle/ruby/2.0/gems/mini_portile-0.6.2/lib/mini_portile.rb:108:in `cook'
        from extconf.rb:278:in `block in process_recipe'
        from extconf.rb:177:in `tap'
        from extconf.rb:177:in `process_recipe'
        from extconf.rb:475:in `<main>'

extconf failed, exit code 1

Gem files will remain installed in /var/app/ondeck/vendor/bundle/ruby/2.0/gems/nokogiri-1.6.6.2 for inspection.
Results logged to /var/app/ondeck/vendor/bundle/ruby/2.0/extensions/x86_64-linux/2.0/nokogiri-1.6.6.2/gem_make.out
An error occurred while installing nokogiri (1.6.6.2), and Bundler cannot
continue.
Make sure that `gem install nokogiri -v '1.6.6.2'` succeeds before bundling.

2015-06-01 09:26:35,325 [ERROR] (31168 MainThread) [directoryHooksExecutor.py-33] [root directoryHooksExecutor error] Script /opt/elasticbeanstalk/hooks/appdeploy/pre/10_
bundle_install.sh failed with returncode 5
```

どうやら、nokogiriっていうgemパッケージのbuildで失敗していて、libxml2あたりのバージョンが合わないらしい。
nokogiriが必要かどうか知らないけれど、railsはデフォルトで試しているのでたぶん必要なんだろう。

手で直すことはできそうだけど、そんなことしたらElastic Beanstalkを使うメリットが全くなくなってしまう。

## 対処方法

Elastic Beanstalkでは、`eb`コマンド実行時にextensionという形で細かい調整が可能らしい。
よくわからない中、`$RAILS_HOME/.ebextensions/nokogiri_command.config`ファイルをrailsアプリの中に配置した。

```nokogiri_command.config
commands:
  test:
    command: "bundle config build.nokogiri --use-system-libraries"
```

以下のコマンドで、awsへpushしてHello Worldが実行できた。

```shell
$ git add .
$ git commit -m "nokogirigirigiri"
$ eb aws.push
```

## 参考
* [AWS Elastic Beanstalk に Rails を構築する手順。](http://qiita.com/jr_qrank/items/aadc076ebf93a2692d14)
* [誰かのgithubのnokogiri.config](https://gist.github.com/jgautsch/b1d48d75f060c8fd5f26#file-nokogiri_command-config)