Contenu connexe
Similaire à Chef+serverspec+werckerでインフラCIする話 (20)
Chef+serverspec+werckerでインフラCIする話
- 2. 自己紹介
• 森田 真之
• CrowdWorks Inc. 所属
• まだ入社して1週間ぐらい
• 先日まで某SIerでインフラ担当してた
• アカウント
• twitter: minamijoyo
• hatena:minamijoyo
• github:minamijoyo
• 最近作ったもの
• GitHubコミットメッセージの文例が検索できるサービス
• http://commit-m.minamijoyo.com/
@minamijoyo
- 3. Chef
• Infrastructure as a code
• インフラ構築の自動化
• インフラ構成をコードで管理
• 変更履歴がバージョン管理できる
• 変更内容をプルリクでレビューできる
• コミュニティーのクックブックが充実してるので、自分で書かなくてもある程
度それっぽいものは作れる。
• 中身がブラックボックスになりがちなので、自分で書いた方が分かりやすい
けど、ミドルウェアのバージョンアップのメンテとか考えると一長一短あり。
- 4. その他の構成管理ツール
• Puppet
• わりと昔からあるやつ。
• Ansible
• ChefやPuppetより後発でシンプルさ重視。
• ymlで書くので設定ファイルChefよりコード量は少ないイメージ。
• RubyっぽさがないのでRubyわからない人に抵抗がない。ツール自体はPython製。
• Itamae
• ChefっぽいDSL。
• serverspecで使われてるSpecInfraをバックエンドに使用。
• Cookpadの中の人が作ってるらしい。
- 6. インフラCI環境の構築
• VirtualBoxのインストール(ローカル開発用)
• https://www.virtualbox.org/
• Vagrantのインストール(ローカル開発用)
• https://www.vagrantup.com/
• bundle install
• Gemfile後述
• berks vendor cookbooks
• Berksfile後述
• vagrant up またはaws ec2 run-instances xxx
• knife solo bootstrap (ユーザ名)@(IPアドレス) -i (SSH のパス) -N (chefのノード定義)
• serverspec
• vagrant destroy またはaws ec2 terminate-instances xxx
- 8. とあるwercker.ymlの例
box: wercker/ruby
build:
steps:
- bundle-install
- script:
name: make ssh dir
code: mkdir -m 700 -p $HOME/.ssh
- create-file:
name: put ssh private key
filename: $HOME/.ssh/aws-wercker.pem
overwrite: true
hide-from-log: true
content: $AWS_SSH_KEY_PRIVATE
- script:
name: chmod ssh private key
code: chmod 400 $HOME/.ssh/aws-wercker.pem
- script:
name: delete ec2.env file if exists
code: rm -f ec2.env
- script:
name: aws ec2 run-instances
code: bundle exec ruby ec2-run-instances.rb
- script:
name: print ec2.env for debug
code: cat ec2.env
- script:
name: export environments
code: source ec2.env
- script:
name: ssh connect test
code: ssh ec2-user@$TARGET_IP -i $HOME/.ssh/aws-wercker.pem -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null hostname
- script:
name: berks vendor cookbooks
code: bundle exec berks vendor cookbooks
- script:
name: knife solo bootstrap
code: bundle exec knife solo bootstrap ec2-user@$TARGET_IP -i $HOME/.ssh/aws-wercker.pem -N commitm-ap
- script:
name: serverspec
code: bundle exec rake serverspec:commitm-ap
- script:
name: aws ec2 terminate-instances
code: bundle exec ruby ec2-terminate-instances.rb
https://github.com/minamijoyo/commit-infra
- 11. roles
{
"name": "ap",
"chef_type": "role",
"json_class": "Chef::Role",
"default_attributes": {
"rvm": {
"user": "root",
"default_ruby": "ruby-2.2",
"user_default_ruby": "ruby-2.2",
"rubies": ["ruby-2.2"]
}
},
"override_attributes": {},
"run_list": [
"recipe[rvm::system]",
"recipe[sqlite]",
"recipe[nodejs]",
"recipe[mysql::client]"
]
}
- 13. Rakefileの例
require 'rake'
require 'rspec/core/rake_task'
require 'json'
require 'erb'
hosts = JSON.parse(ERB.new(File.read('hosts.json')).result)
desc "Run serverspec to all hosts"
task :serverspec => 'serverspec:all'
namespace :serverspec do
task :all => hosts.map { ¦host¦ 'serverspec:' + host['name'] }
hosts.each do ¦host¦
desc "Run serverspec to #{host['name']}"
RSpec::Core::RakeTask.new(host['name'].to_sym) do ¦t¦
ENV['TARGET_HOST'] = host['name']
t.pattern = 'spec/{' + host['roles'].join(',') + '}/*_spec.rb'
end
end
end
- 15. specファイルの例
require 'spec_helper'
describe "rvm spec" do
# rvm install
describe command('which rvm') do
let(:disable_sudo) { true }
its(:exit_status) { should eq 0 }
end
# ruby versions
describe command('ruby -v') do
let(:disable_sudo) { true }
its(:stdout) { should match /ruby 2.2.0p0.+/ }
end
end