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 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 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`) container config file (usually kept under `/var/lib/lxc/<container>/config`)
prior to starting it. prior to starting it.

View file

@ -7,8 +7,10 @@ module Vagrant
end end
def call(env) 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 container_name = env[:machine].name.to_s
elsif env[:machine].provider_config.container_name
container_name = env[:machine].provider_config.container_name
else else
container_name = "#{env[:root_path].basename}_#{env[:machine].name}" container_name = "#{env[:root_path].basename}_#{env[:machine].name}"
container_name.gsub!(/[^-a-z0-9_]/i, "") container_name.gsub!(/[^-a-z0-9_]/i, "")

View file

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