Class: Opsicle::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/opsicle/config.rb

Constant Summary collapse

FOG_CONFIG_PATH =
'~/.fog'
OPSICLE_CONFIG_PATH =
'./.opsicle'
SESSION_DURATION =
3600
MissingConfig =
Class.new(StandardError)
MissingEnvironment =
Class.new(StandardError)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#environmentObject (readonly)

Returns the value of attribute environment.



10
11
12
# File 'lib/opsicle/config.rb', line 10

def environment
  @environment
end

Class Method Details

.instanceObject



12
13
14
# File 'lib/opsicle/config.rb', line 12

def self.instance
  @instance ||= new
end

Instance Method Details

#aws_configObject



20
21
22
23
24
25
26
27
28
# File 'lib/opsicle/config.rb', line 20

def aws_config
  return @aws_config if @aws_config
  if fog_config[:mfa_serial_number]
    creds = get_session.credentials
    @aws_config = { access_key_id: creds.access_key_id, secret_access_key: creds.secret_access_key, session_token: creds.session_token }
  else
    @aws_config = { access_key_id: fog_config[:aws_access_key_id], secret_access_key: fog_config[:aws_secret_access_key] }
  end
end

#aws_credentialsObject



16
17
18
# File 'lib/opsicle/config.rb', line 16

def aws_credentials
  Aws::Credentials.new(aws_config[:access_key_id], aws_config[:secret_access_key], aws_config[:session_token])
end

#configure_aws_environment!(environment) ⇒ Object



39
40
41
# File 'lib/opsicle/config.rb', line 39

def configure_aws_environment!(environment)
@environment = environment.to_sym
end

#fog_configObject



30
31
32
33
# File 'lib/opsicle/config.rb', line 30

def fog_config
  return @fog_config if @fog_config
  @fog_config = load_config(File.expand_path(FOG_CONFIG_PATH))
end

#get_mfa_tokenObject



51
52
53
54
# File 'lib/opsicle/config.rb', line 51

def get_mfa_token
  return @token if @token
  @token = Output.ask("Enter MFA token: "){ |q|  q.validate = /^\d{6}$/ }
end

#get_sessionObject



56
57
58
59
60
61
62
63
64
# File 'lib/opsicle/config.rb', line 56

def get_session
  return @session if @session
  sts = Aws::STS::Client.new(access_key_id: fog_config[:aws_access_key_id],
                             secret_access_key: fog_config[:aws_secret_access_key],
                             region: 'us-east-1')
  @session = sts.get_session_token(duration_seconds: session_duration,
                                   serial_number: fog_config[:mfa_serial_number],
                                   token_code: get_mfa_token)
end

#load_config(file) ⇒ Object

Raises:



43
44
45
46
47
48
49
# File 'lib/opsicle/config.rb', line 43

def load_config(file)
  raise MissingConfig, "Missing configuration file: #{file}  Run 'opsicle help'" unless File.exist?(file)
  env_config = symbolize_keys(YAML.load_file(file))[environment] rescue {}
  raise MissingEnvironment, "Configuration for the \'#{environment}\' environment could not be found in #{file}" unless env_config != nil

  env_config
end

#opsworks_configObject



35
36
37
# File 'lib/opsicle/config.rb', line 35

def opsworks_config
  @opsworks_config ||= load_config(OPSICLE_CONFIG_PATH)
end

#session_durationObject



66
67
68
69
# File 'lib/opsicle/config.rb', line 66

def session_duration
  fog_config = load_config(File.expand_path(FOG_CONFIG_PATH))
  fog_config[:session_duration] || SESSION_DURATION
end

#symbolize_keys(hash) ⇒ Object

We want all ouf our YAML loaded keys to be symbols taken from devblog.avdi.org/2009/07/14/recursively-symbolize-keys/



73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/opsicle/config.rb', line 73

def symbolize_keys(hash)
  hash.inject({}){|result, (key, value)|
    new_key = case key
              when String then key.to_sym
              else key
              end
    new_value = case value
                when Hash then symbolize_keys(value)
                else value
                end
    result[new_key] = new_value
    result
  }
end