16
16

More than 5 years have passed since last update.

Serverspec on AWS 入門

Last updated at Posted at 2014-08-19

AWS上でServerspecを試してみた。

  • EC2インスタンスを2台作成(t2.micro,OSはAmazonLinux)
    • ここではServerspec実行側のEC2をserver01,Serverspecでテストされる側のEC2をserver02とします。
  • server01のEC2のSecuretyGroupは以下の様な感じで設定を追加(FTP用),左からType,Protocol,PortRange,Source
Custom TCP Rule TCP 21 0.0.0.0/0
Custom TCP Rule TCP 60000 - 60010 0.0.0.0/0
  • ec2-userでserver01にSSHログイン
  • 既にgemとRubyはインストールされているのですが一応確認
ruby -v
gem -v
  • 足りないモジュールを追加
sudo su -
yum update
yum vsftpd
gem install rake
gem install serverspec
  • FTPファイルアップロードの為に以下を実行
useradd ftpuser

passwd ftpuser
New UNIX password: *********
Retype new UNIX password: *********
passwd: all authentication tokens updated successfully

service vsftpd start
  • SSH鍵を以下のパスにFTPでアップロード
/root/.ssh/
mv {アップロードしたSSHKeyのファイル名} serverspec
  • SSHログイン情報を記載したファイル作成
vi /root/.ssh/config
/root/.ssh/config
Host server02
  HostName {server02のPrivateIPsを記載}
  Port 22
  User ec2-user
  IdentityFile ~/.ssh/serverspec-key
  • Serverspecディレクトリを作成したいパスに移動後、Serverspecコード生成
mkdir /etc/severspec
cd /etc/severspec
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: n
Input target host name: server02
 + spec/
 + spec/server02/
 + spec/server02/httpd_spec.rb
 + spec/spec_helper.rb
 + Rakefile

[root@XXX.XX.XX.XX serverspec]# ls -l
total 8
-rw-r--r-- 1 root root  148 Aug 19 21:24 Rakefile
drwxr-xr-x 3 root root 4096 Aug 19 21:24 spec


  • 以下の通りにファイルを編集 /etc/serverspec/spec/spec_helper.rb
spec_helper.rb

require 'serverspec'
require 'pathname'
require 'net/ssh'

include SpecInfra::Helper::Ssh
include SpecInfra::Helper::DetectOS

RSpec.configure do |c|
  if ENV['ASK_SUDO_PASSWORD']
    require 'highline/import'
    c.sudo_password = ask("Enter sudo password: ") { |q| q.echo = false }
  else
    c.sudo_password = ENV['SUDO_PASSWORD']
  end
  c.before :all do
    block = self.class.metadata[:example_group_block]
    if RUBY_VERSION.start_with?('1.8')
      file = block.to_s.match(/.*@(.*):[0-9]+>/)[1]
    else
      file = block.source_location.first
    end
    host  = File.basename(Pathname.new(file).dirname)
    if c.host != host
      c.ssh.close if c.ssh
      c.host  = host
      options = Net::SSH::Config.for(c.host)
      user    = options[:user] || Etc.getlogin
      c.ssh   = Net::SSH.start(host, user, options)

      c.request_pty = true ★★★ この一行を追記する! ★★★

    end
  end
end

※上記設定を入れないとServerspec実行時に以下のエラーメッセージが出力される。

Please set "SpecInfra.configuration.request_pty = true" or "c.request_pty = true" in your spec_helper.rb or other appropriate file.
  • 一応以下にテスト内容が記載されているファイルがあることを確認 /etc/serverspec/spec/server02/httpd_spec.rb
httpd_spec.rb
require 'spec_helper'

describe package('httpd') do
  it { should be_installed }
end

describe service('httpd') do
  it { should be_enabled   }
  it { should be_running   }
end

describe port(80) do
  it { should be_listening }
end

describe file('/etc/httpd/conf/httpd.conf') do
  it { should be_file }
  its(:content) { should match /ServerName server02/ }
end

これでserver01側の設定はOK。

  • 続いて、server02にSSHログイン(ec2-user)
  • 以下のコマンドを実行
sudo yum install httpd
sudo service httpd start

これですべての準備OK、あとはテストを実行するだけ

  • server01で以下のコマンド実行 rake spec
16
16
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
16
16