Simplify code related to container naming [GH-132]

This commit is contained in:
Fabio Rehm 2014-02-02 18:35:48 -02:00
parent e3b8ead93b
commit 786bb8a3fe
3 changed files with 23 additions and 34 deletions

View file

@ -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). 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, ### Container naming
vagrant-lxc will attempt to generate a unique container name for you.
However, you may use the `container_name` attribute to explicitly set By default vagrant-lxc will attempt to generate a unique container name
the container name to a value of your choosing, or you can use for you. However, if the container name is important to you, you may use the
`use_machine_name` to ensure that the container name is the same as the `container_name` attribute to set it explicitly from the `provider` block:
vagrant machine name:
```ruby ```ruby
Vagrant.configure("2") do |config| Vagrant.configure("2") do |config|
config.vm.box = "quantal64" 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 config.vm.define "db" do |node|
Vagrant.configure("2") do |config| node.vm.provider :lxc do |lxc|
config.vm.box = "quantal64" lxc.container_name = :machine # Sets the container name to 'db'
config.vm.define "foo" do |inst| lxc.container_name = 'mysql' # Sets the container name to 'mysql'
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"
end end
end end
end end
@ -130,7 +118,7 @@ end
This plugin requires **a lot** of `sudo`ing since [user namespaces](https://wiki.ubuntu.com/UserNamespace) 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) 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. < 1.8.4 (like Ubuntu 12.04) as you might be affected by a bug.
### Base boxes ### Base boxes

View file

@ -7,14 +7,17 @@ module Vagrant
end end
def call(env) def call(env)
if env[:machine].provider_config.use_machine_name container_name = env[:machine].provider_config.container_name
container_name = env[:machine].name.to_s
elsif env[:machine].provider_config.container_name case container_name
container_name = env[:machine].provider_config.container_name when :machine
else container_name = env[:machine].name.to_s
container_name = "#{env[:root_path].basename}_#{env[:machine].name}" when String
container_name.gsub!(/[^-a-z0-9_]/i, "") # Nothing to do here, move along...
container_name << "-#{Time.now.to_i}" else
container_name = "#{env[:root_path].basename}_#{env[:machine].name}"
container_name.gsub!(/[^-a-z0-9_]/i, "")
container_name << "-#{Time.now.to_i}"
end end
env[:machine].provider.driver.create( env[:machine].provider.driver.create(

View file

@ -12,10 +12,8 @@ module Vagrant
# on /etc/sudoers # on /etc/sudoers
attr_accessor :sudo_wrapper attr_accessor :sudo_wrapper
# A boolean that sets the container name to the machine name # A string to explicitly set the container name (use :machine) to set it
attr_accessor :use_machine_name # to the corresponding machine name.
# A string to explicitly set the container name
attr_accessor :container_name attr_accessor :container_name
def initialize def initialize