Class: Base2::AsgStartStopHandler

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

Instance Method Summary collapse

Constructor Details

#initialize(asg_id) ⇒ AsgStartStopHandler

Returns a new instance of AsgStartStopHandler.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/asg_start_stop_handler.rb', line 7

def initialize(asg_id)
  @asg_name = asg_id

  credentials = Base2::AWSCredentials.get_session_credentials("stopasg_#{@asg_name}")
  @asg_client = Aws::AutoScaling::Client.new(retry_limit: 20)
  if credentials != nil
    @asg_client = Aws::AutoScaling::Client.new(credentials: credentials, retry_limit: 20)
  end

  asg_details = @asg_client.describe_auto_scaling_groups(
      auto_scaling_group_names: [@asg_name]
  )
  if asg_details.auto_scaling_groups.size() == 0
    raise "Couldn't find ASG #{@asg_name}"
  end
  @asg = asg_details.auto_scaling_groups[0]
end

Instance Method Details

#start(configuration) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/asg_start_stop_handler.rb', line 53

def start(configuration)
  if configuration.nil?
    $log.warn("No configuration found for #{@asg_name}, skipping..")
    return
  end
  $log.info("Starting ASG #{@asg_name} with following configuration\n#{configuration}")

  # restore asg sizes
  @asg_client.update_auto_scaling_group({
      auto_scaling_group_name: @asg_name,
      min_size: configuration['min_size'],
      max_size: configuration['max_size'],
      desired_capacity: configuration['desired_capacity']
  })
end

#stopObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/asg_start_stop_handler.rb', line 25

def stop
  # check if already stopped
  if @asg.min_size == @asg.max_size and @asg.max_size == @asg.desired_capacity and @asg.min_size == 0
    $log.info("ASG #{@asg_name} already stopped")
    # nil and false configurations are not saved
    return nil
  else
    # store asg configuration to S3
    configuration = {
        desired_capacity: @asg.desired_capacity,
        min_size: @asg.min_size,
        max_size: @asg.max_size
    }

    $log.info("Setting desired capacity to 0/0/0 for ASG #{@asg.auto_scaling_group_name}A")
    # set asg configuration to 0/0/0
    puts @asg.auto_scaling_group_name
    @asg_client.update_auto_scaling_group({
        auto_scaling_group_name: "#{@asg.auto_scaling_group_name}",
        min_size: 0,
        max_size: 0,
        desired_capacity: 0
    })
    return configuration
  end

end