Class: AwsRegion::AwsDbInstance
Overview
Class to handle RDS Db instances
Instance Attribute Summary collapse
-
#endpoint ⇒ Object
Returns the value of attribute endpoint.
-
#id ⇒ Object
Returns the value of attribute id.
-
#region ⇒ Object
Returns the value of attribute region.
-
#tags ⇒ Object
Returns the value of attribute tags.
Instance Method Summary collapse
-
#delete(options = {}) ⇒ Boolean
Delete a database and be sure to capture a final stapshot.
-
#get_latest_db_snapshot(options = {}) ⇒ Hash
Get the name of the latest snapshot.
-
#initialize(region, options = {}) ⇒ AwsDbInstance
constructor
Creates an AwsDbInstance for an existing instance or creates a new database * :instance - If specified, create an instance of this class using this RDS instance.
-
#purge_db_snapshots ⇒ Boolean
Purge db snapshots, keep just one - the latest.
-
#status ⇒ String
Get the status of a database.
-
#wait(options = {:desired_status => "available", :timeout => 600}) ⇒ Boolean
Wait for the database to get to a state - we are usually waiting for it to be “available” * :desired_status - Default: “available” - The RDS Status that is sought * :timeout - Default: 600 seconds.
Methods inherited from AwsBase
Constructor Details
#initialize(region, options = {}) ⇒ AwsDbInstance
Creates an AwsDbInstance for an existing instance or creates a new database
-
:instance - If specified, create an instance of this class using this RDS instance.
-
:opts - [Hash] - Includes parameters for constructing the database. The format is:
-
:db_instance_identifier - RDS instance identifier
-
:db_subnet_group_name - DB Subgroup name
-
:publicly_accessible - [true|false]
-
:db_instance_class - RDS db instance class
-
:availability_zone - RDS/Aws availability zone
-
:multi_az - [true|false]
-
:engine - RDS engine (Only tested with Mysql at this point)
-
-
:tags - Tags to be applied to RDS instance. The follow are required. Arbitrary tags may also be added.
-
:environment - Environment designation - can be anything. Used to locate instance with other aws-tools
-
:purpose - Purpose designation - can be anything. Used to locate instance with other aws-tools
-
:name - Name will appear in the Aws web page if you set this
-
:snapshot_name - Name of the snapshot that will be used to construct the new instance. This name will be matched with the RDS db_instance_identifier. The latest snapshot will be used.
-
:vpc_security_group_ids: - Comma separated list of security groups that will be applied to this instance
-
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 |
# File 'lib/aws_region.rb', line 345 def initialize(region, = {}) @region = region opts = [:opts] if !.has_key?(:instance) @id = opts[:db_instance_identifier] snapshot_name = [:snapshot_name] if 0 < @region.find_db_instances({:instance_id => @id}).length log "Error, instance: #{@id} already exists" return end last = self.get_latest_db_snapshot({:snapshot_name => snapshot_name}) log "Restoring: #{last.db_instance_identifier}, snapshot: #{last.db_instance_identifier} from : #{last.snapshot_create_time}" opts[:db_snapshot_identifier] = last.db_snapshot_identifier response = @region.rds.restore_db_instance_from_db_snapshot(opts) @_instance = response[:db_instance] @region.rds.({:resource_name => "arn:aws:rds:#{@region.region}:#{@region.account_id}:db:#{@id}", :tags => [{:key => "environment", :value => [:environment]}, {:key => "purpose", :value => [:purpose]}]}) self.wait opts = {:db_instance_identifier => @id, :vpc_security_group_ids => [:vpc_security_group_ids]} @region.rds.modify_db_instance(opts) else @_instance = [:instance] @id = @_instance[:db_instance_identifier] end @tags = {} = @region.rds.({:resource_name => "arn:aws:rds:#{@region.region}:#{@region.account_id}:db:#{@id}"}) [:tag_list].each do |t| @tags[t[:key].to_sym] = t[:value] end @endpoint = @_instance.endpoint[:address] end |
Instance Attribute Details
#endpoint ⇒ Object
Returns the value of attribute endpoint.
325 326 327 |
# File 'lib/aws_region.rb', line 325 def endpoint @endpoint end |
#id ⇒ Object
Returns the value of attribute id.
325 326 327 |
# File 'lib/aws_region.rb', line 325 def id @id end |
#region ⇒ Object
Returns the value of attribute region.
325 326 327 |
# File 'lib/aws_region.rb', line 325 def region @region end |
#tags ⇒ Object
Returns the value of attribute tags.
325 326 327 |
# File 'lib/aws_region.rb', line 325 def @tags end |
Instance Method Details
#delete(options = {}) ⇒ Boolean
Delete a database and be sure to capture a final stapshot
383 384 385 386 387 388 389 390 391 392 393 394 |
# File 'lib/aws_region.rb', line 383 def delete(={}) log "Deleting database: #{@id}" opts = {:db_instance_identifier => @id, :skip_final_snapshot => false, :final_db_snapshot_identifier => "#{@id}-#{Time.now.strftime("%Y-%m-%d-%H-%M-%S")}"} begin i = @region.rds.delete_db_instance(opts) rescue Exception => e return false end true end |
#get_latest_db_snapshot(options = {}) ⇒ Hash
Get the name of the latest snapshot
458 459 460 461 462 463 464 465 466 467 468 469 470 |
# File 'lib/aws_region.rb', line 458 def get_latest_db_snapshot(={}) snapshot_name = .has_key?(:snapshot_name) ? [:snapshot_name] : @id last = nil last_t = 0 @region.rds.describe_db_snapshots[:db_snapshots].each do |i| if i.db_instance_identifier == snapshot_name and (last.nil? or i.snapshot_create_time > last_t) last = i last_t = i.snapshot_create_time end end last end |
#purge_db_snapshots ⇒ Boolean
Purge db snapshots, keep just one - the latest
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 |
# File 'lib/aws_region.rb', line 398 def purge_db_snapshots latest = 0 @region.rds.describe_db_snapshots[:db_snapshots].each do |i| if i.snapshot_type == "manual" and i.db_instance_identifier == @id if i.snapshot_create_time.to_i > latest latest = i.snapshot_create_time.to_i end end end @region.rds.describe_db_snapshots[:db_snapshots].each do |i| if i.snapshot_type == "manual" and i.db_instance_identifier == @id if i.snapshot_create_time.to_i != latest log "Removing snapshot: #{i.db_snapshot_identifier}/#{i.snapshot_create_time.to_s}" begin @region.rds.delete_db_snapshot({:db_snapshot_identifier => i.db_snapshot_identifier}) rescue log "Error removing snapshot: #{i.db_snapshot_identifier}/#{i.snapshot_create_time.to_s}" return false end else log "Keeping snapshot: #{i.db_snapshot_identifier}/#{i.snapshot_create_time.to_s}" end end end true end |
#status ⇒ String
Get the status of a database
452 453 454 |
# File 'lib/aws_region.rb', line 452 def status @_instance.db_instance_status end |
#wait(options = {:desired_status => "available", :timeout => 600}) ⇒ Boolean
Wait for the database to get to a state - we are usually waiting for it to be “available”
-
:desired_status - Default: “available” - The RDS Status that is sought
-
:timeout - Default: 600 seconds. - The time to wait for the status before returning failure
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 |
# File 'lib/aws_region.rb', line 430 def wait( = {:desired_status => "available", :timeout => 600}) inst = @region.find_db_instances({:instance_id => @id})[0] if !inst log "Error, instance: #{@id} not found" return false end t0 = Time.now.to_i while inst.status != [:desired_status] inst = @region.find_db_instances({:instance_id => @id})[0] log "Database: #{@id} at #{@endpoint}. Current status: #{inst.status}" if Time.now.to_i - t0 > [:timeout] log "Timed out waiting for database: #{@id} at #{@endpoint} to move into status: #{[:desired_status]}. Current status: #{inst.status}" return false end sleep 20 end return true end |