Class: Ufo::DSL::Helper::Vars

Inherits:
Object
  • Object
show all
Extended by:
Memoist
Defined in:
lib/ufo/dsl/helper/vars.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Vars

Returns a new instance of Vars.



7
8
9
10
11
# File 'lib/ufo/dsl/helper/vars.rb', line 7

def initialize(options={})
  # use either file or text. text takes higher precedence
  @file = options[:file]
  @text = options[:text]
end

Instance Method Details

#accountObject



93
94
95
# File 'lib/ufo/dsl/helper/vars.rb', line 93

def 
  aws_data.
end

#aws_dataObject



84
85
86
# File 'lib/ufo/dsl/helper/vars.rb', line 84

def aws_data
  AwsData.new
end

#contentObject



13
14
15
# File 'lib/ufo/dsl/helper/vars.rb', line 13

def content
  @text || read(@file)
end

#envObject



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/ufo/dsl/helper/vars.rb', line 26

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

#expand_secret(value) ⇒ Object



49
50
51
52
53
54
55
56
57
58
# File 'lib/ufo/dsl/helper/vars.rb', line 49

def expand_secret(value)
  case value
  when /^ssm:/i
    value.sub(/^ssm:/i, "arn:aws:ssm:#{region}:#{}:parameter/")
  when /^secretsmanager:/i
    value.sub(/^secretsmanager:/i, "arn:aws:secretsmanager:#{region}:#{}:secret:")
  else
    value # assume full arn has been passed
  end
end

#filtered_lines(content) ⇒ Object



74
75
76
77
78
79
80
81
82
# File 'lib/ufo/dsl/helper/vars.rb', line 74

def filtered_lines(content)
  lines = content.split("\n")
  # remove comment at the end of the line
  lines.map! { |l| l.sub(/\s+#.*/,'').strip }
  # filter out commented lines
  lines = lines.reject { |l| l =~ /(^|\s)#/i }
  # filter out empty lines
  lines = lines.reject { |l| l.strip.empty? }
end

#read(path) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/ufo/dsl/helper/vars.rb', line 17

def read(path)
  full_path = "#{Ufo.root}/#{path}"
  unless File.exist?(full_path)
    puts "The #{full_path} env file could not be found.  Are you sure it exists?"
    exit 1
  end
  IO.read(full_path)
end

#regionObject



89
90
91
# File 'lib/ufo/dsl/helper/vars.rb', line 89

def region
  aws_data.region
end

#remove_surrounding_quotes(s) ⇒ Object



64
65
66
67
68
69
70
71
72
# File 'lib/ufo/dsl/helper/vars.rb', line 64

def remove_surrounding_quotes(s)
  if s =~ /^"/ && s =~ /"$/
    s.sub(/^["]/, '').gsub(/["]$/,'') # remove surrounding double quotes
  elsif s =~ /^'/ && s =~ /'$/
    s.sub(/^[']/, '').gsub(/[']$/,'') # remove surrounding single quotes
  else
    s
  end
end

#secretsObject



40
41
42
43
44
45
46
47
# File 'lib/ufo/dsl/helper/vars.rb', line 40

def secrets
  secrets = env
  secrets.map do |item|
    value = item.delete(:value)
    item[:valueFrom] = substitute(expand_secret(value))
  end
  secrets
end

#substitute(value) ⇒ Object



60
61
62
# File 'lib/ufo/dsl/helper/vars.rb', line 60

def substitute(value)
  value.gsub(":UFO_ENV", Ufo.env)
end