Class: EC2Launcher::DynamicHostnameGenerator

Inherits:
Object
  • Object
show all
Includes:
HostNames::HostNameGeneration
Defined in:
lib/ec2launcher/dynamic_hostname_generator.rb

Overview

Helper class to generate unique host names

Instance Method Summary collapse

Methods included from HostNames::HostNameGeneration

#generate_fqdn, #generate_short_name

Constructor Details

#initialize(prefix = nil, suffix = nil) ⇒ DynamicHostnameGenerator

Creates a new generator for dynamic host names.

Parameters:

  • prefix (String) (defaults to: nil)

    Optional prefix for the hostname.

  • suffix (String) (defaults to: nil)

    Optional suffix for the hostname.



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

def initialize(prefix = nil, suffix = nil)
  @prefix = prefix
  @suffix = suffix
  
  if prefix
    @prefix = prefix.slice(0, prefix.length - 1) if prefix =~ /[.]$/
  end

  if suffix
    @suffix = suffix.slice(1, suffix.length) if suffix =~ /^[.]/
    @suffix = @suffix.slice(0, @suffix.length - 1) if @suffix =~ /[.]$/
  end

  @prefix = nil if @prefix && @prefix.size < 1
  @suffix = nil if @suffix && @suffix.size < 1
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



45
46
47
48
49
50
51
52
53
54
# File 'lib/ec2launcher/dynamic_hostname_generator.rb', line 45

def generate_dynamic_hostname(instance_id)
  instance_id_name = (instance_id =~ /^i-/ ? instance_id.gsub(/^i-/, '') : instance_id)

  short_name = @prefix
  short_name ||= ""
  short_name += instance_id_name
  short_name += ".#{@suffix}" if @suffix

  short_name
end