vagrant-lxc 0.3.4 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/development/site.pp CHANGED
@@ -24,7 +24,7 @@ exec {
24
24
  # Install dependencies
25
25
  package {
26
26
  [ 'libffi-dev', 'bsdtar', 'exuberant-ctags', 'ruby1.9.1-dev', 'htop', 'git',
27
- 'build-essential', 'redir', 'curl' ]:
27
+ 'build-essential', 'redir', 'curl', 'vim', 'btrfs-tools' ]:
28
28
  ensure => 'installed'
29
29
  ;
30
30
 
data/example/Vagrantfile CHANGED
@@ -3,6 +3,7 @@
3
3
 
4
4
  # Not really needed, but useful while developing so that vagrant picks it up
5
5
  Vagrant.require_plugin 'vagrant-lxc'
6
+ Vagrant.require_plugin 'vagrant-omnibus'
6
7
 
7
8
  Vagrant.configure("2") do |config|
8
9
  config.vm.synced_folder "/tmp", "/vagrant_data"
@@ -22,35 +23,25 @@ echo "Hi there I'm a shell script used for provisioning"
22
23
  puppet.manifest_file = "site.pp"
23
24
  end
24
25
 
25
- config.vm.provision :chef_solo do |chef|
26
- chef.add_recipe "hello-world"
27
- chef.log_level = :debug
28
- end
29
-
30
26
  port = 8080
31
- releases = %w(precise quantal raring)
27
+ releases = %w(precise quantal raring wheezy squeeze sid)
32
28
  releases.each do |release|
33
29
  config.vm.define(release) do |lxc_config|
34
30
  lxc_config.vm.box = "#{release}64"
35
- lxc_config.vm.box_url = "http://dl.dropbox.com/u/13510779/lxc-#{release}-amd64-2013-05-08.box"
36
- lxc_config.vm.network :forwarded_port, guest: 80, host: (port += 1)
31
+ lxc_config.vm.box_url = "http://dl.dropbox.com/u/13510779/lxc-#{release}-amd64-2013-07-12.box"
37
32
  # Uncomment if you want to try out a box built locally
38
- # lxc_config.vm.box_url = "../boxes/output/lxc-#{release}64.box"
33
+ # lxc_config.vm.box_url = "../boxes/output/lxc-#{release}-amd64-2013-07-12.box"
34
+ lxc_config.vm.network :forwarded_port, guest: 80, host: (port += 1)
39
35
  lxc_config.vm.hostname = "lxc-#{release}64-example"
40
- end
41
- end
42
36
 
43
- port = 8090
44
- releases = %w(wheezy squeeze sid)
45
- releases.each do |release|
46
- config.vm.define(release) do |lxc_config|
47
- lxc_config.vm.box = "#{release}64"
48
- lxc_config.vm.box_url = "http://dl.dropbox.com/u/13510779/lxc-#{release}-amd64-2013-05-08.box"
49
- lxc_config.vm.network :forwarded_port, guest: 80, host: (port += 1)
50
- # Uncomment if you want to try out a box built locally
51
- # lxc_config.vm.box_url = "../boxes/output/lxc-#{release}64.box"
52
- # Does not work yet:
53
- # lxc_config.vm.hostname = "lxc-#{release}64-example"
37
+ if %w(precise quantal raring squeeze).include? release
38
+ lxc_config.omnibus.chef_version = :latest
39
+
40
+ lxc_config.vm.provision :chef_solo do |chef|
41
+ chef.add_recipe "hello-world"
42
+ chef.log_level = :debug
43
+ end
44
+ end
54
45
  end
55
46
  end
56
47
  end
@@ -11,6 +11,8 @@ module Vagrant
11
11
 
12
12
  config = env[:machine].provider_config
13
13
 
14
+ config.customize 'utsname', env[:machine].id
15
+
14
16
  env[:ui].info I18n.t("vagrant_lxc.messages.starting")
15
17
  env[:machine].provider.driver.start(config.customizations)
16
18
  raise Vagrant::Errors::VMFailedToBoot if !wait_for_boot
@@ -14,6 +14,7 @@ module Vagrant
14
14
  env[:machine].provider.driver.create(
15
15
  container_name,
16
16
  env[:lxc_template_src],
17
+ env[:lxc_template_config],
17
18
  env[:lxc_template_opts]
18
19
  )
19
20
 
@@ -3,6 +3,7 @@ module Vagrant
3
3
  module Action
4
4
  # Prepare arguments to be used for lxc-create
5
5
  class HandleBoxMetadata
6
+ SUPPORTED_VERSIONS = [2, 3]
6
7
  def initialize(app, env)
7
8
  @app = app
8
9
  @logger = Log4r::Logger.new("vagrant::lxc::action::handle_box_metadata")
@@ -22,6 +23,10 @@ module Vagrant
22
23
  @env[:lxc_template_opts] = template_opts
23
24
  @env[:lxc_template_src] = template_src
24
25
 
26
+ if template_config_file.exist?
27
+ @env[:lxc_template_config] = template_config_file.to_s
28
+ end
29
+
25
30
  @app.call env
26
31
  end
27
32
 
@@ -29,9 +34,15 @@ module Vagrant
29
34
  @template_src ||= @box.directory.join('lxc-template').to_s
30
35
  end
31
36
 
37
+ def template_config_file
38
+ @template_config_file ||= @box.directory.join('lxc.conf')
39
+ end
40
+
32
41
  def template_opts
33
42
  @template_opts ||= @box.metadata.fetch('template-opts', {}).dup.merge!(
34
43
  '--tarball' => rootfs_tarball,
44
+ # TODO: Deprecate this, the rootfs should be ready for vagrant-lxc
45
+ # SSH access at this point
35
46
  '--auth-key' => Vagrant.source_root.join('keys', 'vagrant.pub').expand_path.to_s
36
47
  )
37
48
  end
@@ -41,8 +52,10 @@ module Vagrant
41
52
  end
42
53
 
43
54
  def validate_box
44
- if @box.metadata.fetch('version').to_i != 2
45
- raise Errors::InvalidBoxVersion.new name: @box.name
55
+ unless SUPPORTED_VERSIONS.include? @box.metadata.fetch('version').to_i
56
+ raise Errors::IncompatibleBox.new name: @box.name,
57
+ found: @box.metadata.fetch('version').to_i,
58
+ supported: SUPPORTED_VERSIONS.join(' and ')
46
59
  end
47
60
 
48
61
  unless File.exists?(template_src)
@@ -45,6 +45,9 @@ module Vagrant
45
45
  box_dir = @env[:machine].box.directory
46
46
  FileUtils.cp box_dir.join('lxc-template').to_s, @env['package.directory'].to_s
47
47
  FileUtils.cp box_dir.join('metadata.json').to_s, @env['package.directory'].to_s
48
+ if (conf = box_dir.join('lxc.conf')).exist?
49
+ FileUtils.cp conf.to_s, @env['package.directory'].to_s
50
+ end
48
51
  end
49
52
  end
50
53
  end
@@ -33,12 +33,12 @@ module Vagrant
33
33
  Pathname.new(base_path.join('config').read.match(/^lxc\.rootfs\s+=\s+(.+)$/)[1])
34
34
  end
35
35
 
36
- def create(name, template_path, template_options = {})
36
+ def create(name, template_path, config_file, template_options = {})
37
37
  @cli.name = @container_name = name
38
38
 
39
39
  import_template(template_path) do |template_name|
40
40
  @logger.debug "Creating container..."
41
- @cli.create template_name, template_options
41
+ @cli.create template_name, config_file, template_options
42
42
  end
43
43
  end
44
44
 
@@ -131,6 +131,8 @@ module Vagrant
131
131
  TEMPLATES_PATH_LOOKUP = %w(
132
132
  /usr/share/lxc/templates
133
133
  /usr/lib/lxc/templates
134
+ /usr/lib64/lxc/templates
135
+ /usr/local/lib/lxc/templates
134
136
  )
135
137
  def templates_path
136
138
  return @templates_path if @templates_path
@@ -39,20 +39,25 @@ module Vagrant
39
39
  end
40
40
 
41
41
  def state
42
- if @name && run(:info, '--name', @name) =~ /^state:[^A-Z]+([A-Z]+)$/
42
+ if @name && run(:info, '--name', @name, retryable: true) =~ /^state:[^A-Z]+([A-Z]+)$/
43
43
  $1.downcase.to_sym
44
44
  elsif @name
45
45
  :unknown
46
46
  end
47
47
  end
48
48
 
49
- def create(template, template_opts = {})
49
+ def create(template, config_file, template_opts = {})
50
+ if config_file
51
+ config_opts = ['-f', config_file]
52
+ end
53
+
50
54
  extra = template_opts.to_a.flatten
51
55
  extra.unshift '--' unless extra.empty?
52
56
 
53
57
  run :create,
54
58
  '--template', template,
55
59
  '--name', @name,
60
+ *(config_opts),
56
61
  *extra
57
62
  end
58
63
 
@@ -14,8 +14,8 @@ module Vagrant
14
14
  class RootFSTarballMissing < Vagrant::Errors::VagrantError
15
15
  error_key(:lxc_invalid_box_version)
16
16
  end
17
- class InvalidBoxVersion < Vagrant::Errors::VagrantError
18
- error_key(:lxc_invalid_box_version)
17
+ class IncompatibleBox < Vagrant::Errors::VagrantError
18
+ error_key(:lxc_incompatible_box)
19
19
  end
20
20
  end
21
21
  end
@@ -1,5 +1,5 @@
1
1
  module Vagrant
2
2
  module LXC
3
- VERSION = "0.3.4"
3
+ VERSION = "0.4.0"
4
4
  end
5
5
  end
data/locales/en.yml CHANGED
@@ -17,8 +17,12 @@ en:
17
17
  force_shutdown: |-
18
18
  Forcing shutdown of container...
19
19
 
20
- # TODO: Review messages below
21
20
  vagrant:
21
+ commands:
22
+ status:
23
+ stopped: |-
24
+ The container is currently stopped. Run `vagrant up` to bring it up again.
25
+
22
26
  actions:
23
27
  lxc:
24
28
  compressing_rootfs: Compressing container's rootfs...
@@ -33,6 +37,10 @@ en:
33
37
  For more information on the failure, enable detailed logging by setting
34
38
  the environment variable VAGRANT_LOG to DEBUG.
35
39
 
40
+ lxc_incompatible_box: |-
41
+ The base box you are trying to use is not compatible with the installed
42
+ vagrant-lxc version. Supported box versions are %{supported} but %{found} was found.
43
+
36
44
  lxc_template_file_missing: |-
37
45
  The template file used for creating the container was not found for %{name}
38
46
  box.
data/spec/Vagrantfile CHANGED
@@ -8,10 +8,7 @@ ENV['BOX_NAME'] ||= 'quantal64'
8
8
 
9
9
  Vagrant.configure("2") do |config|
10
10
  config.vm.box = ENV['BOX_NAME']
11
- # FIXME: Find out why this does not work for debian boxes
12
- unless %w( squeeze64 wheezy64 sid64 ).include? config.vm.box
13
- config.vm.hostname = 'lxc-test-box'
14
- end
11
+ config.vm.hostname = 'lxc-test-box'
15
12
 
16
13
  config.cache.enable :apt
17
14
 
@@ -20,10 +20,7 @@ describe 'Sanity check' do
20
20
  end
21
21
 
22
22
  it "is able to be SSH'ed" do
23
- expected = 'lxc-test-box'
24
- # HACK:
25
- expected = ENV['BOX_NAME'].gsub(/64$/, '') if %w( squeeze64 wheezy64 sid64 ).include? ENV['BOX_NAME']
26
- expect(vagrant_ssh('hostname')).to eq expected
23
+ expect(vagrant_ssh('hostname')).to eq 'lxc-test-box'
27
24
  end
28
25
 
29
26
  it 'mounts shared folders with the right permissions' do
@@ -1,11 +1,12 @@
1
1
  require 'unit_helper'
2
2
 
3
+ require 'tmpdir'
3
4
  require 'vagrant-lxc/action/clear_forwarded_ports'
4
5
 
5
6
  describe Vagrant::LXC::Action::ClearForwardedPorts do
6
- let(:app) { mock(:app, call: true) }
7
- let(:env) { {machine: machine, ui: stub(info: true)} }
8
- let(:machine) { mock(:machine, data_dir: data_dir) }
7
+ let(:app) { double(:app, call: true) }
8
+ let(:env) { {machine: machine, ui: double(info: true)} }
9
+ let(:machine) { double(:machine, data_dir: data_dir) }
9
10
  let!(:data_dir) { Pathname.new(Dir.mktmpdir) }
10
11
  let(:pids_dir) { data_dir.join('pids') }
11
12
  let(:pid) { 'a-pid' }
@@ -3,11 +3,11 @@ require 'unit_helper'
3
3
  require 'vagrant-lxc/action/compress_rootfs'
4
4
 
5
5
  describe Vagrant::LXC::Action::CompressRootFS do
6
- let(:app) { mock(:app, call: true) }
7
- let(:env) { {machine: machine, ui: stub(info: true)} }
8
- let(:machine) { fire_double('Vagrant::Machine', provider: provider) }
9
- let(:provider) { fire_double('Vagrant::LXC::Provider', driver: driver) }
10
- let(:driver) { fire_double('Vagrant::LXC::Driver', compress_rootfs: compressed_rootfs_path) }
6
+ let(:app) { double(:app, call: true) }
7
+ let(:env) { {machine: machine, ui: double(info: true)} }
8
+ let(:machine) { instance_double('Vagrant::Machine', provider: provider) }
9
+ let(:provider) { instance_double('Vagrant::LXC::Provider', driver: driver) }
10
+ let(:driver) { instance_double('Vagrant::LXC::Driver', compress_rootfs: compressed_rootfs_path) }
11
11
  let(:compressed_rootfs_path) { '/path/to/rootfs.tar.gz' }
12
12
 
13
13
  subject { described_class.new(app, env) }
@@ -3,15 +3,15 @@ require 'unit_helper'
3
3
  require 'vagrant-lxc/action/forward_ports'
4
4
 
5
5
  describe Vagrant::LXC::Action::ForwardPorts do
6
- let(:app) { mock(:app, call: true) }
7
- let(:env) { {machine: machine, ui: stub(info: true)} }
8
- let(:machine) { mock(:machine) }
6
+ let(:app) { double(:app, call: true) }
7
+ let(:env) { {machine: machine, ui: double(info: true)} }
8
+ let(:machine) { double(:machine) }
9
9
  let!(:data_dir) { Pathname.new(Dir.mktmpdir) }
10
10
  let(:networks) { [[:other_config, {}], [:forwarded_port, {guest: guest_port, host: host_port}]] }
11
11
  let(:host_port) { 8080 }
12
12
  let(:guest_port) { 80 }
13
- let(:provider) { fire_double('Vagrant::LXC::Provider', driver: driver) }
14
- let(:driver) { fire_double('Vagrant::LXC::Driver', assigned_ip: container_ip) }
13
+ let(:provider) { instance_double('Vagrant::LXC::Provider', driver: driver) }
14
+ let(:driver) { instance_double('Vagrant::LXC::Driver', assigned_ip: container_ip) }
15
15
  let(:container_ip) { '10.0.1.234' }
16
16
  let(:pid) { 'a-pid' }
17
17
 
@@ -5,10 +5,10 @@ require 'vagrant-lxc/errors'
5
5
  require 'vagrant-lxc/action/handle_box_metadata'
6
6
 
7
7
  describe Vagrant::LXC::Action::HandleBoxMetadata do
8
- let(:app) { mock(:app, call: true) }
9
- let(:env) { {machine: machine, ui: stub(info: true)} }
10
- let(:machine) { mock(:machine, box: box) }
11
- let(:box) { mock(:box, name: 'box-name', metadata: metadata, directory: box_directory) }
8
+ let(:app) { double(:app, call: true) }
9
+ let(:env) { {machine: machine, ui: double(info: true)} }
10
+ let(:machine) { double(:machine, box: box) }
11
+ let(:box) { double(:box, name: 'box-name', metadata: metadata, directory: box_directory) }
12
12
  let(:box_directory) { Pathname.new('/path/to/box') }
13
13
  let(:version) { '2' }
14
14
  let(:metadata) { {'template-opts' => {'--foo' => 'bar'}, 'version' => version} }
@@ -50,7 +50,7 @@ describe Vagrant::LXC::Action::HandleBoxMetadata do
50
50
  metadata['version'] = '1'
51
51
  expect {
52
52
  subject.call(env)
53
- }.to raise_error(Vagrant::LXC::Errors::InvalidBoxVersion)
53
+ }.to raise_error(Vagrant::LXC::Errors::IncompatibleBox)
54
54
  end
55
55
 
56
56
  it 'raises an error if the rootfs tarball cant be found' do
@@ -3,39 +3,57 @@ require 'unit_helper'
3
3
  require 'vagrant-lxc/action/setup_package_files'
4
4
 
5
5
  describe Vagrant::LXC::Action::SetupPackageFiles do
6
- let(:app) { mock(:app, call: true) }
7
- let(:env) { {machine: machine, tmp_path: tmp_path, ui: stub(info: true), 'package.rootfs' => rootfs_path} }
8
- let(:machine) { fire_double('Vagrant::Machine', box: box) }
6
+ let(:app) { double(:app, call: true) }
7
+ let(:env) { {machine: machine, tmp_path: tmp_path, ui: double(info: true), 'package.rootfs' => rootfs_path} }
8
+ let(:machine) { instance_double('Vagrant::Machine', box: box) }
9
9
  let!(:tmp_path) { Pathname.new(Dir.mktmpdir) }
10
- let(:box) { fire_double('Vagrant::Box', directory: tmp_path.join('box')) }
10
+ let(:box) { instance_double('Vagrant::Box', directory: tmp_path.join('box')) }
11
11
  let(:rootfs_path) { tmp_path.join('rootfs-amd64.tar.gz') }
12
12
 
13
13
  subject { described_class.new(app, env) }
14
14
 
15
15
  before do
16
16
  box.directory.mkdir
17
- [box.directory.join('lxc-template'), box.directory.join('metadata.json'), rootfs_path].each do |file|
17
+ files = %w( lxc-template metadata.json lxc.conf ).map { |f| box.directory.join(f) }
18
+ (files + [rootfs_path]).each do |file|
18
19
  file.open('w') { |f| f.puts file.to_s }
19
20
  end
20
21
 
21
22
  subject.stub(recover: true) # Prevents files from being removed on specs
22
- subject.call(env)
23
23
  end
24
24
 
25
25
  after do
26
26
  FileUtils.rm_rf(tmp_path.to_s)
27
27
  end
28
28
 
29
- it 'copies box lxc-template to package directory' do
30
- env['package.directory'].join('lxc-template').should be_file
31
- end
29
+ context 'when all files exist' do
30
+ before { subject.call(env) }
31
+
32
+ it 'copies box lxc-template to package directory' do
33
+ env['package.directory'].join('lxc-template').should be_file
34
+ end
32
35
 
33
- it 'copies metadata.json to package directory' do
34
- env['package.directory'].join('metadata.json').should be_file
36
+ it 'copies metadata.json to package directory' do
37
+ env['package.directory'].join('metadata.json').should be_file
38
+ end
39
+
40
+ it 'copies box lxc.conf to package directory' do
41
+ env['package.directory'].join('lxc-template').should be_file
42
+ end
43
+
44
+ it 'moves the compressed rootfs to package directory' do
45
+ env['package.directory'].join(rootfs_path.basename).should be_file
46
+ env['package.rootfs'].should_not be_file
47
+ end
35
48
  end
36
49
 
37
- it 'moves the compressed rootfs to package directory' do
38
- env['package.directory'].join(rootfs_path.basename).should be_file
39
- env['package.rootfs'].should_not be_file
50
+ context 'when lxc.conf file is not present' do
51
+ before do
52
+ box.directory.join('lxc.conf').delete
53
+ end
54
+
55
+ it 'does not blow up' do
56
+ expect { subject.call(env) }.to_not raise_error
57
+ end
40
58
  end
41
59
  end
@@ -38,13 +38,14 @@ describe Vagrant::LXC::Driver::CLI do
38
38
  describe 'create' do
39
39
  let(:template) { 'quantal-64' }
40
40
  let(:name) { 'quantal-container' }
41
+ let(:config_file) { 'config' }
41
42
  let(:template_args) { { '--extra-param' => 'param', '--other' => 'value' } }
42
43
 
43
44
  subject { described_class.new(name) }
44
45
 
45
46
  before do
46
47
  subject.stub(:run) { |*args| @run_args = args }
47
- subject.create(template, template_args)
48
+ subject.create(template, config_file, template_args)
48
49
  end
49
50
 
50
51
  it 'issues a lxc-create with provided template, container name and hash of arguments' do
@@ -52,6 +53,7 @@ describe Vagrant::LXC::Driver::CLI do
52
53
  :create,
53
54
  '--template', template,
54
55
  '--name', name,
56
+ '-f', config_file,
55
57
  '--',
56
58
  '--extra-param', 'param',
57
59
  '--other', 'value'
@@ -124,7 +126,7 @@ describe Vagrant::LXC::Driver::CLI do
124
126
 
125
127
  it 'calls lxc-info with the right arguments' do
126
128
  subject.state
127
- subject.should have_received(:run).with(:info, '--name', name)
129
+ subject.should have_received(:run).with(:info, '--name', name, retryable: true)
128
130
  end
129
131
 
130
132
  it 'maps the output of lxc-info status out to a symbol' do