Class: Qops::Environment

Inherits:
Object
  • Object
show all
Includes:
Quandl::Configurable
Defined in:
lib/qops/environment.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(profile: nil, force_config: false, verbose: false) ⇒ Environment

Returns a new instance of Environment.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# 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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_sym, *arguments, &block) ⇒ Object (private)

rubocop:disable Style/MethodMissing



228
229
230
231
232
233
234
# 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

Class Method Details

.file_nameObject



9
10
11
# File 'lib/qops/environment.rb', line 9

def self.file_name
  'opsworks'
end

.notifiersObject



13
14
15
16
17
18
19
20
21
22
23
# 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


25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/qops/environment.rb', line 25

def self.print_with_colour(message, level = :normal)
  case level
  when :error
    puts Rainbow(message).bg(:black).red
  when :warning
    puts Rainbow(message).bg(:black).yellow
  when :good
    puts Rainbow(message).bg(:black).green
  else
    puts message
  end
end

Instance Method Details

#application_id(options = {}) ⇒ Object



107
108
109
110
# 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 = {}) ⇒ Object



103
104
105
# File 'lib/qops/environment.rb', line 103

def apps(_options = {})
  opsworks.describe_apps(stack_id: stack_id).apps
end

#autoscale_typeObject



128
129
130
# File 'lib/qops/environment.rb', line 128

def autoscale_type
  configuration.autoscale_type || nil
end

#chef_version(options = {}) ⇒ Object



99
100
101
# File 'lib/qops/environment.rb', line 99

def chef_version(options = {})
  stack(options).configuration_manager.version.to_f
end

#clean_commands_to_ignoreObject



142
143
144
# 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_linesObject



116
117
118
# File 'lib/qops/environment.rb', line 116

def command_log_lines
  configuration.command_log_lines || 100
end

#cookbook_jsonObject



162
163
164
# File 'lib/qops/environment.rb', line 162

def cookbook_json
  configuration.cookbook_json || 'custom.json'
end

#deploy_typeObject



112
113
114
# File 'lib/qops/environment.rb', line 112

def deploy_type
  configuration.deploy_type.to_s
end

#ebs_optimizeObject



190
191
192
# File 'lib/qops/environment.rb', line 190

def ebs_optimize
  !configuration.ebs_optimize.nil? ? configuration.ebs_optimize : !deploy_type !~ /production/
end

#ec2Object



154
155
156
# File 'lib/qops/environment.rb', line 154

def ec2
  @_ec2_client ||= Aws::EC2::Client.new(**@_aws_config)
end

#elbObject



158
159
160
# File 'lib/qops/environment.rb', line 158

def elb
  @_elb_client ||= Aws::ElasticLoadBalancing::Client.new(**@_aws_config)
end

#file_nameObject



146
147
148
# File 'lib/qops/environment.rb', line 146

def file_name
  self.class.file_name
end

#force_config?Boolean

Returns:

  • (Boolean)


174
175
176
# File 'lib/qops/environment.rb', line 174

def force_config?
  @_force_config
end

#hostname_prefixObject



186
187
188
# File 'lib/qops/environment.rb', line 186

def hostname_prefix
  configuration.hostname_prefix || ''
end

#layer_id(_options = {}) ⇒ Object



91
92
93
94
95
96
97
# 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 = {}) ⇒ Object



87
88
89
# File 'lib/qops/environment.rb', line 87

def layers(_options = {})
  opsworks.describe_layers(stack_id: stack_id).layers
end

#max_instance_durationObject

Default 1 days



138
139
140
# File 'lib/qops/environment.rb', line 138

def max_instance_duration
  configuration.max_instance_duration || 86_400
end

#opsworksObject



150
151
152
# File 'lib/qops/environment.rb', line 150

def opsworks
  @_opsworks_client ||= Aws::OpsWorks::Client.new(**@_aws_config)
end

#opsworks_os(options = {}) ⇒ Object



132
133
134
135
# File 'lib/qops/environment.rb', line 132

def opsworks_os(options = {})
  return configuration.os if @_force_config
  stack(options).default_os
end

#option?(key) ⇒ Boolean

Returns:

  • (Boolean)


178
179
180
# 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_sizeObject



182
183
184
# File 'lib/qops/environment.rb', line 182

def root_volume_size
  configuration.root_volume_size || 30
end

#stack(options = {}) ⇒ Object



67
68
69
70
71
72
73
74
75
# 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 = {}) ⇒ Object



77
78
79
80
# 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 = {}) ⇒ Object



82
83
84
85
# File 'lib/qops/environment.rb', line 82

def subnet(options = {})
  return configuration.subnet if force_config?
  stack(options).default_subnet_id
end

#verbose?Boolean

Returns:

  • (Boolean)


166
167
168
# File 'lib/qops/environment.rb', line 166

def verbose?
  @_verbose
end

#verbose_output(text) ⇒ Object



170
171
172
# File 'lib/qops/environment.rb', line 170

def verbose_output(text)
  self.class.print_with_colour(text, :warning) if verbose?
end

#wait_deployObject



124
125
126
# File 'lib/qops/environment.rb', line 124

def wait_deploy
  configuration.wait_deploy || 180
end

#wait_iterationsObject



120
121
122
# File 'lib/qops/environment.rb', line 120

def wait_iterations
  configuration.wait_iterations || 600
end