Class: CfnManage::StartStopHandler::DocumentDb

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

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of DocumentDb.



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

def initialize(cluster_id, options = {})
  @cluster_id = cluster_id
  credentials = CfnManage::AWSCredentials.get_session_credentials("startstopcluster_#{cluster_id}")
  @docdb_client = Aws::DocDB::Client.new(retry_limit: 20)
  if credentials != nil
    @docdb_client = Aws::DocDB::Client.new(credentials: credentials, retry_limit: 20)
  end
  cluster = @docdb_client.describe_db_clusters({ db_cluster_identifier: @cluster_id })
  @docdb_cluster = cluster.db_clusters.first
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
# File 'lib/cfn_manage/handlers/documentdb.rb', line 19

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

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

#stopObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/cfn_manage/handlers/documentdb.rb', line 39

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

  if @docdb_cluster.status != 'available'
    $log.info("DocDB Cluster #{@cluster_id} is not in a available state. State: #{@docdb_cluster.status}")
    return {}
  end
  # stop docdb cluster and wait for it to be fully stopped
  $log.info("Stopping DocDB cluster #{@cluster_id}")
  @docdb_client.stop_db_cluster({ db_cluster_identifier: @cluster_id })
  unless CfnManage.skip_wait?
    $log.info("Waiting DocDB cluster to be stopped #{@cluster_id}")
    wait('stopped')
  end
  return {}
end

#wait(completed_state) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/cfn_manage/handlers/documentdb.rb', line 59

def wait(completed_state)
  # reached state must be steady, at least a minute.
  state_count = 0
  steady_count = 4
  attempts = 0

  until attempts == (max_attempts = 60*6) do
    # Declare client and cluster variable a second time inside the loop so it re-evaluates each time.
    docdb = @docdb_client.describe_db_clusters({ db_cluster_identifier: @cluster_id })
    cluster = docdb.db_clusters.first
    $log.info("DocDB 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("DocDB Cluster #{@cluster_id} did not enter #{completed_state} state, however continuing operations...")
  end
end