Class: Terraspace::Terraform::Args::Default

Inherits:
Object
  • Object
show all
Extended by:
Memoist
Defined in:
lib/terraspace/terraform/args/default.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mod, name, options = {}) ⇒ Default

Returns a new instance of Default.



5
6
7
8
# File 'lib/terraspace/terraform/args/default.rb', line 5

def initialize(mod, name, options={})
  @mod, @name, @options = mod, name.underscore, options
  @quiet = @options[:quiet].nil? ? true : @options[:quiet]
end

Class Method Details

.terraform_init_log(mod_name) ⇒ Object

Use different tmp log file in case uses run terraspace up in 2 terminals at the same time

Log for init is in /tmp because using shell >> redirection It requires full path since we’re running terraform within the .terraspace-cache folder This keeps the printed command shorter:

=> terraform init -get >> /tmp/terraspace/log/init/demo.log


121
122
123
# File 'lib/terraspace/terraform/args/default.rb', line 121

def terraform_init_log(mod_name)
  "#{Terraspace.tmp_root}/log/init/#{mod_name}.log"
end

Instance Method Details

#apply_argsObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/terraspace/terraform/args/default.rb', line 26

def apply_args
  args = auto_approve_arg
  var_files = @options[:var_files]
  if var_files
    var_files.each do |file|
      copy_to_cache(plan)
    end
    args << var_files.map { |f| "-var-file #{f}" }.join(' ')
  end

  args << input_option

  # must be at the end
  plan = @options[:plan]
  if plan
    copy_to_cache(plan)
    args << " #{plan}"
  end
  args
end

#argsObject



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/terraspace/terraform/args/default.rb', line 10

def args
  # https://terraspace.cloud/docs/ci-automation/
  ENV['TF_IN_AUTOMATION'] = '1' if @options[:auto]

  args_meth = "#{@name}_args"
  if respond_to?(args_meth)
    send(args_meth)
  else
    []
  end
end

#auto_approve_argObject



106
107
108
# File 'lib/terraspace/terraform/args/default.rb', line 106

def auto_approve_arg
  @options[:yes] || @options[:auto] ? ["-auto-approve"] : []
end

#destroy_argsObject



102
103
104
# File 'lib/terraspace/terraform/args/default.rb', line 102

def destroy_args
  auto_approve_arg
end

#expanded_outObject



98
99
100
# File 'lib/terraspace/terraform/args/default.rb', line 98

def expanded_out
  @options[:out]
end

#force_unlock_argsObject



22
23
24
# File 'lib/terraspace/terraform/args/default.rb', line 22

def force_unlock_args
  [" -force #{@options[:lock_id]}"]
end

#init_argsObject



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/terraspace/terraform/args/default.rb', line 56

def init_args
  args = "-get"
  args << input_option
  args << " -reconfigure" if @options[:reconfigure]

  # must be at the end
  if @quiet
    log_path = self.class.terraform_init_log(@mod.name)
    FileUtils.mkdir_p(File.dirname(log_path))
    args << " >> #{log_path}"
  end
  [args]
end

#input_optionObject



47
48
49
50
51
52
53
54
# File 'lib/terraspace/terraform/args/default.rb', line 47

def input_option
  option = if @options[:auto]
             "false"
           else
             @options[:input] ? @options[:input] : "false"
           end
  " -input=#{option}"
end

#output_argsObject



70
71
72
73
74
75
# File 'lib/terraspace/terraform/args/default.rb', line 70

def output_args
  args = []
  args << "-json" if @options[:format] == "json"
  args << "> #{expanded_out}" if @options[:out]
  args
end

#plan_argsObject



77
78
79
80
81
82
83
84
85
# File 'lib/terraspace/terraform/args/default.rb', line 77

def plan_args
  args = []
  args << input_option
  args << "-destroy" if @options[:destroy]
  args << "-out #{expanded_out}" if @options[:out]
  # Note: based on the @options[:out] will run an internal hook to copy plan
  # file back up to the root project folder for use. Think this is convenient and expected behavior.
  args
end

#show_argsObject



87
88
89
90
91
92
93
94
95
96
# File 'lib/terraspace/terraform/args/default.rb', line 87

def show_args
  args = []
  args << " -json" if @options[:json]
  plan = @options[:plan]
  if plan
    copy_to_cache(@options[:plan])
    args << " #{@options[:plan]}" # terraform show /path/to/plan
  end
  args
end