Class: EC2::Host::Config

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

Class Attribute Summary (collapse)

Class Method Summary (collapse)

Class Attribute Details

+ (Object) aws_access_key_id



45
46
47
48
# File 'lib/ec2/host/config.rb', line 45

def self.aws_access_key_id
  # ref. aws cli and terraform
  @aws_access_key_id ||= ENV['AWS_ACCESS_KEY_ID'] || config.fetch('AWS_ACCESS_KEY_ID', nil)
end

+ (Object) aws_credential_file



55
56
57
58
59
60
# File 'lib/ec2/host/config.rb', line 55

def self.aws_credential_file
  @aws_credential_file ||=
    ENV['AWS_CREDENTIALS_FILE'] || config.fetch('AWS_CREDENTIALS_FILE', nil) ||
    ENV['AWS_CREDENTIAL_FILE'] || config.fetch('AWS_CREDENTIAL_FILE', nil) || # ref. aws cli (supported lately)
    File.expand_path('~/.aws/credentials')
end

+ (Object) aws_profile



39
40
41
42
43
# File 'lib/ec2/host/config.rb', line 39

def self.aws_profile
  @aws_profile ||=
    ENV['AWS_PROFILE'] || config.fetch('AWS_PROFILE', nil) || # ref. old aws cli
    ENV['AWS_DEFAULT_PROFILE'] || config.fetch('AWS_DEFAULT_PROFILE', 'default') # ref. aws cli and terraform
end

+ (Object) aws_region



32
33
34
35
36
37
# File 'lib/ec2/host/config.rb', line 32

def self.aws_region
  @aws_region ||=
    ENV['AWS_REGION'] || config.fetch('AWS_REGION', nil) || # ref. old aws cli
    ENV['AWS_DEFAULT_REGION'] || config.fetch('AWS_DEFAULT_REGION', nil) || # ref. aws cli and terraform
    aws_config['region'] || raise('AWS_REGION nor AWS_DEFAULT_REGION is not set')
end

+ (Object) aws_secret_access_key



50
51
52
53
# File 'lib/ec2/host/config.rb', line 50

def self.aws_secret_access_key
  # ref. aws cli and terraform
  @aws_secret_access_key ||= ENV['AWS_SECRET_ACCESS_KEY'] || config.fetch('AWS_SECRET_ACCESS_KEY', nil)
end

+ (Object) config_file



28
29
30
# File 'lib/ec2/host/config.rb', line 28

def self.config_file
  @config_file ||= ENV.fetch('EC2_HOST_CONFIG_FILE', File.exist?('/etc/sysconfig/ec2-host') ? '/etc/sysconfig/ec2-host' : '/etc/default/ec2-host')
end

+ (Object) hostname_tag



83
84
85
# File 'lib/ec2/host/config.rb', line 83

def self.hostname_tag
  @hostname_tag ||= ENV['HOSTNAME_TAG'] || config.fetch('HOSTNAME_TAG', 'Name')
end

+ (Object) log_level



79
80
81
# File 'lib/ec2/host/config.rb', line 79

def self.log_level
  @log_level ||= ENV['LOG_LEVEL'] || config.fetch('LOG_LEVEL', 'info')
end

+ (Object) optional_array_tags



91
92
93
# File 'lib/ec2/host/config.rb', line 91

def self.optional_array_tags
  @optional_array_tags ||= (ENV['OPTIONAL_ARRAY_TAGS'] || config.fetch('OPTIONAL_ARRAY_TAGS', '')).split(',')
end

+ (Object) optional_string_tags



95
96
97
# File 'lib/ec2/host/config.rb', line 95

def self.optional_string_tags
  @optional_string_tags ||= (ENV['OPTIONAL_STRING_TAGS'] || config.fetch('OPTIONAL_STRING_TAGS', '')).split(',')
end

+ (Object) roles_tag



87
88
89
# File 'lib/ec2/host/config.rb', line 87

def self.roles_tag
  @roles_tag ||= ENV['ROLES_TAG'] || config.fetch('ROLES_TAG', 'Roles')
end

Class Method Details

+ (Object) array_tag_delimiter



103
104
105
# File 'lib/ec2/host/config.rb', line 103

def self.array_tag_delimiter
  @array_tag_delimiter ||= ENV['ARRAY_TAG_DELIMITER'] || config.fetch('ARRAY_TAG_DELIMITER', ',')
end

+ (Object) aws_config



66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/ec2/host/config.rb', line 66

def self.aws_config
  return @aws_config if @aws_config
  if File.readable?(aws_config_file)
    ini = IniFile.load(aws_config_file).to_h
    if aws_profile == 'default'
      @aws_config = ini['default']
    else
      @aws_config = ini["profile #{aws_profile}"]
    end
  end
  @aws_config ||= {}
end

+ (Object) aws_config_file



62
63
64
# File 'lib/ec2/host/config.rb', line 62

def self.aws_config_file
  @aws_config_file ||= ENV['AWS_CONFIG_FILE'] || config.fetch('AWS_CONFIG_FILE', nil) || File.expand_path('~/.aws/config')
end

+ (Object) config



129
130
131
132
133
134
135
136
137
138
# File 'lib/ec2/host/config.rb', line 129

def self.config
  return @config if @config
  @config = {}
  File.readlines(config_file).each do |line|
    next if line.start_with?('#')
    key, val = line.chomp.split('=', 2)
    @config[key] = val
  end
  @config
end

+ (Object) configure(params)



22
23
24
25
26
# File 'lib/ec2/host/config.rb', line 22

def self.configure(params)
  params.each do |key, val|
    send("#{key}=", val)
  end
end

+ (Object) optional_array_options

private



113
114
115
116
117
# File 'lib/ec2/host/config.rb', line 113

def self.optional_array_options
  @optional_array_options ||= Hash[optional_array_tags.map {|tag|
    [StringUtil.singularize(StringUtil.underscore(tag)), tag]
  }]
end

+ (Object) optional_options



125
126
127
# File 'lib/ec2/host/config.rb', line 125

def self.optional_options
  @optional_options ||= optional_array_options.merge(optional_string_options)
end

+ (Object) optional_string_options



119
120
121
122
123
# File 'lib/ec2/host/config.rb', line 119

def self.optional_string_options
  @optional_string_options ||= Hash[optional_string_tags.map {|tag|
    [StringUtil.underscore(tag), tag]
  }]
end

+ (Object) role_max_depth



107
108
109
# File 'lib/ec2/host/config.rb', line 107

def self.role_max_depth
  @role_max_depth ||= Integer(ENV['ROLE_MAX_DEPTH'] || config.fetch('ROLE_MAX_DEPTH', 3))
end

+ (Object) role_tag_delimiter



99
100
101
# File 'lib/ec2/host/config.rb', line 99

def self.role_tag_delimiter
  @role_tag_delimiter ||= ENV['ROLE_TAG_DELIMITER'] || config.fetch('ROLE_TAG_DELIMITER', ':')
end