Sha256: 0e3f26faeba23615989d5f93a44f4af320b3d562fbf98cc43b9f3345a1f34618

Contents?: true

Size: 1.09 KB

Versions: 5

Compression:

Stored size: 1.09 KB

Contents

module Forger
  class Base
    # constants really only used by script classes
    BUILD_ROOT = "/tmp/forger/#{File.basename(Dir.pwd)}"
    SCRIPTS_INFO_PATH = "#{BUILD_ROOT}/data/scripts_info.txt"

    def initialize(options={})
      @options = options.clone
      @name = randomize(@options[:name])
      Forger.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:
    #
    #   forger create server --randomize
    #
    # instead of:
    #
    #   forger 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

5 entries across 5 versions & 1 rubygems

Version Path
forger-3.0.2 lib/forger/base.rb
forger-3.0.1 lib/forger/base.rb
forger-3.0.0 lib/forger/base.rb
forger-2.0.5 lib/forger/base.rb
forger-2.0.4 lib/forger/base.rb