Class: Capistrano::Terraform

Inherits:
Object
  • Object
show all
Defined in:
lib/capistrano/terraform/terraform.rb

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Terraform

Returns a new instance of Terraform.



6
7
8
9
10
11
12
13
14
15
# File 'lib/capistrano/terraform/terraform.rb', line 6

def initialize(params = {})
  @path = nil
  @var = nil
  @var_file = nil
  @target = nil
  @after_publish = nil
  @deploy = nil
  @backend_config = nil
  update(params)
end

Instance Method Details

#apply_cmd_lineObject



158
159
160
161
162
163
# File 'lib/capistrano/terraform/terraform.rb', line 158

def apply_cmd_line
  cmd_ary = [Terraforms.terraform_cmd, :apply]
  cmd_ary << fetch(:terraform_apply_opts, [])
  cmd_ary << "'#{plan_outfile}'"
  return cmd_ary.flatten
end

#create_apply_task(terraform_id) ⇒ Object



224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/capistrano/terraform/terraform.rb', line 224

def create_apply_task(terraform_id)
  task = Rake::Task.define_task "terraform:#{terraform_id}:apply" do
    terraform_obj = Terraforms.find(terraform_id)
    terraform_cmd = terraform_obj.apply_cmd_line

    if roles(:terraform).first
      on roles(:terraform).first do |_terraform_remote|
        info "Running terraform:#{terraform_id}:apply..."
        within terraform_obj.run_path do
          execute :pwd
          execute(*terraform_cmd)
        end
      end
    else
      run_locally do
        info "Running terraform:#{terraform_id}:apply..."
        within terraform_obj.run_path do
          execute :pwd
          execute(*terraform_cmd)
        end
      end
    end
  end
  task.comment = "apply specific terraform: #{terraform_id}"
end

#create_clean_task(terraform_id) ⇒ Object



250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/capistrano/terraform/terraform.rb', line 250

def create_clean_task(terraform_id)
  task = Rake::Task.define_task "terraform:#{terraform_id}:clean" do
    terraform_obj = Terraforms.find(terraform_id)
    terraform_cmd = [:rm, '-rf', '.terraform', plan_outfile]

    if roles(:terraform).first
      on roles(:terraform).first do |_terraform_remote|
        info "Cleaning terraform:#{terraform_id}..."
        within terraform_obj.run_path do
          execute :pwd
          execute(*terraform_cmd)
        end
      end
    else
      run_locally do
        info "Cleaning terraform:#{terraform_id}:plan..."
        within terraform_obj.run_path do
          execute :pwd
          execute(*terraform_cmd)
        end
      end
    end
  end
  task.comment = "clean specific terraform: #{terraform_id}"
end

#create_init_task(terraform_id) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/capistrano/terraform/terraform.rb', line 172

def create_init_task(terraform_id)
  task = Rake::Task.define_task "terraform:#{terraform_id}:init" do
    terraform_obj = Terraforms.find(terraform_id)
    terraform_cmd = terraform_obj.init_cmd_line

    if roles(:terraform).first
      on roles(:terraform).first do |_terraform_remote|
        info "Running terraform:#{terraform_id}:init..."
        within terraform_obj.run_path do
          execute :pwd
          raise 'nyi'
        end
      end
    else
      run_locally do
        info "Running terraform:#{terraform_id}:init..."
        within terraform_obj.run_path do
          execute :pwd
          execute(*terraform_cmd)
        end
      end
    end
  end
  task.comment = "initialize specific terraform: #{terraform_id}"
end

#create_plan_task(terraform_id) ⇒ Object



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/capistrano/terraform/terraform.rb', line 198

def create_plan_task(terraform_id)
  task = Rake::Task.define_task "terraform:#{terraform_id}:plan" do
    terraform_obj = Terraforms.find(terraform_id)
    terraform_cmd = terraform_obj.plan_cmd_line

    if roles(:terraform).first
      on roles(:terraform).first do |_terraform_remote|
        info "Running terraform:#{terraform_id}:plan..."
        within terraform_obj.run_path do
          execute :pwd
          execute(*terraform_cmd)
        end
      end
    else
      run_locally do
        info "Running terraform:#{terraform_id}:plan..."
        within terraform_obj.run_path do
          execute :pwd
          execute(*terraform_cmd)
        end
      end
    end
  end
  task.comment = "plan specific terraform: #{terraform_id}"
end

#create_tasks(terraform_id) ⇒ Object



165
166
167
168
169
170
# File 'lib/capistrano/terraform/terraform.rb', line 165

def create_tasks(terraform_id)
  create_init_task(terraform_id)
  create_plan_task(terraform_id)
  create_clean_task(terraform_id)
  create_apply_task(terraform_id)
end

#deploy?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/capistrano/terraform/terraform.rb', line 82

def deploy?
  return @deploy != false
end

#init_cmd_lineObject



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/capistrano/terraform/terraform.rb', line 107

def init_cmd_line
  cmd_ary = [Terraforms.terraform_cmd, :init]
  cmd_ary << fetch(:terraform_init_opts, [])
  cmd_ary << '-reconfigure' # always reconfigure to preserve states across stages

  root_relative = Terraforms.root_path.relative_path_from(run_path)
  fetch(:terraform_backend_config, []).uniq.each do |bec|
    if bec.include?('=')
      cmd_ary << "-backend-config='#{bec}'"
    else
      cmd_ary << "-backend-config='#{root_relative.join(bec)}'"
    end
  end

  @backend_config&.each do |bec|
    cmd_ary << "-backend-config='#{bec}'"
  end

  return cmd_ary.flatten
end

#plan_cmd_lineObject



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/capistrano/terraform/terraform.rb', line 128

def plan_cmd_line
  cmd_ary = [Terraforms.terraform_cmd, :plan]
  cmd_ary << fetch(:terraform_plan_opts, [])
  cmd_ary << "-out='#{plan_outfile}'"

  root_relative = Terraforms.root_path.relative_path_from(run_path)

  fetch(:terraform_var_file, []).uniq.each do |tf_var_file|
    cmd_ary << "-var-file='#{root_relative.join(tf_var_file)}'"
  end

  fetch(:terraform_var, []).uniq.each do |tf_var|
    cmd_ary << "-var='#{tf_var}'"
  end

  @var_file&.each do |var_file|
    cmd_ary << "-var-file='#{var_file}'"
  end

  @var&.each do |var|
    cmd_ary << "-var='#{var}'"
  end

  targets.each do |target|
    cmd_ary << "-target='#{target}'"
  end

  return cmd_ary.flatten
end

#plan_outfileObject



97
98
99
100
# File 'lib/capistrano/terraform/terraform.rb', line 97

def plan_outfile
  # fetch :terraform_plan_outfile, 'tf.plan'
  'tf.plan'
end

#run_pathObject



102
103
104
105
# File 'lib/capistrano/terraform/terraform.rb', line 102

def run_path
  return Terraforms.root_path.join(@path) if @path
  return Terraforms.root_path
end

#targetsObject



86
87
88
89
90
91
92
93
94
95
# File 'lib/capistrano/terraform/terraform.rb', line 86

def targets
  targets_ary = []
  Terraforms.targets.each do |t|
    targets_ary << t
  end
  @target&.each do |t|
    targets_ary << t
  end
  return targets_ary.uniq
end

#to_hashObject



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/capistrano/terraform/terraform.rb', line 66

def to_hash
  return {
    path: @path,
    var: @var,
    var_file: @var_file,
    target: @target,
    backend_config: @backend_config,
    after_publish: @after_publish,
    deploy: @deploy,
  }.compact
end

#to_sObject



78
79
80
# File 'lib/capistrano/terraform/terraform.rb', line 78

def to_s
  return to_hash
end

#update(params = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/capistrano/terraform/terraform.rb', line 17

def update(params = {})
  # handle path updates
  @path = params[:path] if params.key?(:path)

  # handle var_file upserts
  if params.key?(:var_file)
    @var_file ||= []
    @var_file.push(params[:var_file])
    @var_file.flatten!
  end
  @var_file = nil if @var_file == []

  # handle var upserts
  if params.key?(:var)
    @var ||= []
    @var.push(params[:var])
    @var&.flatten!&.uniq!
  end
  @var = nil if @var == []

  # handle target upserts
  if params.key?(:target)
    @target ||= []
    @target.push(params[:target])
    @target&.flatten!&.uniq!
  end
  @target = nil if @target == []

  # handle after_publish upserts
  if params.key?(:after_publish)
    @after_publish = params[:after_publish]
  end
  @after_publish = nil unless @after_publish == true

  # handle deploy upserts
  if params.key?(:deploy)
    @deploy = params[:deploy]
  end
  @deploy = nil unless @deploy == false

  # handle backend_config upserts
  if params.key?(:backend_config)
    @backend_config ||= []
    @backend_config.push(params[:backend_config])
    @backend_config&.flatten!&.uniq!
  end
  @backend_config = nil if @backend_config == []
end