EC2onRailsを使って、既存のRailsアプリをEC2上で動くようにするための手順のメモです。 EC2onRailsを複数アプリ対応にする方法のメモ を適用した状態を想定しています。

  1. config/deploy.rb を用意 (サンプルファイルはこちら:server_config_files_rootを適当に設定し、指定したPATHにディレクトリを作る。
  2. Capfileを以下のような感じで用意。
       1  load 'deploy' if respond_to?(:namespace) # cap2 differentiator
       2  load 'config/deploy'
       3  require 'ec2onrails/recipes'
       4  
       5  set :deploy_to, "/mnt/app/<app_name>"
       6  
       7  # override default start/stop/restart tasks
       8  namespace :deploy do
       9    desc <<-"DESC"
      10      Overrides the default Capistrano deploy:start, directly calls \
      11      /etc/init.d/mongrel #{application}
      12    DESC
      13    task :start, :roles => :app do
      14      run "/etc/init.d/mongrel start #{application}"
      15    end
      16  
      17    desc <<-"DESC"
      18      Overrides the default Capistrano deploy:stop, directly calls \
      19      /etc/init.d/mongrel #{application}
      20    DESC
      21    task :stop, :roles => :app do
      22      run "/etc/init.d/mongrel stop #{application}"
      23    end
      24  
      25    desc <<-"DESC"
      26      Overrides the default Capistrano deploy:restart, directly calls \
      27      /etc/init.d/mongrel #{application}
      28    DESC
      29    task :restart, :roles => :app do
      30      run "/etc/init.d/mongrel restart #{application}"
      31    end
      32  end
    
    EC2onRailsが上書きしたデフォルトのタスクをさらに上書きして、複数アプリ対応できるようにしています。  3. 最後に、mongrel_clusterを使うので、config/mongrel_cluster.yml ファイルを用意します。
       1  ---
       2  cwd: /mnt/app/<app_name>/current
       3  port: 8000
       4  environment: production
       5  pid_file: log/mongrel.pid
       6  servers: 3
    
    ポート番号はサーバをシェアするアプリ間で適当に割り振ります。

あとは、以下のコマンドを実行してデプロイします。

   1  cap ec2onrails:setup
   2  cap deploy:cold

最後に、EC2onRailsを複数アプリ対応にする方法のメモに書いてあるような、EC2上のApacheのVirtualHostの設定と、ProxyBalancerの設定を行います。

posted by Png genki on Wed 21 May 2008 at 18:27

自前のSVNリポジトリで開発しつつ、 適宜必要に応じてRubyforgeやGitHubのリポジトリにPushする方法のメモです。

まずは以下のコマンドでremoteを追加します。

   1  % git remote add rubyforge gitosis@rubyforge.org:openid-fu.git

addの第一パラメータのrubyforgeというのは適当につけた名前です。 あとは、pushするだけです。

   1  % git push rubyforge master

Rubyforgeにpushするためには、 事前に公開鍵を登録しておく必要があります(See Also#2参照)

それから こちら でも報告されていますが、 Rubyforgeに登録する公開鍵の末尾のコメントに、メールアドレスなどの ユニークな文字列を指定する必要があるようです。

See Also

posted by Png genki on Sun 18 May 2008 at 05:47

既存のSVNリポジトリを使いつつ、ローカルではGitの利便性を享受するために、 git-svnを使う方法のメモです。以下はopenid-fuのリポジトリを使った例です。

  1. まずは普通にSVNリポジトリにファイルをimportしておきます。既存のものがある場合はそれを使います。
  2. git-svnでリポジトリをcloneします。
       1  % git svn clone https://svn.s21g.com/public/openid-fu/ openid-fu
    
    これでopenid-fu/にクローンされました。
  3. ファイルの変更などをSVNにコミットする場合は、 普通にGitを使うように、git addでGit管理下に置き、git commitします。 そのあと、
       1  % git svn dcommit
    
    すると、SVNにコミットされます。
  4. SVN上の変更は、git svn rebaseする事でローカルに反映されます。 svn upに相当する感じですね。

追記

SVN::Core.pmがない、などといわれる場合は、libsvn-perlを 入れる必要があるようです。

posted by Png genki on Sun 18 May 2008 at 05:26

RSpecは面白そうだったのだけれど、なかなか移行する機会が無くて使っていなかったのですが、今は縁があってRSpecでViewのテストを書いています。

で、partialのテストをする場合のメモです。

   1  require File.join(File.dirname(__FILE__), '../../spec_helper.rb')
   2  
   3  describe '/foos/_form.html.erb' do
   4    before do
   5      render :partial => 'foos/form', :object => mock('form'),
   6        :locals => {:bar => mock('bar')}
   7    end
   8  
   9    it "should be rendered successfully" do
  10      response.should be_success
  11    end
  12  end

とりあえずこんな感じです。

See Also

posted by Png genki on Fri 16 May 2008 at 01:30 with 1 comment

テストの実行時に利用されるDBは、通常development環境のstructureがコピーされますが、何らかの事情でproduction環境のものを使って欲しい場合もあります。 そんなときは、以下のRakeタスクを実行すればOK.

   1  % rake db:test:clone RAILS_ENV=production

これによって、production環境のstructureがコピーされます。

posted by Png genki on Thu 15 May 2008 at 11:16

http://blog.dreamhost.com/2008/05/13/passenger-for-ruby-on-rails/

話題になっていたPassengerですが、Dreamhostで採用されました。masuidriveさんもオススメのようなので、ぜひ使ったみたいです。

http://rails20.jp/2008/04/passenger/
http://www.moongift.jp/2008/04/passenger/
http://blog.masuidrive.jp/index.php/2008/04/12/deploy_rb_and_apache_conf_generator_for_mod_rail/

posted by Png satoko on Thu 15 May 2008 at 05:24

複数アプリを動かしている場合、アプリ毎にメールアドレスが欲しくなることがあります。
今回はpostfixのvirtual domain、virtual aliasを使って実現しました。1つのサーバ(host名:x.s21g.com)で、a.s21g.com, b.s21g.comのメール受信ができるようになります。流れとしては、

  1. satoko@b.s21g.comで受信したメールを
  2. satoko_at_b@a.s21g.comに転送
  3. それをscript/runncerで処理ということになります。

mailnameの用意

myoriginパラメータで使う名前を設定します。

   1  # cat /etc/mailname
   2  a.s21g.com

virtual aliasesの用意

メール受信時どういう処理をするかです: railsの場合script/runnerに食わせます。
PATHはそれぞれ書き換えてください。

   1  # cat /etc/aliases
   2  postmaster:    root
   3  
   4  #post_at_b: "| cat $1 > /tmp/mail_test.txt" #test用コード
   5  post_at_b: "| /PATH_TO_RUBY/ruby /PATH_TO_RAILS_ROOT/script/runner -e production 'TestMailer.receive(STDIN.read)'"

virtual domainの用意

virtual domainに来た場合どうするかを個別を書きます。

   1  # cat /etc/postfix/virtual
   2  b.s21g.com         anything
   3  post@b.s21g.com post_at_b

postfixの設定を変更

mydestinationにb.s21g.comを設定、myorigin, virtual alias、virtual domainも指定します。

   1  #/etc/postfix/main.cf
   2  mydestination = localhost, localhost.localdomain, localhost, b.s21g.com
   3  myorigin = /etc/mailname
   4  alias_maps = hash:/etc/aliases
   5  alias_database = hash:/etc/aliases
   6  virtual_alias_maps = hash:/etc/postfix/virtual 

各設定を反映

   1  # postalias /etc/aliases
   2  # postmap /etc/postfix/virtual
   3  # /etc/rc.d/init.d/postfix restart

(注意) a.s21g.comはDNSに登録してください。メール送信時、インターネット上でDNSが牽ける名前か相手サーバからチェックされており、その時点で蹴られてしまうからです。
(注意) スパムに使われないようにリレーを設定します:main.cf
relayhost =
mynetworks = 127.0.0.0/8

Refs

http://blog.s21g.com/articles/569
http://www.tmtm.org/postfix/tutorial/index.html
http://furukawa.blogdns.com/furukawa/2006/04/postfix.html
http://www.jitaku-server.net/postfix_virtual_domain.html

posted by Png satoko on Tue 13 May 2008 at 16:50

Gitでファイル一式をまとめてtar ballにする場合、.gitがあるディレクトリに 移動して、以下のようにします。

   1  % git-archive --format=tar HEAD | gzip > foo.tar.gz

これによって、Gitの管理下にあるファイル一式をまとめたtar.gzファイルが出来上がります。余談ですが、tar.gzをtar ballと呼ぶのはなんでだろう。

良く使いそうなので、専用のGitコマンドがあっても良いんじゃないかな?

posted by Png genki on Mon 12 May 2008 at 18:40

svn upしたあとにどのファイルが更新されるかを事前にしらべるには、 以下のようなコマンドを使えば良いようです。

   1  % svn -u stat

svn up -dry-runが出来ると良いんですけどね。

See Also

posted by Png genki on Mon 12 May 2008 at 16:37

以前試しに作ったTinyURL_helperですが、gitを勉強するにあたってgithubに移行してみました。
http://github.com/satoko/tinyurl_helper/tree/master

その手順を書いてみます。

公開リポジトリを作る

  1. githubにログイン
  2. ダッシュボードで"Create a Repository"リンクをクリック
  3. Project Name, Descriptioin, Homepage URLを入力し、レポジトリの公開範囲(Anyone)を選択
  4. Submitボタンをクリック

Next steps:git pushまで

現状svnで管理しているpluginをチェックアウトします

   1  svn co http://svn.s21g.com/public/rails/plugins/tinyurl_helper/ tinyurl_helper
   2  cd tinyurl_helper

Submit後下記のような指示が画面に表示されますが、今回は移行作業なのでtouch READMEなどを飛ばします。

   1    mkdir tinyurl_helper
   2    cd tinyurl_helper
   3    git init
   4    touch README
   5    git add README
   6    git commit -m 'first commit'
   7    git remote add origin git@github.com:satoko/tinyurl_helper.git
   8    git push origin master

実際にやった作業

   1  $ git-init  
   2  $ git-add .
   3  $ git status
   4  $ git commit -m "first commit"
   5  $git remote add origin git@github.com:satoko/tinyurl_helper.git
   6  $git push origin master

(注)public_keyをgithubに設定しておき、pagentにprivate_keyを読み込ませてsshでagent-forward設定をしておきます。でpush!

posted by Png satoko on Mon 12 May 2008 at 01:08
Contents
既存のRailsアプリをEC2onRails化する手順のメモ
git-svn環境からRubyforgeのGitリポジトリにpushする方法のメモ
git-svnを使って既存のSVNリポジトリでGitを使う方法のメモ
RSpecでpartialをテストする場合のメモ
Railsでproduction環境からテスト用DBを再構築する方法
ホスティングサービスDreamhostがPassenger(mod_rails)を採用
postfixでrailsアプリごとにメールアドレスを持つ:virtual domain, virtual alias
Gitでファイル一式をtar ballに固める方法
svn upする時に更新されるファイルを事前に調べる方法
プラグイン:TinyURL_helperの管理をgithubに移行する
Comments
瀧内元気: MacOS版は以下にあります * [genki/ViMouse](https://githu... '23-1
KingofSmack: Here also good reads for this mobile applicatio... '14-5
Spencer: You don't have to re-compile it, this version w... '14-4
staiano: Any chance we can get a recompile for 10.9? '14-1
dsjf: https://gist.github.com/6bf1bf2c3cbb5eb6e7a7 これ... '13-1
Services from s21g
twpro(ツイプロ)
Twitterプロフィールを快適検索
地価2009
土地の値段を調べてみよう
MyRestaurant
自分だけのレストラン手帳
Formula
ブログに数式を埋め込める数式コミュニティ