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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 21e7dde6cd66464fa7aff8bc01d5f1338e65ba5f180d27c6d7fc176fcf9dbff5
4
- data.tar.gz: 84a9f6eb6fd06373cbe68b686048107636cb2821eee5284b9a408062378e05f8
3
+ metadata.gz: 13ffe5bd9a5a78875bccaa274cc525629110332a84ce88c97928549b5857698c
4
+ data.tar.gz: 2543e29eb63533e946de460b89749c7853bcb419bafde2bbcd0b4b61cdc1cf6c
5
5
  SHA512:
6
- metadata.gz: 250624e8f6984ca9cee0685c8d99a990e6fb45c66a88a16a9fd1bc68f430d252c1377d55ca31f1b95806250748e1b8be683fef73f374e1dc708bbba77b65f8e0
7
- data.tar.gz: 5495ddec8a33acf9c2b4a8eeb8026f762d8f37716912bb238d6c797587403e62f0bb8023202d1755b522f6e09292146bc298e97e1bd335febed73124d54a7658
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 = ENV['AWS_REGION']
13
- @account_id = ENV['ACCOUNT_ID']
14
- @lambda_client = Aws::Lambda::Client.new(region: @aws_region)
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
- @aws_region = ENV['AWS_REGION']
10
- @account_id = ENV['ACCOUNT_ID']
11
-
12
- @ec2_client = Aws::EC2::Client.new(region: @aws_region)
13
- @events_client = Aws::CloudWatchEvents::Client.new(region: @aws_region)
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
- @aws_region = ENV['AWS_REGION']
14
- @iam_client = Aws::IAM::Client.new(region: @aws_region)
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: ENV['AWS_ACCESS_KEY_ID'],
18
- secret_access_key: ENV['AWS_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
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Hibernate
4
- VERSION = '0.1.3'
4
+ VERSION = '0.1.5'
5
5
  end
data/lib/hibernate.rb CHANGED
@@ -1,10 +1,7 @@
1
1
  require 'aws-sdk-ec2'
2
2
  require 'aws-sdk-cloudwatch'
3
- require 'dotenv'
4
3
  require 'json'
5
4
 
6
- Dotenv.load
7
-
8
5
  module Hibernate
9
6
  require_relative 'hibernate/version'
10
7
  end
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.3
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-05 00:00:00.000000000 Z
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