カスタムタスクをいくつか

自宅サーバで動かすにあたり、いくつかカスタムタスクを作ってみました。config/deploy.rbにつらつらと書きます。(どうやって分割すればいいんだろう。。)

ポイント

ポイントは大きく2点です。

  • 下のエントリでふれた、ファイルの所有者回りの問題をなんとかするため、開始時にオペレータユーザへ所有権を変更。更新完了後、サーバ起動前にhttpd実行ユーザにアクセス権を再設定。
  • ディストリビューションが用意しているサーバの起動/終了スクリプトを使ってhttpdの再起動を行う。

というわけで以下スクリプトです。

set :chown , '/bin/chown -R'
set :start_stop_daemon, "/etc/init.d/apache2"

## ファイルの所有権変更
# 作業ユーザへ所有権変更
desc "Change file owners to operator."
task :chmod_to_operator do
  sudo "#{chown} #{user} #{deploy_to}"
end

# httpdの実行ユーザ(debianの場合)へ所有者変更
desc "Change file owner to service-deamon."
task :chmod_to_deamon do
  sudo "#{chown} www-data:www-data #{deploy_to}"
end

## start-stop-daemonスクリプトを用いたサービスの再起動三題
desc "Stop web server via start-stop-daemon."
task :disable_web_via_initd, :roles => :app do
  sudo "#{start_stop_daemon} stop"
end

desc "Start the web server via start_stop_daemon"
task :enable_web_via_initd, :roles => :app do
  sudo "#{start_stop_daemon} start"
end

desc "Restart the web server via start_stop_daemon"
task :restart, :roles => :app do
  sudo "#{start_stop_daemon} restart"
end

###############################################################
# 実際の再起動スクリプト
###############################################################
desc <<-DESC
A macro-task that updates the code, fixes the symlink, and restarts
the application servers. (overrided)
DESC
task :deploy do
  disable_web_via_initd
  chmod_to_operator
  transaction do
    update_code
    symlink
  end
  chmod_to_deamon
  enable_web_via_initd
end