Class: TerraformWrapper::Shared::Runner
- Inherits:
-
Object
- Object
- TerraformWrapper::Shared::Runner
show all
- Includes:
- Logging
- Defined in:
- lib/terraform-wrapper/shared/runner.rb
Constant Summary
collapse
- @@lockfile_name =
'.terraform.lock.hcl'
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods included from Logging
configure_logger_for, logger_for
Constructor Details
#initialize(binary:, code:) ⇒ Runner
Returns a new instance of Runner.
24
25
26
27
28
29
30
31
|
# File 'lib/terraform-wrapper/shared/runner.rb', line 24
def initialize(binary:, code:)
logger.fatal("Terraform code location: #{code.path} does not exist!") unless code.check
@binary = binary
@code = code
@initialised = false
end
|
Instance Attribute Details
#binary ⇒ Object
Returns the value of attribute binary.
20
21
22
|
# File 'lib/terraform-wrapper/shared/runner.rb', line 20
def binary
@binary
end
|
#code ⇒ Object
Returns the value of attribute code.
20
21
22
|
# File 'lib/terraform-wrapper/shared/runner.rb', line 20
def code
@code
end
|
#config ⇒ Object
Returns the value of attribute config.
20
21
22
|
# File 'lib/terraform-wrapper/shared/runner.rb', line 20
def config
@config
end
|
#downloaded ⇒ Object
Returns the value of attribute downloaded.
20
21
22
|
# File 'lib/terraform-wrapper/shared/runner.rb', line 20
def downloaded
@downloaded
end
|
#initialised ⇒ Object
Returns the value of attribute initialised.
20
21
22
|
# File 'lib/terraform-wrapper/shared/runner.rb', line 20
def initialised
@initialised
end
|
Instance Method Details
#apply(file: nil) ⇒ Object
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
# File 'lib/terraform-wrapper/shared/runner.rb', line 90
def apply(file: nil)
logger.fatal('Cannot Terraform apply before initialising backend!') unless initialised
parameters = []
parameters.append('-auto-approve')
if !file.nil? and file.is_a?(String) and !file.strip.empty?
logger.fatal("Plan file: #{file} does not exist!") unless File.file?(file)
parameters.append("\"#{file}\"")
else
parameters.concat(variable_files)
parameters.concat(variable_strings)
end
logger.fatal('Terraform apply failed!') unless run(action: 'apply', parameters: parameters)
end
|
#destroy ⇒ Object
130
131
132
133
134
135
136
137
138
139
|
# File 'lib/terraform-wrapper/shared/runner.rb', line 130
def destroy
logger.fatal('Cannot Terraform destroy before initialising backend!') unless initialised
parameters = []
parameters.concat(variable_files)
parameters.concat(variable_strings)
parameters.append('-auto-approve')
logger.fatal('Terraform destroy failed!') unless run(action: 'destroy', parameters: parameters)
end
|
#download ⇒ Object
35
36
37
38
39
40
41
42
|
# File 'lib/terraform-wrapper/shared/runner.rb', line 35
def download
parameters = []
parameters.append('-backend=false')
logger.fatal('Failed to download Terraform modules.') unless run(action: 'init', parameters: parameters)
@downloaded = true
end
|
#fmt(check: true) ⇒ Object
178
179
180
181
182
183
184
|
# File 'lib/terraform-wrapper/shared/runner.rb', line 178
def fmt(check: true)
parameters = []
parameters.append('-check') if check.to_s.downcase == 'true' && check
logger.fatal('Terraform fmt failed!') unless run(action: 'fmt', parameters: parameters)
end
|
#import(address: nil, id: nil) ⇒ Object
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
|
# File 'lib/terraform-wrapper/shared/runner.rb', line 143
def import(address: nil, id: nil)
logger.fatal('Cannot Terraform import before initialising backend!') unless initialised
logger.fatal('Terraform state address for import must be a string!') unless address.is_a?(String)
logger.fatal('Terraform state address for import must not be blank!') if address.strip.empty?
logger.fatal('Identification for infrastructure to import must be a string!') unless id.is_a?(String)
logger.fatal('Identification for infrastructure to import must not be blank!') if id.strip.empty?
parameters = []
parameters.concat(variable_files)
parameters.concat(variable_strings)
parameters.append("'#{address}'")
parameters.append("'#{id}'")
logger.fatal('Terraform import failed!') unless run(action: 'import', parameters: parameters)
end
|
#init(config:) ⇒ Object
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
# File 'lib/terraform-wrapper/shared/runner.rb', line 46
def init(config:)
parameters = []
parameters.append('-reconfigure')
config.backend.hash.each do |key, value|
parameters.append("-backend-config=\"#{key}=#{value}\"")
end
config.auths.map(&:auth)
logger.fatal('Failed to initialise Terraform with backend.') unless run(action: 'init', parameters: parameters)
@config = config
@initialised = true
end
|
#plan(destroy: false, file: nil) ⇒ Object
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
|
# File 'lib/terraform-wrapper/shared/runner.rb', line 109
def plan(destroy: false, file: nil)
logger.fatal('Cannot Terraform plan before initialising backend!') unless initialised
parameters = []
parameters.concat(variable_files)
parameters.concat(variable_strings)
if !file.nil? and file.is_a?(String) and !file.strip.empty?
logger.fatal("Failed to create plan directory: #{directory}") unless ::TerraformWrapper.create_directory(
directory: File.dirname(file), purpose: 'plan'
)
parameters.append("-out=\"#{file}\"")
end
parameters.append('-destroy') if destroy
logger.fatal('Terraform plan failed!') unless run(action: 'plan', parameters: parameters)
end
|
#taint(address: nil) ⇒ Object
164
165
166
167
168
169
170
171
172
173
174
|
# File 'lib/terraform-wrapper/shared/runner.rb', line 164
def taint(address: nil)
logger.fatal('Cannot Terraform taint before initialising backend!') unless initialised
logger.fatal('Terraform state address for taint must be a string!') unless address.is_a?(String)
logger.fatal('Terraform state address for taint must not be blank!') if address.strip.empty?
parameters = []
parameters.append("'#{address}'")
logger.fatal('Terraform taint failed!') unless run(action: 'taint', parameters: parameters)
end
|
#upgrade ⇒ Object
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
# File 'lib/terraform-wrapper/shared/runner.rb', line 64
def upgrade
lockfile_path = File.join(@code.path, @@lockfile_name)
if File.file?(lockfile_path)
logger.info("Removing lock file: #{lockfile_path}")
File.delete(lockfile_path)
end
logger.fatal('Lock file removal failed!') if File.file?(lockfile_path)
parameters = []
parameters.append('-update')
logger.fatal('Failed to upgrade Terraform modules!') unless run(action: 'get', parameters: parameters)
parameters = []
parameters.append('lock')
@binary.provider.platforms.each do |platform|
parameters.append("-platform=\"#{platform}\"")
end
logger.fatal('Failed to upgrade Terraform providers!') unless run(action: 'providers', parameters: parameters)
end
|
#validate ⇒ Object
188
189
190
191
|
# File 'lib/terraform-wrapper/shared/runner.rb', line 188
def validate
logger.fatal('Cannot Terraform validate before downloading modules!') unless downloaded
logger.fatal('Terraform validation failed!') unless run(action: 'validate')
end
|