Class: EC2Launcher::HostnameGenerator

Inherits:
Object
  • Object
show all
Includes:
BackoffRunner, EC2Launcher::HostNames::HostNameGeneration
Defined in:
lib/ec2launcher/hostname_generator.rb

Overview

Helper class to generate sequential, numbered host names

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from EC2Launcher::HostNames::HostNameGeneration

#generate_fqdn, #generate_short_name

Methods included from BackoffRunner

#run_with_backoff, #test_with_backoff

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 host names



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/ec2launcher/hostname_generator.rb', line 24

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)

  @dynamic_generator = EC2Launcher::DynamicHostnameGenerator.new(nil, "#{@prefix}.#{@suffix}")
end

Instance Attribute Details

#prefixObject (readonly)

Returns the value of attribute prefix.



17
18
19
# File 'lib/ec2launcher/hostname_generator.rb', line 17

def prefix
  @prefix
end

#suffixObject (readonly)

Returns the value of attribute suffix.



18
19
20
# File 'lib/ec2launcher/hostname_generator.rb', line 18

def suffix
  @suffix
end

Instance Method Details

#generate_dynamic_hostname(instance_id) ⇒ Object

Given an instance id, generates a dynamic short hostname typically in the form:

prefix + INSTANCE ID + application + environment

Examples:

9803da2.web.prod (no prefix)   
app-d709aa2ab.server.dev (prefix = "app-")

Parameters:

  • instance_id (String)

    AWS EC2 instance id



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

def generate_dynamic_hostname(instance_id)
  short_name = @dynamic_generator.generate_dynamic_hostname(instance_id)
  
  # Cache the new hostname
  @server_name_cache << short_name

  short_name
end

#generate_hostnameObject

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



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/ec2launcher/hostname_generator.rb', line 68

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