hibernate 0.1.3 → 0.1.4
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 +66 -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: bb37df3a1d0e0b64ae191626bfe015bca529ff81582816dea8a9198acc388f0b
|
4
|
+
data.tar.gz: 60eb78bd95762490b910a6dc949c666126c4e72fbd0822c5103b197642de8fbb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3111686232c5dc02bb56a7c9f3b59ed0d9fc90a3ac1f986b32038cbbc41abeb62840228712e10ada6d7920082f1201aaa52b8cc88fbb4a33f53ec6ee843ed42a
|
7
|
+
data.tar.gz: 7d0389381b47eaa7be1d3938219c770b10ed5cca756cb861f4aceca5567e5abb4c460cbc56d9cddab42d548dfb768976573301b359083d045c38c453831aca72
|
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('--account-id=<ACCOUNT_ID>', 'Specify the AWS account ID') do |account_id|
|
24
|
+
ENV['AWS_ACCOUNT_ID'] = account_id
|
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,66 @@
|
|
1
|
+
require 'yaml'
|
2
|
+
require 'fileutils'
|
3
|
+
|
4
|
+
module Hibernate
|
5
|
+
class ConfigLoader
|
6
|
+
CACHE_FILE_PATH = File.expand_path('~/.aws_account_cache')
|
7
|
+
|
8
|
+
def initialize(config_path = 'config.yaml')
|
9
|
+
@config_path = config_path
|
10
|
+
@account_id = fetch_account_id
|
11
|
+
@config = load_config
|
12
|
+
validate_config
|
13
|
+
end
|
14
|
+
|
15
|
+
def aws_credentials
|
16
|
+
account_config = @config.dig('aws_accounts', @account_id)
|
17
|
+
if account_config.nil?
|
18
|
+
raise "Account ID #{@account_id} not found in the configuration file."
|
19
|
+
end
|
20
|
+
|
21
|
+
{
|
22
|
+
account_id: @account_id,
|
23
|
+
region: account_config['region'],
|
24
|
+
access_key_id: account_config.dig('credentials', 'access_key_id'),
|
25
|
+
secret_access_key: account_config.dig('credentials', 'secret_access_key')
|
26
|
+
}
|
27
|
+
end
|
28
|
+
|
29
|
+
def self.cache_account_id(account_id)
|
30
|
+
FileUtils.mkdir_p(File.dirname(CACHE_FILE_PATH)) # Ensure directory exists
|
31
|
+
File.write(CACHE_FILE_PATH, account_id)
|
32
|
+
end
|
33
|
+
|
34
|
+
private
|
35
|
+
|
36
|
+
def fetch_account_id
|
37
|
+
if ENV['AWS_ACCOUNT_ID']
|
38
|
+
self.class.cache_account_id(ENV['AWS_ACCOUNT_ID'])
|
39
|
+
ENV['AWS_ACCOUNT_ID']
|
40
|
+
elsif File.exist?(CACHE_FILE_PATH)
|
41
|
+
File.read(CACHE_FILE_PATH).strip
|
42
|
+
else
|
43
|
+
raise "AWS account ID is not set. Please pass an account ID using the --account-id option or set it in the configuration."
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def load_config
|
48
|
+
if File.exist?(@config_path)
|
49
|
+
YAML.load_file(@config_path)
|
50
|
+
else
|
51
|
+
raise "Configuration file not found: #{@config_path}"
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def validate_config
|
56
|
+
unless @config.dig('aws_accounts', @account_id)
|
57
|
+
raise "Please set the 'account_id' in the configuration file."
|
58
|
+
end
|
59
|
+
|
60
|
+
credentials = @config.dig('aws_accounts', @account_id, 'credentials')
|
61
|
+
unless credentials && credentials['access_key_id'] && credentials['secret_access_key']
|
62
|
+
raise "AWS credentials for account ID #{@account_id} are missing or incomplete."
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
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.4
|
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
|