Class: ECSUtil::Config

Inherits:
Hashie::Mash
  • Object
show all
Defined in:
lib/ecsutil/config.rb

Class Method Summary collapse

Class Method Details

.read(path, stage, outputs = {}) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/ecsutil/config.rb', line 9

def self.read(path, stage, outputs = {})
  data = File.read(path).gsub(/(\$tf.([\w]+))+/i) do |m|
    key = $2
    fail "Terraform output key #{key} not found!" unless outputs[key]
    outputs[key]
  end

  result = YAML.load(data).tap do |config|
    fail "App name is required"   unless config["app"]
    fail "App env is required"    unless config["env"]
    fail "Cluster is required"    unless config["cluster"]
    fail "Repository is required" unless config["repository"]
    
    # Check AWS configuration
    if !config["aws_profile"] && !ENV["AWS_PROFILE"]
      fail "AWS profile is not set! Set 'aws_profile' var in config or use AWS_PROFILE env var!"
    end

    # Override environment variable in case if it was set
    ENV["AWS_PROFILE"] = config["aws_profile"]

    # Set stage and namespace
    config["user"] ||= `whoami`.strip
    config["stage"] ||= stage
    config["namespace"] ||= sprintf("%s-%s", config["app"], config["env"])

    # Set default sections
    config["tasks"] ||= {}
    config["scheduled_tasks"] ||= {}
    config["services"] ||= {}

    # Parent dir
    parent_dir = File.expand_path(File.join(File.dirname(path), ".."))

    # Set secrets
    config.merge!(
      "secrets_vaultpass" => File.join(parent_dir, "vaultpass"),
      "secrets_file"      => File.join(File.dirname(path), "#{stage}/secrets"),
      "secrets_key"       => outputs["kms_key"],
      "secrets_prefix"    => sprintf("/%s", config["namespace"]),
      "secrets_data"      => {}
    )

    # Set default vars
    config["git_commit"] ||= `git rev-parse HEAD`.strip
    config["git_branch"] ||= `git rev-parse --abbrev-ref HEAD`.strip
  end

  Config.new(result)
end