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
Defined Under Namespace
Classes: RDSInstanceAlreadyExist
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.
-
#delete_db_instance(db_instance_identifier, db_snapshot_identifier = nil) ⇒ Object
Delete a givne db instance.
-
#does_db_exist?(db_instance_identifier) ⇒ boolean
Determine if a givne db instance exist.
-
#find_latest_snapshot(db_instance_identifier, snapshot_type = 'manual') ⇒ String
Delete a givne db instance.
-
#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.
-
#restore_db(db_instance_identifier, restore_from, options = {}) ⇒ Object
Restore DB from a snapshot.
-
#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.
9 10 11 |
# File 'lib/cucloud/rds_utils.rb', line 9 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.
140 141 142 143 144 |
# File 'lib/cucloud/rds_utils.rb', line 140 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 |
#delete_db_instance(db_instance_identifier, db_snapshot_identifier = nil) ⇒ Object
Delete a givne db instance
34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/cucloud/rds_utils.rb', line 34 def delete_db_instance(db_instance_identifier, db_snapshot_identifier = nil) if does_db_exist?(db_instance_identifier) if db_snapshot_identifier.nil? @rds.delete_db_instance(db_instance_identifier: db_instance_identifier, skip_final_snapshot: true) else @rds.delete_db_instance(db_instance_identifier: db_instance_identifier, final_db_snapshot_identifier: db_snapshot_identifier) end @rds.wait_until(:db_instance_deleted, db_instance_identifier: db_instance_identifier) else raise Aws::RDS::Errors::DBInstanceNotFound.new(db_instance_identifier, '') end end |
#does_db_exist?(db_instance_identifier) ⇒ boolean
Determine if a givne db instance exist
24 25 26 27 28 29 |
# File 'lib/cucloud/rds_utils.rb', line 24 def does_db_exist?(db_instance_identifier) get_instance(db_instance_identifier).instance_create_time true rescue Aws::RDS::Errors::DBInstanceNotFound false end |
#find_latest_snapshot(db_instance_identifier, snapshot_type = 'manual') ⇒ String
Delete a givne db instance
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/cucloud/rds_utils.rb', line 65 def find_latest_snapshot(db_instance_identifier, snapshot_type = 'manual') latest_snapshot_time = Time.new(2002) latest_snap_shot = nil snapshots_info = @rds.describe_db_snapshots( db_instance_identifier: db_instance_identifier, snapshot_type: snapshot_type )[:db_snapshots] snapshots_info.each do |snapshot_info| next if snapshot_info[:status] != 'available' if latest_snapshot_time.to_i < snapshot_info[:snapshot_create_time].to_i latest_snapshot_time = snapshot_info[:snapshot_create_time].to_i latest_snap_shot = snapshot_info end end latest_snap_shot.nil? ? nil : latest_snap_shot[:db_snapshot_identifier] end |
#get_instance(db_instance_identifier) ⇒ Aws::RDS::DBInstance
Get the RDS instance object with the given name
16 17 18 19 |
# File 'lib/cucloud/rds_utils.rb', line 16 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.
107 108 109 110 111 112 |
# File 'lib/cucloud/rds_utils.rb', line 107 def pending_snapshots(rds_instance) snaps = rds_instance.snapshots snaps.select do |snap| snap.status == 'creating' end end |
#restore_db(db_instance_identifier, restore_from, options = {}) ⇒ Object
Restore DB from a snapshot
52 53 54 55 56 57 58 59 60 |
# File 'lib/cucloud/rds_utils.rb', line 52 def restore_db(db_instance_identifier, restore_from, = {}) raise RDSInstanceAlreadyExist if does_db_exist?(db_instance_identifier) db_snapshot_identifier = [:db_snapshot_identifier].nil? ? find_latest_snapshot(restore_from) : [:db_snapshot_identifier] [:db_instance_identifier] = db_instance_identifier [:db_snapshot_identifier] = db_snapshot_identifier @rds.restore_db_instance_from_db_snapshot() 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.
91 92 93 94 95 96 97 98 99 100 |
# File 'lib/cucloud/rds_utils.rb', line 91 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.
122 123 124 125 126 127 128 129 130 |
# File 'lib/cucloud/rds_utils.rb', line 122 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 |