Class: Dome::Environment

Inherits:
Object
  • Object
show all
Defined in:
lib/dome/environment.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#accountObject (readonly)

Returns the value of attribute account.



3
4
5
# File 'lib/dome/environment.rb', line 3

def 
  @account
end

#environmentObject (readonly)

Returns the value of attribute environment.



3
4
5
# File 'lib/dome/environment.rb', line 3

def environment
  @environment
end

Instance Method Details

#accountsObject



14
15
16
# File 'lib/dome/environment.rb', line 14

def accounts
  %W(#{team}-dev #{team}-prd)
end

#aws_credentialsObject



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_messageObject



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_messageObject



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/dome/environment.rb', line 63

def invalid_environment_message
  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 [-4..-1] == '-dev'
          non_production_environments
        else
          production_environments
        end
  puts "Valid environments are: #{env}."
  exit 1
end

#non_production_environmentsObject



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_keysObject



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_environmentsObject



22
23
24
# File 'lib/dome/environment.rb', line 22

def production_environments
  %w(infraprd prd)
end

#teamObject



10
11
12
# File 'lib/dome/environment.rb', line 10

def team
  @account.split('-').first
end

#valid_account?(account_name) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
41
# File 'lib/dome/environment.rb', line 38

def valid_account?()
  puts "Account: #{.colorize(:green)}"
  accounts.include? 
end

#valid_environment?(account_name, environment_name) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
46
47
48
49
50
# File 'lib/dome/environment.rb', line 43

def valid_environment?(, environment_name)
  puts "Environment: #{environment_name.colorize(:green)}"
  if .split('-')[1] == 'dev'
    non_production_environments.include? environment_name
  else
    production_environments.include? environment_name
  end
end