Class: AwsRegion::AwsDbInstance
- Inherits:
-
Object
- Object
- AwsRegion::AwsDbInstance
- Defined in:
- lib/aws_region.rb
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 = {}) ⇒ Object
- #get_latest_db_snapshot(options = {}) ⇒ Object
-
#initialize(region, options = {}) ⇒ AwsDbInstance
constructor
A new instance of AwsDbInstance.
- #purge_db_snapshots ⇒ Object
- #status ⇒ Object
- #wait(options = {:desired_status => "available", :timeout => 600}) ⇒ Object
Constructor Details
#initialize(region, options = {}) ⇒ AwsDbInstance
Returns a new instance of AwsDbInstance.
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 |
# File 'lib/aws_region.rb', line 216 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 puts "Error, instance: #{@id} already exists" return end last = self.get_latest_db_snapshot({:snapshot_name => snapshot_name}) puts "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 = {} = @region.rds.({:resource_name => "arn:aws:rds:#{@region.region}:#{@region.account_id}:db:#{@id}"}) [:tag_list].each do |t| [t[:key].to_sym] = t[:value] end @endpoint = @_instance.endpoint[:address] end |
Instance Attribute Details
#endpoint ⇒ Object
Returns the value of attribute endpoint.
215 216 217 |
# File 'lib/aws_region.rb', line 215 def endpoint @endpoint end |
#id ⇒ Object
Returns the value of attribute id.
215 216 217 |
# File 'lib/aws_region.rb', line 215 def id @id end |
#region ⇒ Object
Returns the value of attribute region.
215 216 217 |
# File 'lib/aws_region.rb', line 215 def region @region end |
#tags ⇒ Object
Returns the value of attribute tags.
215 216 217 |
# File 'lib/aws_region.rb', line 215 def end |
Instance Method Details
#delete(options = {}) ⇒ Object
252 253 254 255 256 257 258 |
# File 'lib/aws_region.rb', line 252 def delete(={}) puts "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")}" } i = @region.rds.delete_db_instance(opts) end |
#get_latest_db_snapshot(options = {}) ⇒ Object
308 309 310 311 312 313 314 315 316 317 318 319 320 |
# File 'lib/aws_region.rb', line 308 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 ⇒ Object
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 |
# File 'lib/aws_region.rb', line 260 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 puts "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 puts "Error removing snapshot: #{i.db_snapshot_identifier}/#{i.snapshot_create_time.to_s}" end else puts "Keeping snapshot: #{i.db_snapshot_identifier}/#{i.snapshot_create_time.to_s}" end end end end |
#status ⇒ Object
304 305 306 |
# File 'lib/aws_region.rb', line 304 def status @_instance.db_instance_status end |
#wait(options = {:desired_status => "available", :timeout => 600}) ⇒ Object
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 |
# File 'lib/aws_region.rb', line 285 def wait( = {:desired_status => "available", :timeout => 600}) inst = @region.find_db_instances({:instance_id => @id})[0] if !inst puts "Error, instance: #{@id} not found" return end t0 = Time.now.to_i while inst.status != [:desired_status] inst = @region.find_db_instances({:instance_id => @id})[0] puts "Database: #{@id} at #{@endpoint}. Current status: #{inst.status}" if Time.now.to_i - t0 > [:timeout] puts "Timed out waiting for database: #{@id} at #{@endpoint} to move into status: #{options[:desired_status]}. Current status: #{inst.status}" return end sleep 20 end end |