うまとま君の技術めも

2015年新卒入社した社畜の勉強内容などなど

Chef-Zero 入門 (Local Mode)

概要

Chef-Client/Server構成で最近開発を行っているのですが、
Cookbook等を開発している時に毎回Serverにアップロードしたりするのは面倒くさいです。。。

そこで、Chef Zeroを使用すればローカル(Chef-Client側)のみで完結するChef環境が作れるらしいので試してみたいと思います。

環境

What is Chef Zero?

https://www.chef.io/blog/2014/06/24/from-solo-to-zero-migrating-to-chef-client-local-mode/

Chef Zero is a full, in-memory, fast-start Chef server intended for development purposes; it persists no data to disk, nor does it have any authentication or authorization. Later, Chef Zero was rolled into Chef Client 11.8.0, thereby giving us Chef Client local mode, which you run with the --local-mode parameter to chef-client.

メモリ上で起動する簡易的なChef-Serverみたいなものらしい

Chef Zeroを試してみる

※ 下記コマンドは全てCentOS上で行っています。

まずは、Chef-DKをインストールします。
方法はいくつかあると思いますが、今回はwgetrpmを取ってきてインストールしたいと思います。

// Install chef-dk
$ wget https://packages.chef.io/stable/el/7/chefdk-0.14.25-1.el7.x86_64.rpm
$ sudo rpm -Uvh chefdk-0.14.25-1.el7.x86_64.rpm

続いて、Chef Repositoryを生成し、
Cookbook・Environment・Roleをそれぞれ作っていきます。

// Create chef-repo
$ chef generate repo chef-repo && cd chef-repo

// Create Cookbook
$ knife cookbook create -o ./cookbooks/ sample
$ vi cookbooks/sample/recipes/default.rb
$ cat cookbooks/sample/recipes/default.rb
file '/tmp/chef-sample.txt' do
  content node[:sample][:echo]
end

// Create env file
$ vi environments/dev.json
$ cat environments/dev.json
{
    "name": "dev",
    "description": "This is dev environment defined as JSON",
    "chef_type": "environment",
    "json_class": "Chef::Environment",
    "default_attributes": {
        "sample": {
            "echo": "HelloWorld!!"
        }
    },
    "override_attributes": {
    },
    "cookbook_versions": {
        "sample": "= 0.1.0"
    }
}

// Create role file
$ vi roles/app.json
$ cat roles/app.json
{
    "name": "app",
    "description": "This is app role defined as JSON",
    "chef_type": "role",
    "json_class": "Chef::Role",
    "default_attributes": {
    },
    "override_attributes": {
    },
    "run_list": [
        "recipe[sample]"
    ]
}

ここで、一旦local-modeでchef-clientコマンドを実行します。
これにより、chef-repoのnodesディレクトリ内にjsonファイルが生成されます。

// Setup node (create nodes/<hostname>.json file)
$ sudo chef-client -z

node情報を登録することができたので、nodeに対してrun_list・envを設定します。
今回はrun_list: role[app]、env: devとします。

// Add run_list env
$ sudo knife node run_list set localhost role[app] -z
$ sudo knife node environment set localhost dev -z
$ knife node show localhost -z
Node Name:   localhost
Environment: dev
FQDN:        localhost
IP:          192.168.0.5
Run List:    role[app]
Roles:
Recipes:
Platform:    centos 7.2.1511
Tags:

これで、local-modeで実行するための準備が整ったので
-z (--local-mode)オプションを付けて、chef-clientコマンドを実行します。

// Deploy
$ sudo chef-client -z
Starting Chef Client, version 12.10.24
resolving cookbooks for run list: ["sample"]
Synchronizing Cookbooks:
  - sample (0.1.0)
Installing Cookbook Gems:
Compiling Cookbooks...
Converging 1 resources
Recipe: sample::default
  * file[/tmp/chef-sample.txt] action create
    - create new file /tmp/chef-sample.txt
    - update content in file /tmp/chef-sample.txt from none to f4e9ad
    --- /tmp/chef-sample.txt    2016-06-05 00:09:29.390000000 +0900
    +++ /tmp/.chef-chef-sample.txt20160605-13608-14dhopg   2016-06-05 00:09:29.390000000 +0900
    @@ -1 +1,2 @@
    +HelloWorld!!
    - restore selinux security context

Running handlers:
Running handlers complete
Chef Client finished, 1/1 resources updated in 01 seconds

無事、意図したtmpファイルを生成することができました。

// Check file
$ cat /tmp/chef-sample.txt
HelloWorld!!

まとめ

今まで、Cookbookを開発するときの良い方法がわからず、開発中でもChef-Serverに対してCookbookをアップロードしたりしていました。。。
Chef-Zeroであれば、いちいちServerに対してアップロードしなくても動作確認することができるようなので、今後はこちらを開発に使っていきたいと思います。