Sha256: 98e50e8d206f65b558aae7d17157f605daa8aec445c64b4e726ef44abd775a3e

Contents?: true

Size: 948 Bytes

Versions: 1

Compression:

Stored size: 948 Bytes

Contents

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

    # 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
    #
    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

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
balancer-0.2.0 lib/balancer/base.rb