Class: Pangea::DirectoryRenderer

Inherits:
Object
  • Object
show all
Defined in:
lib/pangea-orchestrator/renderer.rb

Constant Summary collapse

BIN =
%(tofu).freeze

Instance Method Summary collapse

Instance Method Details

#artifact_jsonObject



31
32
33
# File 'lib/pangea-orchestrator/renderer.rb', line 31

def artifact_json
  File.join(init_dir, %(artifact.tf.json))
end

#create_prepped_state_directory(dir, synthesis) ⇒ Object



39
40
41
42
43
44
# File 'lib/pangea-orchestrator/renderer.rb', line 39

def create_prepped_state_directory(dir, synthesis)
  system %(mkdir -p #{dir}) unless Dir.exist?(dir)
  File.write(File.join(dir, %(artifact.tf.json)), JSON[synthesis])
  system %(cd #{dir} && #{BIN} init -json) unless Dir.exist?(File.join(dir, %(.terraform)))
  true
end

#home_dirObject



19
20
21
# File 'lib/pangea-orchestrator/renderer.rb', line 19

def home_dir
  %(#{Dir.home}/.pangea)
end

#infra_dirObject



23
24
25
# File 'lib/pangea-orchestrator/renderer.rb', line 23

def infra_dir
  %(#{home_dir}/infra)
end

#init_dirObject



27
28
29
# File 'lib/pangea-orchestrator/renderer.rb', line 27

def init_dir
  %(#{infra_dir}/init)
end

#plan(dir) ⇒ Object



54
55
56
# File 'lib/pangea-orchestrator/renderer.rb', line 54

def plan(dir)
  pretty(JSON[File.read(File.join(dir, %(plan.json)))])
end

#pretty(content) ⇒ Object



35
36
37
# File 'lib/pangea-orchestrator/renderer.rb', line 35

def pretty(content)
  JSON.pretty_generate(content)
end

#render_component(&block) ⇒ Object

component is a single resource wrapped in state with available attributes



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/pangea-orchestrator/renderer.rb', line 60

def render_component(&block)
  synthesizer.synthesize(&block)
  resource_type = synthesizer.synthesis[:resource].keys[0]
  resource_name = synthesizer.synthesis[:resource][synthesizer.synthesis[:resource].keys[0]].keys[0]
  dir = File.join(init_dir, resource_type.to_s, resource_name.to_s)
  create_prepped_state_directory(dir, synthesizer.synthesis)
  system %(cd #{dir} && #{BIN} show -json tfplan > plan.json)
  system %(cd #{dir} && #{BIN} apply -auto-approve)

  synthesizer.clear_synthesis!

  {
    resource: resource(dir),
    state: state(dir),
    plan: plan(dir)
  }
end

#resource(dir) ⇒ Object



46
47
48
# File 'lib/pangea-orchestrator/renderer.rb', line 46

def resource(dir)
  JSON[File.read(File.join(dir, %(artifact.tf.json)))]
end

#state(dir) ⇒ Object



50
51
52
# File 'lib/pangea-orchestrator/renderer.rb', line 50

def state(dir)
  pretty(JSON[File.read(File.join(dir, %(terraform.tfstate)))])
end