Merge pull request #43 from fgrehm/v0.1.1

V0.1.1
This commit is contained in:
Fabio Rehm 2013-03-29 08:45:57 -07:00
commit b5b7fc76e3
16 changed files with 80 additions and 88 deletions

1
.gitignore vendored
View file

@ -26,4 +26,3 @@ doc/
/boxes/**/*.tar.gz /boxes/**/*.tar.gz
/boxes/quantal64/rootfs-amd64/ /boxes/quantal64/rootfs-amd64/
/boxes/output/ /boxes/output/
/development/Vagrantfile

View file

@ -1,3 +1,11 @@
## [0.1.1](https://github.com/fgrehm/vagrant-lxc/pull/43)
- Removed support for development under Vagrant < 1.1
- Removed rsync from base quantal64 box to speed up containers creation [#40](https://github.com/fgrehm/vagrant-lxc/issues/40)
- Containers are now named after project's root dir [#14](https://github.com/fgrehm/vagrant-lxc/issues/14)
- Skip Vagrant's built in SSH redirect
- Allow setting rootfs from Vagrantfile [#30](https://github.com/fgrehm/vagrant-lxc/issues/30)
## 0.1.0 ## 0.1.0
- Support for chef added to base quantal64 box - Support for chef added to base quantal64 box

View file

@ -14,7 +14,7 @@ GIT
PATH PATH
remote: . remote: .
specs: specs:
vagrant-lxc (0.1.0) vagrant-lxc (0.1.1.dev)
GEM GEM
remote: https://rubygems.org/ remote: https://rubygems.org/

View file

@ -1,11 +1,11 @@
# vagrant-lxc [![Build Status](https://travis-ci.org/fgrehm/vagrant-lxc.png?branch=master)](https://travis-ci.org/fgrehm/vagrant-lxc) [![Gem Version](https://badge.fury.io/rb/vagrant-lxc.png)](http://badge.fury.io/rb/vagrant-lxc) [![Code Climate](https://codeclimate.com/github/fgrehm/vagrant-lxc.png)](https://codeclimate.com/github/fgrehm/vagrant-lxc) # vagrant-lxc [![Build Status](https://travis-ci.org/fgrehm/vagrant-lxc.png?branch=master)](https://travis-ci.org/fgrehm/vagrant-lxc) [![Gem Version](https://badge.fury.io/rb/vagrant-lxc.png)](http://badge.fury.io/rb/vagrant-lxc) [![Code Climate](https://codeclimate.com/github/fgrehm/vagrant-lxc.png)](https://codeclimate.com/github/fgrehm/vagrant-lxc)
Experimental Linux Containers support for Vagrant >= 1.1. Experimental Linux Containers support for Vagrant 1.1+
## Dependencies ## Dependencies
Vagrant >= 1.1.0, `lxc` and `redir` packages and a Kernel [higher than 3.5.0-17.28](#help-im-unable-to-restart-containers), Vagrant 1.1+ (1.1.4+ recommended), `lxc` and `redir` packages and a Kernel [higher than 3.5.0-17.28](#help-im-unable-to-restart-containers),
which on Ubuntu 12.10 means something like: which on Ubuntu 12.10 means something like:
``` ```
@ -16,7 +16,7 @@ sudo dpkg -i /tmp/vagrant.deb
``` ```
## What is currently supported? (v0.1.0) ## What is currently supported? (v0.1.1)
* Vagrant's `up`, `halt`, `reload`, `destroy`, and `ssh` commands * Vagrant's `up`, `halt`, `reload`, `destroy`, and `ssh` commands
* Shared folders * Shared folders
@ -43,7 +43,7 @@ for the most up to date list.*
## Usage ## Usage
Make sure you have [Vagrant 1.1](http://downloads.vagrantup.com/tags/v1.1.0) and run: Make sure you have [Vagrant 1.1+](http://downloads.vagrantup.com/) and run:
``` ```
vagrant plugin install vagrant-lxc vagrant plugin install vagrant-lxc
@ -63,6 +63,8 @@ Vagrant.configure("2") do |config|
config.vm.synced_folder "/tmp", "/host_tmp" config.vm.synced_folder "/tmp", "/host_tmp"
config.vm.provider :lxc do |lxc| config.vm.provider :lxc do |lxc|
# Set the folder where container's rootfs will be stored when created
lxc.target_rootfs_path = '/path/to/container/rootfs'
# Same as 'customize ["modifyvm", :id, "--memory", "1024"]' for VirtualBox # Same as 'customize ["modifyvm", :id, "--memory", "1024"]' for VirtualBox
lxc.start_opts << 'lxc.cgroup.memory.limit_in_bytes=400M' lxc.start_opts << 'lxc.cgroup.memory.limit_in_bytes=400M'
# Limits swap size # Limits swap size
@ -109,7 +111,6 @@ sudo apt-get install apparmor-utils
sudo aa-complain /usr/bin/lxc-start sudo aa-complain /usr/bin/lxc-start
bundle install bundle install
cd development cd development
ln -s Vagrantfile.1.1 Vagrantfile
bundle exec vagrant up lxc --provider=lxc bundle exec vagrant up lxc --provider=lxc
bundle exec vagrant ssh lxc bundle exec vagrant ssh lxc
``` ```
@ -120,26 +121,16 @@ Keep in mind that you'll probably need to run `sudo aa-complain /usr/bin/lxc-sta
on the host whenever you want to hack on it, otherwise you won't be able to on the host whenever you want to hack on it, otherwise you won't be able to
start nested containers there to try things out. start nested containers there to try things out.
### Using VirtualBox and Vagrant 1.1 for development ### Using VirtualBox for development
``` ```
cd development cd development
ln -s Vagrantfile.1.1 Vagrantfile
bundle exec vagrant up vbox bundle exec vagrant up vbox
# A reload is needed to ensure the updated kernel gets loaded
bundle exec vagrant reload vbox bundle exec vagrant reload vbox
bundle exec vagrant ssh vbox bundle exec vagrant ssh vbox
``` ```
### Using VirtualBox and Vagrant 1.0 for development
```
cd development
ln -s Vagrantfile.1.0 Vagrantfile
vagrant up
vagrant reload
vagrant ssh
```
## Protips ## Protips

View file

@ -161,8 +161,8 @@ copy_ubuntu()
# make a local copy of the miniubuntu # make a local copy of the miniubuntu
echo "Copying rootfs to $rootfs ..." echo "Copying rootfs to $rootfs ..."
mkdir -p $rootfs mkdir -p $(dirname $rootfs)
rsync -a $cache/rootfs-$arch/ $rootfs/ || return 1 mv $cache/rootfs-$arch $rootfs || return 1
return 0 return 0
} }

View file

@ -50,6 +50,8 @@ Vagrant.configure("2") do |config|
config.vm.define :lxc do |lxc_config| config.vm.define :lxc do |lxc_config|
lxc_config.vm.hostname = 'lxc-dev-box' lxc_config.vm.hostname = 'lxc-dev-box'
# Uncomment to test boxes built locally:
# lxc_config.vm.box_url = '../boxes/output/lxc-quantal64.box'
lxc_config.vm.box_url = 'http://dl.dropbox.com/u/13510779/lxc-quantal64-2013-03-26.box' lxc_config.vm.box_url = 'http://dl.dropbox.com/u/13510779/lxc-quantal64-2013-03-26.box'
lxc_config.vm.provider :lxc do |lxc| lxc_config.vm.provider :lxc do |lxc|

View file

@ -1,36 +0,0 @@
# -*- mode: ruby -*-
# vi: set ft=ruby :
def local_cache(box_name)
cache_dir = File.join(File.expand_path(Vagrant::Environment::DEFAULT_HOME),
'cache',
'apt',
box_name)
partial_dir = File.join(cache_dir, 'partial')
FileUtils.mkdir_p(partial_dir) unless File.exists? partial_dir
cache_dir
end
Vagrant::Config.run do |config|
config.vm.box = "quantal64"
config.vm.box_url = "https://github.com/downloads/roderik/VagrantQuantal64Box/quantal64.box"
config.vm.host_name = 'lxc-dev-box'
cache_dir = local_cache(config.vm.box)
config.vm.share_folder "v-root", "/vagrant", "../"
config.vm.share_folder "v-cache", "/var/cache/apt/archives", cache_dir
if defined? VagrantVbguest::Config
config.vbguest.auto_update = false
config.vbguest.no_remote = true
end
config.vm.provision :shell, :path => 'shell-provisioning/upgrade-kernel'
config.vm.provision :puppet do |puppet|
puppet.manifests_path = "."
puppet.manifest_file = "site.pp"
# Pass DEBUG=1 to vagrant commands if you want to make some debugging noise
puppet.options << [ "--verbose", "--debug" ] if ENV["DEBUG"] == '1'
end
end

View file

@ -3,7 +3,12 @@ module Vagrant
module Action module Action
class Create < BaseAction class Create < BaseAction
def call(env) def call(env)
machine_id = env[:machine].provider.container.create(env[:machine].box.metadata) base_name = env[:root_path].basename.to_s
base_name.gsub!(/[^-a-z0-9_]/i, "")
target_rootfs_path = env[:machine].provider_config.target_rootfs_path
machine_id = env[:machine].provider.container.create(base_name, target_rootfs_path, env[:machine].box.metadata)
env[:machine].id = machine_id env[:machine].id = machine_id
env[:just_created] = true env[:just_created] = true
@app.call env @app.call env

View file

@ -54,7 +54,7 @@ module Vagrant
mappings = {} mappings = {}
config.vm.networks.each do |type, options| config.vm.networks.each do |type, options|
if type == :forwarded_port if type == :forwarded_port && options[:id] != 'ssh'
mappings[options[:host]] = options mappings[options[:host]] = options
end end
end end

View file

@ -6,8 +6,15 @@ module Vagrant
# @return [Array] # @return [Array]
attr_reader :start_opts attr_reader :start_opts
# Base directory to store container's rootfs
#
# Defaults to nil, which means it will be stored wherever the lxc template
# tells it to be stored
attr_accessor :target_rootfs_path
def initialize def initialize
@start_opts = [] @start_opts = []
@target_rootfs_path = nil
end end
end end
end end

View file

@ -12,9 +12,6 @@ module Vagrant
# Root folder where containers are stored # Root folder where containers are stored
CONTAINERS_PATH = '/var/lib/lxc' CONTAINERS_PATH = '/var/lib/lxc'
# Default LXC configs
LXC_DEFAULTS_PATH = '/etc/default/lxc'
# Include this so we can use `Subprocess` more easily. # Include this so we can use `Subprocess` more easily.
include Vagrant::Util::Retryable include Vagrant::Util::Retryable
@ -39,13 +36,13 @@ module Vagrant
end end
def rootfs_path def rootfs_path
Pathname.new("#{base_path}/rootfs") Pathname.new(base_path.join('config').read.match(/^lxc\.rootfs\s+=\s+(.+)$/)[1])
end end
def create(metadata = {}) def create(base_name, target_rootfs_path, metadata = {})
@logger.debug('Creating container using lxc-create...') @logger.debug('Creating container using lxc-create...')
@name = SecureRandom.hex(6) @name = "#{base_name}-#{SecureRandom.hex(6)}"
public_key = Vagrant.source_root.join('keys', 'vagrant.pub').expand_path.to_s public_key = Vagrant.source_root.join('keys', 'vagrant.pub').expand_path.to_s
meta_opts = metadata.fetch('template-opts', {}).merge( meta_opts = metadata.fetch('template-opts', {}).merge(
'--auth-key' => public_key, '--auth-key' => public_key,
@ -53,7 +50,7 @@ module Vagrant
) )
@cli.name = @name @cli.name = @name
@cli.create(metadata.fetch('template-name'), meta_opts) @cli.create(metadata.fetch('template-name'), target_rootfs_path, meta_opts)
@name @name
end end
@ -107,7 +104,7 @@ module Vagrant
retryable(:on => LXC::Errors::ExecuteError, :tries => 10, :sleep => 3) do retryable(:on => LXC::Errors::ExecuteError, :tries => 10, :sleep => 3) do
unless ip = get_container_ip_from_ifconfig unless ip = get_container_ip_from_ifconfig
# retry # retry
raise LXC::Errors::ExecuteError, :command => ['arp', '-n'].inspect raise LXC::Errors::ExecuteError, :command => "lxc-attach"
end end
end end
ip ip

View file

@ -31,15 +31,18 @@ module Vagrant
end end
end end
def create(template, template_opts = {}) def create(template, target_rootfs_path, template_opts = {})
extra = template_opts.to_a.flatten extra = template_opts.to_a.flatten
extra.unshift '--' unless extra.empty? extra.unshift '--' unless extra.empty?
rootfs_args = target_rootfs_path ?
['-B', 'dir', '--dir', target_rootfs_path] :
[]
run :create, run :create,
# lxc-create options
'--template', template, '--template', template,
'--name', @name, '--name', @name,
*extra *(rootfs_args + extra)
end end
def destroy def destroy

View file

@ -34,7 +34,7 @@ module Vagrant
end end
end end
# @see Vagrant::Plugin::V1::Provider#action # @see Vagrant::Plugin::V2::Provider#action
def action(name) def action(name)
# Attempt to get the action method from the Action class if it # Attempt to get the action method from the Action class if it
# exists, otherwise return nil to show that we don't support the # exists, otherwise return nil to show that we don't support the
@ -53,7 +53,7 @@ module Vagrant
{ {
:host => @container.assigned_ip, :host => @container.assigned_ip,
:port => 22 # @driver.ssh_port(@machine.config.ssh.guest_port) :port => @machine.config.ssh.guest_port
} }
end end

View file

@ -1,5 +1,5 @@
module Vagrant module Vagrant
module LXC module LXC
VERSION = "0.1.0" VERSION = "0.1.1.dev"
end end
end end

View file

@ -33,19 +33,31 @@ describe Vagrant::LXC::Container::CLI do
subject { described_class.new(name) } subject { described_class.new(name) }
before do before do
subject.stub(:run) subject.stub(:run) { |*args| @run_args = args }
subject.create(template, template_args) subject.create(template, rootfs, template_args)
end end
it 'issues a lxc-create with provided template, container name and hash of arguments' do context 'when no rootfs is passed' do
subject.should have_received(:run).with( let(:rootfs) { nil }
:create,
'--template', template, it 'issues a lxc-create with provided template, container name and hash of arguments' do
'--name', name, subject.should have_received(:run).with(
'--', :create,
'--extra-param', 'param', '--template', template,
'--other', 'value' '--name', name,
) '--',
'--extra-param', 'param',
'--other', 'value'
)
end
end
context 'when the rootfs is passed' do
let(:rootfs) { 'rootfs_path' }
it 'issues a lxc-create with the right rootfs arguments' do
@run_args.join(' ').should =~ /-B dir --dir #{rootfs}/
end
end end
end end

View file

@ -33,22 +33,26 @@ describe Vagrant::LXC::Container do
end end
describe 'creation' do describe 'creation' do
let(:name) { 'random-container-name' } let(:base_name) { 'container-name' }
let(:suffix) { 'random-suffix' }
let(:template_name) { 'template-name' } let(:template_name) { 'template-name' }
let(:rootfs_cache) { '/path/to/cache' } let(:rootfs_cache) { '/path/to/cache' }
let(:target_rootfs) { '/path/to/rootfs' }
let(:public_key_path) { Vagrant.source_root.join('keys', 'vagrant.pub').expand_path.to_s } let(:public_key_path) { Vagrant.source_root.join('keys', 'vagrant.pub').expand_path.to_s }
let(:cli) { fire_double('Vagrant::LXC::Container::CLI', :create => true, :name= => true) } let(:cli) { fire_double('Vagrant::LXC::Container::CLI', :create => true, :name= => true) }
subject { described_class.new(name, cli) } subject { described_class.new(name, cli) }
before do before do
SecureRandom.stub(hex: name) SecureRandom.stub(hex: suffix)
subject.create 'template-name' => template_name, 'rootfs-cache-path' => rootfs_cache, 'template-opts' => { '--foo' => 'bar'} subject.create base_name, target_rootfs, 'template-name' => template_name, 'rootfs-cache-path' => rootfs_cache, 'template-opts' => { '--foo' => 'bar'}
end end
it 'creates container with the right arguments' do it 'creates container with the right arguments' do
cli.should have_received(:name=).with("#{base_name}-#{suffix}")
cli.should have_received(:create).with( cli.should have_received(:create).with(
template_name, template_name,
target_rootfs,
'--auth-key' => public_key_path, '--auth-key' => public_key_path,
'--cache' => rootfs_cache, '--cache' => rootfs_cache,
'--foo' => 'bar' '--foo' => 'bar'