Class: AwsCsshx::ConfigurationFile

Inherits:
Object
  • Object
show all
Defined in:
lib/aws_csshx/configuration_file.rb

Constant Summary collapse

CONFIG_OPTS =
%q{ec2_private_key aws_access_key aws_secret_key aws_region}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(conf) ⇒ ConfigurationFile

Returns a new instance of ConfigurationFile.



9
10
11
12
13
14
15
16
17
# File 'lib/aws_csshx/configuration_file.rb', line 9

def initialize(conf)
  @config_file = conf
  @options = {}
  if File.exists?(conf)
    load_config_from_file @config_file
  else
    raise FileDoesNotExist, "Config file #{@config_file} cannot be found."
  end
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



5
6
7
# File 'lib/aws_csshx/configuration_file.rb', line 5

def options
  @options
end

Instance Method Details

#load_config_from_file(file) ⇒ Object

Iterate over the config file line by line and throw away things not in our config opts array



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/aws_csshx/configuration_file.rb', line 20

def load_config_from_file(file)
  f = File.open(file, "r")
  f.each_line do |line|
    field,value = line.split('=')
    next if field.nil? or value.nil?
    field.strip!.downcase!
    value.strip!
    next unless CONFIG_OPTS.include?(field)
    @options[field] =
      if    field == 'ec2_private_key' then set_ec2_private_key(value)
      elsif field == 'aws_access_key' then set_aws_access_key(value)
      elsif field == 'aws_secret_key' then set_aws_secret_key(value)
      elsif field == 'aws_region' then set_aws_region(value)
      else  puts "No such option #{field} skipping..."
      end
  end
end

#set_aws_access_key(val) ⇒ Object



63
64
65
66
67
68
69
70
71
# File 'lib/aws_csshx/configuration_file.rb', line 63

def set_aws_access_key(val)
  if val
    val
  elsif ENV['AWS_ACCESS_KEY']
    ENV['AWS_ACCESS_KEY']
  elsif ENV['AMAZON_ACCESS_KEY_ID']
    ENV['AMAZON_ACCESS_KEY_ID']
  end
end

#set_aws_region(val) ⇒ Object

Private(ish) methods for config settings



43
44
45
46
47
48
49
50
51
# File 'lib/aws_csshx/configuration_file.rb', line 43

def set_aws_region(val)
  if val
    val
  elsif ENV['AWS_REGION']
    ENV['AWS_REGION']
  else
    'us-east-1'
  end
end

#set_aws_secret_key(val) ⇒ Object



53
54
55
56
57
58
59
60
61
# File 'lib/aws_csshx/configuration_file.rb', line 53

def set_aws_secret_key(val)
  if val
    val
  elsif ENV['AWS_SECRET_KEY']
    ENV['AWS_SECRET_KEY']
  elsif ENV['AMAZON_SECRET_ACCESS_KEY']
    ENV['AMAZON_SECRET_ACCESS_KEY']
  end
end

#set_ec2_private_key(key_file) ⇒ Object



73
74
75
76
77
78
79
80
# File 'lib/aws_csshx/configuration_file.rb', line 73

def set_ec2_private_key(key_file)
  if File.exists?(key_file)
    key_file
  else
    puts "#{key_file} does not exist, using default."
    ENV['EC2_PRIVATE_KEY']
  end
end