Class: EC2Manager
- Inherits:
-
Object
- Object
- EC2Manager
- Defined in:
- lib/hibernate/ec2_manager.rb
Instance Attribute Summary collapse
-
#instance_id ⇒ Object
writeonly
Sets the attribute instance_id.
-
#instance_name ⇒ Object
writeonly
Sets the attribute instance_name.
Instance Method Summary collapse
- #create_event_rule(start_cron, stop_cron) ⇒ Object
- #get_instance_id_by_name ⇒ Object
-
#initialize(instance_name = nil) ⇒ EC2Manager
constructor
A new instance of EC2Manager.
- #list_event_rules(options) ⇒ Object
- #remove_event_rule(rule_name) ⇒ Object
- #update_event_rule(rule_name:, start_cron:, stop_cron:, state:) ⇒ Object
Constructor Details
#initialize(instance_name = nil) ⇒ EC2Manager
Returns a new instance of EC2Manager.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/hibernate/ec2_manager.rb', line 8 def initialize(instance_name = nil) @instance_name = instance_name config_loader = Hibernate::ConfigLoader.new aws_credentials = config_loader.aws_credentials @aws_region = aws_credentials[:region] @account_id = aws_credentials[:account_id] access_key_id = aws_credentials[:access_key_id] secret_access_key = aws_credentials[:secret_access_key] @ec2_client = Aws::EC2::Client.new( region: @aws_region, access_key_id: access_key_id, secret_access_key: secret_access_key ) @events_client = Aws::CloudWatchEvents::Client.new( region: @aws_region, access_key_id: access_key_id, secret_access_key: secret_access_key ) @lambda_function_name = "ec2_auto_shutdown_start_function" @lambda_function_arn = construct_lambda_function_arn @instance_id = get_instance_id_by_name unless @instance_name.nil? @cloudwatch_event_manager = CloudWatchEventManager.new(@events_client, @instance_id, @instance_name, @lambda_function_arn) end |
Instance Attribute Details
#instance_id=(value) ⇒ Object (writeonly)
Sets the attribute instance_id
37 38 39 |
# File 'lib/hibernate/ec2_manager.rb', line 37 def instance_id=(value) @instance_id = value end |
#instance_name=(value) ⇒ Object (writeonly)
Sets the attribute instance_name
37 38 39 |
# File 'lib/hibernate/ec2_manager.rb', line 37 def instance_name=(value) @instance_name = value end |
Instance Method Details
#create_event_rule(start_cron, stop_cron) ⇒ Object
57 58 59 60 61 |
# File 'lib/hibernate/ec2_manager.rb', line 57 def create_event_rule(start_cron, stop_cron) @cloudwatch_event_manager.create_start_rule(start_cron) unless start_cron.nil? @cloudwatch_event_manager.create_stop_rule(stop_cron) unless stop_cron.nil? puts "CloudWatch Events created for instance '#{@instance_name}' (ID: #{@instance_id})." end |
#get_instance_id_by_name ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/hibernate/ec2_manager.rb', line 39 def get_instance_id_by_name response = @ec2_client.describe_instances({ filters: [ { name: "tag:Name", values: [@instance_name] }, { name: "instance-state-name", values: ["running", "stopped"] } ] }) if response.reservations.empty? puts "No EC2 instance found with the name '#{@instance_name}'." exit 1 end instance_id = response.reservations[0].instances[0].instance_id puts "Found EC2 instance ID: #{instance_id} for instance name: #{@instance_name}" instance_id end |
#list_event_rules(options) ⇒ Object
68 69 70 71 |
# File 'lib/hibernate/ec2_manager.rb', line 68 def list_event_rules() [:instance_id] = @instance_id unless @instance_id.nil? @cloudwatch_event_manager.list_event_rules() end |
#remove_event_rule(rule_name) ⇒ Object
63 64 65 66 |
# File 'lib/hibernate/ec2_manager.rb', line 63 def remove_event_rule(rule_name) @cloudwatch_event_manager.remove_rule(rule_name) puts "CloudWatch Events removed for rule: #{rule_name}." end |
#update_event_rule(rule_name:, start_cron:, stop_cron:, state:) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/hibernate/ec2_manager.rb', line 73 def update_event_rule(rule_name:, start_cron:, stop_cron:, state:) rule_exists = @cloudwatch_event_manager.rule_exists?(rule_name) unless rule_exists puts "Rule '#{rule_name}' does not exist." exit 1 end target_instance_id = @cloudwatch_event_manager.get_instance_id_from_rule(rule_name) if target_instance_id.nil? puts "No targets found for the rule '#{rule_name}'." exit 1 end instance_name = get_instance_name_by_id(target_instance_id) @cloudwatch_event_manager.instance_id = target_instance_id @cloudwatch_event_manager.instance_name = instance_name self.instance_id = target_instance_id self.instance_name = instance_name puts "Found instance ID: #{@instance_id} from the rule: #{rule_name}" if start_cron || stop_cron puts "Removing old rule: #{rule_name} as cron expression is being updated." remove_event_rule(rule_name) create_event_rule(start_cron, stop_cron) puts "Created new rule with updated cron expression for instance '#{@instance_name}' (ID: #{@instance_id})." end return if state.nil? @cloudwatch_event_manager.update_rule_state(rule_name, state) puts "Rule '#{rule_name}' has been #{state == 'enable' ? 'enabled' : 'disabled'}." end |