Class: Dome::Environment
- Inherits:
-
Object
- Object
- Dome::Environment
- Defined in:
- lib/dome/environment.rb
Instance Attribute Summary collapse
-
#account ⇒ Object
readonly
Returns the value of attribute account.
-
#environment ⇒ Object
readonly
Returns the value of attribute environment.
Instance Method Summary collapse
- #accounts ⇒ Object
- #aws_credentials ⇒ Object
-
#initialize(directories = Dir.pwd.split('/')) ⇒ Environment
constructor
A new instance of Environment.
- #invalid_account_message ⇒ Object
- #invalid_environment_message ⇒ Object
- #non_production_environments ⇒ Object
- #populate_aws_access_keys ⇒ Object
- #production_environments ⇒ Object
- #team ⇒ Object
- #valid_account?(account_name) ⇒ Boolean
- #valid_environment?(account_name, environment_name) ⇒ Boolean
Constructor Details
#initialize(directories = Dir.pwd.split('/')) ⇒ Environment
Returns a new instance of Environment.
5 6 7 8 |
# File 'lib/dome/environment.rb', line 5 def initialize(directories = Dir.pwd.split('/')) @environment = directories[-1] @account = directories[-2] end |
Instance Attribute Details
#account ⇒ Object (readonly)
Returns the value of attribute account.
3 4 5 |
# File 'lib/dome/environment.rb', line 3 def account @account end |
#environment ⇒ Object (readonly)
Returns the value of attribute environment.
3 4 5 |
# File 'lib/dome/environment.rb', line 3 def environment @environment end |
Instance Method Details
#accounts ⇒ Object
14 15 16 |
# File 'lib/dome/environment.rb', line 14 def accounts %W(#{team}-dev #{team}-prd) end |
#aws_credentials ⇒ Object
26 27 28 29 30 |
# File 'lib/dome/environment.rb', line 26 def aws_credentials @aws_credentials ||= AWS::ProfileParser.new.get(@account) rescue RuntimeError raise "No credentials found for account: '#{@account}'." end |
#invalid_account_message ⇒ Object
52 53 54 55 56 57 58 59 60 61 |
# File 'lib/dome/environment.rb', line 52 def puts "\n'#{@account}' is not a valid account.\n".colorize(:red) puts "The 'account' and 'environment' variables are assigned based on your current directory.\n".colorize(:red) puts "The expected directory structure is '.../<account>/<environment>'\n".colorize(:red) puts "Valid accounts are: #{accounts}." puts "\nEither:" puts '1. Set your .aws/config to one of the valid accounts above.' puts '2. Ensure you are running this from the correct directory.' exit 1 end |
#invalid_environment_message ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/dome/environment.rb', line 63 def puts "\n'#{@environment}' is not a valid environment for the account: '#{@account}'.\n".colorize(:red) puts "The 'account' and 'environment' variables are assigned based on your current directory.\n".colorize(:red) puts "The expected directory structure is '.../<account>/<environment>'\n".colorize(:red) env = if account[-4..-1] == '-dev' non_production_environments else production_environments end puts "Valid environments are: #{env}." exit 1 end |
#non_production_environments ⇒ Object
18 19 20 |
# File 'lib/dome/environment.rb', line 18 def non_production_environments %w(infradev dev sit qa qa1 qa2 stg) end |
#populate_aws_access_keys ⇒ Object
32 33 34 35 36 |
# File 'lib/dome/environment.rb', line 32 def populate_aws_access_keys ENV['AWS_ACCESS_KEY_ID'] = aws_credentials[:access_key_id] ENV['AWS_SECRET_ACCESS_KEY'] = aws_credentials[:secret_access_key] ENV['AWS_DEFAULT_REGION'] = aws_credentials[:region] end |
#production_environments ⇒ Object
22 23 24 |
# File 'lib/dome/environment.rb', line 22 def production_environments %w(infraprd prd) end |
#team ⇒ Object
10 11 12 |
# File 'lib/dome/environment.rb', line 10 def team @account.split('-').first end |
#valid_account?(account_name) ⇒ Boolean
38 39 40 41 |
# File 'lib/dome/environment.rb', line 38 def valid_account?(account_name) puts "Account: #{account_name.colorize(:green)}" accounts.include? account_name end |
#valid_environment?(account_name, environment_name) ⇒ Boolean
43 44 45 46 47 48 49 50 |
# File 'lib/dome/environment.rb', line 43 def valid_environment?(account_name, environment_name) puts "Environment: #{environment_name.colorize(:green)}" if account_name.split('-')[1] == 'dev' non_production_environments.include? environment_name else production_environments.include? environment_name end end |