2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Chefを触ってみた

Last updated at Posted at 2014-10-26

目的

  • EC2のインスタンスの初期化を自動化したい
  • Specでのテストをできるようにしたい

環境

  • AWS
  • Ubuntu Server 14.04 LTS

使用したVersion

name version
ChefDK 0.3.0
knife-solo 0.4.2
knife-spec 1.0.0
kitchen-ec2 0.8.0

インストール手順

ChefDKのインストール

$ wget http://opscode-omnibus-packages.s3.amazonaws.com/ubuntu/12.04/x86_64/chefdk_0.3.0-1_amd64.deb
$ sudo dpkg -i chefdk_0.3.0-1_amd64.deb

knife plugin のインストール

$ chef gem install knife-solo
$ chef gem install knife-spec

EC2用のkitchen driverのインストール

$ chef gem install kitchen-ec2

リポジトリの準備

リポジトリの作成

$ knife solo init repo_name

cookbook の作成

$ knife cookbook create cookbook_name -o cookbooks_path

kitchen の初期化

$ kitchen init --driver=kitchen-ec2

recipe 作成後の validate

$ knife cookbook test cookbook_name

ChefSpecでのテスト

berkshelf を利用する場合

require 'chefspec/berkshelf'

の追記が必要

ChefSpecのコード作成

下記参考
https://github.com/sethvargo/chefspec
https://github.com/sethvargo/chefspec/tree/master/examples

テスト実行

$ chef exec rspec

注意

  • masterのサンプルでは ChefSpec::SoloRunner を使っているが、stableなバージョンでは、まだ ChefSpec::Runner なので注意が必要
  • knifeでcookbookを作るとBerksfileが site :opscode になっているが、最新だと source "http://api.berkshelf.com/" こうしないと動かない

Test Kitchen + Serverspecでのテスト

.kitchen.yamlの設定

https://github.com/test-kitchen/kitchen-ec2
ここを参考に設定

AWS の access key ID と secret access key は
AWS のアカウント→ Security Credentials から発行できる

Test Kitchenの操作

インスタンス作成

$ kitchen create

レシピの適用

$ kitchen converge

spec実行

$ kitchen verify

インスタンス削除

$ kitchen destroy

上記4つを全部実行

$ kitchen test

Serverspecのコードの作成

下記参考
http://serverspec.org/resource_types.html

注意

  • repoルートでkitchen実行時に、Berksfileがあると、site-cookbooksが見つからなくなってしまった。一時的にBerksfileをリネームして対応

Knife SoloでのCookbook の適用

ノードの初期化

$ knife solo prepare node_name

ノードへの適用

$ knife solo cook node_name

注意

  • cookbook の depends にある cookbook は Berksfile に記載しないと上手くいかなかった
2
2
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
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?