Total Pageviews

Tuesday, 17 December 2013

创建ruby的隔离环境的工具-isolate

我们知道pyhton有virtualenv或virtualenvwrapper来安装python的虚拟环境(隔离环境,独立于系统的python环境)。ruby也有类似的工具-isolate.

Description

Isolate is a very simple RubyGems sandbox. It provides a way to express and automatically install your project’s Gem dependencies.

Wha?

When Isolate runs, it uses GEM_HOME, GEM_PATH, and a few other tricks to separate your code from the system’s RubyGems configuration, leaving it free to run in blissful solitude.
Isolate is very, very, very stupid simple. For a much more full-featured Gem bundler, check out Yehuda Katz and Carl Lerche’s Bundler: It does a lot of fancy AOT dependency resolution, supports non-gem (including git) resources, and is probably a better fit for you.

Requirements

RubyGems 1.8.2 or better, Ruby 1.8.7 or better.

Getting Started

Rails 2

In config/preinitializer.rb:
require "rubygems"
require "isolate/now"
In Isolate:
gem "rails", "2.3.5"
gem "aasm",  "2.0.0"

env :development, :test do
  gem "sqlite3-ruby", "1.2.5"
end

env :production do
  gem "memcached", "0.19.2"
end
Try running rake environment. Before anything else happens, Isolate will make sure you have copies of every gem you need (extend the example above to cover all your dependencies). If they’re already installed on your system Isolate will use them, otherwise a private copy will be installed under tmp/isolate.

Rails 3

In config/boot.rb:
require "rubygems"
require "isolate/now"
Construct your Isolate file as above. Be sure to remove any references to Bundler.setup and Bundler.require from config/boot.rb and config/application.rb.
Don’t forget to require files from your isolated gems in the appropriate places. Unlike bundler isolate does not automatically require any files.

Sinatra, Rack, and Anything Else

There’s nothing special about Rails, it’s just an easy first example. You can use Isolate with any library or framework by simply putting an Isolate file in the root of your project and requiring isolate/now as early as possible in the startup process.
When you’re starting up, Isolate tries to determine its environment by looking at the ISOLATE_ENV, RACK_ENV, and RAILS_ENV env vars. If none are set, it defaults to development.

Library Development

If you’re using Hoe to manage your library, you can use Isolate’s Hoe plugin to automatically install your lib’s development, runtime, and test dependencies without polluting your system RubyGems, and run your tests/specs in total isolation.
Assuming you have a recent Hoe and Isolate is installed, it’s as simple as putting:
Hoe.plugin :isolate
before the Hoe.spec call in your Rakefile.
If you’re not using Hoe, you can use an Isolate.now! block at the top of your Rakefile. See the RDoc for details.

Rake

Isolate provides a few useful Rake tasks. If you’re requiring isolate/now, you’ll get them automatically when you run Rake. If not, you can include them by requiring isolate/rake.

isolate:env

This task shows you the current Isolate settings and gems.
$ rake isolate:env

     path: tmp/isolate/ruby-1.8
      env: development
    files: Isolate

  cleanup? true
  enabled? true
  install? true
multiruby? true
   system? true
  verbose? true

[all environments]
gem rails, = 2.3.5
gem aasm, = 2.0.0

[development, test]
gem sqlite3-ruby, = 1.2.5

[production]
gem memcached, = 0.19.2

isolate:sh

This task allows you to run a subshell or a command in the isolated environment, making any command-line tools available on your PATH.
# run a single command in an isolated subshell
$ rake isolate:sh['gem list']

# run a new isolated subshell
$ rake isolate:sh

isolate:stale

This task lists gems that have a more recent released version than the one you’re using.
$ rake isolate:stale
aasm (2.0.0 < 2.1.5)

Further Reading

require "isolate/now" is sugar for Isolate.now!, which creates, configures, and activates a singleton version of Isolate's sandbox. Isolate.now! takes a few useful options, and lets you define an entire environment inline without using an external file.
For detailed information on Isolate.now! and the rest of the public API, please see the RDoc.

Not Gonna Happen

  • Autorequire. Unlike config.gems or other solutions, Isolate expects you to be a good little Rubyist and manually require the libraries you use.
  • Support for Git or other SCMs. You’re welcome to write an extension that supports ‘em, but Isolate itself is focused on packaged, released gems.

Installation

$ gem install isolate

Meta

RDoc rubydoc.info/gems/isolate/frames
Bugs github.com/jbarnette/isolate/issues
IRC #isolate on Freenode

isolate@librelist.com (Mailing List)

from https://github.com/jbarnette/isolate