class AutoElasticache::Elasticache

Constants

ENVIRONMENT_NAME_FILE
SERVERS_FILE

Attributes

config_endpoint[RW]

Public Class Methods

new() click to toggle source
# File lib/auto_elasticache/elasticache.rb, line 11
def initialize
  @config_endpoint = configuration_endpoint
end

Public Instance Methods

refresh_servers() click to toggle source
# File lib/auto_elasticache/elasticache.rb, line 25
def refresh_servers
  elasticache = Dalli::ElastiCache.new(@config_endpoint)
  File.open(SERVERS_FILE, 'w') {|f| f.write(elasticache.servers.join("\n")) }
  return elasticache.servers
end
servers() click to toggle source
# File lib/auto_elasticache/elasticache.rb, line 15
def servers
  if File.exists?(SERVERS_FILE)
    return File.read(SERVERS_FILE).split("\n")
  else
    elasticache = Dalli::ElastiCache.new(@config_endpoint)
    File.open(SERVERS_FILE, 'w') {|f| f.write(elasticache.servers.join("\n")) }
    return elasticache.servers
  end
end

Private Instance Methods

aws_config() click to toggle source
# File lib/auto_elasticache/elasticache.rb, line 64
def aws_config
  @aws_config ||= AWS.config(YAML.load_file("config/auto_elasticache.yml")[Rails.env])
end
configuration_endpoint() click to toggle source
# File lib/auto_elasticache/elasticache.rb, line 34
def configuration_endpoint
  aws_config

  # Find the stack
  cf = AWS::CloudFormation.new
  env_name = environment_name
  stack_name = cf.stacks.each {|s| break s.name if s.parameters["AWSEBEnvironmentName"] == env_name}

  # Find the ElastiCache resource physical id
  ec_id = cf.stacks[stack_name].resources.each {|r| break r.physical_resource_id if r.resource_type == "AWS::ElastiCache::CacheCluster"}

  # Find the ElastiCache configuration endpoint
  ec = AWS::ElastiCache.new
  config_endpoint = ec.client.describe_cache_clusters(:cache_cluster_id => ec_id)[:cache_clusters].first[:configuration_endpoint]
  "#{config_endpoint[:address]}:#{config_endpoint[:port]}"
end
environment_name() click to toggle source
# File lib/auto_elasticache/elasticache.rb, line 51
def environment_name
  if File.exists?(ENVIRONMENT_NAME_FILE)
    return File.read(ENVIRONMENT_NAME_FILE)
  else
    instance_id = `/opt/aws/bin/ec2-metadata -i | awk '{print $2}'`.strip
    aws_config
    ec2 = AWS::EC2.new
    env_name = ec2.instances[instance_id].tags["elasticbeanstalk:environment-name"]
    File.open(ENVIRONMENT_NAME_FILE, 'w') {|f| f.write(env_name) }
    env_name
  end
end