Class: Cucloud::RdsUtils
- Inherits:
-
Object
- Object
- Cucloud::RdsUtils
- Defined in:
- lib/cucloud/rds_utils.rb
Overview
RdsUtils class - for interacting with the AWS relational database service
Instance Method Summary collapse
-
#create_snapshot_and_wait_until_available(rds_instance, tags = []) ⇒ Aws::RDS::DBSnapshot
Create a new snapshot of the instance, if no snapshots are already in the process of being created, and wait indefinitely until the snapshot is complete.
-
#get_instance(db_instance_identifier) ⇒ Aws::RDS::DBInstance
Get the RDS instance object with the given name.
-
#initialize(rds_client = Aws::RDS::Client.new) ⇒ RdsUtils
constructor
A new instance of RdsUtils.
-
#pending_snapshots(rds_instance) ⇒ Collection<Aws::RDS::DBSnapshot>
Return list of pending snapshots for the instance.
-
#start_snapshot(rds_instance, tags = []) ⇒ Aws::RDS::DBSnapshot
Begins the creation of a snapshot of the given RDS instance.
-
#wait_until_snapshot_available(snapshot, max_attempts = nil, delay = 10) ⇒ Aws::RDS::DBSnapshot
Wait for the completion and availability of a snapshot.
Constructor Details
#initialize(rds_client = Aws::RDS::Client.new) ⇒ RdsUtils
Returns a new instance of RdsUtils.
4 5 6 |
# File 'lib/cucloud/rds_utils.rb', line 4 def initialize(rds_client = Aws::RDS::Client.new) @rds = rds_client end |
Instance Method Details
#create_snapshot_and_wait_until_available(rds_instance, tags = []) ⇒ Aws::RDS::DBSnapshot
Create a new snapshot of the instance, if no snapshots are already in the process of being created, and wait indefinitely until the snapshot is complete.
72 73 74 75 76 |
# File 'lib/cucloud/rds_utils.rb', line 72 def create_snapshot_and_wait_until_available(rds_instance, = []) return nil unless pending_snapshots(rds_instance).empty? snap = start_snapshot(rds_instance, ) wait_until_snapshot_available(snap, nil, 10) end |
#get_instance(db_instance_identifier) ⇒ Aws::RDS::DBInstance
Get the RDS instance object with the given name
11 12 13 14 |
# File 'lib/cucloud/rds_utils.rb', line 11 def get_instance(db_instance_identifier) resource = Aws::RDS::Resource.new(client: @rds) resource.db_instance(db_instance_identifier) end |
#pending_snapshots(rds_instance) ⇒ Collection<Aws::RDS::DBSnapshot>
Return list of pending snapshots for the instance. New snapshots cannot be created if there are any snapshots in the process of being created for the given instance.
39 40 41 42 43 44 |
# File 'lib/cucloud/rds_utils.rb', line 39 def pending_snapshots(rds_instance) snaps = rds_instance.snapshots snaps.select do |snap| snap.status == 'creating' end end |
#start_snapshot(rds_instance, tags = []) ⇒ Aws::RDS::DBSnapshot
Begins the creation of a snapshot of the given RDS instance. This is a non-blocking call so it will return before the snapshot is created and available.
23 24 25 26 27 28 29 30 31 32 |
# File 'lib/cucloud/rds_utils.rb', line 23 def start_snapshot(rds_instance, = []) date = Time.new snap_id = rds_instance.db_instance_identifier + '-' + date.year.to_s + '-' \ + zerofill2(date.month) + '-' + zerofill2(date.day) + '-' \ + zerofill2(date.hour) + '-' + zerofill2(date.min) rds_instance.create_snapshot( db_snapshot_identifier: snap_id, tags: ) end |
#wait_until_snapshot_available(snapshot, max_attempts = nil, delay = 10) ⇒ Aws::RDS::DBSnapshot
Wait for the completion and availability of a snapshot.
54 55 56 57 58 59 60 61 62 |
# File 'lib/cucloud/rds_utils.rb', line 54 def wait_until_snapshot_available(snapshot, max_attempts = nil, delay = 10) snapshot.wait_until(max_attempts: max_attempts, delay: delay) do |snap| # Status == available is a conservative test for completion. # A more liberal test would be percent_progress == 100. snap.status == 'available' end rescue Aws::Waiters::Errors::WaiterFailed nil end |