From 786bb8a3fe782cf6df6bd4351ac33c6aa4417b52 Mon Sep 17 00:00:00 2001 From: Fabio Rehm Date: Sun, 2 Feb 2014 18:35:48 -0200 Subject: [PATCH] Simplify code related to container naming [GH-132] --- README.md | 32 ++++++++++---------------------- lib/vagrant-lxc/action/create.rb | 19 +++++++++++-------- lib/vagrant-lxc/config.rb | 6 ++---- 3 files changed, 23 insertions(+), 34 deletions(-) diff --git a/README.md b/README.md index 0ed4cc6..07f5b10 100644 --- a/README.md +++ b/README.md @@ -95,32 +95,20 @@ 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). -You also have some control over the container name. By default, -vagrant-lxc will attempt to generate a unique container name for you. -However, you may use the `container_name` attribute to explicitly set -the container name to a value of your choosing, or you can use -`use_machine_name` to ensure that the container name is the same as the -vagrant machine name: +### 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.provider :lxc do |lxc| - # Same effect as 'customize ["modifyvm", :id, "--memory", "1024"]' for VirtualBox - lxc.customize 'cgroup.memory.limit_in_bytes', '1024M' - lxc.container_name = "my-container" # Set the container name explicitly - end -end -``` -```ruby -Vagrant.configure("2") do |config| - config.vm.box = "quantal64" - config.vm.define "foo" do |inst| - inst.vm.provider :lxc do |lxc| - # Same effect as 'customize ["modifyvm", :id, "--memory", "1024"]' for VirtualBox - lxc.customize 'cgroup.memory.limit_in_bytes', '1024M' - lxc.use_machine_name = true # Set container name to "foo" + 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 @@ -130,7 +118,7 @@ end 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 diff --git a/lib/vagrant-lxc/action/create.rb b/lib/vagrant-lxc/action/create.rb index 38f7f62..dc519bb 100644 --- a/lib/vagrant-lxc/action/create.rb +++ b/lib/vagrant-lxc/action/create.rb @@ -7,14 +7,17 @@ module Vagrant end def call(env) - if env[:machine].provider_config.use_machine_name - container_name = env[:machine].name.to_s - elsif env[:machine].provider_config.container_name - container_name = env[:machine].provider_config.container_name - else - 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, "") + container_name << "-#{Time.now.to_i}" end env[:machine].provider.driver.create( diff --git a/lib/vagrant-lxc/config.rb b/lib/vagrant-lxc/config.rb index c9e63a5..a780a81 100644 --- a/lib/vagrant-lxc/config.rb +++ b/lib/vagrant-lxc/config.rb @@ -12,10 +12,8 @@ module Vagrant # on /etc/sudoers attr_accessor :sudo_wrapper - # A boolean that sets the container name to the machine name - attr_accessor :use_machine_name - - # A string to explicitly set the container name + # A string to explicitly set the container name (use :machine) to set it + # to the corresponding machine name. attr_accessor :container_name def initialize