BerkshelfでCookbookを管理する #opschef_ja
この記事は1年以上前に投稿されました。情報が古い可能性がありますので、ご注意ください。
Berkshelfとは、Cookbookとその依存関係の管理を行うツールです。
同様にBundlerライクなツールとしてLibrarian-Chefがありますが、2013年4月25日現在、Berkshelfを使うことが推奨されています。
@somearaが勧めている(berkshelf > librarian-chef)ほか、@jtimbermanがCookbook管理を切り替える(Chef Repository Berkshelf Conversion)など、OpscodeでBerkshelf採用が相次いでいるようです。#ChefConf 2013でもThe Berkshelf Wayと題したプレゼンが行われました(YouTube, SlideShare)。[2013/05/09 修正]
Berkshelfのインストール
RubyGemsでインストールできます。
ubuntu@workstation:~$ sudo /opt/chef/embedded/bin/gem install berkshelf --no-rdoc --no-ri : : : Successfully installed timers-1.1.0 Successfully installed celluloid-0.13.0 Successfully installed i18n-0.6.1 Successfully installed activesupport-3.2.13 Successfully installed multipart-post-1.2.0 Successfully installed faraday-0.8.7 Successfully installed hashie-2.0.4 Successfully installed chozo-0.6.1 Successfully installed addressable-2.3.4 Successfully installed solve-0.4.2 Successfully installed net-http-persistent-2.8 Successfully installed retryable-1.3.2 Successfully installed ridley-0.9.0 Successfully installed minitar-0.5.4 Successfully installed thor-0.18.1 Successfully installed berkshelf-1.4.0 16 gems installed ubuntu@workstation:~$
Berkshelfの使い方
適当なディレクトリ内でBerksfileファイルを作成します。
この例ではOpscode Cookbook MySQLを対象としています。
ubuntu@workstation:~$ cd chef-repo/ ubuntu@workstation:~/chef-repo$ vi Berksfile site :opscode cookbook 'mysql' ubuntu@workstation:~/chef-repo$
では、MySQL Cookbookを取得してみます。
Berkshelfはberksコマンドで操作します。
ubuntu@workstation:~/chef-repo$ /opt/chef/embedded/bin/berks install Installing mysql (3.0.0) from site: 'http://cookbooks.opscode.com/api/v1/cookbooks' Installing openssl (1.0.2) from site: 'http://cookbooks.opscode.com/api/v1/cookbooks' Installing build-essential (1.4.0) from site: 'http://cookbooks.opscode.com/api/v1/cookbooks' ubuntu@workstation:~/chef-repo$
このように、目的のMySQL Cookbookだけでなく、依存しているCookbookも自動的に取得します。取得されたCookbookは~/.berkshelf/cookbooks/に配置されます。
ubuntu@workstation:~/chef-repo$ ls -l ~/.berkshelf/cookbooks/ 合計 12 drwxrwxr-x 4 ubuntu ubuntu 4096 4月 25 10:05 build-essential-1.4.0 drwxrwxr-x 6 ubuntu ubuntu 4096 4月 25 10:05 mysql-3.0.0 drwxrwxr-x 4 ubuntu ubuntu 4096 4月 25 10:05 openssl-1.0.2 ubuntu@workstation:~/chef-repo$
なお、Cookbookの配置場所を変更するには、環境変数BERKSHELF_PATHを用います。
ubuntu@workstation:~/chef-repo$ BERKSHELF_PATH=/home/ubuntu/chef-repo /opt/chef/embedded/bin/berks install Installing mysql (3.0.0) from site: 'http://cookbooks.opscode.com/api/v1/cookbooks' Installing openssl (1.0.2) from site: 'http://cookbooks.opscode.com/api/v1/cookbooks' Installing build-essential (1.4.0) from site: 'http://cookbooks.opscode.com/api/v1/cookbooks' ubuntu@workstation:~/chef-repo$ ubuntu@workstation:~/chef-repo$ ls -l /home/ubuntu/chef-repo/cookbooks 合計 12 drwxrwxr-x 4 ubuntu ubuntu 4096 4月 25 10:32 build-essential-1.4.0 drwxrwxr-x 6 ubuntu ubuntu 4096 4月 25 10:32 mysql-3.0.0 drwxrwxr-x 4 ubuntu ubuntu 4096 4月 25 10:32 openssl-1.0.2 ubuntu@workstation:~/chef-repo$
また、取得したCookbookとそのヴァージョンはBerkshelf.lockファイルに記録されます。
このファイルがあると、取得したCookbookのヴァージョンを他でも使い回すことができます。
ubuntu@workstation:~/chef-repo$ cat Berksfile.lock cookbook 'mysql', :locked_version => '3.0.0' cookbook 'openssl', :locked_version => '1.0.2' cookbook 'build-essential', :locked_version => '1.4.0' ubuntu@workstation:~/chef-repo$
取得したCookbookをberksコマンドでChef Serverにアップロードします。
この際、Chef ServerのSSL証明書が必要となるので、Chef Serverの/var/opt/chef-server/nginx/caディレクトリから*.crtファイルを取得しておいてください。
ubuntu@workstation:~/chef-repo$ SSL_CERT_FILE=ubuntu.crt /opt/chef/embedded/bin/berks upload Using mysql (3.0.0) Using openssl (1.0.2) Using build-essential (1.4.0) Uploading mysql (3.0.0) to: 'https://ubuntu:443/' Uploading openssl (1.0.2) to: 'https://ubuntu:443/' Uploading build-essential (1.4.0) to: 'https://ubuntu:443/' ubuntu@workstation:~/chef-repo$
ubuntu@workstation:~/chef-repo$ knife cookbook show mysql mysql 3.0.0 2.1.0 1.3.0 ubuntu@workstation:~/chef-repo$ knife cookbook show openssl openssl 1.0.2 1.0.0 ubuntu@workstation:~/chef-repo$ knife cookbook show build-essential build-essential 1.4.0 1.2.0 1.1.2 ubuntu@workstation:~/chef-repo$
このようにアップロードされました。
なお、SSL証明書の検証を無効にする場合はSSL Errorsを参考に、~/.berkshelf/config.jsonファイルを作成して設定を追加してください。
ubuntu@workstation:~/chef-repo$ vi ~/.berkshelf/config.json
{
"ssl": {
"verify": false
}
}
ubuntu@workstation:~/chef-repo$
ubuntu@workstation:~/chef-repo$ /opt/chef/embedded/bin/berks upload Using mysql (3.0.0) Using openssl (1.0.2) Using build-essential (1.4.0) Uploading mysql (3.0.0) to: 'https://ubuntu:443/' Uploading openssl (1.0.2) to: 'https://ubuntu:443/' Uploading build-essential (1.4.0) to: 'https://ubuntu:443/' ubuntu@workstation:~/chef-repo$
Opscode Community Cookbook以外のCookbookも管理できます。
pathを指定するとローカルにあるCookbookへのパス、gitを指定するとgitレポジトリを利用できます。
ubuntu@workstation:~/chef-repo$ vi Berksfile site :opscode cookbook 'mysql' cookbook 'cloudstack', path: '/home/ubuntu/chef-repo/my-cookbooks/cloudstack' cookbook 'apache2-take', git: 'git://github.com/cl-lab-k/apache2-take.git' ubuntu@workstation:~/chef-repo$
ubuntu@workstation:~/chef-repo$ /opt/chef/embedded/bin/berks install Using mysql (3.0.0) Using cloudstack (0.4.0) at path: '/home/ubuntu/chef-repo/my-cookbooks/cloudstack' Installing apache2-take (0.1.0) from git: 'git://github.com/cl-lab-k/apache2-take.git' with branch: '19e6a71f00fb36a655b8eab1e4e16e1fe8c6bfd5' Using openssl (1.0.2) Using build-essential (1.4.0) Installing selinux (0.5.6) from site: 'http://cookbooks.opscode.com/api/v1/cookbooks' Installing ntp (1.3.2) from site: 'http://cookbooks.opscode.com/api/v1/cookbooks' Installing yum (2.2.0) from site: 'http://cookbooks.opscode.com/api/v1/cookbooks' ubuntu@workstation:~/chef-repo$
ubuntu@workstation:~/chef-repo$ /opt/chef/embedded/bin/berks upload Using mysql (3.0.0) Using cloudstack (0.4.0) at path: '/home/ubuntu/chef-repo/my-cookbooks/cloudstack' Installing apache2-take (0.1.0) from git: 'git://github.com/cl-lab-k/apache2-take.git' with branch: '19e6a71f00fb36a655b8eab1e4e16e1fe8c6bfd5' Using openssl (1.0.2) Using build-essential (1.4.0) Using selinux (0.5.6) Using ntp (1.3.2) Using yum (2.2.0) Uploading mysql (3.0.0) to: 'https://ubuntu:443/' Uploading cloudstack (0.4.0) to: 'https://ubuntu:443/' Uploading apache2-take (0.1.0) to: 'https://ubuntu:443/' Uploading openssl (1.0.2) to: 'https://ubuntu:443/' Uploading build-essential (1.4.0) to: 'https://ubuntu:443/' Uploading selinux (0.5.6) to: 'https://ubuntu:443/' Uploading ntp (1.3.2) to: 'https://ubuntu:443/' Uploading yum (2.2.0) to: 'https://ubuntu:443/' ubuntu@workstation:~/chef-repo$
Berkshelfには他にも多数の機能が用意されています。詳しくはBerkshelfを参照してください。
