前回 rmateを使ってリモートサーバ上のファイルをローカルのTextMate2やSublime Text2/3で編集する方法について書きましたが、 続編として、今回はシェルスクリプト版のrmateのインストールをchef(knife-solo)でやってみました。
環境
- リモートサーバOS : CentOS 6.4
- ローカルマシン : Mac OS X 10.9
前提条件
- ローカルマシンに以下がインストール済みであること。
- chef
- knife-solo
- Berkshelf
- リモートサーバにchefがインストール済みであること。
“Opscode Community”にユーザー登録済み&秘密鍵取得済みであること。
chefのレポジトリのファイル、ディレクトリ構成
└── chef-repo
├── Berksfile
├── Berksfile.lock
├── Gemfile
├── Gemfile.lock
├── cookbooks
├── data_bags
├── environments
├── nodes
├── roles
├── site-cookbooks
└── vendor
用意するレシピ
- ssh_known_hosts(Opscode Community製)
- githubのssh公開鍵のfinger print取り込み用レシピ
- rmateインストール用のレシピ
rmateのインストールは、rmateをgithubからcloneして、/usr/local/binにコピーするだけの内容です。
githubにsshで接続するために、予めgithubのssh公開鍵のfinger printをリモートサーバに取り込んでおく必要があるのでそのためのレシピも作成します。
finger printの取り込みにはOpscode Communityで公開されているssh_known_hostsクックブックから、ssh_known_hosts_entryというLWRPを使います。
ssh_known_hostsクックブックのインストール
Berkshelfを使ってインストールします。まずはBerksfileにssh_known_hostsを追加します。
site :opscode
cookbook 'ssh_known_hosts'
あとはberks installでクックブックをインストールするだけです。クックブックはcookbooksディレクトリにインストールします。
$ bundle exec berks install --path cookbooks
Berkshelfを使わずに、“knife cookbook site install ssh_known_hosts -o cookbooksでインストールしてもOKです。
githubのssh公開鍵のfinger print取り込み用レシピの作成
クックブックを作成します。
$ knife cookbook create ssh_known_git_hosts -o site-cookbooks
レシピ内でssh_known_hostsクックブックに定義されているLWRPssh_known_hosts_entryを使うために、metadata.rbにクックブックの依存関係を追加します。
依存関係の定義はdependsを使って書きます。
# metadata.rb
name 'ssh_known_git_hosts'
maintainer 'YOUR_COMPANY_NAME'
maintainer_email 'YOUR_EMAIL'
license 'All rights reserved'
description 'Installs/Configures ssh_known_git_hosts'
long_description IO.read(File.join(File.dirname(__FILE__), 'README.md'))
version '0.1.0'
depends 'ssh_known_hosts' # クックブックの依存関係の定義次にレシピを書きます。
# ssh_known_git_hosts/recipes/default.rb
node['git_hosts'].each do |git_host|
ssh_known_hosts_entry git_host
endssh_known_hosts_entryは、ssh_known_hosts_entry 'ホスト名'のように使います。例えばssh_known_hosts_entry 'github.com'と書くと
デフォルトでは/etc/ssh/ssh_known_hostsにgithub.comのssh公開鍵のfinger printを追加してくれます。(冪等性も担保されていて、既に追加済みであれば処理がskipされます。)
今回作成したレシピではホスト名の部分をAttribute化したので、Attributeのデフォルト値も定義しておきます。
# ssh_known_git_hosts/attributes/default.rb
default['git_hosts'] = %w(github.com)github.comの他に、例えばbitbucketのfinger printも追加したい場合はdefault['git_hosts'] = %w(github.com bitbucket.org)のように書きます。
rmateインストール用のレシピの作成
クックブックを作成します。
$ knife cookbook create rmate -o site-cookbooks
レシピを書きます。
# rmate/recipes/default.rb
temp_dir = Chef::Config[:file_cache_path]
git File.join(temp_dir, 'rmate') do
repository node['rmate']['git_repository']
revision node['rmate']['git_revision']
action :sync
notifies :run, 'bash[cp_rmate]'
end
bash 'cp_rmate' do
code <<-EOF
cp -f #{File.join(temp_dir, 'rmate', 'rmate')} #{node['rmate']['bin_path']}
chmod +x "#{node['rmate']['bin_path']}/rmate"
EOF
action :nothing
endrmateのgitリポジトリはchefのfile_cache_pathにcloneするようにしています。file_cache_pathは、CentOS 6.4の環境では
デフォルトで/var/chef/cacheに設定されるようです。
リポジトリをcloneした後、rmateを/usr/local/binにコピーして実行権限を付与します。これはシェルスクリプトで書いています。
gitリソースのactionにsyncを指定しているので、リモートリポジトリ(今回はgithub上のリポジトリ)に更新があれば最新版をcloneしてきて
既存のrmateを上書き、という動きになります。
Attributeは以下です。
# rmate/attributes/default.rb
default["rmate"]["git_repository"] = "https://github.com/aurora/rmate.git"
default["rmate"]["git_revision"] = "master"
default["rmate"]["bin_path"] = "/usr/local/bin"Chef Solo実行
ssh_known_git_hostsとrmateのレシピを実行するようにNode Object(jsonファイル)を作成します。
{
"run_list":[
"recipe[ssh_known_git_hosts]",
"recipe[rmate]"
]
}Chef Soloを実行してレシピをノードに適用します。
$ knife solo cook {node名(任意)}
おまけ
rmateをgemでインストールする場合のレシピです。gem_packageリソースを使います。Rubyの環境はrbenv前提です。
gem_package "rmate" do
gem_binary "/usr/local/rbenv/shims/gem"
action :install
end