Class: EC2Launcher::HostnameGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/ec2launcher/hostname_generator.rb

Overview

Helper class to generate sequential, numbered host names

Instance Method Summary collapse

Constructor Details

#initialize(ec2, environment, application) ⇒ HostnameGenerator

Returns a new instance of HostnameGenerator.

Parameters:

  • ec2 (AWS::EC2)

    EC2 object used to query for existing instances

  • environment (EC2Launcher::Environment)

    Environment to use for generating host names

  • application (EC2Launcher::Application)

    Application to use for generating hostn ames



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/ec2launcher/hostname_generator.rb', line 14

def initialize(ec2, environment, application)
  @ec2 = ec2
  @server_name_cache = nil

  @prefix = application.basename
  @prefix ||= application.name

  @env_suffix = environment.short_name
  @env_suffix ||= environment.name
  
  @suffix = @env_suffix
  unless application.name_suffix.nil?
    @suffix = "#{application.name_suffix}.#{@env_suffix}"
  end

  @host_number_regex = Regexp.new("#{@prefix}(\\d+)[.]#{@suffix.gsub(/[.]/, "[.]")}.*")

  # Load and cache instance names
  load_instances(@prefix, @suffix)
end

Instance Method Details

#generate_hostnameObject

Generates a new host name and automatically caches it so that future requests don’t use the same name.



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/ec2launcher/hostname_generator.rb', line 37

def generate_hostname()
  # Find next host number
  host_number = get_next_host_number()

  # Build short host name
  short_name = "#{@prefix}#{host_number}.#{@suffix}"

  # Cache the new hostname
  @server_name_cache << short_name

  short_name
end

#generate_long_name(short_hostname, domain_name = nil) ⇒ Object



50
51
52
53
54
55
56
57
# File 'lib/ec2launcher/hostname_generator.rb', line 50

def generate_long_name(short_hostname, domain_name = nil)
  hostname = short_hostname
  unless domain_name.nil?
    hostname += ".#{domain_name}"
  end

  hostname
end

#generate_short_name(long_name, domain_name = nil) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/ec2launcher/hostname_generator.rb', line 59

def generate_short_name(long_name, domain_name = nil)
  short_hostname = long_name
  unless domain_name.nil?
    short_hostname = long_name.gsub(/.#{domain_name}/, '')
  end
  short_hostname
end