class Qops::Environment

Public Class Methods

file_name() click to toggle source
# File lib/qops/environment.rb, line 9
def self.file_name
  'opsworks'
end
new(profile: nil, force_config: false, verbose: false) click to toggle source
# File lib/qops/environment.rb, line 38
def initialize(profile: nil, force_config: false, verbose: false)
  @_aws_config = { region: configuration.region }
  @_aws_config[:profile] = profile unless profile.nil?

  @_force_config = force_config
  @_verbose = verbose

  if profile.nil?
    opsworks.config.credentials.credentials
  else
    parsed_creds = Aws.shared_config.instance_variable_get('@parsed_credentials')[profile]
    role_credentials = Aws::AssumeRoleCredentials.new(
      role_arn: parsed_creds['role_arn'],
      role_session_name: profile
    )
    @_aws_config[:credentials] = role_credentials

    puts Rainbow("Using AWS profile #{profile}").bg(:black).green
  end

  Aws.config.update(@_aws_config)

  puts Rainbow('Forcing Qops to read the opsworks parameter strictly from yaml') if force_config
  %w[deploy_type region app_name].each do |v|
    fail "Please configure #{v} before continuing." unless option?(v)
  end
  fail 'Please configure stack_id or stack_name before continuing' unless option?('stack_id') || option?('stack_name')
end
notifiers() click to toggle source
# File lib/qops/environment.rb, line 13
def self.notifiers
  return @_notifiers unless @_notifiers.nil?
  if File.exist?('config/quandl/slack.yml')
    @_notifiers ||= Quandl::Slack.autogenerate_notifiers
  else
    @_notifiers = false
    print_with_colour('Slack notifications disabled. Could not find slack configuration at: config/quandl/slack.yml', :warning)
  end
rescue NoMethodError => e
  print_with_colour("Slack notifications disabled due to an error. #{e}", :warning)
end
print_with_colour(message, level = :normal) click to toggle source

Public Instance Methods

application_id(options = {}) click to toggle source
# File lib/qops/environment.rb, line 107
def application_id(options = {})
  return configuration.application_id if force_config?
  apps(options).first.app_id
end
apps(_options = {}) click to toggle source
# File lib/qops/environment.rb, line 103
def apps(_options = {})
  opsworks.describe_apps(stack_id: stack_id).apps
end
autoscale_type() click to toggle source
# File lib/qops/environment.rb, line 128
def autoscale_type
  configuration.autoscale_type || nil
end
chef_version(options = {}) click to toggle source
# File lib/qops/environment.rb, line 99
def chef_version(options = {})
  stack(options).configuration_manager.version.to_f
end
clean_commands_to_ignore() click to toggle source
# File lib/qops/environment.rb, line 142
def clean_commands_to_ignore
  configuration.clean_commands_to_ignore.present? ? configuration.clean_commands_to_ignore : %w[update_custom_cookbooks update_agent configure shutdown]
end
command_log_lines() click to toggle source
# File lib/qops/environment.rb, line 116
def command_log_lines
  configuration.command_log_lines || 100
end
cookbook_json() click to toggle source
# File lib/qops/environment.rb, line 162
def cookbook_json
  configuration.cookbook_json || 'custom.json'
end
deploy_type() click to toggle source
# File lib/qops/environment.rb, line 112
def deploy_type
  configuration.deploy_type.to_s
end
ebs_optimize() click to toggle source
# File lib/qops/environment.rb, line 190
def ebs_optimize
  !configuration.ebs_optimize.nil? ? configuration.ebs_optimize : !deploy_type !~ /production/
end
ec2() click to toggle source
# File lib/qops/environment.rb, line 154
def ec2
  @_ec2_client ||= Aws::EC2::Client.new(**@_aws_config)
end
elb() click to toggle source
# File lib/qops/environment.rb, line 158
def elb
  @_elb_client ||= Aws::ElasticLoadBalancing::Client.new(**@_aws_config)
end
file_name() click to toggle source
# File lib/qops/environment.rb, line 146
def file_name
  self.class.file_name
end
force_config?() click to toggle source
# File lib/qops/environment.rb, line 174
def force_config?
  @_force_config
end
hostname_prefix() click to toggle source
# File lib/qops/environment.rb, line 186
def hostname_prefix
  configuration.hostname_prefix || ''
end
layer_id(_options = {}) click to toggle source
# File lib/qops/environment.rb, line 91
def layer_id(_options = {})
  return configuration.layer_id if force_config?
  name = configuration.layer_name
  verbose_output("Searching for layer : #{name}")
  layer = layers.find { |l| l.name.match(/#{name}/i) }
  layer.layer_id
end
layers(_options = {}) click to toggle source
# File lib/qops/environment.rb, line 87
def layers(_options = {})
  opsworks.describe_layers(stack_id: stack_id).layers
end
max_instance_duration() click to toggle source

Default 1 days

# File lib/qops/environment.rb, line 138
def max_instance_duration
  configuration.max_instance_duration || 86_400
end
opsworks() click to toggle source
# File lib/qops/environment.rb, line 150
def opsworks
  @_opsworks_client ||= Aws::OpsWorks::Client.new(**@_aws_config)
end
opsworks_os(options = {}) click to toggle source
# File lib/qops/environment.rb, line 132
def opsworks_os(options = {})
  return configuration.os if @_force_config
  stack(options).default_os
end
option?(key) click to toggle source
# File lib/qops/environment.rb, line 178
def option?(key)
  respond_to?(key.to_sym) || configuration.instance_variable_get(:@table).keys.include?(key.to_sym)
end
root_volume_size() click to toggle source
# File lib/qops/environment.rb, line 182
def root_volume_size
  configuration.root_volume_size || 30
end
stack(options = {}) click to toggle source
# File lib/qops/environment.rb, line 67
def stack(options = {})
  return @_stack if @_stack
  # find out if the config is using stack id or name
  key = search_key(options)
  value = options[key] || configuration.send(key)
  # aws uses the term 'name' to reference a stack name
  key = :name if key == :stack_name
  @_stack = search_stack(key, value)
end
stack_id(options = {}) click to toggle source
# File lib/qops/environment.rb, line 77
def stack_id(options = {})
  return configuration.stack_id if force_config?
  stack(options).stack_id
end
subnet(options = {}) click to toggle source
# File lib/qops/environment.rb, line 82
def subnet(options = {})
  return configuration.subnet if force_config?
  stack(options).default_subnet_id
end
verbose?() click to toggle source
# File lib/qops/environment.rb, line 166
def verbose?
  @_verbose
end
verbose_output(text) click to toggle source
# File lib/qops/environment.rb, line 170
def verbose_output(text)
  self.class.print_with_colour(text, :warning) if verbose?
end
wait_deploy() click to toggle source
# File lib/qops/environment.rb, line 124
def wait_deploy
  configuration.wait_deploy || 180
end
wait_iterations() click to toggle source
# File lib/qops/environment.rb, line 120
def wait_iterations
  configuration.wait_iterations || 600
end

Private Instance Methods

identity_from_config() click to toggle source
# File lib/qops/environment.rb, line 196
def identity_from_config
  configuration.stack_id ? :stack_id : :stack_name
end
method_missing(method_sym, *arguments, &block) click to toggle source
Calls superclass method
# File lib/qops/environment.rb, line 228
def method_missing(method_sym, *arguments, &block) # rubocop:disable Style/MethodMissing
  if configuration.respond_to?(method_sym)
    configuration.send(method_sym, *arguments, &block)
  else
    super
  end
end
search_key(options = {}) click to toggle source
# File lib/qops/environment.rb, line 200
def search_key(options = {})
  if !options[:name].nil?
    :name
  elsif !options[:stack_id].nil?
    :stack_id
  else
    id = identity_from_config
    self.class.print_with_colour("Using opsworks.yml config #{id}: #{configuration.send(id)}", :good)
    id
  end
end
search_stack(key, value) click to toggle source
# File lib/qops/environment.rb, line 212
def search_stack(key, value)
  verbose_output("Searching for stack : #{value}")

  stack = opsworks.describe_stacks.stacks.find do |s|
    verbose_output("Found stack: #{s.send(key)}")
    s.send(key) == value
  end

  unless stack
    self.class.print_with_colour("Could not find stack with #{key} = #{value}", :error)
    exit(-1)
  end

  stack
end