Class: Terraform::Stacks

Inherits:
Object
  • Object
show all
Defined in:
lib/terraform_dsl/stacks.rb

Overview

Singleton to keep track of stack templates

Defined Under Namespace

Classes: MalformedConfig

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.stacksObject (readonly)

Returns the value of attribute stacks.



11
12
13
# File 'lib/terraform_dsl/stacks.rb', line 11

def stacks
  @stacks
end

Class Method Details

.base_configObject



37
38
39
40
41
42
# File 'lib/terraform_dsl/stacks.rb', line 37

def base_config
  {
    'common' => {},
    'stacks' => [ base_stack_config ],
  }
end

.base_stack_configObject



44
45
46
47
48
49
50
51
52
# File 'lib/terraform_dsl/stacks.rb', line 44

def base_stack_config
  {
    'name' => 'SET_NAME',
    'uuid' => uuid,
    'description' => 'SET_A_DESCRIPTION',
    'root' => 'SET_A_TEMPLATE',
    'variables' => {},
  }
end

.dirObject



58
59
60
# File 'lib/terraform_dsl/stacks.rb', line 58

def dir
  File.expand_path(File.join(ENV['TERRAFORM_DATA_DIR'] || 'data', 'stacks'))
end

.initObject



25
26
27
28
29
30
31
# File 'lib/terraform_dsl/stacks.rb', line 25

def init
  config = ENV['STACK_CONFIG'] || 'stacks.yml'
  fail "stacks.yaml already exists at #{File.expand_path(config)}" if File.exist?(config)
  File.open(config,'w') do |f|
    f.write(YAML.dump(base_config))
  end
end

.loadObject



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/terraform_dsl/stacks.rb', line 13

def load
  config = ENV['STACK_CONFIG'] || 'stacks.yml'
  fail 'stacks.yml must exist in root directory, or specify STACK_CONFIG pointing to stacks.yml' unless File.exist?(config)
  stack_specs = YAML.load(File.read(config))
  fail MalformedConfig, 'Stacks must be an array' unless stack_specs.key?('stacks') && stack_specs['stacks'].is_a?(Array)
  common = stack_specs['common'] || {}
  stack_specs['stacks'].each do |stack_spec|
    stack = Stack.load(stack_spec.merge(common))
    @stacks[stack.name] = stack
  end
end

.reset!Object



54
55
56
# File 'lib/terraform_dsl/stacks.rb', line 54

def reset!
  @stacks ||= {}
end

.uuidObject



33
34
35
# File 'lib/terraform_dsl/stacks.rb', line 33

def uuid
  "#{Time.now.utc.to_i}_#{SecureRandom.urlsafe_base64(6)}"
end