Class: ForemanOpentofu::AppWrapper

Inherits:
Object
  • Object
show all
Includes:
HclFormat
Defined in:
app/services/foreman_opentofu/app_wrapper.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from HclFormat

#array_to_hcl, #block_to_hcl, #default_opts, #format_value, #hash_to_hcl, #prefix_hcl, #to_hcl

Constructor Details

#initialize(workdir, opts = {}) ⇒ AppWrapper

TODO: for future versions

- manage temp-work-dir; problem: no auto-remove after finished :-(
- handle ENVVars if applicable
- handle stderr and stdout separately
- use JSON-output for easier parsing
- do we need locking or has the object be atomic


13
14
15
16
# File 'app/services/foreman_opentofu/app_wrapper.rb', line 13

def initialize(workdir, opts = {})
  @workdir = workdir
  @variables = opts[:variables]
end

Instance Attribute Details

#workdirObject (readonly)

Returns the value of attribute workdir.



4
5
6
# File 'app/services/foreman_opentofu/app_wrapper.rb', line 4

def workdir
  @workdir
end

Instance Method Details

#apply(params = []) ⇒ Object

run ‘tofu apply’ with specified ‘params’. Note: it is not supported to specify a tofu plan file in the ‘params’-parameter.

If the 'plan'-method has run on this object, then 'apply' uses the created plan.


67
68
69
70
71
72
# File 'app/services/foreman_opentofu/app_wrapper.rb', line 67

def apply(params = [])
  params_parsed = parse_params(params)
  params_parsed.append(planfile) if planned?

  tofu_execute('apply', ['-auto-approve'].concat(params_parsed))
end

#base_commandObject



23
# File 'app/services/foreman_opentofu/app_wrapper.rb', line 23

def base_command() 'tofu' end

#conffileObject



20
# File 'app/services/foreman_opentofu/app_wrapper.rb', line 20

def conffile()   File.join(workdir, 'main.tf')      end

#create_variables_fileObject

write variables definition file based on @variables into ‘variables.tf’



27
28
29
30
31
32
33
34
35
36
# File 'app/services/foreman_opentofu/app_wrapper.rb', line 27

def create_variables_file
  File.open(vardeffile, 'w') do |f|
    @variables.each_key do |var|
      data = { 'type' => :string }
      data['sensitive'] = var.to_s == 'password'

      f << block_to_hcl(['variable', var], data)
    end
  end
end

#default_paramsObject



42
43
44
45
46
# File 'app/services/foreman_opentofu/app_wrapper.rb', line 42

def default_params
  [
    '-no-color',
  ]
end

#destroy(params = []) ⇒ Object



74
75
76
# File 'app/services/foreman_opentofu/app_wrapper.rb', line 74

def destroy(params = [])
  tofu_execute('destroy', ['-auto-approve'].concat(parse_params(params)))
end

#init(params = [], &block) ⇒ Object

optional: specify block to access command-pipe (object of class ‘IO`), e.g. tofu.init do |t|

until t.eof? do
  puts("Message from Tofu: #{t.gets}")
end

end



54
55
56
57
58
# File 'app/services/foreman_opentofu/app_wrapper.rb', line 54

def init(params = [], &block)
  create_variables_file if @variables

  tofu_execute('init', ['-input=false'].concat(parse_params(params)), &block)
end

#main_configurationObject



91
92
93
# File 'app/services/foreman_opentofu/app_wrapper.rb', line 91

def main_configuration
  File.read(conffile)
end

#main_configuration=(config) ⇒ Object



95
96
97
# File 'app/services/foreman_opentofu/app_wrapper.rb', line 95

def main_configuration=(config)
  File.write(conffile, config)
end

#output(params = []) ⇒ Object



78
79
80
# File 'app/services/foreman_opentofu/app_wrapper.rb', line 78

def output(params = [])
  JSON.parse(tofu_execute('output', ['-json'].concat(parse_params(params))))
end

#plan(params = []) ⇒ Object



60
61
62
# File 'app/services/foreman_opentofu/app_wrapper.rb', line 60

def plan(params = [])
  tofu_execute('plan', ["-out=#{planfile}"].concat(parse_params(params)))
end

#planfileObject

rubocop:disable Style/SingleLineMethods



19
# File 'app/services/foreman_opentofu/app_wrapper.rb', line 19

def planfile()   File.join(workdir, 'plan.bin')     end

#planned?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'app/services/foreman_opentofu/app_wrapper.rb', line 87

def planned?
  File.exist?(planfile)
end

#show_plan(params = []) ⇒ Object

TODO: find better name ;-)



83
84
85
# File 'app/services/foreman_opentofu/app_wrapper.rb', line 83

def show_plan(params = [])
  JSON.parse(tofu_execute('show', ['-json', planfile].concat(parse_params(params))))
end

#vardeffileObject



21
# File 'app/services/foreman_opentofu/app_wrapper.rb', line 21

def vardeffile() File.join(workdir, 'variables.tf') end

#variable_paramsObject



38
39
40
# File 'app/services/foreman_opentofu/app_wrapper.rb', line 38

def variable_params
  @variables.map { |var, val| ['-var', "#{var}=#{val}"] }.flatten
end