Class: Ufo::TaskDefinition::Helpers::Vars
Instance Method Summary
collapse
#pretty_home, #pretty_path, #pretty_time
#names
#aws_data
Constructor Details
#initialize(options = {}) ⇒ Vars
Returns a new instance of Vars.
10
11
12
13
14
|
# File 'lib/ufo/task_definition/helpers/vars.rb', line 10
def initialize(options={})
@file = options[:file]
@text = options[:text]
end
|
Instance Method Details
#content ⇒ Object
16
17
18
|
# File 'lib/ufo/task_definition/helpers/vars.rb', line 16
def content
@text || read(@file)
end
|
#conventional_pattern(name, value) ⇒ Object
Examples with config.secrets.provider = “ssm”
.secrets
DB_NAME
Results
DB_NAME=:APP/:ENV/:SECRET_NAME
88
89
90
91
92
93
94
95
96
|
# File 'lib/ufo/task_definition/helpers/vars.rb', line 88
def conventional_pattern(name, value)
secrets = Ufo.config.secrets
provider = secrets.provider namespace = provider == "ssm" ? "parameter/" : "secret:"
pattern = secrets.pattern[provider] pattern = pattern.sub(':SECRET_NAME', name)
"arn:aws:#{provider}:#{region}:#{account}:#{namespace}#{pattern}"
end
|
#env ⇒ Object
29
30
31
32
33
34
35
36
37
38
39
40
41
|
# File 'lib/ufo/task_definition/helpers/vars.rb', line 29
def env
lines = filtered_lines(content)
lines.map do |line|
key,*value = line.strip.split("=").map do |x|
remove_surrounding_quotes(x.strip)
end
value = value.join('=')
{
name: key,
value: value,
}
end
end
|
#expansion(arn) ⇒ Object
arn:aws:ssm:us-west-2:111111111111:parameter/demo/dev/DB-NAME arn:aws:ssm:us-west-2:111111111111:parameter/demo/dev/DB-NAME
70
71
72
73
74
75
76
|
# File 'lib/ufo/task_definition/helpers/vars.rb', line 70
def expansion(arn)
md = arn.match(/(.*:)(parameter\/|secret:)(.*)/)
prefix, type, name = md[1], md[2], md[3]
expanded_name = names.expansion(name, dasherize: false) "#{prefix}#{type}#{expanded_name}"
end
|
#filtered_lines(content) ⇒ Object
108
109
110
111
112
113
114
115
116
|
# File 'lib/ufo/task_definition/helpers/vars.rb', line 108
def filtered_lines(content)
lines = content.split("\n")
lines.map! { |l| l.sub(/\s+#.*/,'').strip }
lines = lines.reject { |l| l =~ /(^|\s)#/i }
lines = lines.reject { |l| l.strip.empty? }
end
|
#normalize_to_arn(name, value) ⇒ Object
55
56
57
58
59
60
61
62
63
64
65
66
|
# File 'lib/ufo/task_definition/helpers/vars.rb', line 55
def normalize_to_arn(name, value)
case value
when /^ssm:/i
value.sub(/^ssm:/i, "arn:aws:ssm:#{region}:#{account}:parameter/")
when /^secretsmanager:/i
value.sub(/^secretsmanager:/i, "arn:aws:secretsmanager:#{region}:#{account}:secret:")
when '' conventional_pattern(name, value)
else
value end
end
|
#read(path) ⇒ Object
20
21
22
23
24
25
26
27
|
# File 'lib/ufo/task_definition/helpers/vars.rb', line 20
def read(path)
full_path = "#{Ufo.root}/#{path}"
unless File.exist?(full_path)
puts "The #{pretty_path(full_path)} env file could not be found. Are you sure it exists?"
exit 1
end
IO.read(full_path)
end
|
#remove_surrounding_quotes(s) ⇒ Object
98
99
100
101
102
103
104
105
106
|
# File 'lib/ufo/task_definition/helpers/vars.rb', line 98
def remove_surrounding_quotes(s)
if s =~ /^"/ && s =~ /"$/
s.sub(/^["]/, '').gsub(/["]$/,'') elsif s =~ /^'/ && s =~ /'$/
s.sub(/^[']/, '').gsub(/[']$/,'') else
s
end
end
|
#secrets ⇒ Object
43
44
45
46
47
48
49
50
51
52
53
|
# File 'lib/ufo/task_definition/helpers/vars.rb', line 43
def secrets
secrets = env
secrets.map do |item|
value = item.delete(:value)
arn = normalize_to_arn(item[:name], value)
value = expansion(arn)
value = value.sub('parameter//','parameter/') item[:valueFrom] = value
end
secrets
end
|