Class: CfnManage::StartStopHandler::AuroraCluster

Inherits:
Object
  • Object
show all
Defined in:
lib/cfn_manage/handlers/aurora_cluster.rb

Instance Method Summary collapse

Constructor Details

#initialize(cluster_id, options = {}) ⇒ AuroraCluster

Returns a new instance of AuroraCluster.



8
9
10
11
12
13
14
15
16
17
# File 'lib/cfn_manage/handlers/aurora_cluster.rb', line 8

def initialize(cluster_id, options = {})
  @cluster_id = cluster_id
  credentials = CfnManage::AWSCredentials.get_session_credentials("startstopcluster_#{cluster_id}")
  @rds_client = Aws::RDS::Client.new(retry_limit: 20)
  if credentials != nil
    @rds_client = Aws::RDS::Client.new(credentials: credentials, retry_limit: 20)
  end
  rds = Aws::RDS::Resource.new(client: @rds_client)
  @rds_cluster = rds.db_cluster(cluster_id)
end

Instance Method Details

#start(configuration) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/cfn_manage/handlers/aurora_cluster.rb', line 19

def start(configuration)
  if @rds_cluster.status == 'available'
    $log.info("Aurora Cluster #{@cluster_id} is already in available state")
    return
  end

  if @rds_cluster.engine_mode != 'provisioned'
    $log.info("Aurora Cluster #{@cluster_id} is not a provisioned cluster and cannot be started using this method.")
    return
  end

  # start rds cluster
  if @rds_cluster.status == 'stopped'
    $log.info("Starting Aurora cluster #{@cluster_id}")
    @rds_client.start_db_cluster({ db_cluster_identifier: @cluster_id })
    unless CfnManage.skip_wait?
      # wait cluster to become available
      $log.info("Waiting Aurora cluster to become available #{@cluster_id}")
      wait('available')
    end
  else
    $log.info("Aurora Cluster #{@cluster_id} is not in a stopped state. State: #{@rds_cluster.status}")
  end
end

#stopObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/cfn_manage/handlers/aurora_cluster.rb', line 44

def stop
  if @rds_cluster.status == 'stopped'
    $log.info("Aurora Cluster #{@cluster_id} is already stopped")
    return {}
  end

  if @rds_cluster.status != 'available'
    $log.info("Aurora Cluster #{@cluster_id} is not in a available state. State: #{@rds_cluster.status}")
    return {}
  end

  if @rds_cluster.engine_mode != 'provisioned'
    $log.info("Aurora Cluster #{@cluster_id} is not a provisioned cluster and cannot be stopped using this method.")
    return {}
  end
  # stop rds cluster and wait for it to be fully stopped
  $log.info("Stopping aurora cluster #{@cluster_id}")
  @rds_client.stop_db_cluster({ db_cluster_identifier: @cluster_id })
  unless CfnManage.skip_wait?
    $log.info("Waiting aurora cluster to be stopped #{@cluster_id}")
    wait('stopped')
  end
  return {}
end

#wait(completed_state) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/cfn_manage/handlers/aurora_cluster.rb', line 69

def wait(completed_state)
  # reached state must be steady, at least a minute.
  state_count = 0
  steady_count = 4
  attempts = 0
  rds = Aws::RDS::Resource.new(client: @rds_client)
  until attempts == (max_attempts = 60*6) do
    cluster = rds.db_cluster(@cluster_id)
    $log.info("Aurora Cluster #{cluster.db_cluster_identifier} state: #{cluster.status}, waiting for #{completed_state}")

    if cluster.status == "#{completed_state}"
      state_count = state_count + 1
      $log.info("#{state_count}/#{steady_count}")
    else
      state_count = 0
    end
    break if state_count == steady_count
    attempts = attempts + 1
    sleep(15)
  end

  if attempts == max_attempts
    $log.error("RDS Aurora Cluster #{@cluster_id} did not enter #{state} state, however continuing operations...")
  end
end