2013-12-06 17:43:19 +01:00
|
|
|
# Ruby-LXC
|
|
|
|
|
|
|
|
## Introduction
|
|
|
|
|
|
|
|
Ruby-LXC is a Ruby binding for liblxc. It allows the creation and management
|
|
|
|
of Linux Containers from Ruby scripts.
|
|
|
|
|
|
|
|
## Build and installation
|
|
|
|
|
2014-01-14 12:48:19 +01:00
|
|
|
Assuming a current installation of LXC is available, to install Ruby-LXC
|
|
|
|
simply run the commands below
|
2013-12-06 17:43:19 +01:00
|
|
|
|
2013-12-16 21:29:15 +01:00
|
|
|
```sh
|
|
|
|
bundle install
|
|
|
|
bundle exec rake compile
|
2014-05-24 06:42:20 +02:00
|
|
|
bundle exec rake gem
|
2014-06-06 16:23:48 +02:00
|
|
|
gem install pkg/ruby-lxc-1.1.1.gem
|
2013-12-16 21:29:15 +01:00
|
|
|
```
|
|
|
|
or just add this to your ```Gemfile```
|
|
|
|
```ruby
|
2014-05-24 06:49:54 +02:00
|
|
|
gem "ruby-lxc", github: "lxc/ruby-lxc", require: "lxc"
|
2013-12-16 21:29:15 +01:00
|
|
|
```
|
2013-12-06 17:43:19 +01:00
|
|
|
|
|
|
|
## Usage
|
|
|
|
|
2013-12-16 21:29:15 +01:00
|
|
|
- Container lifecycle management (create, start, stop and destroy containers)
|
|
|
|
```ruby
|
|
|
|
c = LXC::Container.new('foo')
|
|
|
|
c.create('ubuntu') # create a container named foo with ubuntu template
|
|
|
|
c.start
|
2014-01-14 12:48:19 +01:00
|
|
|
# attach to a running container
|
|
|
|
c.attach do
|
|
|
|
LXC.run_command('ifconfig eth0')
|
|
|
|
end
|
2013-12-16 21:29:15 +01:00
|
|
|
c.stop
|
|
|
|
c.destroy
|
|
|
|
```
|
|
|
|
|
2014-01-14 12:48:19 +01:00
|
|
|
- Container inspection
|
|
|
|
```ruby
|
|
|
|
c.name
|
|
|
|
c.config_path
|
|
|
|
c.config_item('lxc.cap.drop')
|
|
|
|
c.cgroup_item('memory.limit_in_bytes')
|
|
|
|
c.init_pid
|
|
|
|
c.interfaces
|
|
|
|
c.ip_addresses
|
|
|
|
c.state
|
|
|
|
```
|
|
|
|
|
|
|
|
- Additional state changing operations (freezing, unfreezing and cloning
|
|
|
|
containers)
|
2013-12-16 21:29:15 +01:00
|
|
|
```ruby
|
|
|
|
c.freeze
|
|
|
|
c.unfreeze
|
|
|
|
c.reboot
|
|
|
|
c.shutdown
|
|
|
|
```
|
|
|
|
|
|
|
|
- Clone a container
|
|
|
|
```ruby
|
2014-01-14 12:48:19 +01:00
|
|
|
# clone foo into bar. Parent container has to be frozen or stopped.
|
|
|
|
clone = c.clone('bar')
|
2013-12-16 21:29:15 +01:00
|
|
|
```
|
|
|
|
|
|
|
|
- Wait for a state change
|
|
|
|
```ruby
|
2014-01-14 12:48:19 +01:00
|
|
|
# wait until container goes to STOPPED state, else timeout after 10 seconds
|
|
|
|
c.wait(:stopped, 10)
|
2013-12-16 21:29:15 +01:00
|
|
|
```
|
|
|
|
|
2014-01-14 12:48:19 +01:00
|
|
|
Check the provided rdoc documentation for a full list of methods. You can
|
|
|
|
generate it running
|
|
|
|
```sh
|
|
|
|
rake rdoc
|
|
|
|
```
|