Module: Pangea::Processor

Defined in:
lib/pangea/processor.rb

Overview

ingests files and builds a context for rendering Pangea programming

Class Method Summary collapse

Class Method Details

.binObject



39
40
41
# File 'lib/pangea/processor.rb', line 39

def bin
  'tofu'
end

.configObject



25
26
27
28
29
# File 'lib/pangea/processor.rb', line 25

def config
  @config ||= Pangea::Utils.symbolize(
    Pangea::Config.config
  )
end

.namespaceObject



31
32
33
# File 'lib/pangea/processor.rb', line 31

def namespace
  @namespace ||= ENV.fetch('PANGEA_NAMESPACE')
end

.namespace_configObject



43
44
45
46
47
48
49
# File 'lib/pangea/processor.rb', line 43

def namespace_config
  sns = ''
  config[:namespaces].each_key do |ns|
    sns = config[:namespaces][ns] if ns.to_s.eql?(namespace.to_s)
  end
  @namespace_config ||= sns
end

.process(content) ⇒ Object



17
18
19
# File 'lib/pangea/processor.rb', line 17

def process(content)
  instance_eval(content)
end

.register_action(action) ⇒ Object



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

def register_action(action)
  permitted_actions = i[plan apply show destroy]
  @action = action if permitted_actions.map(&:to_s).include?(action.to_s)
end

.s3Object



35
36
37
# File 'lib/pangea/processor.rb', line 35

def s3
  @s3 = Aws::S3::Client.new
end

.synthesizerObject



21
22
23
# File 'lib/pangea/processor.rb', line 21

def synthesizer
  @synthesizer ||= TerraformSynthesizer.new
end

.template(name, &block) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/pangea/processor.rb', line 51

def template(name, &block)
  prefix = "#{namespace}/#{name}"
  pangea_home = %(#{Dir.home}/.pangea/#{namespace})
  local_cache = File.join(pangea_home, prefix)
  `mkdir -p #{local_cache}` unless Dir.exist?(local_cache)
  synthesizer.synthesize(&block)
  sns = namespace_config
  unless synthesizer.synthesis[:terraform]
    synthesizer.synthesize do
      terraform do
        backend(
          s3: {
            key: prefix,
            dynamodb_table: sns[:state][:config][:lock].to_s,
            bucket: sns[:state][:config][:bucket].to_s,
            region: sns[:state][:config][:region].to_s,
            encrypt: true
          }
        )
      end
    end
  end
  File.write(
    File.join(
      local_cache, 'main.tf.json'
    ), JSON[synthesizer.synthesis]
  )

  system("cd #{local_cache} && #{bin} init -input=false") unless File.exist?(
    File.join(
      local_cache,
      '.terraform.lock.hcl'
    )
  )

  if @action.to_s == 'apply'
    system "cd #{local_cache} && #{bin} apply -auto-approve"
  elsif @action.to_s == 'plan'
    system "cd #{local_cache} && #{bin} plan"
  elsif @action.to_s == 'destroy'
    system "cd #{local_cache} && #{bin} destroy -auto-approve"
  end

  template = Pangea::Utils.symbolize(
    JSON[File.read(
      File.join(local_cache, 'main.tf.json')
    )]
  )
  puts JSON.pretty_generate(template) if @action.to_s.eql?('show')
  { template: template }
end