hibernate 0.1.5 → 0.1.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/bin/hibernate CHANGED
@@ -1,154 +1,91 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require 'optparse'
4
- require_relative '../lib/hibernate/lambda_setup'
5
- require_relative '../lib/hibernate/ec2_manager'
6
-
7
- class HibernateCLI
8
- def self.run
9
- command = nil
10
- options = {}
11
-
12
- if ARGV.include?('setup')
13
- command = :setup
14
- ARGV.delete('setup')
15
- elsif ARGV.include?('rule')
16
- command = :manage_ec2
17
- ARGV.delete('rule')
18
- end
19
-
20
- parser = OptionParser.new do |parser|
21
- parser.banner = "Usage: hibernate [command] [options]"
22
-
23
- parser.on('--profile=<PROFILE_NAME>', 'Specify the profile name') do |profile|
24
- ENV['AWS_PROFILE'] = profile
25
- end
26
-
27
- parser.on('--instance-name=<INSTANCE_NAME>', 'Specify the EC2 instance name') do |instance_name|
28
- options[:instance_name] = instance_name
29
- end
30
-
31
- parser.on('--start-expression=<START_CRON>', 'Specify the cron expression to start the instance') do |start_cron|
32
- options[:start_cron] = start_cron
33
- end
34
-
35
- parser.on('--stop-expression=<STOP_CRON>', 'Specify the cron expression to stop the instance') do |stop_cron|
36
- options[:stop_cron] = stop_cron
37
- end
38
-
39
- parser.on('--start-instance=<true/false>', 'Filter start rules') do |start_instance|
40
- options[:start_instance] = start_instance == 'true'
41
- end
42
-
43
- parser.on('--stop-instance=<true/false>', 'Filter stop rules') do |stop_instance|
44
- options[:stop_instance] = stop_instance == 'true'
45
- end
46
-
47
- parser.on('--rule-name=<RULE Name>', 'Specify the rule name to remove or update') do |rule_name|
48
- options[:rule_name] = rule_name
49
- end
50
-
51
- parser.on('--update', 'Update an existing rule') do
52
- options[:update] = true
53
- end
54
-
55
- parser.on('--remove', 'Remove an existing rule') do
56
- options[:remove] = true
57
- end
58
-
59
- parser.on('--create', 'Create a new rule') do
60
- options[:create] = true
61
- end
62
-
63
- parser.on('--list', 'List rules') do
64
- options[:list] = true
65
- end
66
-
67
- parser.on('--state=<enable/disable>', 'Set rule state to either enable or disable') do |state|
68
- options[:state] = state
69
- end
70
- end
3
+ require 'hibernate'
4
+
5
+ SUB_COMMANDS = %w[setup rule].freeze
6
+ RULE_COMMANDS = %w[create list update remove].freeze
7
+
8
+ global_opts = Optimist::options do
9
+ version "Hibernate #{Hibernate::VERSION} (c) #{Date.today.year} Manish Sharma"
10
+ banner <<-USAGE
11
+ Automate the shutdown and start of our EC2 instances.
12
+
13
+ Usage:
14
+ hibernate <command> [options]
15
+
16
+ where <command> are:
17
+ setup : setup the AWS IAM role and Lambda
18
+ rule : Manage the schedule for start/stop of EC2 Instance.
19
+ - create : Create a new rule
20
+ - list : List existing rules
21
+ - update : Update an existing rule
22
+ - remove : Remove an existing rule
23
+ and [options] are:
24
+ these are specific to the <command>. use hibernate <command> --help to learn about [options] for that command.
25
+ USAGE
26
+ stop_on SUB_COMMANDS
27
+ end
71
28
 
72
- begin
73
- parser.parse!(ARGV)
74
- rescue OptionParser::ParseError => e
75
- puts e.message
76
- puts parser
77
- exit 1
29
+ def parse_options_for_rule(subcmd)
30
+ case subcmd
31
+ when 'create'
32
+ Optimist::options do
33
+ opt :profile, 'Specify the profile name', short: '-p', type: :string
34
+ opt :instance_name, 'Specify the EC2 instance name', short: '-i', type: :string
35
+ opt :start, 'Specify the cron expression to start the instance', short: '-s', type: :string
36
+ opt :stop, 'Specify the cron expression to stop the instance', short: '-e', type: :string
78
37
  end
79
-
80
- if command.nil?
81
- puts "Please provide a command."
82
- puts parser
83
- exit
38
+ when 'list'
39
+ Optimist::options do
40
+ opt :profile, 'Specify the profile name', short: '-p', type: :string
41
+ opt :instance_name, 'Specify the EC2 instance name', short: '-i', type: :string
42
+ opt :start, 'List only the start action rules', short: '-s'
43
+ opt :stop, 'List only the stop action rules', short: '-e'
84
44
  end
85
-
86
- case command
87
- when :setup
88
- create_lambda_function
89
- when :manage_ec2
90
- if options[:update]
91
- update_ec2_rule_command(options)
92
- elsif options[:remove]
93
- remove_ec2_rule_command(options)
94
- elsif options[:create]
95
- create_ec2_rule_command(options)
96
- elsif options[:list]
97
- list_ec2_rule_command(options)
98
- end
45
+ when 'update'
46
+ Optimist::options do
47
+ opt :profile, 'Specify the profile name', short: '-p', type: :string
48
+ opt :rule, 'Specify the rule name to update', short: '-r', type: :string
49
+ opt :start, 'Specify the cron expression to start the instance', short: '-s', type: :string
50
+ opt :stop, 'Specify the cron expression to stop the instance', short: '-e', type: :string
51
+ opt :state, 'Set rule state to either enable or disable', short: '-a', type: :string, default: nil
99
52
  end
100
- end
101
-
102
- def self.create_lambda_function
103
- LambdaSetup.new.run
104
- end
105
-
106
- def self.create_ec2_rule_command(options)
107
- if options[:instance_name].nil? || (options[:start_cron].nil? && options[:stop_cron].nil?)
108
- puts "Please provide the instance name, and at least one cron expression (start or stop)."
109
- puts "Usage: hibernate rule --create --instance-name=<INSTANCE_NAME> --start-expression=<START_CRON> --stop-expression=<STOP_CRON>"
110
- exit
111
- else
112
- ec2_manager = EC2Manager.new(options[:instance_name])
113
- ec2_manager.create_event_rule(options[:start_cron], options[:stop_cron])
53
+ when 'remove'
54
+ Optimist::options do
55
+ opt :profile, 'Specify the profile name', short: '-p', type: :string
56
+ opt :instance_name, 'Specify the EC2 instance name', short: '-i', type: :string
57
+ opt :rule, 'Specify the rule name to remove', short: '-r', type: :string
114
58
  end
59
+ else
60
+ Optimist::die "unknown subcommand #{subcmd.inspect}"
115
61
  end
62
+ end
116
63
 
117
- def self.remove_ec2_rule_command(options)
118
- if options[:rule_name].nil?
119
- puts "Please provide the rule name to remove."
120
- puts "Usage: hibernate rule --remove --rule-name=<RULE NAME>"
121
- exit
122
- else
123
- ec2_manager = EC2Manager.new(options[:instance_name])
124
- ec2_manager.remove_event_rule(options[:rule_name])
125
- end
64
+ cmd = ARGV.shift # get the subcommand
65
+ options = case cmd
66
+ when 'setup'
67
+ Optimist::options do
68
+ opt :profile, 'Specify the profile name', short: '-p', type: :string
126
69
  end
127
-
128
- def self.list_ec2_rule_command(options)
129
- ec2_manager = EC2Manager.new(options[:instance_name])
130
- ec2_manager.list_event_rules(options)
131
- end
132
-
133
- def self.update_ec2_rule_command(options)
134
- if options[:rule_name].nil?
135
- puts "Please provide the rule name to update."
136
- puts "Usage: hibernate rule --update --rule-name=<RULE_NAME> [--start-expression=<START_CRON>] [--stop-expression=<STOP_CRON>] [--state=<enable/disable>]"
137
- exit
138
- elsif options[:start_cron].nil? && options[:stop_cron].nil? && options[:state].nil?
139
- puts "Please provide atleast one attribute to update."
140
- puts "Usage: hibernate rule --update --rule-name=<RULE_NAME> [--start-expression=<START_CRON>] [--stop-expression=<STOP_CRON>] [--state=<enable/disable>]"
141
- exit
142
- else
143
- ec2_manager = EC2Manager.new(options[:instance_name])
144
- ec2_manager.update_event_rule(
145
- rule_name: options[:rule_name],
146
- start_cron: options[:start_cron],
147
- stop_cron: options[:stop_cron],
148
- state: options[:state]
149
- )
150
- end
70
+ when 'rule'
71
+ subcmd = ARGV.shift
72
+ unless RULE_COMMANDS.include?(subcmd)
73
+ Optimist::die "unknown subcommand #{subcmd.inspect}"
151
74
  end
75
+ parse_options_for_rule(subcmd)
76
+ else
77
+ Optimist::die "unknown command #{cmd.inspect}"
152
78
  end
153
79
 
154
- HibernateCLI.run
80
+ puts "Global options: #{global_opts.inspect}"
81
+ puts "Subcommand: #{cmd.inspect}"
82
+ puts "Subcommand options: #{options.inspect}"
83
+ puts "Remaining arguments: #{ARGV.inspect}"
84
+
85
+ HibernateCli.setup_profile(options[:profile])
86
+ case cmd
87
+ when 'setup'
88
+ HibernateCli.create_lambda_function
89
+ when 'rule'
90
+ HibernateCli.handle_rule_subcommand(subcmd, options)
91
+ end
@@ -0,0 +1,13 @@
1
+ aws_accounts:
2
+ profile_name:
3
+ account_id: "<account_id_1>"
4
+ region: us-east-1
5
+ credentials:
6
+ access_key_id: ACCESS_KEY_1
7
+ secret_access_key: SECRET_KEY_1
8
+ profile_other:
9
+ account_id:<account_id_2>
10
+ region: us-west-2
11
+ credentials:
12
+ access_key_id: ACCESS_KEY_2
13
+ secret_access_key: SECRET_KEY_2
data/docs/index.html ADDED
@@ -0,0 +1,182 @@
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>EC2 Auto Shutdown and Start</title>
7
+ <link rel="stylesheet" href="style.css">
8
+ <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
9
+ <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/themes/prism.min.css" />
10
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/prism.min.js"></script>
11
+ </head>
12
+ <body>
13
+ <header>
14
+ <h1>Hibernate</h1>
15
+ </header>
16
+ <main>
17
+ <section id="project-overview">
18
+ <h2>Project Overview</h2>
19
+ <p>This project automates the start and stop of AWS EC2 instances based on scheduled CloudWatch Events using an AWS Lambda function. It leverages Ruby AWS SDK for creating the Lambda function, CloudWatch Events, and EC2 instance management. You can configure the instance and schedule using the CLI.</p>
20
+ <div>
21
+ <pre class="mermaid">
22
+ graph TD
23
+ A[User - Command Line Interface]
24
+ B[CloudWatch Events]
25
+ C[Lambda Function]
26
+ D{Start or Stop EC2 Instance?}
27
+ E[Start EC2 Instance]
28
+ F[Stop EC2 Instance]
29
+ G[CloudWatch Logs]
30
+
31
+ subgraph AWS Cloud
32
+ B
33
+ C
34
+ D
35
+ E
36
+ F
37
+ G
38
+ end
39
+
40
+ A -->|Configure CloudWatch Events| B
41
+ B -->|Trigger Event| C
42
+ C -->|Execute Script| D
43
+ D -->|Start| E
44
+ D -->|Stop| F
45
+ C -->|Log Execution| G
46
+ A -.->|Monitor Logs| G
47
+
48
+ classDef default fill:#f7f7f7,stroke:#2c3e50,stroke-width:2px;
49
+ classDef input fill:#3498db,stroke:#2c3e50,stroke-width:2px,color:#fff;
50
+ classDef process fill:#1abc9c,stroke:#2c3e50,stroke-width:2px,color:#fff;
51
+ classDef decision fill:#f39c12,stroke:#2c3e50,stroke-width:2px;
52
+ classDef output fill:#2ecc71,stroke:#2c3e50,stroke-width:2px,color:#fff;
53
+ classDef storage fill:#e74c3c,stroke:#2c3e50,stroke-width:2px,color:#fff;
54
+
55
+ class A input;
56
+ class B,C process;
57
+ class D decision;
58
+ class E,F output;
59
+ class G storage;
60
+ </pre>
61
+ </div>
62
+ </section>
63
+
64
+ <section id="features">
65
+ <h2>Features</h2>
66
+ <ul>
67
+ <li><strong>Lambda function</strong>: Automatically manages the start and stop of EC2 instances.</li>
68
+ <li><strong>CloudWatch Events</strong>: Schedules based on cron expressions to run the Lambda function at specific times.</li>
69
+ <li><strong>CLI Interface</strong>: Easy-to-use interface for setting up the Lambda function and managing EC2 instance operations.</li>
70
+ </ul>
71
+ </section>
72
+
73
+ <section id="prerequisites">
74
+ <h2>Prerequisites</h2>
75
+ <ul>
76
+ <li><strong>AWS CLI</strong>: Ensure that the AWS CLI is installed and configured with access to your AWS account.</li>
77
+ <li><strong>Ruby 3.x</strong>: The project requires Ruby version 3.x. Install Ruby using a version manager like RVM or rbenv.</li>
78
+ <li><strong>AWS SDK for Ruby</strong>: The project uses <code class="language-shell">aws-sdk-ec2</code> and <code class="language-shell">aws-sdk-cloudwatchevents</code> gems.</li>
79
+ <li><strong>IAM Permissions</strong>: Ensure the AWS user you are using has permissions to manage CloudWatch Events, EC2, Lambda, and IAM roles.</li>
80
+ </ul>
81
+ </section>
82
+
83
+ <section id="setup-instructions">
84
+ <h2>Setup Instructions</h2>
85
+ <h3>1. Clone the Repository</h3>
86
+ <pre><code class="language-shell">git clone https://github.com/maniSHarma7575/hibernate.git
87
+ cd hibernate</code></pre>
88
+
89
+ <h3>2. Environment Configuration</h3>
90
+ <p>Create a <code class="language-shell">config.yaml</code> file at the root of your project and configure your AWS account details as follows:</p>
91
+ <pre><code class="language-shell">aws_accounts:
92
+ profile_name:
93
+ account_id: "<account_id_1>"
94
+ region: us-east-1
95
+ credentials:
96
+ access_key_id: ACCESS_KEY_1
97
+ secret_access_key: SECRET_KEY_1
98
+ profile_other:
99
+ account_id: <account_id_2>
100
+ region: us-west-2
101
+ credentials:
102
+ access_key_id: ACCESS_KEY_2
103
+ secret_access_key: SECRET_KEY_2</code></pre>
104
+
105
+ <h3>3. Install Dependencies</h3>
106
+ <p>Make sure you have the required gems and build the gem by running:</p>
107
+ <pre><code class="language-shell">rake install</code></pre>
108
+ </section>
109
+
110
+ <section id="command-usage">
111
+ <h2>Command Usage</h2>
112
+ <p>The <code class="language-shell">hibernate</code> CLI tool provides commands for setting up the Lambda function and managing EC2 instance schedules.</p>
113
+
114
+ <h3>Setup Command</h3>
115
+ <pre><code class="language-shell">hibernate setup --profile &lt;PROFILE_NAME&gt;</code></pre>
116
+ <p><strong>Options:</strong></p>
117
+ <ul>
118
+ <li><strong>-p, --profile</strong>: Specify the AWS profile name (required).</li>
119
+ </ul>
120
+ <p><strong>Example:</strong></p>
121
+ <pre><code class="language-shell">hibernate setup --profile production</code></pre>
122
+
123
+ <h3>Rule Command</h3>
124
+ <p>Manage schedules for EC2 instance start/stop actions.</p>
125
+
126
+ <h4>Create a Schedule</h4>
127
+ <pre><code class="language-shell">hibernate rule create --profile &lt;PROFILE_NAME&gt; --instance-name &lt;INSTANCE_NAME&gt; --start &lt;CRON_START&gt; --stop &lt;CRON_STOP&gt;</code></pre>
128
+ <p><strong>Options:</strong></p>
129
+ <ul>
130
+ <li><strong>-p, --profile</strong>: Specify the AWS profile name (required).</li>
131
+ <li><strong>-i, --instance-name</strong>: Specify the EC2 instance name (required).</li>
132
+ <li><strong>-s, --start</strong>: Specify the cron expression for starting the instance (optional).</li>
133
+ <li><strong>-e, --stop</strong>: Specify the cron expression for stopping the instance (optional).</li>
134
+ </ul>
135
+
136
+ <h4>List Schedules</h4>
137
+ <pre><code class="language-shell">hibernate rule list --profile &lt;PROFILE_NAME&gt; --instance-name &lt;INSTANCE_NAME&gt; [--start] [--stop]</code></pre>
138
+ <p><strong>Options:</strong></p>
139
+ <ul>
140
+ <li><strong>-p, --profile</strong>: Specify the AWS profile name (required).</li>
141
+ <li><strong>-i, --instance-name</strong>: Specify the EC2 instance name (required).</li>
142
+ <li><strong>-s, --start</strong>: List only the start action rules (optional).</li>
143
+ <li><strong>-e, --stop</strong>: List only the stop action rules (optional).</li>
144
+ </ul>
145
+
146
+ <h4>Update a Schedule</h4>
147
+ <pre><code class="language-shell">hibernate rule update --profile &lt;PROFILE_NAME&gt; --rule &lt;RULE_NAME&gt; --start &lt;NEW_CRON_START&gt; --stop &lt;NEW_CRON_STOP&gt; [--state &lt;enable|disable&gt;]</code></pre>
148
+ <p><strong>Options:</strong></p>
149
+ <ul>
150
+ <li><strong>-p, --profile</strong>: Specify the AWS profile name (required).</li>
151
+ <li><strong>-r, --rule</strong>: Specify the rule name to update (required).</li>
152
+ <li><strong>-s, --start</strong>: Specify the new cron expression for starting the instance (optional).</li>
153
+ <li><strong>-e, --stop</strong>: Specify the new cron expression for stopping the instance (optional).</li>
154
+ <li><strong>-a, --state</strong>: Set the rule state to either enable or disable (optional).</li>
155
+ </ul>
156
+
157
+ <h4>Remove a Schedule</h4>
158
+ <pre><code class="language-shell">hibernate rule remove --profile &lt;PROFILE_NAME&gt; --instance-name &lt;INSTANCE_NAME&gt; --rule &lt;RULE_NAME&gt;</code></pre>
159
+ <p><strong>Options:</strong></p>
160
+ <ul>
161
+ <li><strong>-p, --profile</strong>: Specify the AWS profile name (required).</li>
162
+ <li><strong>-i, --instance-name</strong>: Specify the EC2 instance name (required).</li>
163
+ <li><strong>-r, --rule</strong>: Specify the rule name to remove (required).</li>
164
+ </ul>
165
+ </section>
166
+ <section id="additional-resources">
167
+ <h2>Additional Resources</h2>
168
+ <ul>
169
+ <li><a href="https://aws.amazon.com/documentation/lambda/">AWS Lambda Documentation</a></li>
170
+ <li><a href="https://aws.amazon.com/documentation/cli/">AWS CLI Documentation</a></li>
171
+ <li><a href="https://aws.amazon.com/documentation/ec2/">AWS EC2 Documentation</a></li>
172
+ <li><a href="https://rubydoc.info/gems/aws-sdk-ec2/Aws/SDK/EC2">AWS SDK for Ruby EC2 Documentation</a></li>
173
+ <li><a href="https://rubydoc.info/gems/aws-sdk-cloudwatchevents/Aws/SDK/CloudWatchEvents">AWS SDK for Ruby CloudWatch Events Documentation</a></li>
174
+ </ul>
175
+ </section>
176
+ </main>
177
+
178
+ <footer>
179
+ <p>&copy; 2024 Manish Sharma. All rights reserved.</p>
180
+ </footer>
181
+ </body>
182
+ </html>
data/docs/style.css ADDED
@@ -0,0 +1,79 @@
1
+ /* Reset some basic elements */
2
+ body, h1, h2, h3, p, ul {
3
+ margin: 0;
4
+ padding: 0;
5
+ }
6
+
7
+ /* Apply a modern font */
8
+ body {
9
+ font-family: 'Roboto', sans-serif;
10
+ line-height: 1.6;
11
+ background-color: #F6F6F6; /* Light background for better contrast */
12
+ color: #4E4E4E; /* Charcoal text */
13
+ }
14
+
15
+ /* Header styling */
16
+ header {
17
+ background: black; /* Brown */
18
+ color: white;
19
+ padding: 1rem 0;
20
+ text-align: center;
21
+ }
22
+
23
+ /* Main styling */
24
+ main {
25
+ max-width: 800px;
26
+ margin: 2rem auto;
27
+ padding: 1rem;
28
+ background: white;
29
+ box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
30
+ border-radius: 8px;
31
+ }
32
+
33
+ /* Section headings */
34
+ h2 {
35
+ color: #8B5A2B; /* Brown */
36
+ margin-bottom: 0.5rem;
37
+ }
38
+
39
+ /* Links */
40
+ a {
41
+ color: #FF5733; /* Bright Orange */
42
+ text-decoration: none;
43
+ }
44
+
45
+ a:hover {
46
+ text-decoration: underline;
47
+ }
48
+
49
+ /* List styling */
50
+ ul {
51
+ list-style: none;
52
+ }
53
+
54
+ ul li {
55
+ padding: 0.5rem 0;
56
+ }
57
+
58
+ /* Button styling */
59
+ button {
60
+ background: #D2B48C; /* Tan */
61
+ color: white;
62
+ padding: 0.5rem 1rem;
63
+ border: none;
64
+ border-radius: 4px;
65
+ cursor: pointer;
66
+ transition: background 0.3s;
67
+ }
68
+
69
+ button:hover {
70
+ background: #FFD700; /* Gold */
71
+ }
72
+
73
+ /* Footer styling */
74
+ footer {
75
+ text-align: center;
76
+ padding: 1rem 0;
77
+ background: #4E4E4E; /* Charcoal */
78
+ color: white;
79
+ }
data/hibernate.gemspec ADDED
@@ -0,0 +1,43 @@
1
+ # my_gem.gemspec
2
+ require_relative 'lib/hibernate/version'
3
+
4
+ Gem::Specification.new do |spec|
5
+ spec.name = "hibernate"
6
+ spec.version = Hibernate::VERSION
7
+ spec.summary = "Automating the shutdown and start of our EC2 instances"
8
+ spec.description = "A Ruby gem to automate the shutdown and start of our EC2 instances"
9
+ spec.authors = ["Manish Sharma"]
10
+ spec.email = ["sharma.manish7575@gmail.com"]
11
+ spec.homepage = "https://github.com/maniSHarma7575/hibernate"
12
+ spec.license = "MIT"
13
+
14
+ spec.files = `git ls-files`.split($\)
15
+ spec.executables = spec.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
16
+ spec.bindir = 'bin'
17
+ spec.require_paths = ["lib"]
18
+
19
+ spec.required_ruby_version = '>= 3.2.2'
20
+
21
+ spec.add_dependency "aws-sdk-ec2", "~> 1.478"
22
+ spec.add_dependency "aws-sdk-cloudwatch", "~> 1.103"
23
+ spec.add_dependency "aws-sdk-lambda", "~> 1.136"
24
+ spec.add_dependency "aws-sdk-cloudwatchevents", "~> 1.83"
25
+ spec.add_dependency "aws-sdk-iam", "~> 1.111"
26
+
27
+ spec.add_dependency "dotenv", "~> 3.1"
28
+ spec.add_dependency "json", "~> 2.7"
29
+ spec.add_dependency "rubyzip", "~> 2.3"
30
+ spec.add_dependency "optimist", "~> 3.1"
31
+
32
+ # Specify development dependencies
33
+ spec.add_development_dependency "rspec", "~> 3.13"
34
+ spec.add_development_dependency "rubocop", "~> 1.66"
35
+
36
+ spec.metadata = {
37
+ "source_code_uri" => "https://github.com/maniSHarma7575/hibernate",
38
+ "homepage_uri" => "https://manisharma7575.github.io/hibernate/",
39
+ "bug_tracker_uri" => "https://github.com/maniSHarma7575/hibernate/issues"
40
+ }
41
+
42
+ spec.requirements << 'Git'
43
+ end
@@ -221,9 +221,10 @@ class CloudWatchEventManager
221
221
 
222
222
  def matches_criteria?(rule_instance_id, action, options)
223
223
  instance_id_match = options[:instance_id].nil? || options[:instance_id] == rule_instance_id
224
- action_match = (options[:start_instance] && action == 'start') ||
225
- (options[:stop_instance] && action == 'stop') ||
226
- (options[:start_instance].nil? && options[:stop_instance].nil?)
224
+ action_match = (options[:start] && action == 'start') ||
225
+ (options[:stop] && action == 'stop') ||
226
+ (options[:start].nil? && options[:stop].nil?) ||
227
+ ((!options[:start].nil? && !options[:stop].nil?) && (!options[:start] && !options[:stop]))
227
228
 
228
229
  instance_id_match && action_match
229
230
  end
@@ -0,0 +1,62 @@
1
+ class HibernateCli
2
+ def self.setup_profile(profile)
3
+ ENV['AWS_PROFILE'] = profile
4
+ end
5
+
6
+ def self.create_lambda_function
7
+ LambdaSetup.new.run
8
+ end
9
+
10
+ def self.handle_rule_subcommand(subcmd, options)
11
+ case subcmd
12
+ when 'create'
13
+ create_ec2_rule_command(options)
14
+ when 'list'
15
+ list_ec2_rule_command(options)
16
+ when 'update'
17
+ update_ec2_rule_command(options)
18
+ when 'remove'
19
+ remove_ec2_rule_command(options)
20
+ end
21
+ end
22
+
23
+ def self.create_ec2_rule_command(options)
24
+ validate_options(options, %i[instance_name], 'create')
25
+ ec2_manager = EC2Manager.new(options[:instance_name])
26
+ ec2_manager.create_event_rule(options[:start], options[:stop])
27
+ end
28
+
29
+ def self.list_ec2_rule_command(options)
30
+ ec2_manager = EC2Manager.new(options[:instance_name])
31
+ ec2_manager.list_event_rules(options)
32
+ end
33
+
34
+ def self.update_ec2_rule_command(options)
35
+ validate_options(options, %i[rule], 'update')
36
+ ec2_manager = EC2Manager.new(options[:instance_name])
37
+ ec2_manager.update_event_rule(
38
+ rule_name: options[:rule],
39
+ start_cron: options[:start],
40
+ stop_cron: options[:stop],
41
+ state: options[:state]
42
+ )
43
+ end
44
+
45
+ def self.remove_ec2_rule_command(options)
46
+ validate_options(options, [:rule], 'remove')
47
+ ec2_manager = EC2Manager.new(options[:instance_name])
48
+ ec2_manager.remove_event_rule(options[:rule])
49
+ end
50
+
51
+ private
52
+
53
+ def self.validate_options(options, required_fields, command)
54
+ missing_fields = required_fields.select { |field| options[field].nil? }
55
+ return if missing_fields.empty?
56
+
57
+ puts "Please provide #{missing_fields.join(', ')}."
58
+ puts "Usage: hibernate rule --#{command} #{required_fields.map { |field| "--#{field.to_s.tr('_', '-')}" }.join(' ')}"
59
+ exit
60
+ end
61
+ end
62
+
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Hibernate
4
- VERSION = '0.1.5'
4
+ VERSION = '0.1.7'
5
5
  end
data/lib/hibernate.rb CHANGED
@@ -1,7 +1,13 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'aws-sdk-ec2'
2
4
  require 'aws-sdk-cloudwatch'
3
5
  require 'json'
6
+ require 'optimist'
7
+ require 'hibernate/lambda_setup'
8
+ require 'hibernate/ec2_manager'
9
+ require 'hibernate/version'
10
+ require 'hibernate/hibernate_cli'
4
11
 
5
12
  module Hibernate
6
- require_relative 'hibernate/version'
7
13
  end
@@ -0,0 +1,8 @@
1
+ # Placeholder for your test cases
2
+ require 'hibernate'
3
+
4
+ RSpec.describe Hibernate do
5
+ it 'has a version number' do
6
+ expect(Hibernate::VERSION).not_to be nil
7
+ end
8
+ end