chef-soloでWordpressビルドしてserverspecでテストする
ありがちですがAWS上にローンチしたCentOS 6.4にchef-soloでWordpressをインストールして
serverspecでテストしてみたいと思います。
①AWSでCentos6.4(x86_64)launch(各種値はデフォルト)する。
②Chef-soloをインストール
opscode社提供のインストールシェルスプリクトを使います。
curl -L https://www.opscode.com/chef/install.sh | bash % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 101 6790 101 6790 0 0 5396 0 0:00:01 0:00:01 --:--:-- 18351 Downloading Chef for el... % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 22.6M 100 22.6M 0 0 593k 0 0:00:39 0:00:39 --:--:-- 702k Installing Chef warning: /tmp/tmp.9QT3KjBE/chef-.x86_64.rpm: Header V4 DSA/SHA1 Signature, key ID 83ef826a: NOKEY Preparing... ########################################### [100%] 1:chef ########################################### [100%] Thank you for installing Chef!
※ -v オプションでバージョン固定可能
bash install.sh -v 11.4.4
③/opt/chefにインストールされているかを確認する。
ls /opt/chef/ bin embedded version-manifest.txt
ls /opt/chef/bin/ chef-apply chef-service-manager chef-solo knife restclient chef-client chef-shell erubis ohai shef
④chef-soloが設定ファイル(cookbook)配置場所とするディレクトリを作る。
mkdir -p /var/chef/cookbooks
⑤gitをインストールし④で作成したディレクトリをgitレポジトリにする。
yum install git cd /var/chef/cookbooks git init . touch README git add README git commit -m "add readme." 以下が出力されればOKです。 Committer: rootYour name and email address were configured automatically based on your username and hostname. Please check that they are accurate. You can suppress this message by setting them explicitly: git config --global user.name "Your Name" git config --global user.email you@example.com If the identity used for this commit is wrong, you can fix it with: git commit --amend --author='Your Name ' 0 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 README
⑥レシピ作成
今回は試しに「Wordpress」をインストールするレシピとします。
Opscodeコミュニティサイトからknifeコマンドでwordpressインストールの
cookbookをダウンロードする。
knife cookbook site install wordpress WARNING: No knife configuration file found Installing wordpress to /var/chef/cookbooks Checking out the master branch. Creating pristine copy branch chef-vendor-wordpress ~~~途中の出力は省略~~~ Uncompressing build-essential version 1.4.0. removing downloaded tarball No changes made to build-essential Checking out the master branch. ※ブランチは自動で切れます。
⑦Chef-Solo用のjsonファイルを作成し実行する。
vi dna.json ----- { "run_list": ["recipe[wordpress]"], "mysql": { "server_root_password": "password", "server_debian_password": "password", "server_repl_password": "password" } } -----
実行する。
chef-solo -j dna.json [2013-08-13T08:47:03+00:00] WARN: ***************************************** [2013-08-13T08:47:03+00:00] WARN: Did not find config file: /etc/chef/solo.rb, using command line options. [2013-08-13T08:47:03+00:00] WARN: ***************************************** Starting Chef Client, version 11.6.0 Compiling Cookbooks... ~~~途中の出力は省略~~~ - restart service service[apache2] Recipe: wordpress::default * log[wordpress_install_message] action write Chef Client finished, 110 resources updated
⑧インストールされたことを確認する。
EIPもしくは付与されたCNAMEをブラウザのURLに貼り付けアクセスする。
Wordpressのインストール設定画面が出れば成功です。
※公式CentOSはiptablesが設定されているので80番だけ通すかテスト的に
停止させて下さい。
⑨テスト実行環境(serverspec)を構築する。
gemを利用する為にChef-Soloインストール時に付属されているgemにパスを通す。
export PATH=$PATH:/opt/chef/embedded/bin/
serverspec をインストールする。
gem install serverspec
⑩テスト作成
serverspec-initコマンドで環境を指定します。
serverspec-init Select a backend type: 1) SSH 2) Exec (local) Select number:2 今回はローカルの環境をテストするため「2」を入力します。 ssh経由にてリモートのサーバをテストすることも可能です。
デフォルトでhttpd_spec.rbというテストケースが用意されているので、中身を確認します。
cat spec/localhost/httpd_spec.rb require 'spec_helper' describe package('httpd') do ←Apacheがインストールされているか? it { should be_installed } end describe service('httpd') do ←httpdデーモンが起動しているか? it { should be_enabled } it { should be_running } end describe port(80) do ←ポート80番が開放されているか? it { should be_listening } end describe file('/etc/httpd/conf/httpd.conf') do ←httpd.conf内のServerNameがlocalhost以外で it { should be_file } 指定されているか? it { should contain "ServerName localhost" } end
ひとまずこの状態でテストを実行してみます。
rake spec /opt/chef/embedded/bin/ruby -S rspec spec/localhost/httpd_spec.rb .....F Failures: 1) File "/etc/httpd/conf/httpd.conf" Failure/Error: it { should contain "ServerName localhost" } grep -q -- ServerName\ localhost /etc/httpd/conf/httpd.conf expected File "/etc/httpd/conf/httpd.conf" to contain "ServerName localhost" # ./spec/localhost/httpd_spec.rb:18:in `block (2 levels) in' Finished in 0.33599 seconds 6 examples, 1 failure Failed examples: rspec ./spec/localhost/httpd_spec.rb:18 # File "/etc/httpd/conf/httpd.conf" rake aborted! /opt/chef/embedded/bin/ruby -S rspec spec/localhost/httpd_spec.rb failed Tasks: TOP => spec (See full trace by running task with --trace)
Fはエラー箇所で、should contain "ServerName localhost"で引っかかっていることが
わかります。
今回はテストの為ServerName設定しないので、この箇所を消去して替わりにMySQLの起動
関連のテストを記載します。
vi spec/localhost/httpd_spec.rb ----- require 'spec_helper' describe package('httpd') do ←Apacheがインストールされているか? it { should be_installed } end describe service('httpd') do ←httpデーモンが起動しているか? it { should be_enabled } it { should be_running } end describe port(80) do ←ポート80番が開放されているか? it { should be_listening } end describe package('mysql-server') do ←mysql-serverがインストールされているか? it { should be_installed } end describe service('mysqld') do ←mysqlデーモンがインストールされているか? it { should be_enabled } it { should be_running } end describe port(3306) do ←ポート3306番がインストールされているか? it { should be_listening } end describe file('/etc/my.cnf') do ←MySQL設定ファイル(/etc/my.cnf)が存在するか? it { should be_file } end -----
実行する。
rake spec /opt/chef/embedded/bin/ruby -S rspec spec/localhost/httpd_spec.rb ......... Finished in 0.31783 seconds 9 examples, 0 failures
Fが無いのでテスト結果が問題ないことを表しています。
it { }がテストしている項目となりますので、9箇所テストしていることになります。
⑫Wordpressインストール可否のテストを実施
chef-soloでビルドしたWordpressのインストールが成功しているか確認するspecファイルを書く。
インストール成功の確認はインストール直後に表示される設定トップページの文字列を拾って
specファイルの文字列と比べることで成功を判断します。
vi spec/localhost/wordpress_inst_spec.rb ----- require 'spec_helper' describe command('curl http://localhost/wp-admin/install.php') do it { should return_stdout /Welcome to the famous five minute WordPress installation process!/ } end -----
実行する。
rake spec /opt/chef/embedded/bin/ruby -S rspec spec/localhost/httpd_spec.rb spec/localhost/wordpress_inst_spec.rb .......... Finished in 0.64412 seconds 10 examples, 0 failures
Fが無いのでテスト結果が問題ないことを表しています。
9箇所テストが10箇所になっていて今回追加したWordpressも問題ないことを示しています。
【参考箇所】 OPSCODE社 入門Chef Solo serverspecドキュメント