hibernate 0.1.3 → 0.1.5
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.
- checksums.yaml +4 -4
- data/bin/hibernate +4 -0
- data/lib/hibernate/cloud_watch_event_manager.rb +9 -3
- data/lib/hibernate/config_loader.rb +56 -0
- data/lib/hibernate/ec2_manager.rb +20 -5
- data/lib/hibernate/lambda_setup.rb +12 -5
- data/lib/hibernate/version.rb +1 -1
- data/lib/hibernate.rb +0 -3
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 13ffe5bd9a5a78875bccaa274cc525629110332a84ce88c97928549b5857698c
|
4
|
+
data.tar.gz: 2543e29eb63533e946de460b89749c7853bcb419bafde2bbcd0b4b61cdc1cf6c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4c0db9fdb1c8ebbd341cdca34dd56db61a89b975ab8a815b34991a09bb9858c325ada48c8fa5708cf99244e6fb7b8aae9b0566fadb15fff1d9ea7be8d8143d03
|
7
|
+
data.tar.gz: 0307afb2b28ec2d44d7901d6e60cc3c63c0ed228015a8829690d3676922aeb6c28f90d197df680c8ca77b268941b890a0c0dbbd8989862b7fdb28a635d3b6559
|
data/bin/hibernate
CHANGED
@@ -20,6 +20,10 @@ class HibernateCLI
|
|
20
20
|
parser = OptionParser.new do |parser|
|
21
21
|
parser.banner = "Usage: hibernate [command] [options]"
|
22
22
|
|
23
|
+
parser.on('--profile=<PROFILE_NAME>', 'Specify the profile name') do |profile|
|
24
|
+
ENV['AWS_PROFILE'] = profile
|
25
|
+
end
|
26
|
+
|
23
27
|
parser.on('--instance-name=<INSTANCE_NAME>', 'Specify the EC2 instance name') do |instance_name|
|
24
28
|
options[:instance_name] = instance_name
|
25
29
|
end
|
@@ -2,16 +2,22 @@ require 'aws-sdk-cloudwatchevents'
|
|
2
2
|
require 'json'
|
3
3
|
require 'dotenv/load'
|
4
4
|
require 'digest'
|
5
|
+
require_relative 'config_loader'
|
5
6
|
|
6
7
|
class CloudWatchEventManager
|
7
8
|
def initialize(events_client, instance_id = nil, instance_name = nil, lambda_function_arn)
|
9
|
+
config_loader = Hibernate::ConfigLoader.new
|
8
10
|
@events_client = events_client
|
9
11
|
@instance_id = instance_id
|
10
12
|
@instance_name = instance_name
|
11
13
|
@lambda_function_arn = lambda_function_arn
|
12
|
-
@aws_region =
|
13
|
-
@account_id =
|
14
|
-
@lambda_client = Aws::Lambda::Client.new(
|
14
|
+
@aws_region = config_loader.aws_credentials[:region]
|
15
|
+
@account_id = config_loader.aws_credentials[:account_id]
|
16
|
+
@lambda_client = Aws::Lambda::Client.new(
|
17
|
+
region: @aws_region,
|
18
|
+
access_key_id: config_loader.aws_credentials[:access_key_id],
|
19
|
+
secret_access_key: config_loader.aws_credentials[:secret_access_key]
|
20
|
+
)
|
15
21
|
end
|
16
22
|
|
17
23
|
attr_writer :instance_id, :instance_name
|
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'yaml'
|
2
|
+
|
3
|
+
module Hibernate
|
4
|
+
class ConfigLoader
|
5
|
+
def initialize(config_path = 'config.yaml')
|
6
|
+
@config_path = config_path
|
7
|
+
@config = load_config
|
8
|
+
@profile = ENV['AWS_PROFILE'] || default_profile
|
9
|
+
validate_config
|
10
|
+
end
|
11
|
+
|
12
|
+
def aws_credentials
|
13
|
+
account_config = @config.dig('aws_accounts', @profile)
|
14
|
+
|
15
|
+
if account_config.nil?
|
16
|
+
raise "Profile #{@profile} not found in the configuration file."
|
17
|
+
end
|
18
|
+
|
19
|
+
{
|
20
|
+
account_id: account_config['account_id'],
|
21
|
+
region: account_config['region'],
|
22
|
+
access_key_id: account_config.dig('credentials', 'access_key_id'),
|
23
|
+
secret_access_key: account_config.dig('credentials', 'secret_access_key')
|
24
|
+
}
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def load_config
|
30
|
+
if File.exist?(@config_path)
|
31
|
+
YAML.load_file(@config_path)
|
32
|
+
else
|
33
|
+
raise "Configuration file not found: #{@config_path}"
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def validate_config
|
38
|
+
unless @config.dig('aws_accounts', @profile)
|
39
|
+
raise "Profile #{@profile} is not defined in the configuration file."
|
40
|
+
end
|
41
|
+
|
42
|
+
credentials = @config.dig('aws_accounts', @profile, 'credentials')
|
43
|
+
unless credentials && credentials['access_key_id'] && credentials['secret_access_key']
|
44
|
+
raise "AWS credentials for profile #{@profile} are missing or incomplete."
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def default_profile
|
49
|
+
default_accounts = @config['aws_accounts'].select { |_, account| account['default'] }
|
50
|
+
if default_accounts.empty?
|
51
|
+
raise "No default profile found in the configuration."
|
52
|
+
end
|
53
|
+
default_accounts.keys.first
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -2,15 +2,30 @@ require 'aws-sdk-ec2'
|
|
2
2
|
require 'dotenv/load'
|
3
3
|
require 'json'
|
4
4
|
require_relative 'cloud_watch_event_manager' # Adjust the path to where the new class is located
|
5
|
+
require_relative 'config_loader'
|
5
6
|
|
6
7
|
class EC2Manager
|
7
8
|
def initialize(instance_name = nil)
|
8
9
|
@instance_name = instance_name
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
@
|
13
|
-
@
|
10
|
+
config_loader = Hibernate::ConfigLoader.new
|
11
|
+
aws_credentials = config_loader.aws_credentials
|
12
|
+
|
13
|
+
@aws_region = aws_credentials[:region]
|
14
|
+
@account_id = aws_credentials[:account_id]
|
15
|
+
access_key_id = aws_credentials[:access_key_id]
|
16
|
+
secret_access_key = aws_credentials[:secret_access_key]
|
17
|
+
|
18
|
+
@ec2_client = Aws::EC2::Client.new(
|
19
|
+
region: @aws_region,
|
20
|
+
access_key_id: access_key_id,
|
21
|
+
secret_access_key: secret_access_key
|
22
|
+
)
|
23
|
+
|
24
|
+
@events_client = Aws::CloudWatchEvents::Client.new(
|
25
|
+
region: @aws_region,
|
26
|
+
access_key_id: access_key_id,
|
27
|
+
secret_access_key: secret_access_key
|
28
|
+
)
|
14
29
|
|
15
30
|
@lambda_function_name = "ec2_auto_shutdown_start_function"
|
16
31
|
@lambda_function_arn = construct_lambda_function_arn
|
@@ -1,21 +1,28 @@
|
|
1
1
|
require 'aws-sdk-lambda'
|
2
2
|
require 'aws-sdk-iam'
|
3
3
|
require 'zip'
|
4
|
-
require 'dotenv/load'
|
5
4
|
require 'fileutils'
|
6
5
|
require 'pry'
|
6
|
+
require_relative 'config_loader'
|
7
7
|
|
8
8
|
class LambdaSetup
|
9
9
|
def initialize
|
10
|
+
config_loader = Hibernate::ConfigLoader.new
|
11
|
+
@aws_region = config_loader.aws_credentials[:region]
|
12
|
+
|
10
13
|
@lambda_role_name = "ec2-auto-shutdown-start"
|
11
14
|
@lambda_handler = "ec2_auto_shutdown_start_function"
|
12
15
|
@lambda_zip = "lambda_function.zip"
|
13
|
-
@
|
14
|
-
|
16
|
+
@iam_client = Aws::IAM::Client.new(
|
17
|
+
region: @aws_region,
|
18
|
+
access_key_id: config_loader.aws_credentials[:access_key_id],
|
19
|
+
secret_access_key: config_loader.aws_credentials[:secret_access_key]
|
20
|
+
)
|
21
|
+
|
15
22
|
@lambda_client = Aws::Lambda::Client.new(
|
16
23
|
region: @aws_region,
|
17
|
-
access_key_id:
|
18
|
-
secret_access_key:
|
24
|
+
access_key_id: config_loader.aws_credentials[:access_key_id],
|
25
|
+
secret_access_key: config_loader.aws_credentials[:secret_access_key]
|
19
26
|
)
|
20
27
|
end
|
21
28
|
|
data/lib/hibernate/version.rb
CHANGED
data/lib/hibernate.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hibernate
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Manish Sharma
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-10-
|
11
|
+
date: 2024-10-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: aws-sdk-ec2
|
@@ -119,6 +119,7 @@ files:
|
|
119
119
|
- bin/hibernate
|
120
120
|
- lib/hibernate.rb
|
121
121
|
- lib/hibernate/cloud_watch_event_manager.rb
|
122
|
+
- lib/hibernate/config_loader.rb
|
122
123
|
- lib/hibernate/ec2_manager.rb
|
123
124
|
- lib/hibernate/lambda_function/ec2_auto_shutdown_start_function.rb
|
124
125
|
- lib/hibernate/lambda_setup.rb
|