一个服务器同时运行多个不同版本的Rails应用[转台湾thegiive文章]

Geez,在seemoon家看到了一个也说运行edge rails的说明那么
rails _2.2.0_myapp
和rake rails:freeze:edge TAG=rel_1-1-6
有什么不一样呢?


很简单,比如你有一个项目叫做multiapp,你的开发环境中有2.1,2.2,2.3三个rails版本,你想用2.2来开发:
rails _2.2.0_ multiapp



生成app时加入以下划线为头尾的版本号来指定rails版本。

http://lightyror.thegiive.net/2007/04/rails-version.html,以下為其原文內容。
目前使用 Rails 的網站,Version 分成好幾派。
0.X
1.0
1.1
1.2

Rails 0.X 的就是幾年前就開始在使用 Rails 的始祖,據我所知國內也是有網站還再用 0.X ,他們之所以死撐不換的原因只有一個,就是怕升級上去原本的 code 根本不相容。1.0 的時候我沒趕上,那可能要問一些長輩才知道 1.0 的模樣是怎麼樣子。

我進入 Rails 圈子剛好是 1.1 的剛剛出的時代,那時候 RJS 剛剛出來,大家叫好又叫座。我也因此全新投入了 Rails,1.1 的穩定度很高,速度也不賴。 Rails .12 是今年才出的,REST 加上 has_many :through 實在非常的吸引人,不過有 benchmark 表示 Rails 1.2 在速度上輸給 Rails 1.1,不過也沒輸太多。所以綜觀起來,要使用 Rails 1.1 或是 1.2 是要看你的需求而定的,沒有一定要使用那個版本的建議。

我現在手邊的 Project 都是用 1.1 ,而且絕大多數已經上線在跑的,短期間不太可能轉換到 1.2 。但是我一定會花很多時間在了解 1.2 的情況。也就是說,我希望能夠在我的機器上面裝 1.1 跟 1.2,有沒有辦法達成呢?

當然有,這很基本!!!
首先,你的 gem 已經安裝了你所要求的 Rails Version,像是我希望在我的機器上面可以自由使用 1.1 或是 1.2 的 Rails,所以我的 gem 安裝情況就是

rails (1.2.3, 1.1.6)
Web-application framework with template engine, control-flow layer,
and ORM.


問題來了,要怎麼一次安裝兩個以上的 version 呢?假設你的 Rails 已經安裝了 1.1.6 ,可是又要安裝 1.2.3 ,就這樣打吧。
sudo gem install -v=1.2.3 rails
當然,如果你想安裝的是最新的 Rails Release,那麼這樣也是可以的
sudo gem i rails
如此就可以在同一台機器上面安裝不同 version 的 rails 。

確定了你的 gem 已經安裝了多個 version,我們必須了較 freeze code 的概念,很多時候我們的某個 Project 的 code 是在某個版本的 rails 開發的。當rails 出了新的 release,原本run 好好的 code 就可能出現一堆 error,所以我們必須要在這個 Project 也包入 Rails 這個 version 的 code。使用方式如下

rake rails:freeze:edge TAG=rel_1-1-6
rake rails:freeze:edge TAG=rel_1-2-3

顧名思義,你可以再 TAG 下面指定你要的 Rails Version,這個指令會把 Rails 放入 vender/rails/ 底下,以後伺服器執行前,他都會去這個資料夾尋找,如果有 vender/rails/ 的資料夾,他就不會使用系統預設的 Rails version ,而是使用已經包在這個 Rails 資料夾的 version。

當然,如果我們將已經包好的 Rails Version 解除,重新使用系統預設的 Rails Version,就這樣打即可

rake rails:unfreeze

他做的事情其實就只是 rm -fr vender/rails/ 資料夾而已。

最後一點,如果你不確定你的 Rails Package 到底使用那個 Version 的 Rails ,你可以打入

ruby script/about

他會跟你講的一清二楚的。

Simplifying Edge Rails Setup
Post by Robert Evans
This entry is by Robert Evans, a fulltime Ruby/Rails developer. You can check out what he is doing over on his blog - http://robertrevans.com. He has several exciting projects that will be released within the next 2 months, so go check them out.
I am a huge fan of edge Rails and use it for every project that I work on. My only complaint about this great framework, is that I constantly have to do the same things over and over to setup my application. It can take a decent amount of time to create your directory, checkout edge Rails via subversion, run rails myproject using edge Rails, and then download all the wonderful plugins. Seriously, you could have a blog written in the amount of time that it takes to get everything setup and ready to go. So, we need to DRY up this process.

I’ve written a very simple shell script that does all the work for you. It’ll create a directory, download edge rails, create the edge project, setup the database.yml file, and get all the plugins I would normally use. And what do we need to do? Just type ./set_rails myprojectname postgres at the prompt and sit back and let it do the work for you.

Let’s take a walk through this script and see what it is actually doing and talk about some ideas that you can do to personalize it for you.



#!/bin/sh
clear
echo "Creating Rails Folder..."
mkdir -p $1/vendor
cd $1

echo "Downloading Edge Rails..."
svn export http://dev.rubyonrails.org/svn/rails/trunk vendor/rails
echo "Setting up Edge Rails Project..."
ruby vendor/rails/railties/bin/rails .

This is the first part of our shell script. As you can see it clears the prompt, gives you a message that it is creating a folder for you. It takes the name of that folder you passed when you typed *myprojectname* into the prompt as the first argument of the script call. That first argument is stored in $1 of our script and the second argument is stored in $2.

Next, we grab the latest edge Rails from the svn server and run the command to setup our rails application. You may be more used to seeing/doing rails myprojectname, but for edge Rails, we run ruby vendor/rails/railties/bin/rails . to create our project on edge rails.



echo "I'm going to remove the README file and add a CHANGELOG file..."
rm README
touch CHANGELOG
echo "Removing the public/index.html file..."
rm public/index.html
echo "Creating the config/database.yml.sample file..."
touch config/database.yml.sample
echo "login: &login
  adapter: $2
  host: localhost
  username:
  password:

development:
  database: $1_dev
  <<: *login

test:
  database: $1_test
  <<: *login

production:
  database: $1_production
  <<: *login" > config/database.yml.sample

echo "Editing the database.yml file..."
cp config/database.yml.sample config/database.yml
echo "Creating the app/views/layout/application.rhtml file..."
touch app/views/layouts/application.rhtml
echo "Creating the public/stylesheets/screen.css file..."
touch public/stylesheets/screen.css

This snippet does a few things: removes some files, creates a sample YAML file for our db to be checked into Subversion and also adds our database information to that YAML file and then copies the sample YAML file contents over to the database.yml file.

The database YAML file takes the second argument you passed via the script call, postgres, and uses it as the adapter value telling our application that we are using that specific database program for this application. So you could pass postgres, mysql, sqlite or whatever database you are using. Also, the first argument is used to setup the names of our development, test and production databases.

Here is an instance where you could add your username and password for your development database and not worry about having to open that file again to set it. We then create an application.rhtml file within our app/views/layouts directory and a stylesheet file called screen.css. I also never use the README file that is installed by default, so I remove that and add the CHANGELOG file. This has been a great to keep a text based account of what you have completed and/or need to complete for this specific project. Works well within teams.



clear
echo "Adding an assortment of plugins..."

./script/plugin install http://svn.techno-weenie.net/projects/plugins/restful_authentication/
./script/plugin install http://svn.techno-weenie.net/projects/plugins/acts_as_versioned/
./script/plugin install http://svn.techno-weenie.net/projects/plugins/acts_as_paranoid/
./script/plugin install http://svn.techno-weenie.net/projects/plugins/acts_as_attachment/
............

This is where a lot of time can be taken up: installing plugins. Nothing fancy here, just some plugins being installed.

RaPT, being worked on by Geoffrey Grosenbach, has the nice addition of plugin packs, thanks to Luke Redpath, which would be a nice replacement here in the script. In the future, I’ll be changing the installation of plugins over to a RaPT plugin pack so that it is generic enough to install whatever plugins you have in your own pack.

There is one last snippet of code, but it is just letting you know your edge Rails application has been made and sends you off with a ‘Happy Programming!’.

You can download this script off my website, here as set_rails.zip

Happy Programming!

Robert Evans

你可能感兴趣的:(应用服务器,SVN,css,Ruby,Rails)