Class: Pangea::CLI::Operations

Inherits:
Object
  • Object
show all
Defined in:
lib/pangea/cli/operations.rb

Overview

Runs tofu operations (init, plan, apply, destroy, output) against the synthesized Terraform JSON in the workspace directory.

By default, plan / apply / destroy use OpenTofu’s -json event stream and render a Nord-themed human summary via Pangea::CLI::TofuEvents. When ‘PANGEA_VERBOSE=1` is set, tofu runs in pass-through mode and colors are surrendered to Terraform’s native output.

Defined Under Namespace

Classes: StageOutcome

Constant Summary collapse

TOFU =
'tofu'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Operations

Returns a new instance of Operations.



36
37
38
39
# File 'lib/pangea/cli/operations.rb', line 36

def initialize(config)
  @config = config
  @synthesizer = Synthesizer.new(config)
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



18
19
20
# File 'lib/pangea/cli/operations.rb', line 18

def config
  @config
end

#last_outcomeObject (readonly)

The structured outcome of the most recent plan/apply/destroy call on this Operations instance. nil if none has run yet. Cascade reads this after each stage to aggregate a recap.



92
93
94
# File 'lib/pangea/cli/operations.rb', line 92

def last_outcome
  @last_outcome
end

Instance Method Details

#applyObject



71
72
73
74
75
76
77
78
# File 'lib/pangea/cli/operations.rb', line 71

def apply
  synth
  in_workspace do
    tofu_init
    Theme.section('apply')
    run_tofu('apply', '-auto-approve')
  end
end

#destroyObject



80
81
82
83
84
85
86
87
# File 'lib/pangea/cli/operations.rb', line 80

def destroy
  synth
  in_workspace do
    tofu_init
    Theme.section('destroy')
    run_tofu('destroy', '-auto-approve')
  end
end

#initObject



94
95
96
97
# File 'lib/pangea/cli/operations.rb', line 94

def init
  synth
  in_workspace { tofu_init }
end

#outputObject



99
100
101
102
103
# File 'lib/pangea/cli/operations.rb', line 99

def output
  Dir.chdir(config.workspace_dir) do
    tofu('output', '-json')
  end
end

#planObject



62
63
64
65
66
67
68
69
# File 'lib/pangea/cli/operations.rb', line 62

def plan
  synth
  in_workspace do
    tofu_init
    Theme.section('plan')
    run_tofu('plan')
  end
end

#synth(json_output: false) ⇒ String

Synthesize template to JSON and write to workspace.

Parameters:

  • json_output (Boolean) (defaults to: false)

    if true, also print JSON to stdout

Returns:

  • (String)

    path to the written .tf.json file



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/pangea/cli/operations.rb', line 44

def synth(json_output: false)
  t = Theme
  t.section('synth')
  t.structured_log(
    [:info, 'Synthesizing'],
    [:path, config.template_file.to_s],
    [:deprecation, 'in namespace'],
    [:namespace, config.namespace.to_s],
  )
  manifest = @synthesizer.synthesize
  json = JSON.pretty_generate(manifest)
  output_path = File.join(config.workspace_dir, "#{config.template_name}.tf.json")
  File.write(output_path, json)
  t.structured_log([:info, 'Wrote'], [:path, output_path])
  $stdout.puts json if json_output
  output_path
end