refactor set_domain_name to avoid duplicate names when running multiple guests.

This commit is contained in:
Bradley Smith
2014-07-04 10:47:29 -06:00
parent 53612a439e
commit 0e9458db6c
3 changed files with 34 additions and 19 deletions

View File

@@ -1,3 +1,4 @@
require 'securerandom'
module VagrantPlugins module VagrantPlugins
module ProviderLibvirt module ProviderLibvirt
module Action module Action
@@ -10,16 +11,7 @@ module VagrantPlugins
end end
def call(env) def call(env)
require 'securerandom' env[:domain_name] = build_domain_name(env)
config = env[:machine].provider_config
if config.default_prefix.nil?
env[:domain_name] = env[:root_path].basename.to_s.dup
else
env[:domain_name] = config.default_prefix.to_s
end
env[:domain_name].gsub!(/[^-a-z0-9_]/i, '')
env[:domain_name] << '_'
env[:domain_name] << env[:machine].name.to_s
begin begin
@logger.info("Looking for domain #{env[:domain_name]} through list #{env[:libvirt_compute].servers.all}") @logger.info("Looking for domain #{env[:domain_name]} through list #{env[:libvirt_compute].servers.all}")
@@ -34,13 +26,32 @@ module VagrantPlugins
@logger.info("Looking for domain #{env[:domain_name]}") @logger.info("Looking for domain #{env[:domain_name]}")
if domain != nil unless domain.nil?
raise ProviderLibvirt::Errors::DomainNameExists, raise ProviderLibvirt::Errors::DomainNameExists,
:domain_name => env[:domain_name] :domain_name => env[:domain_name]
end end
@app.call(env) @app.call(env)
end end
# build domain name
# avoids `domain about to create is already taken`
# @example
# 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?
env[:root_path].basename.to_s.dup
else
config.default_prefix.to_s
end
domain_name.gsub!(/[^-a-z0-9_]/i, '')
domain_name << "_#{postfix}"
end
end end
end end

View File

@@ -1,5 +1,5 @@
module VagrantPlugins module VagrantPlugins
module ProviderLibvirt module ProviderLibvirt
VERSION = '0.0.18' VERSION = '0.0.19'
end end
end end

View File

@@ -16,6 +16,10 @@ Gem::Specification.new do |gem|
gem.require_paths = ['lib'] gem.require_paths = ['lib']
gem.version = VagrantPlugins::ProviderLibvirt::VERSION gem.version = VagrantPlugins::ProviderLibvirt::VERSION
gem.add_development_dependency "rspec-core", "~> 2.12.2"
gem.add_development_dependency "rspec-expectations", "~> 2.12.1"
gem.add_development_dependency "rspec-mocks", "~> 2.12.1"
gem.add_runtime_dependency 'fog', '~> 1.15' gem.add_runtime_dependency 'fog', '~> 1.15'
gem.add_runtime_dependency 'ruby-libvirt', '~> 0.4.0' gem.add_runtime_dependency 'ruby-libvirt', '~> 0.4.0'
gem.add_runtime_dependency 'nokogiri', '~> 1.5.9' gem.add_runtime_dependency 'nokogiri', '~> 1.5.9'