Class: Tapjoy::AutoscalingBootstrap::Autoscaling::Group

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

Overview

Class for Autoscaling groups

Instance Method Summary collapse

Constructor Details

#initializeGroup

Initialize the class



8
9
10
11
12
13
# File 'lib/tapjoy/autoscaling_bootstrap/autoscaling/group.rb', line 8

def initialize
  @scaler_name = Tapjoy::AutoscalingBootstrap.scaler_name
  @config_name = Tapjoy::AutoscalingBootstrap.config_name
  @elb_name    = Tapjoy::AutoscalingBootstrap.elb_name
  @create_elb  = Tapjoy::AutoscalingBootstrap.create_elb
end

Instance Method Details

#create(config:, aws_env:, user_data:) ⇒ Object

Create autoscaling group



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/tapjoy/autoscaling_bootstrap/autoscaling/group.rb', line 16

def create(config:, aws_env:, user_data:)
  if exists
    begin
      zero_autoscale_group
    rescue Aws::AutoScaling::Errors::ValidationError => err
      abort("Cannot remove existing AS group #{@scaler_name}. Error: #{err}")
    end
  else
    "Scaling group #{@scaler_name} does not exist, continuing..."
  end

  Tapjoy::AutoscalingBootstrap.config.create(config, aws_env,
    user_data)

  puts "Creating scaling group: #{@scaler_name}"
  Tapjoy::AutoscalingBootstrap::AWS::Autoscaling::Group.create(**config)
  create_termination_notification(config)
end

#encode_user_data(user_data) ⇒ Object

Encode user data into required base64 form



41
42
43
# File 'lib/tapjoy/autoscaling_bootstrap/autoscaling/group.rb', line 41

def encode_user_data(user_data)
  Base64.encode64("#{user_data}")
end

#existsObject

Check if autoscale group exists



36
37
38
# File 'lib/tapjoy/autoscaling_bootstrap/autoscaling/group.rb', line 36

def exists
  !Tapjoy::AutoscalingBootstrap::AWS::Autoscaling::Group.describe.nil?
end

#generate_tags(tags) ⇒ Object

Create tags array to pass to autoscaling group



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/tapjoy/autoscaling_bootstrap/autoscaling/group.rb', line 46

def generate_tags(tags)
  tag_array = Array.new
  return [] if tags.nil? || tags.empty?
  tags.each do |t|
    return [] if t.nil?
    t.each_pair do |key, value|
      tag_array << {
        resource_id: @scaler_name,
        resource_type: 'auto-scaling-group',
        key: key.to_s,
        value: value,
        propagate_at_launch: true,
      }
    end
  end
  tag_array
end

#scale(config) ⇒ Object

Scale auto scaling groups



65
66
67
68
69
70
71
72
73
74
# File 'lib/tapjoy/autoscaling_bootstrap/autoscaling/group.rb', line 65

def scale(config)
  if config[:scaling_type].eql?('dynamic')
    Tapjoy::AutoscalingBootstrap::AWS::Autoscaling::Group.resize(
      **config[:instance_counts])
  elsif config[:scaling_type].eql?('static')
    scale_static(config)
  else
    abort('Unknown scaling type')
  end
end