Class: Balancer::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/balancer/base.rb

Direct Known Subclasses

Profile

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Base

Returns a new instance of Base.



3
4
5
6
7
# File 'lib/balancer/base.rb', line 3

def initialize(options={})
  @options = options.clone
  @name = randomize(@options[:name])
  Balancer.validate_in_project!
end

Instance Method Details

#derandomize(name) ⇒ Object

Strip the random string at end of the ec2 instance name



29
30
31
32
33
34
35
# File 'lib/balancer/base.rb', line 29

def derandomize(name)
  if @options[:randomize]
    name.sub(/-(\w{3})$/,'') # strip the random part at the end
  else
    name
  end
end

#randomize(name) ⇒ Object

Appends a short random string at the end of the ec2 instance name. Later we will strip this same random string from the name. Very makes it convenient. We can just type:

balancer create server --randomize

instead of:

balancer create server-123 --profile server


19
20
21
22
23
24
25
26
# File 'lib/balancer/base.rb', line 19

def randomize(name)
  if @options[:randomize]
    random = (0...3).map { (65 + rand(26)).chr }.join.downcase # Ex: jhx
    [name, random].join('-')
  else
    name
  end
end