Class: Tapjoy::AutoscalingBootstrap::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/tapjoy/autoscaling_bootstrap.rb

Overview

Base class for generic methods used throughout the gem

Instance Method Summary collapse

Instance Method Details

#aws_wait(tries) ⇒ Object

Exponential backup



196
197
198
199
200
# File 'lib/tapjoy/autoscaling_bootstrap.rb', line 196

def aws_wait(tries)
  backoff_sleep = 2 ** tries
  puts "Sleeping for #{backoff_sleep}..."
  sleep backoff_sleep
end

#check_as_clobber(create_as_group:, clobber_as:, **unused_values) ⇒ Object

Check autoscaling clobber



110
111
112
# File 'lib/tapjoy/autoscaling_bootstrap.rb', line 110

def check_as_clobber(create_as_group:, clobber_as:, **unused_values)
  create_as_group && Tapjoy::AutoscalingBootstrap.group.exists && !clobber_as
end

#check_clobber(opts, config) ⇒ Object

Check if we allow clobbering and need to clobber



104
105
106
107
# File 'lib/tapjoy/autoscaling_bootstrap.rb', line 104

def check_clobber(opts, config)
  fail Tapjoy::AutoscalingBootstrap::Errors::ClobberRequired if check_as_clobber(**opts, **config)
  puts "We don't need to clobber"
end

#check_yaml_version(config) ⇒ Object

See what version of the yaml we are running



115
116
117
118
# File 'lib/tapjoy/autoscaling_bootstrap.rb', line 115

def check_yaml_version(config)
  # If we don't have one specified assume v1
  config[:version] ? config[:version] : "1.0.0"
end

#configure_environment(opts) ⇒ Object

configure environment



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/tapjoy/autoscaling_bootstrap.rb', line 163

def configure_environment(opts)
  filename = opts[:filename]
  facet_file    = filename
  config_dir    = File.expand_path('../..', facet_file)
  userdata_dir  = "#{File.expand_path('../../..', facet_file)}/userdata"

  common_path   = File.join(config_dir, 'common')
  defaults_hash = self.load_yaml(File.join(common_path, 'defaults.yaml'))
  facet_hash    = self.load_yaml(facet_file)
  env = opts[:env] || facet_hash[:environment] || defaults_hash[:environment]
  Tapjoy::AutoscalingBootstrap.valid_env?(common_path, env)
  env_hash      = self.load_yaml(File.join(common_path, "#{env}.yaml"))

  new_config = defaults_hash.merge!(env_hash).merge(facet_hash)

  # TODO: Only run this part of the configuration and return aws_env and user_data for the older yaml file verison

  new_config[:config_dir] = config_dir
  new_config[:instance_ids] = opts[:instance_ids] if opts.key?(:instance_ids)
  aws_env = self.get_security_groups(common_path, env, new_config[:group])
  new_config.merge!(aws_env)

  new_config[:autoscale] = false unless new_config[:scaling_type].eql?('dynamic')
  new_config[:tags] << {Name: new_config[:name]}
  Tapjoy::AutoscalingBootstrap.scaler_name = "#{new_config[:name]}-group"
  Tapjoy::AutoscalingBootstrap.config_name = "#{new_config[:name]}-config"
  user_data = self.generate_user_data(userdata_dir,
    new_config[:bootstrap_script], new_config)

  [new_config, aws_env, user_data]
end

#confirm_config(keypair:, zones:, security_groups:, instance_type:, image_id:, iam_instance_profile:, prompt:, use_vpc: use_vpc, vpc_subnets: nil, has_elb: has_elb, config:, termination_policies:, **unused_values) ⇒ Object

Confirm config settings before running autoscaling code



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/tapjoy/autoscaling_bootstrap.rb', line 137

def confirm_config(keypair:, zones:, security_groups:, instance_type:,
  image_id:, iam_instance_profile:, prompt:, use_vpc: use_vpc,
  vpc_subnets: nil, has_elb: has_elb, config:, termination_policies:,
  **unused_values)

  puts '  Preparing to configure the following autoscaling group:'
  puts "  Launch Config:        #{Tapjoy::AutoscalingBootstrap.config_name}"
  puts "  Auto Scaler:          #{Tapjoy::AutoscalingBootstrap.scaler_name}"
  puts "  ELB:                  #{elb_list(config)}" if has_elb
  puts "  Key Pair:             #{keypair}"
  puts "  Zones:                #{zones.join(',')}"
  puts "  Groups:               #{security_groups.sort.join(',')}"
  puts "  Instance Type:        #{instance_type}"
  puts "  Image ID:             #{image_id}"
  puts "  IAM Role:             #{iam_instance_profile}"
  puts "  VPC Subnets:          #{vpc_subnets}" if use_vpc
  puts "  Termination Policies: #{termination_policies.sort.join(',')}"

  puts "\n\nNOTE! Continuing may have adverse effects if you end up " \
  "deleting an IN-USE PRODUCTION scaling group. Don't be dumb."
  return true unless prompt
  agree('Is this information correct? [y/n]')
end

#elb_list(config) ⇒ Object

Clean list of ELBs



132
133
134
# File 'lib/tapjoy/autoscaling_bootstrap.rb', line 132

def elb_list(config)
  config[:elb].map(&:keys).flatten.join(',')
end

#generate_user_data(userdata_dir, bootstrap_script, config) ⇒ Object

Using variables passed in, generate user data file



96
97
98
99
100
101
# File 'lib/tapjoy/autoscaling_bootstrap.rb', line 96

def generate_user_data(userdata_dir, bootstrap_script, config)

  ERB.new(
    File.new(File.join(userdata_dir, bootstrap_script)).read, nil, '-'
  ).result(binding)
end

#get_security_groups(config_dir, env, group) ⇒ Object

Get AWS Environment



121
122
123
124
125
126
127
128
129
# File 'lib/tapjoy/autoscaling_bootstrap.rb', line 121

def get_security_groups(config_dir, env, group)

  # Check environment file
  unless File.readable?("#{config_dir}/#{env}.yaml")
    fail Tapjoy::AutoscalingBootstrap::Errors::InvalidEnvironment
  end

  security_groups = {security_groups: group.split(',')}
end

#load_yaml(filename) ⇒ Object

Confirm that yaml is readable and then convert to hash



90
91
92
93
# File 'lib/tapjoy/autoscaling_bootstrap.rb', line 90

def load_yaml(filename)
  abort("ERROR: '#{filename}' is not readable") unless File.readable?(filename)
  Hash[YAML.load_file(filename)]
end

#sec_group_exists(groups) ⇒ Object

Check if security group exists and create it if it does not



203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/tapjoy/autoscaling_bootstrap.rb', line 203

def sec_group_exists(groups)
  groups.each do |group|
    begin
      puts "Verifying #{group} exists..."
      group = Tapjoy::AutoscalingBootstrap::AWS::EC2.describe_security_groups(group)
    rescue Aws::EC2::Errors::InvalidGroupNotFound => err
      STDERR.puts "Warning: #{err}"
      puts "Creating #{group} for #{Tapjoy::AutoscalingBootstrap.scaler_name}"
      Tapjoy::AutoscalingBootstrap::AWS::EC2.create_security_group(group)
    end
  end
end