Module: EC2Launcher::HostNames::HostNameGeneration

Included in:
DynamicHostnameGenerator, EC2Launcher::HostnameGenerator
Defined in:
lib/ec2launcher/hostnames/host_name_generation.rb

Instance Method Summary collapse

Instance Method Details

#generate_fqdn(short_hostname, domain_name = nil) ⇒ Object

Given a short host name and domain name, generate a Fully Qualified Domain Name.

Parameters:

  • short_hostname (String)

    Shortened host name.

  • domain_name (String) (defaults to: nil)

    Optional domain name ie ‘example.com’

Raises:

  • (ArgumentError)


11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/ec2launcher/hostnames/host_name_generation.rb', line 11

def generate_fqdn(short_hostname, domain_name = nil)
  raise ArgumentError, 'short_hostname is invalid' if short_hostname.nil?

  hostname = short_hostname
  unless domain_name.nil?
    unless hostname =~ /[.]$/ || domain_name =~ /^[.]/
      hostname += "."
    end
    hostname += domain_name
  end

  hostname
end

#generate_short_name(long_name, domain_name = nil) ⇒ Object

Given a FQDN and a domain name, produce a shortened version of the host name without the domain.

Parameters:

  • long_name (String)

    FQDN ie ‘foo1.prod.example.com’

  • domain_name (String) (defaults to: nil)

    Optional domain name ie ‘example.com’

Raises:

  • (ArgumentError)


30
31
32
33
34
35
36
37
38
39
# File 'lib/ec2launcher/hostnames/host_name_generation.rb', line 30

def generate_short_name(long_name, domain_name = nil)
  raise ArgumentError, 'long_name is invalid' if long_name.nil?

  short_hostname = long_name
  unless domain_name.nil?
    short_hostname = long_name.gsub(/#{domain_name}/, '')
    short_hostname = short_hostname.slice(0, short_hostname.length - 1) if short_hostname =~ /[.]$/
  end
  short_hostname
end