class AwsRegion::AwsDbInstance
Class to handle RDS Db instances
Attributes
Public Class Methods
Creates an AwsDbInstance
for an existing instance or creates a new database @param region [String] - - Value from REGION static hash @param options [Hash] - Can contain:
-
: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
-
# File lib/aws_region.rb, line 345 def initialize(region, options = {}) @region = region opts = options[:opts] if !options.has_key?(:instance) @id = opts[:db_instance_identifier] snapshot_name = options[: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.add_tags_to_resource({:resource_name => "arn:aws:rds:#{@region.region}:#{@region.account_id}:db:#{@id}", :tags => [{:key => "environment", :value => options[:environment]}, {:key => "purpose", :value => options[:purpose]}]}) self.wait opts = {:db_instance_identifier => @id, :vpc_security_group_ids => options[:vpc_security_group_ids]} @region.rds.modify_db_instance(opts) else @_instance = options[:instance] @id = @_instance[:db_instance_identifier] end @tags = {} _tags = @region.rds.list_tags_for_resource({:resource_name => "arn:aws:rds:#{@region.region}:#{@region.account_id}:db:#{@id}"}) _tags[:tag_list].each do |t| @tags[t[:key].to_sym] = t[:value] end @endpoint = @_instance.endpoint[:address] end
Public Instance Methods
Delete a database and be sure to capture a final stapshot @return [Boolean] - A return value of true only means that the command was issued. The caller should follow up later with a call to determine status in order to know when the delete has been completed
# File lib/aws_region.rb, line 383 def delete(options={}) 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 the name of the latest snapshot @return [Hash] - Hash describing RDS Snapshot. See [RDS Tech Docs](docs.aws.amazon.com/sdkforruby/api/Aws/RDS/V20130909.html)
# File lib/aws_region.rb, line 458 def get_latest_db_snapshot(options={}) snapshot_name = options.has_key?(:snapshot_name) ? options[: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, keep just one - the latest @return [Boolean]
# 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
Get the status of a database @return [String] - Current status of this database
# File lib/aws_region.rb, line 452 def status @_instance.db_instance_status end
Wait for the database to get to a state - we are usually waiting for it to be “available” @param options [Hash] - Can be:
-
:desired_status - Default: “available” - The RDS Status that is sought
-
:timeout - Default: 600 seconds. - The time to wait for the status before returning failure
@return [Boolean]
# File lib/aws_region.rb, line 430 def wait(options = {: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 != options[: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 > options[:timeout] log "Timed out waiting for database: #{@id} at #{@endpoint} to move into status: #{options[:desired_status]}. Current status: #{inst.status}" return false end sleep 20 end return true end