From b34dd7d8e70206cb990135613a8632cae37d039a Mon Sep 17 00:00:00 2001 From: Michael Owings Date: Fri, 31 Jan 2014 11:01:27 -0600 Subject: [PATCH] Allow two ways to name a container --- README.md | 31 +++++++++++++++++++++++++++++++ lib/vagrant-lxc/action/create.rb | 4 +++- lib/vagrant-lxc/config.rb | 13 +++++++++---- 3 files changed, 43 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 36e9f64..f679e06 100644 --- a/README.md +++ b/README.md @@ -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//config`) prior to starting it. diff --git a/lib/vagrant-lxc/action/create.rb b/lib/vagrant-lxc/action/create.rb index abca848..38f7f62 100644 --- a/lib/vagrant-lxc/action/create.rb +++ b/lib/vagrant-lxc/action/create.rb @@ -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, "") diff --git a/lib/vagrant-lxc/config.rb b/lib/vagrant-lxc/config.rb index 59739ec..c9e63a5 100644 --- a/lib/vagrant-lxc/config.rb +++ b/lib/vagrant-lxc/config.rb @@ -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)