diff --git a/CHANGELOG.md b/CHANGELOG.md index 0ca5036..8e46d7f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,14 @@ -## [0.7.1](https://github.com/fgrehm/vagrant-lxc/compare/v0.7.0...master) (unreleased) +## [0.8.0](https://github.com/fgrehm/vagrant-lxc/compare/v0.7.0...v0.8.0) (Feb 26, 2014) + +FEATURES: + + - Support for naming containers from Vagrantfiles [#132](https://github.com/fgrehm/vagrant-lxc/issues/132) + +IMPROVEMENTS: + + - Use a safer random name for containers [#152](https://github.com/fgrehm/vagrant-lxc/issues/152) + - Improve Ubuntu 13.10 compatibility [#190](https://github.com/fgrehm/vagrant-lxc/pull/190) / [#197](https://github.com/fgrehm/vagrant-lxc/pull/197) + - Improved mac address detection from lxc configs [#226](https://github.com/fgrehm/vagrant-lxc/pull/226) BUG FIXES: diff --git a/Gemfile.lock b/Gemfile.lock index 53b5ef5..e1a5d71 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -25,7 +25,7 @@ GIT PATH remote: . specs: - vagrant-lxc (0.7.1.dev) + vagrant-lxc (0.8.0) GEM remote: https://rubygems.org/ diff --git a/README.md b/README.md index 488a1be..125df53 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # 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) [![Coverage Status](https://coveralls.io/repos/fgrehm/vagrant-lxc/badge.png?branch=master)](https://coveralls.io/r/fgrehm/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) [![Coverage Status](https://coveralls.io/repos/fgrehm/vagrant-lxc/badge.png?branch=master)](https://coveralls.io/r/fgrehm/vagrant-lxc) [![Gittip](http://img.shields.io/gittip/fgrehm.svg)](https://www.gittip.com/fgrehm/) [LXC](http://lxc.sourceforge.net/) provider for [Vagrant](http://www.vagrantup.com/) 1.1+ @@ -95,11 +95,30 @@ prior to starting it. For other configuration options, please check the [lxc.conf manpages](http://manpages.ubuntu.com/manpages/quantal/man5/lxc.conf.5.html). +### Container naming + +By default vagrant-lxc will attempt to generate a unique container name +for you. However, if the container name is important to you, you may use the +`container_name` attribute to set it explicitly from the `provider` block: + +```ruby +Vagrant.configure("2") do |config| + config.vm.box = "quantal64" + + config.vm.define "db" do |node| + node.vm.provider :lxc do |lxc| + lxc.container_name = :machine # Sets the container name to 'db' + lxc.container_name = 'mysql' # Sets the container name to 'mysql' + end + end +end +``` + ### Avoiding `sudo` passwords This plugin requires **a lot** of `sudo`ing since [user namespaces](https://wiki.ubuntu.com/UserNamespace) are not supported on mainstream kernels. Have a look at the [Wiki](https://github.com/fgrehm/vagrant-lxc/wiki/Avoiding-'sudo'-passwords) -to find out how to work around that specially if you are running an OS with sudo +to find out how to work around that specially if you are running an OS with `sudo` < 1.8.4 (like Ubuntu 12.04) as you might be affected by a bug. ### Base boxes @@ -123,14 +142,6 @@ list if you have a problem and feel free to use the [issue tracker](https://gith propose new functionality and / or report bugs. -## Support - -Support this project and [others by fgrehm](https://github.com/fgrehm) -via [gittip](https://www.gittip.com/fgrehm/). - -[![Support via Gittip](https://rawgithub.com/twolfson/gittip-badge/0.1.0/dist/gittip.png)](https://www.gittip.com/fgrehm/) - - ## Contributing 1. Fork it @@ -138,5 +149,3 @@ via [gittip](https://www.gittip.com/fgrehm/). 3. Commit your changes (`git commit -am 'Add some feature'`) 4. Push to the branch (`git push origin my-new-feature`) 5. Create new Pull Request - -[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/fgrehm/vagrant-lxc/trend.png)](https://bitdeli.com/free "Bitdeli Badge") diff --git a/lib/vagrant-lxc/action/create.rb b/lib/vagrant-lxc/action/create.rb index 018db0d..1676e21 100644 --- a/lib/vagrant-lxc/action/create.rb +++ b/lib/vagrant-lxc/action/create.rb @@ -7,9 +7,20 @@ module Vagrant end def call(env) - container_name = "#{env[:root_path].basename}_#{env[:machine].name}" - container_name.gsub!(/[^-a-z0-9_]/i, "") - container_name << "-#{Time.now.to_i}" + container_name = env[:machine].provider_config.container_name + + case container_name + when :machine + container_name = env[:machine].name.to_s + when String + # Nothing to do here, move along... + else + container_name = "#{env[:root_path].basename}_#{env[:machine].name}" + container_name.gsub!(/[^-a-z0-9_]/i, "") + # milliseconds + random number suffix to allow for simultaneous + # `vagrant up` of the same box in different dirs + container_name << "_#{(Time.now.to_f * 1000.0).to_i}_#{rand(100000)}" + end env[:machine].provider.driver.create( container_name, diff --git a/lib/vagrant-lxc/config.rb b/lib/vagrant-lxc/config.rb index 2065bc8..1a47058 100644 --- a/lib/vagrant-lxc/config.rb +++ b/lib/vagrant-lxc/config.rb @@ -12,9 +12,14 @@ module Vagrant # on /etc/sudoers attr_accessor :sudo_wrapper + # A string to explicitly set the container name. To use the vagrant + # machine name, set this to :machine + attr_accessor :container_name + def initialize @customizations = [] @sudo_wrapper = UNSET_VALUE + @container_name = UNSET_VALUE end # Customize the container by calling `lxc-start` with the given @@ -34,6 +39,7 @@ module Vagrant def finalize! @sudo_wrapper = nil if @sudo_wrapper == UNSET_VALUE + @container_name = nil if @container_name == UNSET_VALUE end def validate(machine) diff --git a/lib/vagrant-lxc/driver.rb b/lib/vagrant-lxc/driver.rb index dbedcfb..3c14554 100644 --- a/lib/vagrant-lxc/driver.rb +++ b/lib/vagrant-lxc/driver.rb @@ -40,7 +40,7 @@ module Vagrant end def mac_address - @mac_address ||= config_string.match(/^lxc\.network\.hwaddr\s+=\s+(.+)$/)[1] + @mac_address ||= config_string.match(/^lxc\.network\.hwaddr\s*+=\s*+(.+)$/)[1] end def config_string diff --git a/lib/vagrant-lxc/driver/cli.rb b/lib/vagrant-lxc/driver/cli.rb index 80e538f..f672bfd 100644 --- a/lib/vagrant-lxc/driver/cli.rb +++ b/lib/vagrant-lxc/driver/cli.rb @@ -38,7 +38,7 @@ module Vagrant end def state - if @name && run(:info, '--name', @name, retryable: true) =~ /^state:[^A-Z]+([A-Z]+)$/ + if @name && run(:info, '--name', @name, retryable: true) =~ /^state:[^A-Z]+([A-Z]+)$/i $1.downcase.to_sym elsif @name :unknown @@ -58,6 +58,12 @@ module Vagrant '--name', @name, *(config_opts), *extra + rescue Errors::ExecuteError => e + if e.stderr =~ /already exists/i + raise Errors::ContainerAlreadyExists, name: @name + else + raise + end end def destroy @@ -69,6 +75,7 @@ module Vagrant end def stop + attach '/sbin/halt' run :stop, '--name', @name end diff --git a/lib/vagrant-lxc/errors.rb b/lib/vagrant-lxc/errors.rb index 557cbaa..c75bfa3 100644 --- a/lib/vagrant-lxc/errors.rb +++ b/lib/vagrant-lxc/errors.rb @@ -5,7 +5,16 @@ module Vagrant module Errors class ExecuteError < Vagrant::Errors::VagrantError error_key(:lxc_execute_error) + attr_reader :stderr, :stdout + def initialize(message, *args) + super + if message.is_a?(Hash) + @stderr = message[:stderr] + @stdout = message[:stdout] + end + end end + class NamespacesNotSupported < Vagrant::Errors::VagrantError end @@ -13,6 +22,10 @@ module Vagrant error_key(:lxc_not_installed) end + class ContainerAlreadyExists < Vagrant::Errors::VagrantError + error_key(:lxc_container_already_exists) + end + # Box related errors class TemplateFileMissing < Vagrant::Errors::VagrantError error_key(:lxc_template_file_missing) diff --git a/lib/vagrant-lxc/provider.rb b/lib/vagrant-lxc/provider.rb index abde513..0709b10 100644 --- a/lib/vagrant-lxc/provider.rb +++ b/lib/vagrant-lxc/provider.rb @@ -27,7 +27,7 @@ module Vagrant def ensure_lxc_installed! begin - sudo_wrapper.run("which", "lxc-version") + sudo_wrapper.run("which", "lxc-create") rescue Vagrant::LXC::Errors::ExecuteError raise Errors::LxcNotInstalled end diff --git a/lib/vagrant-lxc/sudo_wrapper.rb b/lib/vagrant-lxc/sudo_wrapper.rb index cfce001..d906d1e 100644 --- a/lib/vagrant-lxc/sudo_wrapper.rb +++ b/lib/vagrant-lxc/sudo_wrapper.rb @@ -51,7 +51,8 @@ module Vagrant if @interrupted @logger.info("Exit code != 0, but interrupted. Ignoring.") else - raise LXC::Errors::ExecuteError, :command => command.inspect + raise LXC::Errors::ExecuteError, + command: command.inspect, stderr: r.stderr, stdout: r.stdout end end end diff --git a/lib/vagrant-lxc/version.rb b/lib/vagrant-lxc/version.rb index 660ea9c..cbecb7a 100644 --- a/lib/vagrant-lxc/version.rb +++ b/lib/vagrant-lxc/version.rb @@ -1,5 +1,5 @@ module Vagrant module LXC - VERSION = "0.7.1.dev" + VERSION = "0.8.0" end end diff --git a/locales/en.yml b/locales/en.yml index 8a24d1d..2958b99 100644 --- a/locales/en.yml +++ b/locales/en.yml @@ -60,3 +60,8 @@ en: lxc_redir_not_installed: |- `redir` is not installed or is not accessible on the PATH. + + lxc_container_already_exists: |- + There is container on your system with the same name you've specified + on your Vagrantfile (%{name}), please choose a different one or + run `lxc-destroy --name %{name}` and try again. diff --git a/spec/unit/driver/cli_spec.rb b/spec/unit/driver/cli_spec.rb index 1b7e00c..9e78353 100644 --- a/spec/unit/driver/cli_spec.rb +++ b/spec/unit/driver/cli_spec.rb @@ -49,20 +49,27 @@ describe Vagrant::LXC::Driver::CLI do before do subject.stub(:run) { |*args| @run_args = args } - subject.create(template, config_file, template_args) end - it 'issues a lxc-create with provided template, container name and hash of arguments' do - subject.should have_received(:run).with( - :create, - '--template', template, - '--name', name, - '-f', config_file, - '--', - '--extra-param', 'param', - '--other', 'value' - ) - end + it 'issues a lxc-create with provided template, container name and hash of arguments' do + subject.create(template, config_file, template_args) + subject.should have_received(:run).with( + :create, + '--template', template, + '--name', name, + '-f', config_file, + '--', + '--extra-param', 'param', + '--other', 'value' + ) + end + + it 'wraps a low level error into something more meaningful in case the container already exists' do + subject.stub(:run) { raise Vagrant::LXC::Errors::ExecuteError, stderr: 'alreAdy Exists' } + expect { + subject.create(template, config_file, template_args) + }.to raise_error(Vagrant::LXC::Errors::ContainerAlreadyExists) + end end describe 'destroy' do @@ -134,6 +141,11 @@ describe Vagrant::LXC::Driver::CLI do it 'maps the output of lxc-info status out to a symbol' do subject.state.should == :stopped end + + it 'is not case sensitive' do + subject.stub(:run).and_return("StatE: STarTED\npid: 2") + subject.state.should == :started + end end describe 'attach' do