diff --git a/Readme.md b/Readme.md index 376bc68..8aa8d56 100644 --- a/Readme.md +++ b/Readme.md @@ -178,16 +178,18 @@ Create a `config.yaml` file at the root of your project and configure your AWS a ```yaml aws_accounts: - : + account_name_1: + account_id: "" region: us-east-1 credentials: - access_key_id: - secret_access_key: - : + access_key_id: ACCESS_KEY_1 + secret_access_key: SECRET_KEY_1 + account_name_2: + account_id: region: us-west-2 credentials: - access_key_id: - secret_access_key: + access_key_id: ACCESS_KEY_2 + secret_access_key: SECRET_KEY_2 ``` ## Usage @@ -199,7 +201,7 @@ The project includes a CLI to manage the setup of the Lambda function and CloudW Run the following command to set up the Lambda function: ```bash -bin/hibernate setup --account-id= +bin/hibernate setup --profile= ``` This command will: @@ -212,9 +214,9 @@ This command will: Use the following command to create or update rules for EC2 instances with scheduled start/stop times: ```bash -bin/hibernate rule --create --account-id= --instance-name= --start-expression="" --stop-expression="" +bin/hibernate rule --create --profile= --instance-name= --start-expression="" --stop-expression="" ``` -- `--account-id=`: (Optional) The AWS account ID to use. This will default to the cached account ID if not specified. +- `--profile=`: (Optional) The Profile to use from config. This will default to the default profile if not specified. - `--instance-name=`: The EC2 instance name tag. - `--start-expression=`: The cron expression for when to start the instance. - `--stop-expression=`: The cron expression for when to stop the instance. @@ -224,7 +226,7 @@ Both `--start-expression` and `--stop-expression` are optional, but at least one #### Example: ```bash -bin/hibernate rule --create --account-id= --instance-name="hibernate" --start-expression="55 9 * * ? *" --stop-expression="50 9 * * ? *" +bin/hibernate rule --create --profile= --instance-name="hibernate" --start-expression="55 9 * * ? *" --stop-expression="50 9 * * ? *" ``` This schedules the instance named "hibernate" to start at 9:55 AM UTC and stop at 9:50 AM UTC. @@ -234,13 +236,13 @@ This schedules the instance named "hibernate" to start at 9:55 AM UTC and stop a To list all scheduled start/stop rules for all EC2 instances: ```bash -bin/hibernate rule --list --account-id= +bin/hibernate rule --list --profile= ``` To list rules for a specific EC2 instance or filter by start/stop rules: ```bash -bin/hibernate rule --list --account-id= [--instance-name=] [--start-instance=true] [--stop-instance=true] +bin/hibernate rule --list --profile= [--instance-name=] [--start-instance=true] [--stop-instance=true] ``` Parameters: @@ -251,8 +253,8 @@ Parameters: #### Examples: ```bash -bin/hibernate rule --list --account-id= --instance-name=hibernate -bin/hibernate rule --list --account-id= --start-instance=true +bin/hibernate rule --list --profile= --instance-name=hibernate +bin/hibernate rule --list --profile= --start-instance=true ``` ### 4. Update Existing Rules @@ -260,7 +262,7 @@ bin/hibernate rule --list --account-id= --start-instance=true To update an existing rule: ```bash -bin/hibernate rule --update --account-id= --rule= [--start-expression=""] [--stop-expression=""] [--state=] +bin/hibernate rule --update --profile= --rule= [--start-expression=""] [--stop-expression=""] [--state=] ``` - `--rule=`: The name of the rule to update. @@ -271,7 +273,7 @@ bin/hibernate rule --update --account-id= --rule= [- #### Example: ```bash -bin/hibernate rule --update --account-id= --rule=StartInstanceRule-i-0ad52c31c25c659aa-9afb6fd6 --start-expression="20 12 * * ? *" +bin/hibernate rule --update --profile= --rule=StartInstanceRule-i-0ad52c31c25c659aa-9afb6fd6 --start-expression="20 12 * * ? *" ``` This updates the start rule to trigger at 12:20 PM UTC. @@ -279,7 +281,7 @@ This updates the start rule to trigger at 12:20 PM UTC. Disable a rule: ```bash -bin/hibernate rule --update --account-id= --rule=StopInstanceRule-i-0ad52c31c25c659aa-d1751fc4 --state=disable +bin/hibernate rule --update --profile= --rule=StopInstanceRule-i-0ad52c31c25c659aa-d1751fc4 --state=disable ``` ### 5. Remove Scheduled Start/Stop Rules @@ -287,7 +289,7 @@ bin/hibernate rule --update --account-id= --rule=StopInstanceRu Use the following command to remove a specific rule: ```bash -bin/hibernate rule --remove --account-id= --rule-name= +bin/hibernate rule --remove --profile= --rule-name= ``` - `--rule-name=`: Specify the rule you want to delete. @@ -295,7 +297,7 @@ bin/hibernate rule --remove --account-id= --rule-name= --rule-name= +bin/hibernate rule --remove --profile= --rule-name= ``` This will remove the specified CloudWatch rule. @@ -312,9 +314,7 @@ cron(Minutes Hours Day-of-month Month Day-of-week Year) ### Summary of Commands -Note on Account ID: The account-id will be cached once you have explicitly mentioned it in the command. For subsequent commands, you can ignore passing the account-id. If you want to override it, simply pass the account-id command line arguments again. - -- **Setup Lambda**: `bin/hibernate setup --account-id=` +- **Setup Lambda**: `bin/hibernate setup --profile=` - **Create/Update Rule**: `bin/hibernate rule --create --instance-name= --start-expression= --stop-expression=` - **List Rules**: `bin/hibernate rule --list [--instance-name=] [--start-instance=true] [--stop-instance=true]` - **Update Rule**: `bin/hibernate rule --update --rule= [--start-expression=] [--stop-expression=] [--state=]` diff --git a/bin/hibernate b/bin/hibernate index f9505b2..85a3e9c 100755 --- a/bin/hibernate +++ b/bin/hibernate @@ -20,8 +20,8 @@ class HibernateCLI parser = OptionParser.new do |parser| parser.banner = "Usage: hibernate [command] [options]" - parser.on('--account-id=', 'Specify the AWS account ID') do |account_id| - ENV['AWS_ACCOUNT_ID'] = account_id + parser.on('--profile=', 'Specify the profile name') do |profile| + ENV['AWS_PROFILE'] = profile end parser.on('--instance-name=', 'Specify the EC2 instance name') do |instance_name| diff --git a/config.yaml.sample b/config.yaml.sample index 815bee4..8693046 100644 --- a/config.yaml.sample +++ b/config.yaml.sample @@ -1,11 +1,12 @@ aws_accounts: - # Replace and with your actual AWS account IDs - "": + account_name_1: + account_id: "" region: us-east-1 credentials: access_key_id: ACCESS_KEY_1 secret_access_key: SECRET_KEY_1 - "": + account_name_2: + account_id: region: us-west-2 credentials: access_key_id: ACCESS_KEY_2 diff --git a/lib/hibernate/config_loader.rb b/lib/hibernate/config_loader.rb index eb705a0..72a31eb 100644 --- a/lib/hibernate/config_loader.rb +++ b/lib/hibernate/config_loader.rb @@ -1,49 +1,31 @@ require 'yaml' -require 'fileutils' module Hibernate class ConfigLoader - CACHE_FILE_PATH = File.expand_path('~/.aws_account_cache') - def initialize(config_path = 'config.yaml') @config_path = config_path - @account_id = fetch_account_id @config = load_config + @profile = ENV['AWS_PROFILE'] || default_profile validate_config end def aws_credentials - account_config = @config.dig('aws_accounts', @account_id) + account_config = @config.dig('aws_accounts', @profile) + if account_config.nil? - raise "Account ID #{@account_id} not found in the configuration file." + raise "Profile #{@profile} not found in the configuration file." end { - account_id: @account_id, + account_id: account_config['account_id'], region: account_config['region'], access_key_id: account_config.dig('credentials', 'access_key_id'), secret_access_key: account_config.dig('credentials', 'secret_access_key') } end - def self.cache_account_id(account_id) - FileUtils.mkdir_p(File.dirname(CACHE_FILE_PATH)) # Ensure directory exists - File.write(CACHE_FILE_PATH, account_id) - end - private - def fetch_account_id - if ENV['AWS_ACCOUNT_ID'] - self.class.cache_account_id(ENV['AWS_ACCOUNT_ID']) - ENV['AWS_ACCOUNT_ID'] - elsif File.exist?(CACHE_FILE_PATH) - File.read(CACHE_FILE_PATH).strip - else - raise "AWS account ID is not set. Please pass an account ID using the --account-id option or set it in the configuration." - end - end - def load_config if File.exist?(@config_path) YAML.load_file(@config_path) @@ -53,14 +35,22 @@ def load_config end def validate_config - unless @config.dig('aws_accounts', @account_id) - raise "Please set the 'account_id' in the configuration file." + unless @config.dig('aws_accounts', @profile) + raise "Profile #{@profile} is not defined in the configuration file." end - credentials = @config.dig('aws_accounts', @account_id, 'credentials') + credentials = @config.dig('aws_accounts', @profile, 'credentials') unless credentials && credentials['access_key_id'] && credentials['secret_access_key'] - raise "AWS credentials for account ID #{@account_id} are missing or incomplete." + raise "AWS credentials for profile #{@profile} are missing or incomplete." + end + end + + def default_profile + default_accounts = @config['aws_accounts'].select { |_, account| account['default'] } + if default_accounts.empty? + raise "No default profile found in the configuration." end + default_accounts.keys.first end end end