江の島エンジニアのTips集
Chef

ChefのTipsなど

Chefを使う際のTipsなどをまとめてみました。 (2015/01/22)

Berkshelf

$ berks vendor cookbooks
$ berks update yum  ##Update後にはberks vendor cookbooksも必要

Attributeの設定

default[:app][:user] = 'user'
default[:app][:group] = 'staff'

レシピ内なら

node.default[:app][:user] = 'user'

directory

directory "/path/to/dir" do
  owner node[:app][:user]
  group node[:app][:group]
  mode 0755
  action :create
  notifies :run, 'bash[touch]', :immediately
end

bash 'touch' do
  user node[:app][:user]
  cwd '/path/to/dir'
  action :nothing
  code <<-EOF
    touch test.txt
  EOF
end

file

file "/path/to/file" do
  content 'foo bar'
end

template

template 'nginx.conf' do
  path "/path/to/nginx.conf"
  source 'nginx.conf.erb'
  owner node[:app][:user]
  group node[:app][:group]
  mode 0644
  variables(data_bag_item('app', 'web')['nginx'])
end

なお、data_bag_itemはtemplateファイル内では使えません。

bash

bash 'build' do
  user  'root'
  code <<-EOH
    configure &&
    make &&
    make install
  EOH
end

cron

cron "run" do
  user node[:app][:user]
  command "/bin/sh /home/#{node[:app][:user]}/bin/run.sh"
  minute "00"
  hour "00"
end
  • crontab -lを実行すると設定結果を確認できます
  • /var/log/cronから実行ログを確認できます

git

project_path = '/path/to/project'

git project do
  repository 'git://github.com/user/project.git'
  user  node[:app][:user]
  group node[:app][:group]
  action :sync
  notifies :run, 'bash[deploy_app]', :immediately
end

bash 'deploy_app' do
  user node[:app][:user]
  cwd project_path
  code <<-EOH
    ./install.sh
  EOH
end

Capistrano

アプリケーションのconfig/deploy/production.rbやstaging.rbに

server 'localhost', user: 'user', roles: ['web']

set :ssh_options, {
  keys: ['/path/to/id_rsa']
}

を追加して

git '/path/to/project' do
  repository node[:deploy][:app][:scm][:repository]
  revision node[:deploy][:app][:scm][:revision]
  user  node[:app][:user]
  group node[:app][:group]
  action :sync
  notifies :run, 'bash[deploy]', :immediately
end

bash 'deploy' do
  user  node[:app][:user]
  group node[:app][:group]
  cwd '/path/to/project'
  code <<-EOH
    bundle install &&
    bundle exec cap #{data_bag_item('app', 'web')['stage']} deploy HOSTS=localhost
  EOH
end

という感じで設定すればOK。

.ssh/config

自動化する場合はAre you sure you want to continue connecting (yes/no)?と聞かれたくないので、StrictHostKeyCheckingnoにします。

Host github.com
  HostName github.com
  IdentityFile ~/.ssh/github.pem
  StrictHostKeyChecking no

依存関係を解決してビルド&インストール

%w(gcc-c++ openssl-devel).each do |name|
  package name do
    action :install
  end
end

app_url = "https://example.com/path/to/app-#{node[:app][:source][:version]}.tar.gz"
app_src_filepath  = "#{Chef::Config['file_cache_path'] || '/tmp'}/app-#{node[:app][:source][:version]}.zip"
app_src_dirpath = ::File.dirname(app_src_filepath)

remote_file app_url do
  source app_url
  path app_src_filepath
  backup false
end

bash 'compile app source' do
  user  'root'
  group 'root'
  cwd app_src_dirpath
  code <<-EOH
    tar xvf #{::File.basename(app_src_filepath)} -d #{app_src_dirpath} &&
    cd app-#{node[:app][:source][:version]} &&
    ./configure && make && make install
  EOH
end

秘密鍵作成

pem_path = "/home/#{node[:app][:user]}/.ssh/id_rsa"

bash 'create id_rsa' do
  user node[:app][:user]
  group node[:app][:group]
  creates pem_path
  code <<-EOH
    ssh-keygen -N "" -f #{pem_path}
    cat #{pem_path}.pub >> /home/#{node[:app][:user]}/.ssh/authorized_keys
  EOH
end
file "/home/#{node[:app][:user]}/.ssh/id_rsa" do
  owner node[:app][:user]
  group node[:app][:group]
  mode 0600
  content node[:deploy][:appshortname][:scm][:ssh_key]
end

authorized_keysバックアップ

bash 'create authorized_keys.original' do
  user  node[:app][:user]
  group node[:app][:group]
  cwd "/home/#{node[:app][:user]}/.ssh"
  code <<-EOH
    cp authorized_keys authorized_keys.original
  EOH
end

authorized_keys更新

Capistranoデプロイ

gem_package 'bundler' do
  action :install
end

directory node[:app][:deploy_path] do
  user node[:app][:user]
  group node[:app][:group]
  mode 0755
  action :create
  recursive true
end

stage = data_bag_item('app', 'web')['stage']

bash 'deploy' do
  user  node[:app][:user]
  group node[:app][:group]
  cwd node[:app][:repository_path]
  code <<-EOH
    bundle install &&
    bundle exec cap #{stage}-local deploy HOSTS=localhost
  EOH
end
  • このエントリーをはてなブックマークに追加
  • follow us in feedly