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
66
# 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::OpsWorks::Client.new(credentials: role_credentials).describe_stacks.stacks.each { |s| puts s }
    @_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



229
230
231
232
233
234
235
# File 'lib/qops/environment.rb', line 229

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



108
109
110
111
# File 'lib/qops/environment.rb', line 108

def application_id(options = {})
  return configuration.application_id if force_config?
  apps(options).first.app_id
end

#apps(_options = {}) ⇒ Object



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

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

#autoscale_typeObject



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

def autoscale_type
  configuration.autoscale_type || nil
end

#chef_version(options = {}) ⇒ Object



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

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

#clean_commands_to_ignoreObject



143
144
145
# File 'lib/qops/environment.rb', line 143

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



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

def command_log_lines
  configuration.command_log_lines || 100
end

#cookbook_jsonObject



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

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

#deploy_typeObject



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

def deploy_type
  configuration.deploy_type.to_s
end

#ebs_optimizeObject



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

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

#ec2Object



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

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

#elbObject



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

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

#file_nameObject



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

def file_name
  self.class.file_name
end

#force_config?Boolean

Returns:

  • (Boolean)


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

def force_config?
  @_force_config
end

#hostname_prefixObject



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

def hostname_prefix
  configuration.hostname_prefix || ''
end

#layer_id(_options = {}) ⇒ Object



92
93
94
95
96
97
98
# File 'lib/qops/environment.rb', line 92

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



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

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

#max_instance_durationObject

Default 1 days



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

def max_instance_duration
  configuration.max_instance_duration || 86_400
end

#opsworksObject



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

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

#opsworks_os(options = {}) ⇒ Object



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

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

#option?(key) ⇒ Boolean

Returns:

  • (Boolean)


179
180
181
# File 'lib/qops/environment.rb', line 179

def option?(key)
  respond_to?(key.to_sym) || configuration.instance_variable_get(:@table).keys.include?(key.to_sym)
end

#root_volume_sizeObject



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

def root_volume_size
  configuration.root_volume_size || 30
end

#stack(options = {}) ⇒ Object



68
69
70
71
72
73
74
75
76
# File 'lib/qops/environment.rb', line 68

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



78
79
80
81
# File 'lib/qops/environment.rb', line 78

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

#subnet(options = {}) ⇒ Object



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

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

#verbose?Boolean

Returns:

  • (Boolean)


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

def verbose?
  @_verbose
end

#verbose_output(text) ⇒ Object



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

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

#wait_deployObject



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

def wait_deploy
  configuration.wait_deploy || 180
end

#wait_iterationsObject



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

def wait_iterations
  configuration.wait_iterations || 600
end