Skip to content

Commit

Permalink
[FIX] Simplify commands
Browse files Browse the repository at this point in the history
  • Loading branch information
maniSHarma7575 committed Oct 19, 2024
1 parent 22fcfc6 commit cebefc7
Show file tree
Hide file tree
Showing 11 changed files with 280 additions and 264 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
.env
*.zip
*.gem
config.yaml
config.yaml
pkg
Gemfile.lock
15 changes: 2 additions & 13 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,19 +1,8 @@
source 'https://rubygems.org'

ruby '3.2.2'

gem 'aws-sdk-ec2'
gem 'aws-sdk-cloudwatch'
gem 'aws-sdk-lambda'
gem 'aws-sdk-cloudwatchevents'
gem 'aws-sdk-iam'

gem 'dotenv'
gem 'json'
gem 'rubyzip'
gem 'rake'
gemspec

group :development do
gem 'rspec'
gem 'rubocop'
gem 'pry'
end
34 changes: 20 additions & 14 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
PATH
remote: .
specs:
hibernate (0.1.6)
aws-sdk-cloudwatch (~> 1.103)
aws-sdk-cloudwatchevents (~> 1.83)
aws-sdk-ec2 (~> 1.478)
aws-sdk-iam (~> 1.111)
aws-sdk-lambda (~> 1.136)
dotenv (~> 3.1)
json (~> 2.7)
optimist (~> 3.1)
rubyzip (~> 2.3)

GEM
remote: https://rubygems.org/
specs:
Expand Down Expand Up @@ -33,6 +47,7 @@ GEM
json (2.7.2)
language_server-protocol (3.17.0.3)
method_source (1.1.0)
optimist (3.1.0)
parallel (1.26.3)
parser (3.3.5.0)
ast (~> 2.4.1)
Expand All @@ -42,6 +57,7 @@ GEM
method_source (~> 1.0)
racc (1.8.1)
rainbow (3.1.1)
rake (13.2.1)
regexp_parser (2.9.2)
rspec (3.13.0)
rspec-core (~> 3.13.0)
Expand Down Expand Up @@ -74,23 +90,13 @@ GEM

PLATFORMS
arm64-darwin-22
ruby

DEPENDENCIES
aws-sdk-cloudwatch
aws-sdk-cloudwatchevents
aws-sdk-ec2
aws-sdk-iam
aws-sdk-lambda
dotenv
json
hibernate!
pry
rspec
rubocop
rubyzip

RUBY VERSION
ruby 3.2.2p53
rake
rspec (~> 3.13)
rubocop (~> 1.66)

BUNDLED WITH
2.5.4
3 changes: 3 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env rake

require 'bundler/gem_tasks'
157 changes: 80 additions & 77 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,15 +164,7 @@ git clone [https://github.com/maniSHarma7575/hibernate.git](https://github.com/m
cd hibernate
```

### 2. Install Dependencies

Make sure you have the required gems by running:

```bash
bundle install
```

### 3. Environment Configuration
### 2. Environment Configuration

Create a `config.yaml` file at the root of your project and configure your AWS account details as follows:

Expand All @@ -192,134 +184,145 @@ aws_accounts:
secret_access_key: SECRET_KEY_2
```
## Usage
The project includes a CLI to manage the setup of the Lambda function and CloudWatch Events. Here’s how you can use it:
### 3. Install Dependencies
### 1. Setup the Lambda Function
Run the following command to set up the Lambda function:
Make sure you have the required gems and build the gem by running:
```bash
bin/hibernate setup --profile=<profile_name>
rake install
```

This command will:
- Create an IAM role (if it doesn't exist).
- Create the Lambda function.
- Attach the necessary permissions for managing EC2 instances.
Here’s how the Usage section for the Readme.md can be structured based on your script:

## Usage

Automate the shutdown and start of EC2 instances using scheduled rules.

### 2. Manage EC2 Instances with Scheduled Start/Stop
### Available Commands

Use the following command to create or update rules for EC2 instances with scheduled start/stop times:
- **setup**: Set up the AWS IAM role and Lambda function for managing EC2 instance schedules.
- **rule**: Manage schedules for EC2 instance start/stop actions.
- **create**: Create a new start/stop schedule.
- **list**: List existing schedules.
- **update**: Update an existing schedule.
- **remove**: Remove an existing schedule.

```bash
bin/hibernate rule --create --profile=<profile_name> --instance-name=<instance_name> --start-expression="<cron>" --stop-expression="<cron>"
hibernate --help
```
- `--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.

Both `--start-expression` and `--stop-expression` are optional, but at least one must be provided.
### Command Usage

#### Example:
#### Setup Command

```bash
bin/hibernate rule --create --profile=<profile_name> --instance-name="hibernate" --start-expression="55 9 * * ? *" --stop-expression="50 9 * * ? *"
hibernate setup --profile <PROFILE_NAME>
```

This schedules the instance named "hibernate" to start at 9:55 AM UTC and stop at 9:50 AM UTC.
Options:

### 3. List Scheduled Start/Stop Rules
- -p, --profile: Specify the AWS profile name (required).

To list all scheduled start/stop rules for all EC2 instances:
#### Rule Command

```bash
bin/hibernate rule --list --profile=<profile_name>
```

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

```bash
bin/hibernate rule --list --profile=<profile_name> [--instance-name=<instance_name>] [--start-instance=true] [--stop-instance=true]
hibernate rule create --profile <PROFILE_NAME> --instance-name <INSTANCE_NAME> --start <CRON_START> --stop <CRON_STOP>
```

Parameters:
- `--instance-name=<instance_name>`: The name tag of the EC2 instance whose rules you want to list (optional).
- `--start-instance=true`: (Optional) Include this flag to list only the start rules.
- `--stop-instance=true`: (Optional) Include this flag to list only the stop rules.
Options:

- -p, --profile: Specify the AWS profile name (required).
- -i, --instance-name: Specify the EC2 instance name (required).
- -s, --start: Specify the cron expression for starting the instance (optional).
- -e, --stop: Specify the cron expression for stopping the instance (optional).

#### Examples:
**List Schedules**

```bash
bin/hibernate rule --list --profile=<profile_name> --instance-name=hibernate
bin/hibernate rule --list --profile=<profile_name> --start-instance=true
hibernate rule list --profile <PROFILE_NAME> --instance-name <INSTANCE_NAME> [--start] [--stop]
```
Options:

### 4. Update Existing Rules
- -p, --profile: Specify the AWS profile name (required).
- -i, --instance-name: Specify the EC2 instance name (required).
- -s, --start: List only the start action rules (optional).
- -e, --stop: List only the stop action rules (optional).

To update an existing rule:
**Update a Schedule**

```bash
bin/hibernate rule --update --profile=<profile_name> --rule=<rule_name> [--start-expression="<new_cron>"] [--stop-expression="<new_cron>"] [--state=<new_state>]
hibernate rule update --profile <PROFILE_NAME> --rule <RULE_NAME> --start <NEW_CRON_START> --stop <NEW_CRON_STOP> [--state <enable|disable>]
```

- `--rule=<rule_name>`: The name of the rule to update.
- `--start-expression="<new_cron>"`: (Optional) The new cron expression for starting the instance.
- `--stop-expression="<new_cron>"`: (Optional) The new cron expression for stopping the instance.
- `--state=<new_state>`: (Optional) The new state of the rule. Use "enable" to enable the rule or "disable" to disable it.
Options:

- -p, --profile: Specify the AWS profile name (required).
- -r, --rule: Specify the rule name to update (required).
- -s, --start: Specify the new cron expression for starting the instance (optional).
- -e, --stop: Specify the new cron expression for stopping the instance (optional).
- -a, --state: Set the rule state to either enable or disable (optional).

#### Example:
**Remove a Schedule**

```bash
bin/hibernate rule --update --profile=<profile_name> --rule=StartInstanceRule-i-0ad52c31c25c659aa-9afb6fd6 --start-expression="20 12 * * ? *"
hibernate rule remove --profile <PROFILE_NAME> --instance-name <INSTANCE_NAME> --rule <RULE_NAME>
```

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

Disable a rule:
- -p, --profile: Specify the AWS profile name (required).
- -i, --instance-name: Specify the EC2 instance name (required).
- -r, --rule: Specify the rule name to remove (required).

Examples

1. Setting up the environment:

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

### 5. Remove Scheduled Start/Stop Rules

Use the following command to remove a specific rule:
2. Creating a start/stop schedule for an EC2 instance:

```bash
bin/hibernate rule --remove --profile=<profile_name> --rule-name=<rule_name>
hibernate rule create --profile production --instance-name my-ec2-instance --start "cron(0 8 * * ? *)" --stop "cron(0 18 * * ? *)"
```

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

#### Example:
3. Listing all schedules for an EC2 instance:

```bash
bin/hibernate rule --remove --profile=<profile_name> --rule-name=<rule_name>
hibernate rule list --profile production --instance-name my-ec2-instance
```

This will remove the specified CloudWatch rule.
4. Updating an existing rule:

### Cron Expressions in AWS CloudWatch Events
```bash
hibernate rule update --profile production --rule my-rule --start "cron(0 7 * * ? *)" --state enable
```

CloudWatch Events uses the following format for cron expressions:
5. Removing a schedule:

```
cron(Minutes Hours Day-of-month Month Day-of-week Year)
```bash
hibernate rule remove --profile production --instance-name my-ec2-instance --rule my-rule
```

- **Example**: `cron(55 9 * * ? *)` runs every day at 9:55 AM UTC.
#### Shortcut Options

### Summary of Commands
You can use these shortcut flags for commonly used options:

- **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>]`
- **Remove Rule**: `bin/hibernate rule --remove --rule-name=<rule_name>`
- -p for --profile
- -i for --instance-name
- -s for --start
- -e for --stop
- -r for --rule
- -a for --state

```bash
hibernate rule create -p production -i my-ec2-instance -s "cron(0 8 * * ? *)" -e "cron(0 18 * * ? *)
```
---
### Resources:
Expand Down
Loading

0 comments on commit cebefc7

Please sign in to comment.