読者です 読者をやめる 読者になる 読者になる

サーバ/インフラエンジニア養成読本 DevOps編(特集2 第2章)

サーバ/インフラエンジニア要請読本DevOps編

開発環境に限らず構築するインフラを標準化するためにDevOpsについて調べていました。このようなプロビジョニングに関してはServerSpecの登場でChef + ServerSpec をVagrant 環境で実行するのがRuby使いとしてはお手軽なように感じました。

今回はサーバ/インフラエンジニア養成読本 DevOps編 [Infrastructure as Code を実践するノウハウが満載! ] (Software Design plus)の特集2の2章を実際に試してみました。

サーバ/インフラエンジニア養成読本 DevOps編 [Infrastructure as Code を実践するノウハウが満載! ] (Software Design plus)

サーバ/インフラエンジニア養成読本 DevOps編 [Infrastructure as Code を実践するノウハウが満載! ] (Software Design plus)

knife-soloのインストール

knife-soloをインストールします。

╭─ymanabe@Yoichiro-no-MacBook-Pro  ~ ‹2.2.4›
╰─$ gem install knife-solo -v 0.5.1                                                                                                                                           2 ↵
Fetching: mixlib-config-2.2.1.gem (100%)
Successfully installed mixlib-config-2.2.1
...
29 gems installed

ServerSpecのインストール

ServerSpecをインストールします。

╭─ymanabe@Yoichiro-no-MacBook-Pro  ~ ‹2.2.4›
╰─$ gem install serverspec -v 2.24.3
Fetching: multi_json-1.11.2.gem (100%)
...
14 gems installed

knife-soloの初期化処理実行

knife-soloの初期化を行います。

╭─ymanabe@Yoichiro-no-MacBook-Pro  ~ ‹2.2.4›
╰─$ knife solo init infra-ci-cookbooks
WARNING: No knife configuration file found
Creating kitchen...
Creating knife.rb in kitchen...
Creating cupboards...

╭─ymanabe@Yoichiro-no-MacBook-Pro  ~ ‹2.2.4›
╰─$ ls -al infra-ci-cookbooks                                                                                                                                                 1 ↵
total 8
drwxr-xr-x  10 ymanabe  staff   340  4  3 13:34 .
drwxr-xr-x+ 71 ymanabe  staff  2414  4  3 15:16 ..
drwxr-xr-x   3 ymanabe  staff   102  4  3 13:34 .chef
-rw-r--r--   1 ymanabe  staff    12  4  3 13:34 .gitignore
drwxr-xr-x   3 ymanabe  staff   102  4  3 13:34 cookbooks
drwxr-xr-x   3 ymanabe  staff   102  4  3 13:34 data_bags
drwxr-xr-x   3 ymanabe  staff   102  4  3 13:34 environments
drwxr-xr-x   3 ymanabe  staff   102  4  3 13:34 nodes
drwxr-xr-x   3 ymanabe  staff   102  4  3 13:34 roles
drwxr-xr-x   3 ymanabe  staff   102  4  3 13:34 site-cookbooks

Vagrantfileの作成

Vagrantfileをinfra-ci-cookbooksに作成します。

╭─ymanabe@Yoichiro-no-MacBook-Pro  ~/vagrant ‹2.2.4›
╰─$ vim Vagrantfile

作成したVagrantfileに利用する仮想環境の設定を記述します。

  1 Vagrant.configure(2) do |config|
  2     config.vm.box = "bento/centos-7.1"
  3     config.vm.define :webapp do |host|
  4         host.vm.hostname="webapp"
  5     end
  6 end

Vagrantを起動する

Vagrantで仮想環境を起動します。

╭─ymanabe@Yoichiro-no-MacBook-Pro  ~/infra-ci-cookbooks ‹2.2.4›
╰─$ vagrant up
Bringing machine 'webapp' up with 'virtualbox' provider...
==> webapp: Importing base box 'bento/centos-7.1'...
==> webapp: Matching MAC address for NAT networking...
==> webapp: Checking if box 'bento/centos-7.1' is up to date...
==> webapp: Setting the name of the VM: infra-ci-cookbooks_webapp_1459664814580_78251
==> webapp: Clearing any previously set network interfaces...
==> webapp: Preparing network interfaces based on configuration...
    webapp: Adapter 1: nat
==> webapp: Forwarding ports...
    webapp: 22 (guest) => 2222 (host) (adapter 1)
==> webapp: Booting VM...
==> webapp: Waiting for machine to boot. This may take a few minutes...
    webapp: SSH address: 127.0.0.1:2222
    webapp: SSH username: vagrant
    webapp: SSH auth method: private key
    webapp:
    webapp: Vagrant insecure key detected. Vagrant will automatically replace
    webapp: this with a newly generated keypair for better security.
    webapp:
    webapp: Inserting generated public key within guest...
    webapp: Removing insecure key from the guest if it's present...
    webapp: Key inserted! Disconnecting and reconnecting using new SSH key...
==> webapp: Machine booted and ready!
==> webapp: Checking for guest additions in VM...
==> webapp: Setting hostname...
==> webapp: Mounting shared folders...
    webapp: /vagrant => /Users/ymanabe/infra-ci-cookbooks

vagrantのssh接続設定を作成する

Vagrantの仮想環境へ接続するための設定を出力します。

╭─ymanabe@Yoichiro-no-MacBook-Pro  ~/infra-ci-cookbooks ‹2.2.4›
╰─$ vagrant ssh-config webapp >> vagrant_ssh_config

╭─ymanabe@Yoichiro-no-MacBook-Pro  ~/infra-ci-cookbooks ‹2.2.4›
╰─$ cat vagrant_ssh_config
Host webapp
  HostName 127.0.0.1
  User vagrant
  Port 2222
  UserKnownHostsFile /dev/null
  StrictHostKeyChecking no
  PasswordAuthentication no
  IdentityFile "/Users/ymanabe/infra-ci-cookbooks/.vagrant/machines/webapp/virtualbox/private_key"
  IdentitiesOnly yes
  LogLevel FATAL

Vagrantで起動したVMへChefをインストールする

Vagrantで起動したVMへChefをインストールします。

╭─ymanabe@Yoichiro-no-MacBook-Pro  ~/infra-ci-cookbooks ‹2.2.4›
╰─$ knife solo prepare webapp -F vagrant_ssh_config
Bootstrapping Chef...
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 19559  100 19559    0     0  18242      0  0:00:01  0:00:01 --:--:-- 18262
el 7 x86_64
Getting information for chef stable 12.8.1 for el...
downloading https://omnitruck-direct.chef.io/stable/chef/metadata?v=12.8.1&p=el&pv=7&m=x86_64
  to file /tmp/install.sh.12712/metadata.txt
trying wget...
sha1    529f02f14d8af8ced469d615c61d1853183b47d2
sha256  a9c80dfc45b5204684cb5344e77230ff3403a5f8d057dd063b0707d785ccd236
url https://packages.chef.io/stable/el/7/chef-12.8.1-1.el7.x86_64.rpm
version 12.8.1
downloaded metadata file looks valid...
downloading https://packages.chef.io/stable/el/7/chef-12.8.1-1.el7.x86_64.rpm
  to file /tmp/install.sh.12712/chef-12.8.1-1.el7.x86_64.rpm
trying wget...
Comparing checksum with sha256sum...
Installing chef 12.8.1
installing with rpm...
warning: /tmp/install.sh.12712/chef-12.8.1-1.el7.x86_64.rpm: Header V4 DSA/SHA1 Signature, key ID 83ef826a: NOKEY
Preparing...                          ################################# [100%]
Updating / installing...
   1:chef-12.8.1-1.el7                ################################# [100%]
Thank you for installing Chef!
Generating node config 'nodes/webapp.json'...

テスト用VMのスナップショットを作成する

ChefがインストールされたVMのスナップショットを作成します。このスナップショットはChefのレシピが作成されたあとに、このスナップショットから適用して正しく反映されるかを確認します。

╭─ymanabe@Yoichiro-no-MacBook-Pro  ~/infra-ci-cookbooks ‹2.2.4›
╰─$ vagrant snapshot save webapp webapp-plane
==> webapp: Snapshotting the machine as 'webapp-plane'...
==> webapp: Snapshot saved! You can restore the snapshot at any time by
==> webapp: using `vagrant snapshot restore`. You can delete it using
==> webapp: `vagrant snapshot delete`.

╭─ymanabe@Yoichiro-no-MacBook-Pro  ~/infra-ci-cookbooks ‹2.2.4›
╰─$ vagrant snapshot list
webapp-plane

ServerSpecの雛形を作成する

ServerSpecの雛形を生成します。

╭─ymanabe@Yoichiro-no-MacBook-Pro  ~/infra-ci-cookbooks ‹2.2.4›
╰─$ serverspec-init
Select OS type:

  1) UN*X
  2) Windows

Select number: 1

Select a backend type:

  1) SSH
  2) Exec (local)

Select number: 1

Vagrant instance y/n: y
Auto-configure Vagrant from Vagrantfile? y/n: y
 + spec/
 + spec/webapp/
 + spec/webapp/sample_spec.rb
 + spec/spec_helper.rb
 + Rakefile
 + .rspec
╭─ymanabe@Yoichiro-no-MacBook-Pro  ~/infra-ci-cookbooks ‹2.2.4›
╰─$ ll
total 24
-rw-r--r--  1 ymanabe  staff   685B  4  3 15:39 Rakefile
-rw-r--r--  1 ymanabe  staff   142B  4  3 15:26 Vagrantfile
drwxr-xr-x  3 ymanabe  staff   102B  4  3 13:34 cookbooks
drwxr-xr-x  3 ymanabe  staff   102B  4  3 13:34 data_bags
drwxr-xr-x  3 ymanabe  staff   102B  4  3 13:34 environments
drwxr-xr-x  4 ymanabe  staff   136B  4  3 15:33 nodes
drwxr-xr-x  3 ymanabe  staff   102B  4  3 13:34 roles
drwxr-xr-x  3 ymanabe  staff   102B  4  3 13:34 site-cookbooks
drwxr-xr-x  4 ymanabe  staff   136B  4  3 15:39 spec
-rw-r--r--  1 ymanabe  staff   284B  4  3 15:27 vagrant_ssh_config

不要なspecを削除する

作成されたSpecのうち、サンプルは不要なため削除します。

╭─ymanabe@Yoichiro-no-MacBook-Pro  ~/infra-ci-cookbooks ‹2.2.4›
╰─$ cat spec/webapp/sample_spec.rb
require 'spec_helper'

describe package('httpd'), :if => os[:family] == 'redhat' do
  it { should be_installed }
end

describe package('apache2'), :if => os[:family] == 'ubuntu' do
  it { should be_installed }
end

describe service('httpd'), :if => os[:family] == 'redhat' do
  it { should be_enabled }
  it { should be_running }
end

describe service('apache2'), :if => os[:family] == 'ubuntu' do
  it { should be_enabled }
  it { should be_running }
end

describe service('org.apache.httpd'), :if => os[:family] == 'darwin' do
  it { should be_enabled }
  it { should be_running }
end

describe port(80) do
  it { should be_listening }
end
╭─ymanabe@Yoichiro-no-MacBook-Pro  ~/infra-ci-cookbooks ‹2.2.4›
╰─$ rm spec/webapp/sample_spec.rb

ServerSpecを作成する

ServerSpecにhttpdがインストールされていることを確認するSpecを記述します。

{
  "run_list": [
    "recipe[httpd]"
  ],
  "automatic": {
    "ipaddress": "webapp"
  }
}

ServerSpecを実行し失敗することを確認します

ServerSpecを実行し、httpdがインストールされていないためエラーとなることを確認します。

╭─ymanabe@Yoichiro-no-MacBook-Pro  ~/infra-ci-cookbooks ‹2.2.4›
╰─$ rake spec:webapp
/Users/ymanabe/.rbenv/versions/2.2.4/bin/ruby -I/Users/ymanabe/.rbenv/versions/2.2.4/lib/ruby/gems/2.2.0/gems/rspec-support-3.4.1/lib:/Users/ymanabe/.rbenv/versions/2.2.4/lib/ruby/gems/2.2.0/gems/rspec-core-3.4.4/lib /Users/ymanabe/.rbenv/versions/2.2.4/lib/ruby/gems/2.2.0/gems/rspec-core-3.4.4/exe/rspec --pattern spec/webapp/\*_spec.rb

Package "httpd"
  should be installed (FAILED - 1)

Service "httpd"
  should be enabled (FAILED - 2)
  should be running (FAILED - 3)

Port "80"
  should be listening (FAILED - 4)

Failures:

  1) Package "httpd" should be installed
     On host `webapp'
     Failure/Error: it{should be_installed}
       expected Package "httpd" to be installed
       sudo -p 'Password: ' /bin/sh -c rpm\ -q\ httpd
       package httpd is not installed

     # ./spec/webapp/http_spec.rb:4:in `block (2 levels) in <top (required)>'

  2) Service "httpd" should be enabled
     On host `webapp'
     Failure/Error: it{should be_enabled}
       expected Service "httpd" to be enabled
       sudo -p 'Password: ' /bin/sh -c systemctl\ --quiet\ is-enabled\ httpd

     # ./spec/webapp/http_spec.rb:8:in `block (2 levels) in <top (required)>'

  3) Service "httpd" should be running
     On host `webapp'
     Failure/Error: it{should be_running}
       expected Service "httpd" to be running
       sudo -p 'Password: ' /bin/sh -c systemctl\ is-active\ httpd
       unknown

     # ./spec/webapp/http_spec.rb:9:in `block (2 levels) in <top (required)>'

  4) Port "80" should be listening
     On host `webapp'
     Failure/Error: it{should be_listening}
       expected Port "80" to be listening
       sudo -p 'Password: ' /bin/sh -c ss\ -tunl\ \|\ grep\ --\ :80\\\

     # ./spec/webapp/http_spec.rb:13:in `block (2 levels) in <top (required)>'

Finished in 0.23034 seconds (files took 6.58 seconds to load)
4 examples, 4 failures

Failed examples:

rspec ./spec/webapp/http_spec.rb:4 # Package "httpd" should be installed
rspec ./spec/webapp/http_spec.rb:8 # Service "httpd" should be enabled
rspec ./spec/webapp/http_spec.rb:9 # Service "httpd" should be running
rspec ./spec/webapp/http_spec.rb:13 # Port "80" should be listening

/Users/ymanabe/.rbenv/versions/2.2.4/bin/ruby -I/Users/ymanabe/.rbenv/versions/2.2.4/lib/ruby/gems/2.2.0/gems/rspec-support-3.4.1/lib:/Users/ymanabe/.rbenv/versions/2.2.4/lib/ruby/gems/2.2.0/gems/rspec-core-3.4.4/lib /Users/ymanabe/.rbenv/versions/2.2.4/lib/ruby/gems/2.2.0/gems/rspec-core-3.4.4/exe/rspec --pattern spec/webapp/\*_spec.rb failed

プロビジョニングを作成する

プロビジョニング内容を作成するため、レシピを作成します。

╭─ymanabe@Yoichiro-no-MacBook-Pro  ~/infra-ci-cookbooks ‹2.2.4›
╰─$ knife cookbook create httpd -o site-cookbooks                                                                                                                             1 ↵
** Creating cookbook httpd in /Users/ymanabe/infra-ci-cookbooks/site-cookbooks
** Creating README for cookbook: httpd
** Creating CHANGELOG for cookbook: httpd
** Creating metadata for cookbook: httpd

作成したレシピにプロビジョニングの内容を記述します。

require 'spec_helper'

describe package('httpd'), :if => os[:family] == 'redhat' do
  it{should be_installed}
end

describe service('httpd'), :if => os[:family] == 'redhat' do
  it{should be_enabled}
  it{should be_running}
end

describe port(80), :if => os[:family] == 'redhat' do
  it{should be_listening}
end

また、適用する設定を変更します。

{
  "run_list": [
    "recipe[httpd]"
  ],
  "automatic": {
    "ipaddress": "webapp"
  }
}

プロビジョニングを実行する

プロビジョニングを実行します。

╭─ymanabe@Yoichiro-no-MacBook-Pro  ~/infra-ci-cookbooks ‹2.2.4› 
╰─$ knife solo cook webapp -F vagrant_ssh_config                                                                                                                                                                                                       100 ↵
Running Chef on webapp...
Checking Chef version...
Uploading the kitchen...
Generating solo config...
Running Chef: sudo chef-solo -c ~/chef-solo/solo.rb -j ~/chef-solo/dna.json
Starting Chef Client, version 12.8.1
Installing Cookbook Gems:
Compiling Cookbooks...
Converging 2 resources
Recipe: httpd::default
  * yum_package[httpd] action install
    - install version 2.4.6-40.el7.centos of package httpd
  * service[httpd] action enable
    - enable service service[httpd]
  * service[httpd] action start
    - start service service[httpd]

Running handlers:
Running handlers complete
Chef Client finished, 3/3 resources updated in 06 seconds

ServerSpecを実行しエラーとならないことを確認します

今度はプロビジョニングされているのでServerSpecを実行してエラーとならないことを確認します。

╭─ymanabe@Yoichiro-no-MacBook-Pro  ~/infra-ci-cookbooks ‹2.2.4›
╰─$ rake spec:webapp
/Users/ymanabe/.rbenv/versions/2.2.4/bin/ruby -I/Users/ymanabe/.rbenv/versions/2.2.4/lib/ruby/gems/2.2.0/gems/rspec-support-3.4.1/lib:/Users/ymanabe/.rbenv/versions/2.2.4/lib/ruby/gems/2.2.0/gems/rspec-core-3.4.4/lib /Users/ymanabe/.rbenv/versions/2.2.4/lib/ruby/gems/2.2.0/gems/rspec-core-3.4.4/exe/rspec --pattern spec/webapp/\*_spec.rb

Package "httpd"
  should be installed

Service "httpd"
  should be enabled
  should be running

Port "80"
  should be listening

Finished in 0.1096 seconds (files took 7.17 seconds to load)
4 examples, 0 failures

パッケージインストールに関するServerSpecの作成と実行

ServerSpecでインストールするべきパッケージのSpecを作成します。

require 'spec_helper'

%w{
dstat
git
tmux
vim-enhanced
yum-plugin-versionlock
zsh
}.each do |pkg|
  describe package(pkg) do
    it {should be_installed}
  end
end

Specを実行してエラーとなることを確認します。

╭─ymanabe@Yoichiro-no-MacBook-Pro  ~/infra-ci-cookbooks ‹2.2.4›
╰─$ rake spec:webapp
/Users/ymanabe/.rbenv/versions/2.2.4/bin/ruby -I/Users/ymanabe/.rbenv/versions/2.2.4/lib/ruby/gems/2.2.0/gems/rspec-support-3.4.1/lib:/Users/ymanabe/.rbenv/versions/2.2.4/lib/ruby/gems/2.2.0/gems/rspec-core-3.4.4/lib /Users/ymanabe/.rbenv/versions/2.2.4/lib/ruby/gems/2.2.0/gems/rspec-core-3.4.4/exe/rspec --pattern spec/webapp/\*_spec.rb

Package "dstat"
  should be installed (FAILED - 1)

Package "git"
  should be installed (FAILED - 2)

Package "tmux"
  should be installed (FAILED - 3)

Package "vim-enhanced"
  should be installed (FAILED - 4)

Package "yum-plugin-versionlock"
  should be installed (FAILED - 5)

Package "zsh"
  should be installed (FAILED - 6)

Package "httpd"
  should be installed

Service "httpd"
  should be enabled
  should be running

Port "80"
  should be listening

Failures:

  1) Package "dstat" should be installed
     On host `webapp'
     Failure/Error: it {should be_installed}
       expected Package "dstat" to be installed
       sudo -p 'Password: ' /bin/sh -c rpm\ -q\ dstat
       package dstat is not installed

     # ./spec/webapp/base_packages_spec.rb:12:in `block (3 levels) in <top (required)>'

  2) Package "git" should be installed
     On host `webapp'
     Failure/Error: it {should be_installed}
       expected Package "git" to be installed
       sudo -p 'Password: ' /bin/sh -c rpm\ -q\ git
       package git is not installed

     # ./spec/webapp/base_packages_spec.rb:12:in `block (3 levels) in <top (required)>'

  3) Package "tmux" should be installed
     On host `webapp'
     Failure/Error: it {should be_installed}
       expected Package "tmux" to be installed
       sudo -p 'Password: ' /bin/sh -c rpm\ -q\ tmux
       package tmux is not installed

     # ./spec/webapp/base_packages_spec.rb:12:in `block (3 levels) in <top (required)>'

  4) Package "vim-enhanced" should be installed
     On host `webapp'
     Failure/Error: it {should be_installed}
       expected Package "vim-enhanced" to be installed
       sudo -p 'Password: ' /bin/sh -c rpm\ -q\ vim-enhanced
       package vim-enhanced is not installed

     # ./spec/webapp/base_packages_spec.rb:12:in `block (3 levels) in <top (required)>'

  5) Package "yum-plugin-versionlock" should be installed
     On host `webapp'
     Failure/Error: it {should be_installed}
       expected Package "yum-plugin-versionlock" to be installed
       sudo -p 'Password: ' /bin/sh -c rpm\ -q\ yum-plugin-versionlock
       package yum-plugin-versionlock is not installed

     # ./spec/webapp/base_packages_spec.rb:12:in `block (3 levels) in <top (required)>'

  6) Package "zsh" should be installed
     On host `webapp'
     Failure/Error: it {should be_installed}
       expected Package "zsh" to be installed
       sudo -p 'Password: ' /bin/sh -c rpm\ -q\ zsh
       package zsh is not installed

     # ./spec/webapp/base_packages_spec.rb:12:in `block (3 levels) in <top (required)>'

Finished in 0.36313 seconds (files took 6.61 seconds to load)
10 examples, 6 failures

Failed examples:

rspec './spec/webapp/base_packages_spec.rb[1:1]' # Package "dstat" should be installed
rspec './spec/webapp/base_packages_spec.rb[2:1]' # Package "git" should be installed
rspec './spec/webapp/base_packages_spec.rb[3:1]' # Package "tmux" should be installed
rspec './spec/webapp/base_packages_spec.rb[4:1]' # Package "vim-enhanced" should be installed
rspec './spec/webapp/base_packages_spec.rb[5:1]' # Package "yum-plugin-versionlock" should be installed
rspec './spec/webapp/base_packages_spec.rb[6:1]' # Package "zsh" should be installed

/Users/ymanabe/.rbenv/versions/2.2.4/bin/ruby -I/Users/ymanabe/.rbenv/versions/2.2.4/lib/ruby/gems/2.2.0/gems/rspec-support-3.4.1/lib:/Users/ymanabe/.rbenv/versions/2.2.4/lib/ruby/gems/2.2.0/gems/rspec-core-3.4.4/lib /Users/ymanabe/.rbenv/versions/2.2.4/lib/ruby/gems/2.2.0/gems/rspec-core-3.4.4/exe/rspec --pattern spec/webapp/\*_spec.rb failed

specに対応するレシピを作成する

specに対応するレシピを作成します。

╭─ymanabe@Yoichiro-no-MacBook-Pro  ~/infra-ci-cookbooks ‹2.2.4›
╰─$ knife cookbook create base_packages -o site-cookbooks
** Creating cookbook base_packages in /Users/ymanabe/infra-ci-cookbooks/site-cookbooks
** Creating README for cookbook: base_packages
** Creating CHANGELOG for cookbook: base_packages
** Creating metadata for cookbook: base_packages

レシピを作成します。

#
# Cookbook Name:: base_packages
# Recipe:: default
#
# Copyright 2016, YOUR_COMPANY_NAME
#
# All rights reserved - Do Not Redistribute
#
%w{
dstat
git
tmux
vim-enhanced
yum-plugin-versionlock
zsh
}.each do |pkg|
  package pkg do
    action :install
  end
end

また、webapp.jsonにレシピを追加します。

{
  "run_list": [
    "recipe[httpd]",
    "recipe[base_packages]"
  ],
  "automatic": {
    "ipaddress": "webapp"
  }
}

プロビジョニングを実行しレシピを反映する

プロビジョニングを実行してレシピを反映します。

╭─ymanabe@Yoichiro-no-MacBook-Pro  ~/infra-ci-cookbooks ‹2.2.4›
╰─$ knife solo cook webapp -F vagrant_ssh_config
Running Chef on webapp...
Checking Chef version...
Uploading the kitchen...
Generating solo config...
Running Chef: sudo chef-solo -c ~/chef-solo/solo.rb -j ~/chef-solo/dna.json
Starting Chef Client, version 12.8.1
Installing Cookbook Gems:
Compiling Cookbooks...
Converging 8 resources
Recipe: httpd::default
  * yum_package[httpd] action install (up to date)
  * service[httpd] action enable (up to date)
  * service[httpd] action start (up to date)
Recipe: base_packages::default
  * yum_package[dstat] action install
    - install version 0.7.2-12.el7 of package dstat
  * yum_package[git] action install
    - install version 1.8.3.1-6.el7_2.1 of package git
  * yum_package[tmux] action install
    - install version 1.8-4.el7 of package tmux
  * yum_package[vim-enhanced] action install
    - install version 7.4.160-1.el7 of package vim-enhanced
  * yum_package[yum-plugin-versionlock] action install
    - install version 1.1.31-34.el7 of package yum-plugin-versionlock
  * yum_package[zsh] action install
    - install version 5.0.2-14.el7 of package zsh

Running handlers:
Running handlers complete
Chef Client finished, 6/9 resources updated in 26 seconds

ServerSpecを実行しGreenとなることを確認する

ServerSpecを実行してテスト結果がGreenとなることを確認します。

╭─ymanabe@Yoichiro-no-MacBook-Pro  ~/infra-ci-cookbooks ‹2.2.4›
╰─$ rake spec:webapp
/Users/ymanabe/.rbenv/versions/2.2.4/bin/ruby -I/Users/ymanabe/.rbenv/versions/2.2.4/lib/ruby/gems/2.2.0/gems/rspec-support-3.4.1/lib:/Users/ymanabe/.rbenv/versions/2.2.4/lib/ruby/gems/2.2.0/gems/rspec-core-3.4.4/lib /Users/ymanabe/.rbenv/versions/2.2.4/lib/ruby/gems/2.2.0/gems/rspec-core-3.4.4/exe/rspec --pattern spec/webapp/\*_spec.rb

Package "dstat"
  should be installed

Package "git"
  should be installed

Package "tmux"
  should be installed

Package "vim-enhanced"
  should be installed

Package "yum-plugin-versionlock"
  should be installed

Package "zsh"
  should be installed

Package "httpd"
  should be installed

Service "httpd"
  should be enabled
  should be running

Port "80"
  should be listening

Finished in 0.38829 seconds (files took 7.23 seconds to load)
10 examples, 0 failures

スナップショットでVMの状態を戻す

作成したプロビジョニングが正しく反映されるかを確認するため、スナップショットを利用してVMの状態を戻します。

╭─ymanabe@Yoichiro-no-MacBook-Pro  ~/infra-ci-cookbooks ‹2.2.4›
╰─$ vagrant snapshot restore webapp webapp-plane
==> webapp: Forcing shutdown of VM...
==> webapp: Restoring the snapshot 'webapp-plane'...
==> webapp: Checking if box 'bento/centos-7.1' is up to date...
==> webapp: Resuming suspended VM...
==> webapp: Booting VM...
==> webapp: Waiting for machine to boot. This may take a few minutes...
    webapp: SSH address: 127.0.0.1:2222
    webapp: SSH username: vagrant
    webapp: SSH auth method: private key
==> webapp: Machine booted and ready!

プロビジョニングを実行する

プロビジョニング適用前のVMにプロビジョニングを適用します。

╭─ymanabe@Yoichiro-no-MacBook-Pro  ~/infra-ci-cookbooks ‹2.2.4›
╰─$ knife solo cook webapp -F vagrant_ssh_config
Running Chef on webapp...
Checking Chef version...
Uploading the kitchen...
Generating solo config...
Running Chef: sudo chef-solo -c ~/chef-solo/solo.rb -j ~/chef-solo/dna.json
Starting Chef Client, version 12.8.1
Installing Cookbook Gems:
Compiling Cookbooks...
Converging 8 resources
Recipe: httpd::default
  * yum_package[httpd] action install
    - install version 2.4.6-40.el7.centos of package httpd
  * service[httpd] action enable
    - enable service service[httpd]
  * service[httpd] action start
    - start service service[httpd]
Recipe: base_packages::default
  * yum_package[dstat] action install
    - install version 0.7.2-12.el7 of package dstat
  * yum_package[git] action install
    - install version 1.8.3.1-6.el7_2.1 of package git
  * yum_package[tmux] action install
    - install version 1.8-4.el7 of package tmux
  * yum_package[vim-enhanced] action install
    - install version 7.4.160-1.el7 of package vim-enhanced
  * yum_package[yum-plugin-versionlock] action install
    - install version 1.1.31-34.el7 of package yum-plugin-versionlock
  * yum_package[zsh] action install
    - install version 5.0.2-14.el7 of package zsh

Running handlers:
Running handlers complete
Chef Client finished, 9/9 resources updated in 33 seconds

ServerSpecでテストがGreenとなることを確認します

╭─ymanabe@Yoichiro-no-MacBook-Pro  ~/infra-ci-cookbooks ‹2.2.4›
╰─$ rake spec:webapp
/Users/ymanabe/.rbenv/versions/2.2.4/bin/ruby -I/Users/ymanabe/.rbenv/versions/2.2.4/lib/ruby/gems/2.2.0/gems/rspec-support-3.4.1/lib:/Users/ymanabe/.rbenv/versions/2.2.4/lib/ruby/gems/2.2.0/gems/rspec-core-3.4.4/lib /Users/ymanabe/.rbenv/versions/2.2.4/lib/ruby/gems/2.2.0/gems/rspec-core-3.4.4/exe/rspec --pattern spec/webapp/\*_spec.rb

Package "dstat"
  should be installed

Package "git"
  should be installed

Package "tmux"
  should be installed

Package "vim-enhanced"
  should be installed

Package "yum-plugin-versionlock"
  should be installed

Package "zsh"
  should be installed

Package "httpd"
  should be installed

Service "httpd"
  should be enabled
  should be running

Port "80"
  should be listening

Finished in 0.40667 seconds (files took 6.97 seconds to load)
10 examples, 0 failures

ここまでで、だいたい1時間位かかりましたがとてもわかり易かったです。

ソースコード

最終的なソースコードは下記のGitHubにあります。

yoichiro-manabe/server-infra-devops-chap2: サーバー/インフラエンジニア要請読本DevOps編 特集2