terraform-wrapper 1.3.2 → 1.3.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,32 +1,25 @@
1
1
  ###############################################################################
2
2
 
3
3
  module TerraformWrapper
4
-
5
- ###############################################################################
4
+ #############################################################################
6
5
 
7
6
  module Shared
8
-
9
- ###############################################################################
7
+ ###########################################################################
10
8
 
11
9
  class Runner
12
-
13
- ###############################################################################
10
+ #########################################################################
14
11
 
15
12
  include TerraformWrapper::Shared::Logging
16
13
 
17
- ###############################################################################
14
+ #########################################################################
18
15
 
19
- @@lockfile_name = ".terraform.lock.hcl"
16
+ @@lockfile_name = '.terraform.lock.hcl'
20
17
 
21
- ###############################################################################
18
+ #########################################################################
22
19
 
23
- attr_reader :binary
24
- attr_reader :code
25
- attr_reader :config
26
- attr_reader :downloaded
27
- attr_reader :initialised
20
+ attr_reader :binary, :code, :config, :downloaded, :initialised
28
21
 
29
- ###############################################################################
22
+ #########################################################################
30
23
 
31
24
  def initialize(binary:, code:)
32
25
  logger.fatal("Terraform code location: #{code.path} does not exist!") unless code.check
@@ -37,22 +30,22 @@ module TerraformWrapper
37
30
  @initialised = false
38
31
  end
39
32
 
40
- ###############################################################################
33
+ #########################################################################
41
34
 
42
35
  def download
43
- parameters = Array.new
44
- parameters.append("-backend=false")
36
+ parameters = []
37
+ parameters.append('-backend=false')
45
38
 
46
- logger.fatal("Failed to download Terraform modules.") unless run(action: "init", parameters: parameters)
39
+ logger.fatal('Failed to download Terraform modules.') unless run(action: 'init', parameters: parameters)
47
40
 
48
41
  @downloaded = true
49
42
  end
50
43
 
51
- ###############################################################################
44
+ #########################################################################
52
45
 
53
46
  def init(config:)
54
- parameters = Array.new
55
- parameters.append("-reconfigure")
47
+ parameters = []
48
+ parameters.append('-reconfigure')
56
49
 
57
50
  config.backend.hash.each do |key, value|
58
51
  parameters.append("-backend-config=\"#{key}=#{value}\"")
@@ -60,13 +53,13 @@ module TerraformWrapper
60
53
 
61
54
  config.auths.map(&:auth)
62
55
 
63
- logger.fatal("Failed to initialise Terraform with backend.") unless run(action: "init", parameters: parameters)
56
+ logger.fatal('Failed to initialise Terraform with backend.') unless run(action: 'init', parameters: parameters)
64
57
 
65
58
  @config = config
66
59
  @initialised = true
67
60
  end
68
61
 
69
- ###############################################################################
62
+ #########################################################################
70
63
 
71
64
  def upgrade
72
65
  lockfile_path = File.join(@code.path, @@lockfile_name)
@@ -76,31 +69,31 @@ module TerraformWrapper
76
69
  File.delete(lockfile_path)
77
70
  end
78
71
 
79
- logger.fatal("Lock file removal failed!") if File.file?(lockfile_path)
72
+ logger.fatal('Lock file removal failed!') if File.file?(lockfile_path)
80
73
 
81
- parameters = Array.new
82
- parameters.append("-update")
83
- logger.fatal("Failed to upgrade Terraform modules!") unless run(action: "get", parameters: parameters)
74
+ parameters = []
75
+ parameters.append('-update')
76
+ logger.fatal('Failed to upgrade Terraform modules!') unless run(action: 'get', parameters: parameters)
84
77
 
85
- parameters = Array.new
86
- parameters.append("lock")
78
+ parameters = []
79
+ parameters.append('lock')
87
80
 
88
81
  @binary.provider.platforms.each do |platform|
89
82
  parameters.append("-platform=\"#{platform}\"")
90
83
  end
91
84
 
92
- logger.fatal("Failed to upgrade Terraform providers!") unless run(action: "providers", parameters: parameters)
85
+ logger.fatal('Failed to upgrade Terraform providers!') unless run(action: 'providers', parameters: parameters)
93
86
  end
94
87
 
95
- ###############################################################################
88
+ #########################################################################
96
89
 
97
90
  def apply(file: nil)
98
- logger.fatal("Cannot Terraform apply before initialising backend!") unless initialised
91
+ logger.fatal('Cannot Terraform apply before initialising backend!') unless initialised
99
92
 
100
- parameters = Array.new
101
- parameters.append("-auto-approve")
93
+ parameters = []
94
+ parameters.append('-auto-approve')
102
95
 
103
- if (not file.nil?) and file.kind_of?(String) and (not file.strip.empty?) then
96
+ if !file.nil? and file.is_a?(String) and !file.strip.empty?
104
97
  logger.fatal("Plan file: #{file} does not exist!") unless File.file?(file)
105
98
  parameters.append("\"#{file}\"")
106
99
  else
@@ -108,95 +101,107 @@ module TerraformWrapper
108
101
  parameters.concat(variable_strings)
109
102
  end
110
103
 
111
- logger.fatal("Terraform apply failed!") unless run(action: "apply", parameters: parameters)
104
+ logger.fatal('Terraform apply failed!') unless run(action: 'apply', parameters: parameters)
112
105
  end
113
106
 
114
- ###############################################################################
107
+ #########################################################################
115
108
 
116
109
  def plan(destroy: false, file: nil)
117
- logger.fatal("Cannot Terraform plan before initialising backend!") unless initialised
110
+ logger.fatal('Cannot Terraform plan before initialising backend!') unless initialised
118
111
 
119
- parameters = Array.new
112
+ parameters = []
120
113
  parameters.concat(variable_files)
121
114
  parameters.concat(variable_strings)
122
115
 
123
- if not file.nil? and file.kind_of?(String) and not file.strip.empty? then
124
- logger.fatal("Failed to create plan directory: #{directory}") unless ::TerraformWrapper.create_directory(directory: File.dirname(file), purpose: "plan")
116
+ if !file.nil? and file.is_a?(String) and !file.strip.empty?
117
+ logger.fatal("Failed to create plan directory: #{directory}") unless ::TerraformWrapper.create_directory(
118
+ directory: File.dirname(file), purpose: 'plan'
119
+ )
125
120
  parameters.append("-out=\"#{file}\"")
126
121
  end
127
122
 
128
- parameters.append("-destroy") if destroy
123
+ parameters.append('-destroy') if destroy
129
124
 
130
- logger.fatal("Terraform plan failed!") unless run(action: "plan", parameters: parameters)
125
+ logger.fatal('Terraform plan failed!') unless run(action: 'plan', parameters: parameters)
131
126
  end
132
127
 
133
- ###############################################################################
128
+ #########################################################################
134
129
 
135
130
  def destroy
136
- logger.fatal("Cannot Terraform destroy before initialising backend!") unless initialised
131
+ logger.fatal('Cannot Terraform destroy before initialising backend!') unless initialised
137
132
 
138
- parameters = Array.new
133
+ parameters = []
139
134
  parameters.concat(variable_files)
140
135
  parameters.concat(variable_strings)
141
- parameters.append("-auto-approve")
136
+ parameters.append('-auto-approve')
142
137
 
143
- logger.fatal("Terraform destroy failed!") unless run(action: "destroy", parameters: parameters)
138
+ logger.fatal('Terraform destroy failed!') unless run(action: 'destroy', parameters: parameters)
144
139
  end
145
140
 
146
- ###############################################################################
141
+ #########################################################################
147
142
 
148
143
  def import(address: nil, id: nil)
149
- logger.fatal("Cannot Terraform import before initialising backend!") unless initialised
144
+ logger.fatal('Cannot Terraform import before initialising backend!') unless initialised
150
145
 
151
- logger.fatal("Terraform state address for import must be a string!") unless address.kind_of?(String)
152
- logger.fatal("Terraform state address for import must not be blank!") if address.strip.empty?
146
+ logger.fatal('Terraform state address for import must be a string!') unless address.is_a?(String)
147
+ logger.fatal('Terraform state address for import must not be blank!') if address.strip.empty?
153
148
 
154
- logger.fatal("Identification for infrastructure to import must be a string!") unless id.kind_of?(String)
155
- logger.fatal("Identification for infrastructure to import must not be blank!") if id.strip.empty?
149
+ logger.fatal('Identification for infrastructure to import must be a string!') unless id.is_a?(String)
150
+ logger.fatal('Identification for infrastructure to import must not be blank!') if id.strip.empty?
156
151
 
157
- parameters = Array.new
152
+ parameters = []
158
153
  parameters.concat(variable_files)
159
154
  parameters.concat(variable_strings)
160
155
 
161
156
  parameters.append("'#{address}'")
162
157
  parameters.append("'#{id}'")
163
158
 
164
- logger.fatal("Terraform import failed!") unless run(action: "import", parameters: parameters)
159
+ logger.fatal('Terraform import failed!') unless run(action: 'import', parameters: parameters)
165
160
  end
166
161
 
167
- ###############################################################################
162
+ #########################################################################
168
163
 
169
164
  def taint(address: nil)
170
- logger.fatal("Cannot Terraform taint before initialising backend!") unless initialised
165
+ logger.fatal('Cannot Terraform taint before initialising backend!') unless initialised
171
166
 
172
- logger.fatal("Terraform state address for taint must be a string!") unless address.kind_of?(String)
173
- logger.fatal("Terraform state address for taint must not be blank!") if address.strip.empty?
167
+ logger.fatal('Terraform state address for taint must be a string!') unless address.is_a?(String)
168
+ logger.fatal('Terraform state address for taint must not be blank!') if address.strip.empty?
174
169
 
175
- parameters = Array.new
170
+ parameters = []
176
171
  parameters.append("'#{address}'")
177
172
 
178
- logger.fatal("Terraform taint failed!") unless run(action: "taint", parameters: parameters)
173
+ logger.fatal('Terraform taint failed!') unless run(action: 'taint', parameters: parameters)
179
174
  end
180
175
 
181
- ###############################################################################
176
+ #########################################################################
177
+
178
+ def fmt(check: true)
179
+ parameters = []
180
+
181
+ parameters.append('-check') if check.to_s.downcase == 'true' && check
182
+
183
+ logger.fatal('Terraform fmt failed!') unless run(action: 'fmt', parameters: parameters)
184
+ end
185
+
186
+ #########################################################################
182
187
 
183
188
  def validate
184
- logger.fatal("Cannot Terraform validate before downloading modules!") unless downloaded
185
- logger.fatal("Terraform validation failed!") unless run(action: "validate")
189
+ logger.fatal('Cannot Terraform validate before downloading modules!') unless downloaded
190
+ logger.fatal('Terraform validation failed!') unless run(action: 'validate')
186
191
  end
187
192
 
188
- ###############################################################################
193
+ #########################################################################
189
194
 
190
- private
195
+ private
191
196
 
192
- ###############################################################################
197
+ #########################################################################
193
198
 
194
- def run(action:, parameters: Array.new)
199
+ def run(action:, parameters: [])
195
200
  result = false
196
201
 
197
- parameters.reject! { |item| not item.kind_of?(String) or item.strip.empty? }
202
+ parameters.reject! { |item| !item.is_a?(String) or item.strip.empty? }
198
203
 
199
- cmdline = [ "\"#{@binary.path}\"", action ].concat(parameters).join(" ")
204
+ cmdline = ["\"#{@binary.path}\"", action].concat(parameters).join(' ')
200
205
 
201
206
  logger.info("Starting Terraform, action: #{action}")
202
207
 
@@ -207,47 +212,44 @@ module TerraformWrapper
207
212
 
208
213
  puts("\n" + ('#' * 80) + "\n\n")
209
214
 
210
- return result
215
+ result
211
216
  end
212
217
 
213
- ###############################################################################
218
+ #########################################################################
214
219
 
215
220
  def variable_files
216
- logger.fatal("Cannot generate variable files until Terraform has been initialised!") unless @initialised
221
+ logger.fatal('Cannot generate variable files until Terraform has been initialised!') unless @initialised
217
222
 
218
- result = Array.new
223
+ result = []
219
224
 
220
225
  @config.variables.files.each do |file|
221
226
  result.append("-var-file=\"#{file}\"")
222
227
  end
223
228
 
224
- return result
229
+ result
225
230
  end
226
231
 
227
- ###############################################################################
232
+ #########################################################################
228
233
 
229
234
  def variable_strings
230
- logger.fatal("Cannot generate variable strings until Terraform has been initialised!") unless @initialised
235
+ logger.fatal('Cannot generate variable strings until Terraform has been initialised!') unless @initialised
231
236
 
232
- result = Array.new
237
+ result = []
233
238
 
234
239
  @config.variables.values.each do |key, value|
235
- result.append("-var=\"#{key.to_s}=#{value}\"")
240
+ result.append("-var=\"#{key}=#{value}\"")
236
241
  end
237
242
 
238
- return result
243
+ result
239
244
  end
240
245
 
241
- ###############################################################################
242
-
246
+ #########################################################################
243
247
  end
244
248
 
245
- ###############################################################################
246
-
249
+ ###########################################################################
247
250
  end
248
251
 
249
- ###############################################################################
250
-
252
+ #############################################################################
251
253
  end
252
254
 
253
255
  ###############################################################################
@@ -1,26 +1,23 @@
1
1
  ###############################################################################
2
2
 
3
3
  module TerraformWrapper
4
-
5
- ###############################################################################
4
+ #############################################################################
6
5
 
7
6
  module Tasks
8
-
9
- ###############################################################################
7
+ ###########################################################################
10
8
 
11
9
  class Apply < ::Rake::TaskLib
12
-
13
- ###############################################################################
10
+ #########################################################################
14
11
 
15
12
  include TerraformWrapper::Shared::Logging
16
13
 
17
- ###############################################################################
14
+ #########################################################################
18
15
 
19
16
  @binary
20
17
  @code
21
18
  @options
22
19
 
23
- ###############################################################################
20
+ #########################################################################
24
21
 
25
22
  def initialize(binary:, code:, options:)
26
23
  @binary = binary
@@ -32,14 +29,14 @@ module TerraformWrapper
32
29
  apply_task
33
30
  end
34
31
 
35
- ###############################################################################
32
+ #########################################################################
36
33
 
37
34
  def apply_task
38
- desc "Applies infrastructure with Terraform for a given configuration on an infrastructure component."
39
- task :apply, [:config, :plan] => :binary do |t, args|
40
- options = @options.merge({"name" => args[:config]})
35
+ desc 'Applies infrastructure with Terraform for a given configuration on an infrastructure component.'
36
+ task :apply, %i[config plan] => :binary do |_t, args|
37
+ options = @options.merge({ 'name' => args[:config] })
41
38
 
42
- logger.info("Processing configuration for Terraform apply...")
39
+ logger.info('Processing configuration for Terraform apply...')
43
40
 
44
41
  config = TerraformWrapper::Shared::Config.new(code: @code, options: options)
45
42
  runner = TerraformWrapper::Shared::Runner.new(binary: @binary, code: @code)
@@ -51,16 +48,13 @@ module TerraformWrapper
51
48
  end
52
49
  end
53
50
 
54
- ###############################################################################
55
-
51
+ #########################################################################
56
52
  end
57
53
 
58
- ###############################################################################
59
-
54
+ ###########################################################################
60
55
  end
61
56
 
62
- ###############################################################################
63
-
57
+ #############################################################################
64
58
  end
65
59
 
66
60
  ###############################################################################
@@ -9,24 +9,21 @@ require 'zip'
9
9
  ###############################################################################
10
10
 
11
11
  module TerraformWrapper
12
-
13
- ###############################################################################
12
+ #############################################################################
14
13
 
15
14
  module Tasks
16
-
17
- ###############################################################################
15
+ ###########################################################################
18
16
 
19
17
  class Binary < ::Rake::TaskLib
20
-
21
- ###############################################################################
18
+ #########################################################################
22
19
 
23
20
  include TerraformWrapper::Shared::Logging
24
21
 
25
- ###############################################################################
22
+ #########################################################################
26
23
 
27
24
  @binary
28
25
 
29
- ###############################################################################
26
+ #########################################################################
30
27
 
31
28
  def initialize(binary:)
32
29
  @binary = binary
@@ -36,19 +33,21 @@ module TerraformWrapper
36
33
  binary_task
37
34
  end
38
35
 
39
- ###############################################################################
36
+ #########################################################################
40
37
 
41
38
  def binary_task
42
- desc "Downloads and extracts the expected version of the Terraform binary if it is not already present."
43
- task :binary do |t, args|
39
+ desc 'Downloads and extracts the expected version of the Terraform binary if it is not already present.'
40
+ task :binary do |_t, _args|
44
41
  logger.info("Checking Terraform binary for platform: #{@binary.platform}, version: #{@binary.version}")
45
42
 
46
- if not @binary.exists then
47
- logger.info("Terraform binary not found. Preparing binary...")
43
+ unless @binary.exists
44
+ logger.info('Terraform binary not found. Preparing binary...')
48
45
 
49
- logger.fatal("Failed to create binary directory: #{directory}") unless ::TerraformWrapper.create_directory(directory: @binary.directory, purpose: "binaries")
46
+ logger.fatal("Failed to create binary directory: #{directory}") unless ::TerraformWrapper.create_directory(
47
+ directory: @binary.directory, purpose: 'binaries'
48
+ )
50
49
 
51
- archive_binary = "terraform"
50
+ archive_binary = 'terraform'
52
51
  archive_file = "terraform_#{@binary.version}_#{@binary.platform}_amd64.zip"
53
52
  archive_path = File.join(@binary.directory, archive_file)
54
53
  archive_uri = "https://releases.hashicorp.com/terraform/#{@binary.version}/#{archive_file}"
@@ -58,8 +57,8 @@ module TerraformWrapper
58
57
  sums_uri = "https://releases.hashicorp.com/terraform/#{@binary.version}/#{sums_file}"
59
58
 
60
59
  begin
61
- download(path: archive_path, uri: archive_uri) if not File.file?(archive_path)
62
- download(path: sums_path, uri: sums_uri) if not File.file?(sums_path)
60
+ download(path: archive_path, uri: archive_uri) unless File.file?(archive_path)
61
+ download(path: sums_path, uri: sums_uri) unless File.file?(sums_path)
63
62
  verify(file: archive_file, path: archive_path, sums: sums_path)
64
63
  extract(archive: archive_path, binary: archive_binary, destination: @binary.path)
65
64
  ensure
@@ -67,38 +66,38 @@ module TerraformWrapper
67
66
  end
68
67
  end
69
68
 
70
- if not @binary.executable then
71
- logger.info("Terraform binary not executable. Setting permissions...")
69
+ unless @binary.executable
70
+ logger.info('Terraform binary not executable. Setting permissions...')
72
71
  executable(path: @binary.path)
73
72
  end
74
73
 
75
- logger.fatal("Problem with checking the Terraform binary!") unless @binary.check
74
+ logger.fatal('Problem with checking the Terraform binary!') unless @binary.check
76
75
  end
77
76
  end
78
77
 
79
- ###############################################################################
78
+ #########################################################################
80
79
 
81
80
  private
82
81
 
83
- ###############################################################################
82
+ #########################################################################
84
83
 
85
84
  def download(path:, uri:)
86
85
  logger.info("Downloading: #{uri}")
87
86
 
88
87
  response = Net::HTTP.get_response(URI(uri))
89
88
 
90
- logger.fatal("Download request did not return HTTP status 200!") if response.code != "200"
91
- logger.fatal("Download response body is not permitted!") unless response.class.body_permitted?
92
- logger.fatal("Download response body is empty!") if response.body.nil?
89
+ logger.fatal('Download request did not return HTTP status 200!') if response.code != '200'
90
+ logger.fatal('Download response body is not permitted!') unless response.class.body_permitted?
91
+ logger.fatal('Download response body is empty!') if response.body.nil?
93
92
 
94
- open(path, "wb") { |file|
93
+ open(path, 'wb') do |file|
95
94
  file.write(response.body)
96
- }
95
+ end
97
96
 
98
- logger.fatal("Download failed!") unless File.file?(path)
97
+ logger.fatal('Download failed!') unless File.file?(path)
99
98
  end
100
99
 
101
- ###############################################################################
100
+ #########################################################################
102
101
 
103
102
  def verify(file:, path:, sums:)
104
103
  logger.info("Checking SHA256 for: #{file}")
@@ -109,26 +108,26 @@ module TerraformWrapper
109
108
 
110
109
  File.readlines(sums).each do |line|
111
110
  begin
112
- fields = line.match /^(?<sum>\S+)\s+(?<file>\S+)$/
113
- sum_file = fields["file"]
114
- sum_sha256 = fields["sum"]
115
- rescue
111
+ fields = line.match(/^(?<sum>\S+)\s+(?<file>\S+)$/)
112
+ sum_file = fields['file']
113
+ sum_sha256 = fields['sum']
114
+ rescue StandardError
116
115
  logger.warn("Unexpected data in sums file: #{sums}")
117
116
  next
118
117
  end
119
118
 
120
- if sum_file == file then
121
- logger.info("Expected SHA256 sum: #{sum_sha256}")
122
- logger.info("Actual SHA256 sum: #{sha256}")
123
- result = (sum_sha256 == sha256)
124
- break
125
- end
119
+ next unless sum_file == file
120
+
121
+ logger.info("Expected SHA256 sum: #{sum_sha256}")
122
+ logger.info("Actual SHA256 sum: #{sha256}")
123
+ result = (sum_sha256 == sha256)
124
+ break
126
125
  end
127
126
 
128
- logger.fatal("Error whilst verifying the SHA256 sum of the downloaded Terraform archive!") unless result
127
+ logger.fatal('Error whilst verifying the SHA256 sum of the downloaded Terraform archive!') unless result
129
128
  end
130
129
 
131
- ###############################################################################
130
+ #########################################################################
132
131
 
133
132
  def extract(archive:, binary:, destination:)
134
133
  logger.info("Extracting: #{archive}")
@@ -139,43 +138,42 @@ module TerraformWrapper
139
138
  end
140
139
  end
141
140
 
142
- logger.fatal("Extraction of Terraform binary: #{binary}, from archive: #{archive} has failed!") unless File.file?(destination)
141
+ return if File.file?(destination)
142
+
143
+ logger.fatal("Extraction of Terraform binary: #{binary}, from archive: #{archive} has failed!")
143
144
  end
144
145
 
145
- ###############################################################################
146
+ #########################################################################
146
147
 
147
148
  def executable(path:)
148
149
  logger.info("Making executable: #{path}")
149
- FileUtils.chmod("+x", path)
150
+ FileUtils.chmod('+x', path)
150
151
  logger.fatal("Setting executable bit on file: #{path} has failed!") unless File.executable?(path)
151
152
  end
152
153
 
153
- ###############################################################################
154
+ #########################################################################
154
155
 
155
156
  def clean(archive:, sums:)
156
157
  [archive, sums].each do |file|
157
- if File.file?(file)
158
- logger.info("Removing file: #{file}")
158
+ next unless File.file?(file)
159
159
 
160
- begin
161
- File.delete(file)
162
- rescue
163
- logger.error("Failed to delete: #{file}, please remove manually.")
164
- end
160
+ logger.info("Removing file: #{file}")
161
+
162
+ begin
163
+ File.delete(file)
164
+ rescue StandardError
165
+ logger.error("Failed to delete: #{file}, please remove manually.")
165
166
  end
166
167
  end
167
168
  end
168
169
 
169
- ###############################################################################
170
-
170
+ #########################################################################
171
171
  end
172
172
 
173
- ###############################################################################
174
-
173
+ ###########################################################################
175
174
  end
176
175
 
177
- ###############################################################################
178
-
176
+ #############################################################################
179
177
  end
180
178
 
181
179
  ###############################################################################