その他
    ホーム技術発信DoRubycapistranoでmongrel,apache,rsync

    capistranoでmongrel,apache,rsync

    この記事はアピリッツの技術ブログ「DoRuby」から移行した記事です。情報が古い可能性がありますのでご注意ください。

    こんにちはtacchiです。
    以前TrinityTさんが書いてたcapistranoのすすめのつづきです。
    更新されないので勝手に書いちゃいます。

    以下の動作をさせたかったので、taskを自分で作ってみようと言う事になりました。
    ・特定のサーバのmongrelの起動、停止、再起動をさせたい
    ・特定サーバのapacheの起動、停止、再起動をさせたい
    ・rsyncでファイルを転送させたい

    これがmongrelの起動、停止、再起動です。

      namespace :mongrel do
        desc ‘app01,app02 mongrelのrestart’
        task :restart, :roles => [:app1, :app2] do
         run “source /home/hoge/.bash_profile;
              /etc/init.d/mongrel_cluster restart;
              isAlive=`ps -ef | grep ‘ruby’ | grep -v grep | wc -l`;
              if [ $isAlive == 0 ]; then
                 echo ‘error mongrel restart’;
                 exit 1;
              fi;
              echo ‘success mongrel restart!’;
              exit 0;”
        end

        desc ‘app01,app02 mongrelのstart’
        task :start, :roles => [:app1, :app2] do
          run “source /home/hoge/.bash_profile;
              /etc/init.d/mongrel_cluster start;
              sleep 3;
              isAlive=`ps -ef | grep ‘ruby’ | grep -v grep | wc -l`;
              if [ $isAlive == 0 ]; then
                 echo ‘error mongrel start’;
                 exit 1;
              fi;
              echo ‘success mongrel start!’;
              exit 0;”
        end

        desc ‘app01,app02 mongrelのstop’
        task :stop, :roles => [:app1, :app2] do
          run “source /home/hoge/.bash_profile;
              /etc/init.d/mongrel_cluster stop;
              sleep 3;
              isAlive=`ps -ef | grep ‘ruby’ | grep -v grep | wc -l`;
              if [ $isAlive != 0 ]; then
                 echo ‘error mongrel stop’;
                 exit 1;
              fi;
              echo ‘success mongrel stop!’;
              exit 0;”
        end
      end

    これがapacheの起動、停止、再起動です。

      namespace :apache do
        task :start, :roles => [:web1,:web2] do
          sudo “/usr/local/apache2/bin/apachectl start;”
          run “ps -ef |grep http”
        end

        task :stop, :roles => [:web1, :web2] do
          sudo “/usr/local/apache2/bin/apachectl stop;”
          run “ps -ef |grep http”
        end

       task :restart, :roles => [:web1, :web2] do
          sudo “/usr/local/apache2/bin/apachectl restart;”
          run “ps -ef |grep http”
        end
      end

    これがrsyncです

    set(:parts_file) do
      Capistrano::CLI.ui.ask ‘enter parts file: ‘
    end

    set(:parts_insert_server) do
      Capistrano::CLI.ui.ask ‘enter remote server ip: ‘
    end

    set :deploy_to, “/path/to/project”

         desc ‘部分的なファイルを転送できます。プロジェクト以下のファイルを指定して下さい。’
      namespace :rsync do
         task :parts, :roles => :local do
           run “/usr/local/bin/rsync -avz –delete -e ssh #{deploy_to}/#{parts_file} #{parts_insert_server}:#{deploy_to}/#{parts_file}”
         end
      end

    てかインデントの為に、nbsp入れないといけないのなんとかならないすかね。。。