Class: Terraframe::Processor

Inherits:
Object
  • Object
show all
Defined in:
lib/terraframe/processor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeProcessor

Returns a new instance of Processor.



17
18
19
20
21
22
23
24
25
# File 'lib/terraframe/processor.rb', line 17

def initialize
  @logger = Logger.new($stderr)
  logger.level = Logger::INFO

  logger.debug "Logger initialized."

  @contexts = {}
  register_context(:aws, Terraframe::AWS::AWSContext.new)
end

Instance Attribute Details

#contextsObject (readonly)

Returns the value of attribute contexts.



14
15
16
# File 'lib/terraframe/processor.rb', line 14

def contexts
  @contexts
end

#loggerObject (readonly)

Returns the value of attribute logger.



13
14
15
# File 'lib/terraframe/processor.rb', line 13

def logger
  @logger
end

Instance Method Details

#apply(inputs, vars) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/terraframe/processor.rb', line 60

def apply(inputs, vars)
  logger.info "Beginning state execution."

  state = State.new(logger, vars, @contexts)
  inputs.each { |input| state.__apply_script(input[0], input[1])}
  state.__build()
end

#load_variable_files(variable_files) ⇒ Object



54
55
56
57
58
# File 'lib/terraframe/processor.rb', line 54

def load_variable_files(variable_files)
  vars = {}
  variable_files.each { |f| vars = vars.deep_merge(YAML::load_file(f)) }
  vars
end

#process_files(scripts, variable_files, override_variables) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/terraframe/processor.rb', line 35

def process_files(scripts, variable_files, override_variables)
  scripts = scripts.map { |f| File.expand_path(f) }
  variable_files = variable_files.map { |f| File.expand_path(f) }

  missing_scripts = scripts.reject { |f| File.exist?(f) }
  missing_variable_files = variable_files.reject { |f| File.exist?(f) }
  unless missing_scripts.empty? && missing_variable_files.empty?
    missing_scripts.each { |f| logger.fatal "Script file not found: #{f}" }
    missing_variable_files.each { |f| logger.fatal "Variable file not found: #{f}" }
    raise "One or more specified files were missing."
  end


  script_pairs = scripts.zip(scripts.map { |f| IO.read(f) })
  vars = load_variable_files(variable_files).deep_merge(override_variables)

  apply(script_pairs.to_h, vars)
end

#register_context(name, context) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/terraframe/processor.rb', line 27

def register_context(name, context)
  name = name.to_sym
  if @contexts[name]
    logger.warn "A context with the name '#{name}' has been registered more than once."
  end
  @contexts[name] = context
end