Class: JFlow::TerminationProtector
- Inherits:
-
Object
- Object
- JFlow::TerminationProtector
- Defined in:
- lib/jflow/termination_protector.rb
Instance Method Summary collapse
- #get_asg_name ⇒ Object
-
#instance_data ⇒ Object
Returns a hash of instance data, including region, instance id + more.
- #instance_id ⇒ Object
- #region ⇒ Object
- #set_protection(protect_status) ⇒ Object
Instance Method Details
#get_asg_name ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/jflow/termination_protector.rb', line 20 def get_asg_name ec2_client = Aws::EC2::Client.new(region: region) = ec2_client.(filters: [ { name: "resource-id", values: [instance_id] } ])[0] asg_name = .select{|tag| tag.key == "aws:autoscaling:groupName"}.first.value JFlow.configuration.logger.debug "Discovered autoscaling group name #{asg_name}" asg_name end |
#instance_data ⇒ Object
Returns a hash of instance data, including region, instance id + more
16 17 18 |
# File 'lib/jflow/termination_protector.rb', line 16 def instance_data @instance_data ||= JSON.parse(Net::HTTP.get(URI.parse('http://169.254.169.254/latest/dynamic/instance-identity/document'))) end |
#instance_id ⇒ Object
11 12 13 |
# File 'lib/jflow/termination_protector.rb', line 11 def instance_id instance_data['instanceId'] end |
#region ⇒ Object
7 8 9 |
# File 'lib/jflow/termination_protector.rb', line 7 def region instance_data['region'] end |
#set_protection(protect_status) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/jflow/termination_protector.rb', line 34 def set_protection(protect_status) JFlow.configuration.logger.debug "Setting termination protection status to #{protect_status} for instance #{@instance_id} in region #{@region}" begin asg_client = Aws::AutoScaling::Client.new(region: region) asg_client.set_instance_protection({ instance_ids: [instance_id], auto_scaling_group_name: get_asg_name, protected_from_scale_in: protect_status }) rescue => e JFlow.configuration.logger.debug "Something went wrong setting termination proection: #{e.inspect}" end end |