From 3a2335f37c6fd60a64aa1573a54c2c82d9efdd7b Mon Sep 17 00:00:00 2001 From: Nico Tonozzi Date: Thu, 31 Jul 2014 09:31:30 -0600 Subject: [PATCH 1/3] Provide an option to randomize domain name This commit lets users enable hostname randomization from their Vagrantfile. Without this option enabled, domain creation will fail when multiple VMs are spun up from the same Vagrantfile, due to a domain name conflict. --- lib/vagrant-libvirt/action/set_name_of_domain.rb | 7 ++++--- lib/vagrant-libvirt/config.rb | 5 +++++ spec/support/environment_helper.rb | 2 ++ spec/vagrant-libvirt/action/set_name_of_domain_spec.rb | 8 +++++++- 4 files changed, 18 insertions(+), 4 deletions(-) diff --git a/lib/vagrant-libvirt/action/set_name_of_domain.rb b/lib/vagrant-libvirt/action/set_name_of_domain.rb index 026a739..e7dba96 100644 --- a/lib/vagrant-libvirt/action/set_name_of_domain.rb +++ b/lib/vagrant-libvirt/action/set_name_of_domain.rb @@ -41,7 +41,6 @@ module VagrantPlugins # development-centos-6-chef-11_1404488971_3b7a569e2fd7c554b852 # @return [String] libvirt domain name def build_domain_name(env) - postfix = "#{Time.now.utc.to_i}_#{SecureRandom.hex(10)}" config = env[:machine].provider_config domain_name = if config.default_prefix.nil? @@ -49,8 +48,11 @@ module VagrantPlugins else config.default_prefix.to_s end + domain_name << '_' + domain_name << env[:machine].name.to_s domain_name.gsub!(/[^-a-z0-9_]/i, '') - domain_name << "_#{postfix}" + domain_name << "_#{Time.now.utc.to_i}_#{SecureRandom.hex(10)}" if config.random_hostname + domain_name end end @@ -58,4 +60,3 @@ module VagrantPlugins end end end - diff --git a/lib/vagrant-libvirt/config.rb b/lib/vagrant-libvirt/config.rb index 3a8d48d..0c1f046 100644 --- a/lib/vagrant-libvirt/config.rb +++ b/lib/vagrant-libvirt/config.rb @@ -40,6 +40,9 @@ module VagrantPlugins # be stored. attr_accessor :storage_pool_name + # Turn on to prevent hostname conflicts + attr_accessor :random_hostname + # Libvirt default network attr_accessor :management_network_name attr_accessor :management_network_address @@ -70,6 +73,7 @@ module VagrantPlugins @password = UNSET_VALUE @id_ssh_key_file = UNSET_VALUE @storage_pool_name = UNSET_VALUE + @random_hostname = UNSET_VALUE @management_network_name = UNSET_VALUE @management_network_address = UNSET_VALUE @@ -182,6 +186,7 @@ module VagrantPlugins @password = nil if @password == UNSET_VALUE @id_ssh_key_file = 'id_rsa' if @id_ssh_key_file == UNSET_VALUE @storage_pool_name = 'default' if @storage_pool_name == UNSET_VALUE + @random_hostname = false if @random_hostname == UNSET_VALUE @management_network_name = 'vagrant-libvirt' if @management_network_name == UNSET_VALUE @management_network_address = '192.168.121.0/24' if @management_network_address == UNSET_VALUE diff --git a/spec/support/environment_helper.rb b/spec/support/environment_helper.rb index 5cbf582..7a4cf70 100644 --- a/spec/support/environment_helper.rb +++ b/spec/support/environment_helper.rb @@ -5,6 +5,8 @@ class EnvironmentHelper attr_writer :default_prefix, :domain_name + attr_accessor :random_hostname, :name + def [](value) self.send(value.to_sym) end diff --git a/spec/vagrant-libvirt/action/set_name_of_domain_spec.rb b/spec/vagrant-libvirt/action/set_name_of_domain_spec.rb index a01ef03..4847c75 100644 --- a/spec/vagrant-libvirt/action/set_name_of_domain_spec.rb +++ b/spec/vagrant-libvirt/action/set_name_of_domain_spec.rb @@ -5,10 +5,16 @@ describe VagrantPlugins::ProviderLibvirt::Action::SetNameOfDomain do @env = EnvironmentHelper.new end - it "builds uniqie domain name" do + it "builds unique domain name" do + @env.random_hostname = true dmn = VagrantPlugins::ProviderLibvirt::Action::SetNameOfDomain.new(Object.new, @env) first = dmn.build_domain_name(@env) second = dmn.build_domain_name(@env) first.should_not eq(second) end + + it "builds simple domain name" do + dmn = VagrantPlugins::ProviderLibvirt::Action::SetNameOfDomain.new(Object.new, @env) + dmn.build_domain_name(@env).should eq("foo_") + end end From eb1dab013145510d04703bfb54d45012f371e1e5 Mon Sep 17 00:00:00 2001 From: Nico Tonozzi Date: Tue, 5 Aug 2014 09:16:52 -0600 Subject: [PATCH 2/3] Update readme and test cases Updated documentation so that users could find the new option. Updated test case to behave as expected. --- README.md | 1 + lib/vagrant-libvirt/action/set_name_of_domain.rb | 3 ++- spec/support/environment_helper.rb | 8 ++------ spec/vagrant-libvirt/action/set_name_of_domain_spec.rb | 3 ++- 4 files changed, 7 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index ab3293f..8e23fbd 100644 --- a/README.md +++ b/README.md @@ -148,6 +148,7 @@ end * `volume_cache` - Controls the cache mechanism. Possible values are "default", "none", "writethrough", "writeback", "directsync" and "unsafe". [See driver->cache in libvirt documentation](http://libvirt.org/formatdomain.html#elementsDisks). * `kernel` - To launch the guest with a kernel residing on host filesystems. Equivalent to qemu `-kernel`. * `initrd` - To specify the initramfs/initrd to use for the guest. Equivalent to qemu `-initrd`. +* `random_hostname` - To create a domain name with with extra information on the end to prevent hostname conflicts. * `cmd_line` - Arguments passed on to the guest kernel initramfs or initrd to use. Equivalent to qemu `-append`. diff --git a/lib/vagrant-libvirt/action/set_name_of_domain.rb b/lib/vagrant-libvirt/action/set_name_of_domain.rb index e7dba96..9d91ea7 100644 --- a/lib/vagrant-libvirt/action/set_name_of_domain.rb +++ b/lib/vagrant-libvirt/action/set_name_of_domain.rb @@ -35,7 +35,8 @@ module VagrantPlugins end # build domain name - # avoids `domain about to create is already taken` + # random_hostname option avoids + # `domain about to create is already taken` # parsable and sortable by epoch time # @example # development-centos-6-chef-11_1404488971_3b7a569e2fd7c554b852 diff --git a/spec/support/environment_helper.rb b/spec/support/environment_helper.rb index 7a4cf70..f49db85 100644 --- a/spec/support/environment_helper.rb +++ b/spec/support/environment_helper.rb @@ -3,9 +3,9 @@ require "pathname" class EnvironmentHelper - attr_writer :default_prefix, :domain_name + attr_writer :domain_name - attr_accessor :random_hostname, :name + attr_accessor :random_hostname, :name, :default_prefix def [](value) self.send(value.to_sym) @@ -19,10 +19,6 @@ class EnvironmentHelper self end - def default_prefix - # noop - end - def root_path Pathname.new("./spec/support/foo") end diff --git a/spec/vagrant-libvirt/action/set_name_of_domain_spec.rb b/spec/vagrant-libvirt/action/set_name_of_domain_spec.rb index 4847c75..69d3319 100644 --- a/spec/vagrant-libvirt/action/set_name_of_domain_spec.rb +++ b/spec/vagrant-libvirt/action/set_name_of_domain_spec.rb @@ -14,7 +14,8 @@ describe VagrantPlugins::ProviderLibvirt::Action::SetNameOfDomain do end it "builds simple domain name" do + @env.default_prefix= 'pre' dmn = VagrantPlugins::ProviderLibvirt::Action::SetNameOfDomain.new(Object.new, @env) - dmn.build_domain_name(@env).should eq("foo_") + dmn.build_domain_name(@env).should eq('pre_') end end From 07926eff77852bdca95b8e5bba830afac41232f7 Mon Sep 17 00:00:00 2001 From: nicot Date: Wed, 6 Aug 2014 18:22:57 -0600 Subject: [PATCH 3/3] Now with less with --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 8e23fbd..60815be 100644 --- a/README.md +++ b/README.md @@ -148,7 +148,7 @@ end * `volume_cache` - Controls the cache mechanism. Possible values are "default", "none", "writethrough", "writeback", "directsync" and "unsafe". [See driver->cache in libvirt documentation](http://libvirt.org/formatdomain.html#elementsDisks). * `kernel` - To launch the guest with a kernel residing on host filesystems. Equivalent to qemu `-kernel`. * `initrd` - To specify the initramfs/initrd to use for the guest. Equivalent to qemu `-initrd`. -* `random_hostname` - To create a domain name with with extra information on the end to prevent hostname conflicts. +* `random_hostname` - To create a domain name with extra information on the end to prevent hostname conflicts. * `cmd_line` - Arguments passed on to the guest kernel initramfs or initrd to use. Equivalent to qemu `-append`.