Skip to content

Commit

Permalink
Merge pull request #18 from maniSHarma7575/17-use-the-profile-instead…
Browse files Browse the repository at this point in the history
…-of-account-id

[UPDATE] Instead of account-id in the command
  • Loading branch information
maniSHarma7575 authored Oct 15, 2024
2 parents edb1831 + 79e3ba1 commit c78a7b4
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 54 deletions.
44 changes: 22 additions & 22 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,16 +178,18 @@ Create a `config.yaml` file at the root of your project and configure your AWS a

```yaml
aws_accounts:
<account_id_1>:
account_name_1:
account_id: "<account_id_1>"
region: us-east-1
credentials:
access_key_id: <your-access-key-id-1>
secret_access_key: <your-secret-access-key-1>
<account_id_2>:
access_key_id: ACCESS_KEY_1
secret_access_key: SECRET_KEY_1
account_name_2:
account_id:<account_id_2>
region: us-west-2
credentials:
access_key_id: <your-access-key-id-2>
secret_access_key: <your-secret-access-key-2>
access_key_id: ACCESS_KEY_2
secret_access_key: SECRET_KEY_2
```
## Usage
Expand All @@ -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=<your_account_id>
bin/hibernate setup --profile=<profile_name>
```

This command will:
Expand All @@ -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=<your_account_id> --instance-name=<instance_name> --start-expression="<cron>" --stop-expression="<cron>"
bin/hibernate rule --create --profile=<profile_name> --instance-name=<instance_name> --start-expression="<cron>" --stop-expression="<cron>"
```
- `--account-id=<your_account_id>`: (Optional) The AWS account ID to use. This will default to the cached account ID if not specified.
- `--profile=<profile_name>`: (Optional) The Profile to use from config. This will default to the default profile if not specified.
- `--instance-name=<instance_name>`: The EC2 instance name tag.
- `--start-expression=<cron>`: The cron expression for when to start the instance.
- `--stop-expression=<cron>`: The cron expression for when to stop the instance.
Expand All @@ -224,7 +226,7 @@ Both `--start-expression` and `--stop-expression` are optional, but at least one
#### Example:

```bash
bin/hibernate rule --create --account-id=<your_account_id> --instance-name="hibernate" --start-expression="55 9 * * ? *" --stop-expression="50 9 * * ? *"
bin/hibernate rule --create --profile=<profile_name> --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.
Expand All @@ -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=<your_account_id>
bin/hibernate rule --list --profile=<profile_name>
```

To list rules for a specific EC2 instance or filter by start/stop rules:

```bash
bin/hibernate rule --list --account-id=<your_account_id> [--instance-name=<instance_name>] [--start-instance=true] [--stop-instance=true]
bin/hibernate rule --list --profile=<profile_name> [--instance-name=<instance_name>] [--start-instance=true] [--stop-instance=true]
```

Parameters:
Expand All @@ -251,16 +253,16 @@ Parameters:
#### Examples:

```bash
bin/hibernate rule --list --account-id=<your_account_id> --instance-name=hibernate
bin/hibernate rule --list --account-id=<your_account_id> --start-instance=true
bin/hibernate rule --list --profile=<profile_name> --instance-name=hibernate
bin/hibernate rule --list --profile=<profile_name> --start-instance=true
```

### 4. Update Existing Rules

To update an existing rule:

```bash
bin/hibernate rule --update --account-id=<your_account_id> --rule=<rule_name> [--start-expression="<new_cron>"] [--stop-expression="<new_cron>"] [--state=<new_state>]
bin/hibernate rule --update --profile=<profile_name> --rule=<rule_name> [--start-expression="<new_cron>"] [--stop-expression="<new_cron>"] [--state=<new_state>]
```

- `--rule=<rule_name>`: The name of the rule to update.
Expand All @@ -271,31 +273,31 @@ bin/hibernate rule --update --account-id=<your_account_id> --rule=<rule_name> [-
#### Example:

```bash
bin/hibernate rule --update --account-id=<your_account_id> --rule=StartInstanceRule-i-0ad52c31c25c659aa-9afb6fd6 --start-expression="20 12 * * ? *"
bin/hibernate rule --update --profile=<profile_name> --rule=StartInstanceRule-i-0ad52c31c25c659aa-9afb6fd6 --start-expression="20 12 * * ? *"
```

This updates the start rule to trigger at 12:20 PM UTC.

Disable a rule:

```bash
bin/hibernate rule --update --account-id=<your_account_id> --rule=StopInstanceRule-i-0ad52c31c25c659aa-d1751fc4 --state=disable
bin/hibernate rule --update --profile=<profile_name> --rule=StopInstanceRule-i-0ad52c31c25c659aa-d1751fc4 --state=disable
```

### 5. Remove Scheduled Start/Stop Rules

Use the following command to remove a specific rule:

```bash
bin/hibernate rule --remove --account-id=<your_account_id> --rule-name=<rule_name>
bin/hibernate rule --remove --profile=<profile_name> --rule-name=<rule_name>
```

- `--rule-name=<rule_name>`: Specify the rule you want to delete.

#### Example:

```bash
bin/hibernate rule --remove --account-id=<your_account_id> --rule-name=<rule_name>
bin/hibernate rule --remove --profile=<profile_name> --rule-name=<rule_name>
```

This will remove the specified CloudWatch rule.
Expand All @@ -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=<your_account_id>`
- **Setup Lambda**: `bin/hibernate setup --profile=<profile_name>`
- **Create/Update Rule**: `bin/hibernate rule --create --instance-name=<instance_name> --start-expression=<cron> --stop-expression=<cron>`
- **List Rules**: `bin/hibernate rule --list [--instance-name=<instance_name>] [--start-instance=true] [--stop-instance=true]`
- **Update Rule**: `bin/hibernate rule --update --rule=<rule_name> [--start-expression=<new_cron>] [--stop-expression=<new_cron>] [--state=<new_state>]`
Expand Down
4 changes: 2 additions & 2 deletions bin/hibernate
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ class HibernateCLI
parser = OptionParser.new do |parser|
parser.banner = "Usage: hibernate [command] [options]"

parser.on('--account-id=<ACCOUNT_ID>', 'Specify the AWS account ID') do |account_id|
ENV['AWS_ACCOUNT_ID'] = account_id
parser.on('--profile=<PROFILE_NAME>', 'Specify the profile name') do |profile|
ENV['AWS_PROFILE'] = profile
end

parser.on('--instance-name=<INSTANCE_NAME>', 'Specify the EC2 instance name') do |instance_name|
Expand Down
7 changes: 4 additions & 3 deletions config.yaml.sample
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
aws_accounts:
# Replace <account_id_1> and <account_id_2> with your actual AWS account IDs
"<account_id_1>":
account_name_1:
account_id: "<account_id_1>"
region: us-east-1
credentials:
access_key_id: ACCESS_KEY_1
secret_access_key: SECRET_KEY_1
"<account_id_2>":
account_name_2:
account_id:<account_id_2>
region: us-west-2
credentials:
access_key_id: ACCESS_KEY_2
Expand Down
44 changes: 17 additions & 27 deletions lib/hibernate/config_loader.rb
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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

0 comments on commit c78a7b4

Please sign in to comment.