Class: Cucloud::AsgUtils

Inherits:
Object
  • Object
show all
Defined in:
lib/cucloud/asg_utils.rb

Overview

AsgUtils - Utilities for AutoScaling groups

Instance Method Summary collapse

Constructor Details

#initialize(asg_client = Aws::AutoScaling::Client.new) ⇒ AsgUtils

Constructor for AsgUtils class

Parameters:

  • asg_client (Aws::AutoScaling::Client) (defaults to: Aws::AutoScaling::Client.new)

    AWS AutoScaling SDK Client



8
9
10
11
# File 'lib/cucloud/asg_utils.rb', line 8

def initialize(asg_client = Aws::AutoScaling::Client.new)
  ## DI for testing purposes
  @asg = asg_client
end

Instance Method Details

#create_launch_configuration(options) ⇒ Seahorse::Client::Response

Create new launch configuration in AWS

Parameters:

  • options (Hash)

    Options hash to be passed along in request

Returns:

  • (Seahorse::Client::Response)

    Empty Seahorse Client Response



52
53
54
55
# File 'lib/cucloud/asg_utils.rb', line 52

def create_launch_configuration(options)
  # https://docs.aws.amazon.com/sdkforruby/api/Aws/AutoScaling/Client.html#create_launch_configuration-instance_method
  @asg.create_launch_configuration(options)
end

#generate_lc_options_hash_with_ami(launch_config, new_ami_id, new_launch_config_name = "cucloud-lc-#{UUID.new.generate}") ⇒ Hash

Generate a hash that can be submitted when creating a new launch config - replace image with desired AMI

Parameters:

  • launch_config (Aws::AutoScaling::Types::LaunchConfiguration)

    Existing launch configuration

  • new_ami_id (String)

    Id of AMI that should be added to the new configuration

  • new_launch_config_name (String) (defaults to: "cucloud-lc-#{UUID.new.generate}")

    Name of new launch configuration (must be unique in AWS account)

Returns:

  • (Hash)

    Options hash to be submitted via AWS SDK



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/cucloud/asg_utils.rb', line 34

def generate_lc_options_hash_with_ami(launch_config, new_ami_id,
                                      new_launch_config_name = "cucloud-lc-#{UUID.new.generate}")

  # make sure we got a valid launch config
  raise 'Not a launch configuration struct' unless launch_config.is_a? Aws::AutoScaling::Types::LaunchConfiguration

  # convert to hash (required for aws sdk) and update necessary values
  config_hash = launch_config.to_h
  config_hash[:launch_configuration_name] = new_launch_config_name
  config_hash[:image_id] = new_ami_id

  # request cannot have arn, created_time or keys with empty values
  config_hash.delete_if { |key, value| key == :launch_configuration_arn || key == :created_time || value == '' }
end

#get_asg_by_name(name) ⇒ Aws::AutoScaling::Types::AutoScalingGroup

Get an autoscale instance by group name

Parameters:

  • name (String)

    A single autoscale group name

Returns:

  • (Aws::AutoScaling::Types::AutoScalingGroup)

    AWS SDK autoscale group struct



16
17
18
19
# File 'lib/cucloud/asg_utils.rb', line 16

def get_asg_by_name(name)
  # https://docs.aws.amazon.com/sdkforruby/api/Aws/AutoScaling/Client.html#describe_auto_scaling_groups-instance_method
  @asg.describe_auto_scaling_groups(auto_scaling_group_names: [name]).auto_scaling_groups.first
end

#get_launch_configuration_by_name(launch_config_name) ⇒ Aws::AutoScaling::Types::LaunchConfiguration

get an instance of the launch configuration for a given autoscaling group

Parameters:

  • launch_config_name (String)

    Name of launch configuration (from ASG)

Returns:

  • (Aws::AutoScaling::Types::LaunchConfiguration)

    AWS SDK Launch Configuration struct



24
25
26
27
# File 'lib/cucloud/asg_utils.rb', line 24

def get_launch_configuration_by_name(launch_config_name)
  # https://docs.aws.amazon.com/sdkforruby/api/Aws/AutoScaling/Client.html#describe_launch_configurations-instance_method
  @asg.describe_launch_configurations(launch_configuration_names: [launch_config_name]).launch_configurations.first
end

#update_asg_launch_configuration!(asg_name, launch_config_name) ⇒ Seahorse::Client::Response

Update autoscale group launch configuration

Parameters:

  • asg_name (String)

    AutoScale group name

  • launch_config_name (String)

    Launch configuration name

Returns:

  • (Seahorse::Client::Response)

    Empty Seahorse Client Response



61
62
63
64
65
# File 'lib/cucloud/asg_utils.rb', line 61

def update_asg_launch_configuration!(asg_name, launch_config_name)
  # https://docs.aws.amazon.com/sdkforruby/api/Aws/AutoScaling/Client.html#update_auto_scaling_group-instance_method
  @asg.update_auto_scaling_group(auto_scaling_group_name: asg_name,
                                 launch_configuration_name: launch_config_name)
end