CentOS7.2（minimal）へのPuppet sever&serverspecインストール手順

## 背景
社内の勉強会で「Infrastructure as Code」をデモして見せたい！
・・・が、何から用意していいか迷ったため、Qiita他資料を参考に、これから環境構築～デモ実践までの道のりをメモする。

## 今回の目的
VirtualBox上にCentOS7.2（minimal）を立て、Puppet severとserverspecを導入する。

## 環境
ホストOS

- Windows 10 Pro
- VirtualBox 5.0.18 

ゲストOS（Puppet server ＆ serverspec動作環境）
ホストOS上のVirtualBoxで動作。ネットワーク設定は以下の通り。
 
- アダプターはブリッジ一つ
- OSはCentOS7.2（CentOS Linux release 7.2.1511 (Core)）

## CentOSの下準備
仮想マシンが立ち上がったら、まずはPuppet severとserverspecを入れる前の下準備。

- SELinuxの無効化

`# vi /etc/selinux/config`

```vim:/etc/selinux/config
SELINUX=disabled
```
（SELinuxが無効化されるのは再起動後）

- firewalld.serviceの停止確認（minimalでは動いてないみたい）

```
# systemctl status firewalld.service
● firewalld.service
   Loaded: not-found (Reason: No such file or directory)
   Active: inactive (dead)
```

- yumパッケージのアップデート

`# yum -y update`

- ここまでやったら再起動

`# reboot`

## Puppet serverのインストール

Christopher Carfiさんの[HOW TO INSTALL PUPPET ON CENTOS7](https://www.godaddy.com/garage/tech/config/install-puppet-centos7/)を参考にインストール。

- パッケージライブラリにPuppetを追加

`# rpm -ivh https://yum.puppetlabs.com/el/7/products/x86_64/puppetlabs-release-7-11.noarch.rpm`

- Puppet serverをインストール

`# yum install -y puppet-server`

- Puppet serverの起動

`# systemctl start  puppetmaster.service`

- Puppet serverの自動起動設定

```
# systemctl enable puppetmaster.service
Created symlink from /etc/systemd/system/multi-user.target.wants/puppetmaster.service to /usr/lib/systemd/system/puppetmaster.service.
```

- Puppet serverの起動状態の確認

```
# systemctl status puppetmaster.service
● puppetmaster.service - Puppet master
   Loaded: loaded (/usr/lib/systemd/system/puppetmaster.service; enabled; vendor preset: disabled)
   Active: active (running) since 水 2016-05-04 00:59:59 JST; 11h ago
 Main PID: 8860 (puppet)
   CGroup: /system.slice/puppetmaster.service
           mq8860 /usr/bin/ruby /usr/bin/puppet master --no-daemonize

 5月 04 00:59:59 TESTSV01 systemd[1]: Started Puppet master.
 5月 04 00:59:59 TESTSV01 systemd[1]: Starting Puppet master...
 5月 04 01:00:01 TESTSV01 puppet[8860]: Notice: Signed certificate request for ca
 5月 04 01:00:05 TESTSV01 puppet[8860]: Notice: testsv01 has a waiting certificate request
 5月 04 01:00:05 TESTSV01 puppet[8860]: Notice: Signed certificate request for testsv01
 5月 04 01:00:05 TESTSV01 puppet[8860]: Notice: Removing file Puppet::SSL::CertificateRequest testsv01 at '/var....pem'
 5月 04 01:00:05 TESTSV01 puppet[8860]: Notice: Removing file Puppet::SSL::CertificateRequest testsv01 at '/var....pem'
 5月 04 01:00:05 TESTSV01 puppet[8860]: Notice: Starting Puppet master version 3.8.7
Hint: Some lines were ellipsized, use -l to show in full.
``` 

(補足)Puppet serverをyumでインストールすると、依存性解決により`ruby`, `rubygems`が同時にインストールされる。

```
# ruby -v
ruby 2.0.0p598 (2014-11-13) [x86_64-linux]`
# gem -v
2.0.14
```

## serverspecのインストール
- gemでserverspecをインストールする。

`# gem install serverspec`

- serverspecでWindowsマシンもテストしたいため、uchiunyoさんの[serverspecでWindowsのテストをお試しする](http://qiita.com/uchiunyo/items/46d02605315f6b146c14)を参考に`winrm`をインストールする。

```
# mkdir serverspec
# cd serverspec/
# vi Gemfile
```

```vim:/root/serverspec/Gemfile
source 'https://rubygems.org'

gem 'serverspec'
gem 'rake'
gem 'winrm'
```

さて、実行。

```
# bundle install --path=vendor/bundle
-bash: bundle: コマンドが見つかりません
```

`bundler`がない。gemでインストールする。

`# gem install bundler`

bundlerがインストールされたところでリトライ。

```
# bundle install --path=vendor/bundle
	Don't run Bundler as root. Bundler can ask for sudo if it is needed, and installing your bundle as root will break this
	application for all non-root users on this machine.
	Fetching gem metadata from https://rubygems.org/
	Fetching version metadata from https://rubygems.org/
	Resolving dependencies...
	Rubygems 2.0.14 is not threadsafe, so your gems will be installed one at a time. Upgrade to Rubygems 2.1.0 or higher to enable parallel gem installation.
	Installing rake 11.1.2
	Installing builder 3.2.2
	Installing diff-lcs 1.2.5
	Installing ffi 1.9.10 with native extensions

	Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension.

	    /usr/bin/ruby extconf.rb
	mkmf.rb can't find header files for ruby at /usr/share/include/ruby.h


	Gem files will remain installed in /root/serverspec/vendor/bundle/ruby/gems/ffi-1.9.10 for inspection.
	Results logged to /root/serverspec/vendor/bundle/ruby/gems/ffi-1.9.10/ext/ffi_c/gem_make.out
	Installing httpclient 2.8.0
	Installing little-plugger 1.1.4
	Installing multi_json 1.11.3
	Installing net-ssh 3.1.1
	Installing net-telnet 0.1.1
	Installing nori 2.6.0
	Installing rspec-support 3.4.1
	Installing rubyntlm 0.6.0
	Installing sfl 2.2
	Using bundler 1.12.1
	Installing gyoku 1.3.1
	An error occurred while installing ffi (1.9.10), and Bundler cannot continue.
	Make sure that `gem install ffi -v '1.9.10'` succeeds before bundling.
	[root@TESTSV01 serverspec]# gem install ffi -v '1.9.10'
	Building native extensions.  This could take a while...
	ERROR:  Error installing ffi:
	        ERROR: Failed to build gem native extension.

	    /usr/bin/ruby extconf.rb
	mkmf.rb can't find header files for ruby at /usr/share/include/ruby.h


	Gem files will remain installed in /usr/local/share/gems/gems/ffi-1.9.10 for inspection.
	Results logged to /usr/local/share/gems/gems/ffi-1.9.10/ext/ffi_c/gem_make.out
```

エラー。何かが足りない。調査すると、satzzさんの["mkmf.rb can't find header files for ruby"の対応](http://qiita.com/satzz/items/0361a7093a57b459e15b)の記事にて、`ruby-devel`が不足していることが原因との内容が。

`# yum -y install ruby-devel`

ruby-develを入れてリトライ。

```
# bundle install --path=vendor/bundle
Don't run Bundler as root. Bundler can ask for sudo if it is needed, and installing your bundle as root will break this
application for all non-root users on this machine.
Fetching gem metadata from https://rubygems.org/
Fetching version metadata from https://rubygems.org/
Resolving dependencies...
Rubygems 2.0.14 is not threadsafe, so your gems will be installed one at a time. Upgrade to Rubygems 2.1.0 or higher to enable parallel gem installation.
Using rake 11.1.2
Using builder 3.2.2
Using diff-lcs 1.2.5
Installing ffi 1.9.10 with native extensions

Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension.

    /usr/bin/ruby extconf.rb
checking for ffi.h... *** 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/ruby
        --with-ffi_c-dir
        --without-ffi_c-dir
        --with-ffi_c-include
        --without-ffi_c-include=${ffi_c-dir}/include
        --with-ffi_c-lib
        --without-ffi_c-lib=${ffi_c-dir}/
        --with-libffi-config
        --without-libffi-config
        --with-pkg-config
        --without-pkg-config
/usr/share/ruby/mkmf.rb:434:in `try_do': The compiler failed to generate an executable file. (RuntimeError)
You have to install development tools first.
        from /usr/share/ruby/mkmf.rb:565:in `try_cpp'
        from /usr/share/ruby/mkmf.rb:1044:in `block in have_header'
        from /usr/share/ruby/mkmf.rb:895:in `block in checking_for'
        from /usr/share/ruby/mkmf.rb:340:in `block (2 levels) in postpone'
        from /usr/share/ruby/mkmf.rb:310:in `open'
        from /usr/share/ruby/mkmf.rb:340:in `block in postpone'
        from /usr/share/ruby/mkmf.rb:310:in `open'
        from /usr/share/ruby/mkmf.rb:336:in `postpone'
        from /usr/share/ruby/mkmf.rb:894:in `checking_for'
        from /usr/share/ruby/mkmf.rb:1043:in `have_header'
        from extconf.rb:16:in `<main>'


Gem files will remain installed in /root/serverspec/vendor/bundle/ruby/gems/ffi-1.9.10 for inspection.
Results logged to /root/serverspec/vendor/bundle/ruby/gems/ffi-1.9.10/ext/ffi_c/gem_make.out
```

出力されるメッセージが変わった！
jamonholmgrenさんの[Error: "You have to install development tools first." #1727](https://github.com/CocoaPods/CocoaPods/issues/1727)の記事から、障壁はコンパイル関連・・・`gcc`がない、もしくはパスが通っていないのでは？と思ったら案の定。

```
# gcc -v
-bash: gcc: コマンドが見つかりません
```

gccをyumでインストールする。

`# yum -y install gcc`

今度こそ！

```
# bundle install --path=vendor/bundle
Don't run Bundler as root. Bundler can ask for sudo if it is needed, and installing your bundle as root will break this
application for all non-root users on this machine.
Fetching gem metadata from https://rubygems.org/
Fetching version metadata from https://rubygems.org/
Resolving dependencies...
Rubygems 2.0.14 is not threadsafe, so your gems will be installed one at a time. Upgrade to Rubygems 2.1.0 or higher to enable parallel gem installation.
Using rake 11.1.2
Using builder 3.2.2
Using diff-lcs 1.2.5
Installing ffi 1.9.10 with native extensions
Using httpclient 2.8.0
Using little-plugger 1.1.4
Using multi_json 1.11.3
Using net-ssh 3.1.1
Using net-telnet 0.1.1
Using nori 2.6.0
Using rspec-support 3.4.1
Using rubyntlm 0.6.0
Using sfl 2.2
Using bundler 1.12.1
Using gyoku 1.3.1
Installing gssapi 1.2.0
Installing logging 2.1.0
Installing net-scp 1.2.1
Installing rspec-core 3.4.4
Installing rspec-expectations 3.4.0
Installing rspec-mocks 3.4.1
Installing winrm 1.8.0
Installing specinfra 2.57.2
Installing rspec-its 1.2.0
Installing rspec 3.4.0
Installing serverspec 2.33.0
Bundle complete! 3 Gemfile dependencies, 26 gems now installed.
Bundled gems are installed into ./vendor/bundle.
[root@TESTSV01 serverspec]#
[root@TESTSV01 serverspec]#
[root@TESTSV01 serverspec]# gem install winrm
Building native extensions.  This could take a while...
Successfully installed ffi-1.9.10
Fetching: gssapi-1.2.0.gem (100%)
Successfully installed gssapi-1.2.0
Fetching: httpclient-2.8.0.gem (100%)
Successfully installed httpclient-2.8.0
Fetching: rubyntlm-0.6.0.gem (100%)
Successfully installed rubyntlm-0.6.0
Fetching: little-plugger-1.1.4.gem (100%)
Successfully installed little-plugger-1.1.4
Fetching: logging-2.1.0.gem (100%)
Successfully installed logging-2.1.0
Fetching: nori-2.6.0.gem (100%)
Successfully installed nori-2.6.0
Fetching: builder-3.2.2.gem (100%)
Successfully installed builder-3.2.2
Fetching: gyoku-1.3.1.gem (100%)
Successfully installed gyoku-1.3.1
Fetching: winrm-1.8.0.gem (100%)
Successfully installed winrm-1.8.0
Parsing documentation for ffi-1.9.10
Installing ri documentation for ffi-1.9.10
Parsing documentation for gssapi-1.2.0
Installing ri documentation for gssapi-1.2.0
Parsing documentation for httpclient-2.8.0
Installing ri documentation for httpclient-2.8.0
Parsing documentation for rubyntlm-0.6.0
Installing ri documentation for rubyntlm-0.6.0
Parsing documentation for little-plugger-1.1.4
Installing ri documentation for little-plugger-1.1.4
Parsing documentation for logging-2.1.0
Installing ri documentation for logging-2.1.0
Parsing documentation for nori-2.6.0
Installing ri documentation for nori-2.6.0
Parsing documentation for builder-3.2.2
Installing ri documentation for builder-3.2.2
Parsing documentation for gyoku-1.3.1
Installing ri documentation for gyoku-1.3.1
Parsing documentation for winrm-1.8.0
Installing ri documentation for winrm-1.8.0
10 gems installed
```

とりあえず、Windowsマシンにも対応可能なserverspecサーバの構築完了！

puppetマニフェスト、serverspec-initコマンドについては次回に持ち越し。。。