OSSのサーバ構築自動化ツール、4製品徹底検証 2016年版実際に検証済み!OSS徹底比較(4)サーバ構築自動化【後編】(6/8 ページ)

» 2016年06月24日 05時00分 公開
[森元敏雄,TIS]

Itamaeのプロフィール

 Itamaeクックパッドの荒井良太氏を中心に開発されている製品である。GitHubにも「Configuration management tool inspired by Chef, but simpler and lightweight. Formerly known as Lightchef.」と記載されており、Chefを簡素化、軽量化する目的で開発されている。当初は「Light Chef」として提供されていた。2013年に最初のStable版がリリースされており、今回の4製品の中では非常に新しい製品となっている。

 ライセンスはMIT Licenseで、現在の最新の安定バージョンは2016年4月7日にリリースされた1.9.6。製品の詳細については、荒井氏が自ら執筆された記事「Itamaeが構成管理を仕込みます! 〜新進気鋭の国産・構成管理ツール〜」が公開されているので、ご一読をお勧めしたい。

Itamaeのインストール

 今回の検証では、CentOS 7.2に最新のStable版、Ver 4.4.2をインストールしている。ItamaeはRubyで開発されており、サーバの設定を行うrecipeはChefを踏襲している。nodeの定義はjson、recipe本体はRubyスクリプトになっており、書式もChefとほぼ同じである。Chefの経験者であれば、すんなり利用することができるだろう。今回、検証で行ったインストールの手順は以下の通りだ。

サーバ側のインストール処理

1.インストール済みパッケージの更新

$ sudo yum update -y

2.Rubyのインストール

$ sudo yum install -y ruby

3.Itamaeのインストール

$ sudo gem install itamae

4.Itamaeのバージョンの確認

$ sudo /usr/local/bin/itamae version
Itamae v1.9.6

5.Itamaeのログ出力フォルダの作成

$ sudo mkdir /var/log/itamae
$ sudo chmod a+w /var/log/itamae

 node側には作業は不要である。このわずかな作業だけで、Itamaeの実行環境は構築が行え、サーバの構築と設定を行うrecipeを作成、実行することができる。

WordPressインストール用のrecipeの作成

1.recipeを格納するフォルダを作成

 node定義ファイルもrecipe本体も、Itamae実行時はファイル名を直接パラメータとして指定する。このため、参照可能であればどこのフォルダでも問題ないが、取りあえず/etc/itamae以下に作成しておく。

$ sudo mkdir -p /etc/itamae/nodes /etc/itamae/cookbooks/wordpress/templates

2.node定義ファイルを作成

 recipeに引き渡すパラメータを定義する。

$ sudo vi /etc/itamae/nodes/tissvv096.json
{
    "ipaddress"        : "10.255.202.96",
    "hostname"         : "tissvv096",
    "mysql_root_pass"  : "FM11AD2+",
    "mysql_wp_db"      : "WordPress",
    "mysql_wp_user"    : "wp_admin",
    "mysql_wp_pass"    : "HB-F1XDJ",
    "wp_unique_phrase" : "FX702PFX801PPB100FX860PPB700PB500PB750PAI1000",
    "wp_os_user"       : "root",
    "wp_os_group"      : "root",
    "wp_latest"        : "https://ja.wordpress.org/latest-ja.tar.gz"
}

3.recipeを作成

 作成したcookbookは以下となる。各処理のTIPSは後ほど、機能ごとに説明を行う。

(1)cookbookとして作成するファイルおよびフォルダの構成

$ t tree -l /etc/itamae
/etc/itamae
├── cookbooks
│   └── wordpress
│       ├── default.rb
│       └── templates
│           ├── my.cnf.erb
│           ├── wordpress.conf.erb
│           └── wordpress.createdb.sql.erb
└── nodes
    └── tissvv096.json

(2)WordPress環境の構築を実際に行うwordpress/default.rbファイルの作成

$ sudo vi /etc/itamae/cookbooks/wordpress/default.rb
# Parameter settings
ipaddress        = node['ipaddress']
hostname         = node['hostname']
mysql_root_pass  = node['mysql_root_pass']
mysql_wp_db      = node['mysql_wp_db']
mysql_wp_user    = node['mysql_wp_user']
mysql_wp_pass    = node['mysql_wp_pass']
wp_unique_phrase = node['wp_unique_phrase']
wp_os_user       = node['wp_os_user']
wp_os_group      = node['wp_os_group']
wp_latest        = node['wp_latest']
# update packages
execute "update packages" do
  command "yum update -y"
end
# install packages
%w( mariadb-server httpd php php-mysql ).each do |pkg|
  package pkg do
    action :install
  end
end
# enable/start mariadb
service "mariadb" do
  action [:enable,:start]
end
# set mariadb root password
execute "create mariadb root" do
  command "mysql -e \"update mysql.user set password=password(\'#{mysql_root_pass}\') where user = \'root\'\""
  only_if "test `mysql -u root -p#{mysql_root_pass} -e \"show databases\" | grep -c mysql` == 0"
end
# create /root/.my.cnf
template "/root/.my.cnf" do
  action :create
  source "/etc/itamae/cookbooks/wordpress/templates/my.cnf.erb"
  variables( mysql_root_pass: "#{mysql_root_pass}" )
  owner "root"
  group "root"
  mode "600"
  not_if "test -f /root/.my.cnf"
end
# restart mariadb
service "mariadb" do
  action :restart
end
# mariadb logrotate setting
execute "modify maridb logrotate config" do
  command "sed -i.bak -e '23,$ s/^#//' /etc/logrotate.d/mariadb"
  user "root"
  not_if "test -f /etc/logrotate.d/mariadb.bak"
end
# create wordpres db/user
template "/tmp/wordpress.createdb.sql" do
  action :create
  source "/etc/itamae/cookbooks/wordpress/templates/wordpress.createdb.sql.erb"
  variables( mysql_wp_db: "#{mysql_wp_db}",
             mysql_wp_user: "#{mysql_wp_user}",
             mysql_wp_pass: "#{mysql_wp_pass}" )
  owner "root"
  group "root"
  not_if "test -f /tmp/wordpress.createdb.sql"
end
execute "create wordpress db/user" do
  command "mysql -u root -p#{mysql_root_pass} < /tmp/wordpress.createdb.sql"
  only_if "test `mysql -u root -p#{mysql_root_pass} -e \"show databases\" | grep -c #{mysql_wp_db}` == 0"
end
execute "remove /tmp/wordpress.createdb.sql" do
  command "rm -f /tmp/wordpress.createdb.sql"
  only_if "test -f /tmp/wordpress.createdb.sql"
end
# insatll wordpress
execute "insatll wordpress" do
  command "curl #{wp_latest} | tar zx -C /var/www"
  user "root"
  not_if "test -f /var/www/wordpress/wp-config-sample.php"
end
# create wordpress config
execute "copy wordpress config" do
  cwd "/var/www/wordpress"
  command "cp -p wp-config-sample.php wp-config.php"
  user "root"
  not_if "test -f /var/www/wordpress/wp-config.php"
end
file "/var/www/wordpress/wp-config.php" do
  action :edit
  block do |content|
    content.gsub!( /(^.+?)database_name_here(.+?$)/, "\\1#{mysql_wp_db}\\2" )
    content.gsub!( /(^.+?)username_here(.+?$)/, "\\1#{mysql_wp_user}\\2" )
    content.gsub!( /(^.+?)password_here(.+?$)/, "\\1#{mysql_wp_pass}\\2" )
    content.gsub!( /(^.+?)put your unique phrase here(.+?$)/, "\\1#{wp_unique_phrase}\\2" )
  end
  owner "root"
  group "root"
  not_if "test `grep -c database_name_here /var/www/wordpress/wp-config.php` == 0"
end
# chown wordpress files
execute "chown wordpress files" do
  command "chown -R #{wp_os_user}:#{wp_os_group} /var/www/wordpress"
  user "root"
  not_if "test `find /var/www/wordpress -not -user #{wp_os_user} -or -not -group #{wp_os_group} | wc -l` == 0"
end
# create wordpress httpd config
template "/etc/httpd/conf.d/wordpress.conf" do
  action :create
  source "/etc/itamae/cookbooks/wordpress/templates/wordpress.conf.erb"
  variables( hostname: "#{hostname}" )
  owner "root"
  group "root"
  not_if "test -f /etc/httpd/conf.d/wordpress.conf"
end
# modify httpd config
execute "backup httpd config" do
  cwd "/etc/httpd/conf"
  command "cp -p httpd.conf httpd.conf.bak"
  user "root"
  not_if "test -f /etc/httpd/conf/httpd.conf.bak"
end
file "/etc/httpd/conf/httpd.conf" do
  action :edit
  block do |content|
    content.gsub!( /^#ServerName.*/, "ServerName #{hostname}" )
  end
  owner "root"
  group "root"
  not_if "test `grep -c \"ServerName #{hostname}\" == 0"
end
# enable/start httpd
service "httpd" do
  action [:enable, :start]
end
# open httpd port in firewall
execute "open firewall http" do
  command "firewall-cmd --add-service=http --zone=public --permanent;\
           firewall-cmd --reload"
  user "root"
  only_if "test `firewall-cmd --zone=public --list-all | grep -c http` == 0"
end

(3)/root/.my.cnfのtemplateファイルの作成

$ sudo vi /etc/itamae/cookbooks/wordpress/templates/my.cnf.erb
[client]
user = root
password = "<%= @mysql_root_pass %>"
[mysqladmin]
user = root
password = "<%= @mysql_root_pass %>"

(4)/etc/httpd/conf.d/wordpress.confのtemplateファイルの作成

$ sudo vi /etc/itamae/cookbooks/wordpress/templates/wordpress.conf.erb
<VirtualHost *:80>
  ServerName <%= @hostname %>
  DocumentRoot /var/www/wordpress
  <Directory "/var/www/wordpress">
    AllowOverride All
    Options -Indexes
  </Directory>
  <Files wp-config.php>
    order allow,deny
    deny from all
  </Files>
</VirtualHost>

(5)WordPressのdb/userを作成するsql文のtemplateファイルの作成

$ sudo vi /etc/itamae/cookbooks/wordpress/templates/wordpress.createdb.sql.erb
create user "<%= @mysql_wp_user %>"@"localhost" identified by "<%= @mysql_wp_pass %>";
create database <%= @mysql_wp_db %>;
grant all privileges on <%= @mysql_wp_db %>.* to "<%= @mysql_wp_user %>"@"localhost";
flush privileges;

 以上でcookbookの作成は終了となる。cookbookはサーバ側でitamaeコマンドで実行する。

4.cookbookの実行前の構文の確認

 itamaeコマンドでcookbookを実行することになるが、最後に"-n"または"--dry-run"オプションを付けると、実行せずに構文チェックのみが行われる。エラーが表示されなければ構文的には問題がない。

$ itamae ssh -h tissvv096 -p 22 -j /etc/itamae/nodes/tissvv096.json -u maintain -i ~/.ssh/id_rsa.pem  /etc/itamae/cookbooks/wordpress/default.rb --dry-run 2>&1 | tee /var/log/itamae/tissvv096_2nd.log

5.WordPressサーバ構築のcookbook実行

 以下のコマンドを実行するとサーバ側からcookbookがダウンロードされ、WordPress環境の構築が行われる。出力されるログは以下となる。

$ itamae ssh -h tissvv096 -p 22 -j /etc/itamae/nodes/tissvv096.json -u maintain -i ~/.ssh/id_rsa.pem  /etc/itamae/cookbooks/wordpress/default.rb 2>&1 | tee /var/log/itamae/tissvv096_2nd.log
INFO : Starting Itamae...
INFO : Loading node data from /etc/itamae/nodes/tissvv096.json...
INFO : Recipe: /etc/itamae/cookbooks/wordpress/default.rb
INFO :   execute[update packages] executed will change from 'false' to 'true'
INFO :   package[mariadb-server] installed will change from 'false' to 'true'
INFO :   package[httpd] installed will change from 'false' to 'true'
INFO :   package[php] installed will change from 'false' to 'true'
INFO :   package[php-mysql] installed will change from 'false' to 'true'
INFO :   service[mariadb] enabled will change from 'false' to 'true'
INFO :   service[mariadb] running will change from 'false' to 'true'
INFO :   execute[create mariadb root] executed will change from 'false' to 'true'
INFO :   template[/root/.my.cnf] exist will change from 'false' to 'true'
INFO :   template[/root/.my.cnf] mode will be '0600'
INFO :   template[/root/.my.cnf] owner will be 'root'
INFO :   template[/root/.my.cnf] group will be 'root'
INFO :   diff:
INFO :   --- /dev/null	2016-05-13 14:38:00.434000293 +0900
INFO :   +++ /tmp/itamae_tmp/1463118454.564642	2016-05-13 14:47:34.616655971 +0900
INFO :   @@ -0,0 +1,7 @@
INFO :   +[client]
INFO :   +user = root
INFO :   +password = "FM11AD2+"
INFO :   +
INFO :   +[mysqladmin]
INFO :   +user = root
INFO :   +password = "FM11AD2+"
INFO :   execute[modify maridb logrotate config] executed will change from 'false' to 'true'
INFO :   template[/tmp/wordpress.createdb.sql] exist will change from 'false' to 'true'
INFO :   template[/tmp/wordpress.createdb.sql] owner will be 'root'
INFO :   template[/tmp/wordpress.createdb.sql] group will be 'root'
INFO :   diff:
INFO :   -
INFO :   +grant all privileges on WordPress.* to "wp_admin"@"localhost";
INFO :   +flush privileges;
INFO :   execute[create wordpress db/user] executed will change from 'false' to 'true'
INFO :   execute[insatll wordpress] executed will change from 'false' to 'true'
INFO :   execute[copy wordpress config] executed will change from 'false' to 'true'
INFO :   file[/var/www/wordpress/wp-config.php] owner will change from 'nobody' to 'root'
INFO :   file[/var/www/wordpress/wp-config.php] group will change from 'UNKNOWN' to 'root'
INFO :   diff:
INFO :   --- /var/www/wordpress/wp-config.php	2016-05-07 09:12:32.000000000 +0900
INFO :   +++ /tmp/itamae_tmp/1463118460.339039	2016-05-13 14:47:40.431656258 +0900
INFO :   @@ -26,13 +26,13 @@
INFO :    
INFO :    // ** MySQL 設定 - この情報はホスティング先から入手してください。 ** //
INFO :    /** WordPress のためのデータベース名 */
INFO :   -define('DB_NAME', 'database_name_here');
INFO :   +define('DB_NAME', 'WordPress');
INFO :    
INFO :    /** MySQL データベースのユーザー名 */
INFO :   -define('DB_USER', 'username_here');
INFO :   +define('DB_USER', 'wp_admin');
INFO :    
INFO :    /** MySQL データベースのパスワード */
INFO :   -define('DB_PASSWORD', 'password_here');
INFO :   +define('DB_PASSWORD', 'HB-F1XDJ');
INFO :    
INFO :    /** MySQL のホスト名 */
INFO :    define('DB_HOST', 'localhost');
INFO :   @@ -52,14 +52,14 @@
INFO :     *
INFO :     * @since 2.6.0
INFO :     */
INFO :   -define('AUTH_KEY',         'put your unique phrase here');
INFO :   -define('SECURE_AUTH_KEY',  'put your unique phrase here');
INFO :   -define('LOGGED_IN_KEY',    'put your unique phrase here');
INFO :   -define('NONCE_KEY',        'put your unique phrase here');
INFO :   -define('AUTH_SALT',        'put your unique phrase here');
INFO :   -define('SECURE_AUTH_SALT', 'put your unique phrase here');
INFO :   -define('LOGGED_IN_SALT',   'put your unique phrase here');
INFO :   -define('NONCE_SALT',       'put your unique phrase here');
INFO :   +define('AUTH_KEY',         'FX702PFX801PPB100FX860PPB700PB500PB750PAI1000');
INFO :   +define('SECURE_AUTH_KEY',  'FX702PFX801PPB100FX860PPB700PB500PB750PAI1000');
INFO :   +define('LOGGED_IN_KEY',    'FX702PFX801PPB100FX860PPB700PB500PB750PAI1000');
INFO :   +define('NONCE_KEY',        'FX702PFX801PPB100FX860PPB700PB500PB750PAI1000');
INFO :   +define('AUTH_SALT',        'FX702PFX801PPB100FX860PPB700PB500PB750PAI1000');
INFO :   +define('SECURE_AUTH_SALT', 'FX702PFX801PPB100FX860PPB700PB500PB750PAI1000');
INFO :   +define('LOGGED_IN_SALT',   'FX702PFX801PPB100FX860PPB700PB500PB750PAI1000');
INFO :   +define('NONCE_SALT',       'FX702PFX801PPB100FX860PPB700PB500PB750PAI1000');
INFO :    
INFO :    /**#@-*/
INFO :    
INFO :   execute[chown wordpress files] executed will change from 'false' to 'true'
INFO :   template[/etc/httpd/conf.d/wordpress.conf] exist will change from 'false' to 'true'
INFO :   template[/etc/httpd/conf.d/wordpress.conf] owner will be 'root'
INFO :   template[/etc/httpd/conf.d/wordpress.conf] group will be 'root'
INFO :   diff:
INFO :   --- /dev/null	2016-05-13 14:38:00.434000293 +0900
INFO :   +++ /tmp/itamae_tmp/1463118460.7118292	2016-05-13 14:47:40.767656275 +0900
INFO :   @@ -0,0 +1,13 @@
INFO :   +<VirtualHost *:80>
INFO :   +  ServerName tissvv096
INFO :   +  DocumentRoot /var/www/wordpress
INFO :   +  <Directory "/var/www/wordpress">
INFO :   +    AllowOverride All
INFO :   +    Options -Indexes
INFO :   +  </Directory>
INFO :   +
INFO :   +  <Files wp-config.php>
INFO :   +    order allow,deny
INFO :   +    deny from all
INFO :   +  </Files>
INFO :   +</VirtualHost>
INFO :   execute[backup httpd config] executed will change from 'false' to 'true'
INFO :   diff:
INFO :   --- /etc/httpd/conf/httpd.conf	2016-05-12 19:16:14.000000000 +0900
INFO :   +++ /tmp/itamae_tmp/1463118461.0208547	2016-05-13 14:47:41.115656292 +0900
INFO :   @@ -92,7 +92,7 @@
INFO :    #
INFO :    # If your host doesn't have a registered DNS name, enter its IP address here.
INFO :    #
INFO :   -#ServerName www.example.com:80
INFO :   +ServerName tissvv096
INFO :    
INFO :    #
INFO :    # Deny access to the entirety of your server's filesystem. You must
INFO :   service[httpd] enabled will change from 'false' to 'true'
INFO :   service[httpd] running will change from 'false' to 'true'
INFO :   execute[open firewall http] executed will change from 'false' to 'true'

6.ブラウザでWordPressの初期設定画面の起動を確認

ALT WordPressの初期設定画面の起動を確認

7.コマンドを再実行した場合、設定済みの処理がスキップされる

yum update以外の全ての処理がスキップされていることが確認できる。

INFO : Starting Itamae...
INFO : Loading node data from /etc/itamae/nodes/tissvv096.json...
INFO : Recipe: /etc/itamae/cookbooks/wordpress/default.rb
INFO :   execute[update packages] executed will change from 'false' to 'true'

Copyright © ITmedia, Inc. All Rights Reserved.

RSSについて

アイティメディアIDについて

メールマガジン登録

@ITのメールマガジンは、 もちろん、すべて無料です。ぜひメールマガジンをご購読ください。