Tbpgr Blog

Employee Experience Engineer tbpgr(てぃーびー) のブログ

Chef | cookbook_fileでファイルを配置

概要

cookbook_fileでファイルを配置

詳細

cookbook_fileでファイルを配置します。

%kitchen%site-cookbooks/some_book/files/default
上記フォルダ配下においたファイルをレシピ内で指定することで
該当ファイルをVMにコピー出来ます。

手順
$ mkdir use_cookbook_file
$ cd use_cookbook_file
$ vagrant init
$ vi Vagrantfile # 編集内容は後述

# キッチン作成
$ knife solo init .
Creating kitchen...
Creating knife.rb in kitchen...
Creating cupboards...

# bashrc向けのレシピテンプレート作成
$ knife cookbook create bash_profile -o site-cookbooks/
** Creating cookbook bash_profile
** Creating README for cookbook: bash_profile
** Creating CHANGELOG for cookbook: bash_profile
** Creating metadata for cookbook: bash_profile

# bashrcの元ファイルを作成(改行コードはLFにすること)
$ cd site-cookbooks/bash_profile/files/default
$ cat <<EOF >.bashrc
alias ll='ls -l'
alias ls='ls -F --color=auto --show-control-char'
alias vi=vim
EOF
$ cd ../recipes/

# bashrcの元ファイルをターゲットに配置するための設定を行う => 詳細は後述
$ vi default.rb

cd ../../

# vim向けのレシピテンプレート作成
$ knife cookbook create vim -o site-cookbooks/
** Creating cookbook vim
** Creating README for cookbook: vim
** Creating CHANGELOG for cookbook: vim
** Creating metadata for cookbook: vim

# vimrcの元ファイルを作成(改行コードはLFにすること)
$ cd ./vimrc/files/default
$ cat <<EOF >.vimrc
"カーソルが何行目の何列目に置かれているかを表示する
set ruler
"OSのクリップボードを使用する
set clipboard=unnamed
"タブ文字表示
set list
"listで表示される文字のフォーマットを指定
set listchars=tab:>-,trail:-,nbsp:%,extends:>,precedes:<
"行番号を表示する
set number
"検索文字の強調表示
set hlsearch
"インクリメンタルサーチ
set incsearch
" 強調表示(色)on or off
syntax on
" タブをスペースに変換
set expandtab
" タブ幅を2に設定
set tabstop=2
" インデント時の幅
set shiftwidth=2
" タブ入力時のタブ幅
set softtabstop=2
" 入力コマンドの表示
set showcmd
" 検索文字列が小文字の場合は大文字小文字を区別なく検索する
set ignorecase
" 検索文字列に大文字が含まれている場合は区別して検索する
set smartcase
" ステータスラインを表示
" 2に設定するといつでも表示
set laststatus=2


"全角スペース・行末のスペース・タブの可視
if has("syntax")
    syntax on
 
    " PODバグ対策
    syn sync fromstart
 
    function! ActivateInvisibleIndicator()
        " 下の行の" "は全角スペー
        syntax match InvisibleJISX0208Space " " display containedin=ALL
        highlight InvisibleJISX0208Space term=underline ctermbg=Blue guibg=darkgray gui=underline
        "syntax match InvisibleTrailedSpace "[ \t]\+$" display containedin=ALL
        "highlight InvisibleTrailedSpace term=underline ctermbg=Red guibg=NONE gui=undercurl guisp=darkorange
        "syntax match InvisibleTab "\t" display containedin=ALL
        "highlight InvisibleTab term=underline ctermbg=white gui=undercurl guisp=darkslategray
    endfunction
 
    augroup invisible
        autocmd! invisible
        autocmd BufNew,BufRead * call ActivateInvisibleIndicator()
    augroup END
endif
EOF
$ cd ../recipes/

# vimrcの元ファイルをターゲットに配置するための設定を行う => 詳細は後述
$ vi default.rb

$ vagrant up
$ vagrant provision

# sshで仮想環境にログイン
$ vagrant ssh

# bash_profileがコピーされたことを確認
vagrant@precise64:~$ cat ~/.bash_profile
export LANG="ja_JP.UTF-8"
alias ll='ls -l'
alias ls='ls -F --color=auto --show-control-char'
alias vi=vim

# vimrcがコピーされたことを確認
vagrant@precise64:~$ cat ~/.vimrc | head -2
"カーソルが何行目の何列目に置かれているかを表示す
set ruler
vagrant@precise64:~$

# vimのインストールおよびvimrcの適用確認
vagrant@precise64:~$ cat <<EOF>hoge.rb
> class Hoge
>   def hoge
>     puts "hoge"
>   end
> end
> EOF
vi hoge.rb
各種設定ファイル

use_cookbook_file/site-cookbooks/bash_profile/recipes/default.rb

#
# Cookbook Name:: bash_profile
# Recipe:: default
#
# Copyright 2013, no name
#
# All rights reserved - Do Not Redistribute
#
cookbook_file '/home/vagrant/.bash_profile' do
  source ".bash_profile"
  owner "vagrant"
  groupt "vagrant"
  mode "0755"
end

use_cookbook_file/site-cookbooks/vim/recipes/default.rb

#
# Cookbook Name:: vim
# Recipe:: default
#
# Copyright 2013, no_name
#
# All rights reserved - no_name
#
log "start update apt-get"
execute "update package index" do
  command "apt-get update"
end.run_action(:run)
log "end   update apt-get"

log "start install vim"
package 'vim' do
  action :install
end
log "end   install vim"

log "start copy .vimrc"
cookbook_file '/home/vagrant/.vimrc' do
  source ".vimrc"
  owner "vagrant"
  group "vagrant"
  mode "0755"
end
log "end   copy .vimrc"
vim起動+設定適用確認