Allow two ways to name a container

This commit is contained in:
Michael Owings 2014-01-31 11:01:27 -06:00 committed by Fabio Rehm
parent ffb573a7f1
commit b34dd7d8e7
3 changed files with 43 additions and 5 deletions

View file

@ -89,6 +89,37 @@ Vagrant.configure("2") do |config|
end
```
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:
```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"
end
end
end
```
vagrant-lxc will then write out `lxc.cgroup.memory.limit_in_bytes='1024M'` to the
container config file (usually kept under `/var/lib/lxc/<container>/config`)
prior to starting it.

View file

@ -7,8 +7,10 @@ module Vagrant
end
def call(env)
if env[:machine].provider_config.static_name
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, "")

View file

@ -12,13 +12,17 @@ module Vagrant
# on /etc/sudoers
attr_accessor :sudo_wrapper
# A String that sets a static name
attr_accessor :static_name
# A boolean that sets the container name to the machine name
attr_accessor :use_machine_name
# A string to explicitly set the container name
attr_accessor :container_name
def initialize
@customizations = []
@sudo_wrapper = UNSET_VALUE
@static_name = UNSET_VALUE
@use_machine_name = UNSET_VALUE
@container_name = UNSET_VALUE
end
# Customize the container by calling `lxc-start` with the given
@ -38,7 +42,8 @@ module Vagrant
def finalize!
@sudo_wrapper = nil if @sudo_wrapper == UNSET_VALUE
@static_name = nil if @static_name == UNSET_VALUE
@use_machine_name = false if @use_machine_name == UNSET_VALUE
@container_name = nil if @container_name == UNSET_VALUE
end
def validate(machine)