class Rebi::ConfigEnvironment

Constants

DEFAULT_CONFIG
DEFAULT_IAM_INSTANCE_PROFILE
NAMESPACE
UPDATEABLE_NS

Attributes

cfg_file[R]
cname_prefix[R]
description[R]
ebextensions[R]
env_file[R]
env_name[R]
environment_variables[R]
instance_num[R]
instance_profile[R]
instance_type[R]
key_name[R]
name[R]
option_settings[R]
options[R]
raw_conf[R]
solution_stack_name[R]
stage[R]
tier[R]

Public Class Methods

new(stage, env_name, env_conf={}) click to toggle source
# File lib/rebi/config_environment.rb, line 55
def initialize stage, env_name, env_conf={}
  @raw_conf = env_conf.with_indifferent_access
  @stage = stage.to_sym
  @env_name = env_name.to_sym
end

Public Instance Methods

app_name() click to toggle source
# File lib/rebi/config_environment.rb, line 70
def app_name
  Rebi.config.app_name
end
cfg() click to toggle source
# File lib/rebi/config_environment.rb, line 162
def cfg
  begin
    return nil if cfg_file.blank?
    return (@cfg ||= YAML.load(ERB.new(IO.read(cfg_file)).result).with_indifferent_access)
  rescue Errno::ENOENT
    raise Rebi::Error::ConfigInvalid.new("cfg_file: #{cfg_file}")
  end
end
default_instance_profile?() click to toggle source
# File lib/rebi/config_environment.rb, line 136
def default_instance_profile?
  self.instance_profile == DEFAULT_IAM_INSTANCE_PROFILE
end
dockerrun() click to toggle source
# File lib/rebi/config_environment.rb, line 158
def dockerrun
  raw_conf[:dockerrun]
end
dotenv() click to toggle source
# File lib/rebi/config_environment.rb, line 198
def dotenv
  env_file.present? ? Dotenv.load(env_file).with_indifferent_access : {}
end
ebignore() click to toggle source
# File lib/rebi/config_environment.rb, line 189
def ebignore
  return @ebignore ||= raw_conf[:ebignore] || ".ebignore"
end
get_opt(namespace, opt_name=nil) click to toggle source
# File lib/rebi/config_environment.rb, line 206
def get_opt namespace, opt_name=nil
  has_value_by_keys(option_settings, namespace, opt_name)
end
get_raw_opt(namespace, opt_name=nil) click to toggle source
# File lib/rebi/config_environment.rb, line 210
def get_raw_opt namespace, opt_name=nil
  has_value_by_keys(raw_conf[:option_settings], namespace, opt_name)
end
hooks() click to toggle source
# File lib/rebi/config_environment.rb, line 254
def hooks
  return @hooks if @hooks

  @hooks = {
    pre: [],
    post: [],
  }.with_indifferent_access

  [:pre, :post].each do |type|
    next unless h = raw_conf[:hooks] && raw_conf[:hooks][type]
    @hooks[type] = h.is_a?(Array) ? h : [h]
  end

  return @hooks
end
name=(n) click to toggle source
# File lib/rebi/config_environment.rb, line 66
def name=n
  raw_conf[:name] ||= @name = n
end
ns(key=nil) click to toggle source
# File lib/rebi/config_environment.rb, line 250
def ns key=nil
  key.present? ? NAMESPACE[key.to_sym] : NAMESPACE
end
opts_array(opts=option_settings) click to toggle source
# File lib/rebi/config_environment.rb, line 214
def opts_array opts=option_settings
  res = []
  opts.each do |namespace, v|
    namespace, resource_name = namespace.split(".").reverse
    v.each do |option_name, value|
      res << {
        resource_name: resource_name,
        namespace: namespace,
        option_name: option_name,
        value: value.to_s,
      }.with_indifferent_access
    end
  end
  return res
end
platform_arn() click to toggle source
# File lib/rebi/config_environment.rb, line 175
def platform_arn
  cfg && cfg[:Platform] && cfg[:Platform][:PlatformArn]
end
post_hooks() click to toggle source
# File lib/rebi/config_environment.rb, line 274
def post_hooks
  hooks[:post]
end
pre_hooks() click to toggle source
# File lib/rebi/config_environment.rb, line 270
def pre_hooks
  hooks[:pre]
end
raw_environment_variables() click to toggle source
# File lib/rebi/config_environment.rb, line 194
def raw_environment_variables
  raw_conf[:environment_variables] || {}
end
web?() click to toggle source
# File lib/rebi/config_environment.rb, line 113
def web?
  !worker?
end
worker?() click to toggle source
# File lib/rebi/config_environment.rb, line 109
def worker?
  tier[:name] == "Worker" ? true : false
end

Private Instance Methods

has_value_by_keys(hash, *keys) click to toggle source
# File lib/rebi/config_environment.rb, line 280
def has_value_by_keys(hash, *keys)
  if keys.empty? || hash.blank?
    return hash
  else
    return hash unless k = keys.shift
    return hash[k] && has_value_by_keys(hash[k], *keys)
  end
end
set_opt_default(opt) click to toggle source
# File lib/rebi/config_environment.rb, line 356
def set_opt_default opt

  opt[ns[:healthreporting]].reverse_merge!({
    SystemType: 'enhanced',
  }.with_indifferent_access)

  opt[ns[:eb_command]].reverse_merge!({
    BatchSize: "50",
    BatchSizeType: "Percentage",
  }.with_indifferent_access)

  unless worker?
    opt[ns[:elb_policies]].reverse_merge!({
      ConnectionDrainingEnabled: true
    }.with_indifferent_access)

    opt[NAMESPACE[:elb_health]].reverse_merge!({
      Interval: 30
    }.with_indifferent_access)

    opt[NAMESPACE[:elb_loadbalancer]].reverse_merge!({
      CrossZone: true
    }.with_indifferent_access)
  end
  return opt
end
set_opt_env_var(opt) click to toggle source
# File lib/rebi/config_environment.rb, line 351
def set_opt_env_var opt
  opt[ns[:app_env]].merge!(dotenv.merge(raw_environment_variables))
  return opt
end
set_opt_instance_num(opt) click to toggle source
# File lib/rebi/config_environment.rb, line 334
def set_opt_instance_num opt
  max = min = 1
  if raw_conf[:instance_num].present?
    min = raw_conf[:instance_num][:min]
    max = raw_conf[:instance_num][:max]
  elsif mi = get_raw_opt(ns[:autoscaling_asg], :MinSize) \
        || ma = get_raw_opt(ns[:autoscaling_asg], :MaxSize)
        min = mi if mi
        max = ma if ma
  end
  opt[ns[:autoscaling_asg]].merge!({
    MaxSize: max,
    MinSize: min,
  }.with_indifferent_access)
  return opt
end
set_opt_instance_profile(opt) click to toggle source
# File lib/rebi/config_environment.rb, line 305
def set_opt_instance_profile opt
  s_role = if raw_conf.key?(:instance_profile)
    raw_conf[:instance_profile]
  elsif role = get_raw_opt(ns[:autoscaling_launch], :IamInstanceProfile)
    role
  else
    DEFAULT_IAM_INSTANCE_PROFILE
  end

  if s_role.present?
    opt[ns[:autoscaling_launch]].merge!({
      IamInstanceProfile: s_role,
    }.with_indifferent_access)
  end
  return opt
end
set_opt_instance_type(opt) click to toggle source
# File lib/rebi/config_environment.rb, line 322
def set_opt_instance_type opt
  itype = raw_conf[:instance_type] \
          || get_raw_opt(ns[:autoscaling_launch], :InstanceType) \
          || "t2.small"
  if itype.present?
    opt[ns[:autoscaling_launch]].merge!({
      InstanceType: itype
    }.with_indifferent_access)
  end
  return opt
end
set_opt_keyname(opt) click to toggle source
# File lib/rebi/config_environment.rb, line 289
def set_opt_keyname opt
  k = if raw_conf.key?(:key_name)
    raw_conf[:key_name]
  elsif get_raw_opt(ns[:autoscaling_launch], :EC2KeyName)
    get_raw_opt(ns[:autoscaling_launch], :EC2KeyName)
  else
    nil
  end
  if k.present?
    opt[ns[:autoscaling_launch]].merge!({
      EC2KeyName: k
    }.with_indifferent_access)
  end
  return opt
end