Class: AwsRegion::AwsDbInstance

Inherits:
AwsBase
  • Object
show all
Defined in:
lib/aws_region.rb

Overview

Class to handle RDS Db instances

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from AwsBase

#log

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

Parameters:

  • region (String)
      • Value from REGION static hash

  • options (Hash) (defaults to: {})
    • Can contain:



344
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
# File 'lib/aws_region.rb', line 344

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.}: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.}:db:#{@id}"})
  _tags[:tag_list].each do |t|
    @tags[t[:key].to_sym] = t[:value]
  end
  @endpoint = @_instance.endpoint[:address]
end

Instance Attribute Details

#endpointObject

Returns the value of attribute endpoint.



324
325
326
# File 'lib/aws_region.rb', line 324

def endpoint
  @endpoint
end

#idObject

Returns the value of attribute id.



324
325
326
# File 'lib/aws_region.rb', line 324

def id
  @id
end

#regionObject

Returns the value of attribute region.



324
325
326
# File 'lib/aws_region.rb', line 324

def region
  @region
end

#tagsObject

Returns the value of attribute tags.



324
325
326
# File 'lib/aws_region.rb', line 324

def tags
  @tags
end

Instance Method Details

#delete(options = {}) ⇒ Boolean

Delete a database and be sure to capture a final stapshot

Returns:

  • (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



382
383
384
385
386
387
388
389
390
391
392
393
# File 'lib/aws_region.rb', line 382

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_latest_db_snapshot(options = {}) ⇒ Hash

Get the name of the latest snapshot

Returns:



457
458
459
460
461
462
463
464
465
466
467
468
469
# File 'lib/aws_region.rb', line 457

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_snapshotsBoolean

Purge db snapshots, keep just one - the latest

Returns:

  • (Boolean)


397
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
# File 'lib/aws_region.rb', line 397

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

#statusString

Get the status of a database

Returns:

  • (String)
    • Current status of this database



451
452
453
# File 'lib/aws_region.rb', line 451

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

Parameters:

  • options (Hash) (defaults to: {:desired_status => "available", :timeout => 600})
    • Can be:

Returns:

  • (Boolean)


429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
# File 'lib/aws_region.rb', line 429

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