Class: VagrantPlugins::ProviderLibvirt::Action::SetNameOfDomain

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-libvirt/action/set_name_of_domain.rb

Overview

Setup name for domain and domain volumes.

Instance Method Summary collapse

Constructor Details

#initialize(app, _env) ⇒ SetNameOfDomain

Returns a new instance of SetNameOfDomain.



9
10
11
12
# File 'lib/vagrant-libvirt/action/set_name_of_domain.rb', line 9

def initialize(app, _env)
  @logger = Log4r::Logger.new('vagrant_libvirt::action::set_name_of_domain')
  @app    = app
end

Instance Method Details

#build_domain_name(env) ⇒ String

build domain name random_hostname option avoids ‘domain about to create is already taken` parsable and sortable by epoch time

Examples:

development-centos-6-chef-11_1404488971_3b7a569e2fd7c554b852

Returns:

  • (String)

    Libvirt domain name



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/vagrant-libvirt/action/set_name_of_domain.rb', line 44

def build_domain_name(env)
  config = env[:machine].provider_config
  domain_name =
    if config.default_prefix.nil?
      env[:root_path].basename.to_s.dup.concat('_')
    elsif config.default_prefix.empty?
      # don't have any prefix, not even "_"
      String.new
    else
      config.default_prefix.to_s.dup
    end
  domain_name << env[:machine].name.to_s
  domain_name.gsub!(/[^-a-z0-9_\.]/i, '')
  domain_name << "_#{Time.now.utc.to_i}_#{SecureRandom.hex(10)}" if config.random_hostname
  domain_name
end

#call(env) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/vagrant-libvirt/action/set_name_of_domain.rb', line 14

def call(env)
  env[:domain_name] = build_domain_name(env)

  begin
    @logger.info("Looking for domain #{env[:domain_name]}")
    # Check if the domain name is not already taken

    domain = env[:machine].provider.driver.connection.servers.all(
      name: env[:domain_name]
    )
  rescue Libvirt::RetrieveError => e
    @logger.info(e.to_s)
    domain = nil
  end

  unless domain.nil?
    raise ProviderLibvirt::Errors::DomainNameExists,
          domain_name: env[:domain_name]
  end

  @app.call(env)
end