terraform-wrapper 0.0.2 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (30) hide show
  1. checksums.yaml +4 -4
  2. data/lib/terraform-wrapper.rb +34 -16
  3. data/lib/terraform-wrapper/common.rb +12 -23
  4. data/lib/terraform-wrapper/shared.rb +7 -1
  5. data/lib/terraform-wrapper/shared/auths.rb +9 -0
  6. data/lib/terraform-wrapper/shared/auths/azure.rb +179 -0
  7. data/lib/terraform-wrapper/shared/auths/common.rb +95 -0
  8. data/lib/terraform-wrapper/shared/backends/aws.rb +34 -29
  9. data/lib/terraform-wrapper/shared/backends/azure.rb +38 -39
  10. data/lib/terraform-wrapper/shared/backends/common.rb +18 -14
  11. data/lib/terraform-wrapper/shared/backends/local.rb +20 -18
  12. data/lib/terraform-wrapper/shared/binary.rb +16 -5
  13. data/lib/terraform-wrapper/shared/code.rb +15 -4
  14. data/lib/terraform-wrapper/shared/config.rb +61 -31
  15. data/lib/terraform-wrapper/shared/latest.rb +11 -7
  16. data/lib/terraform-wrapper/shared/logger.rb +80 -0
  17. data/lib/terraform-wrapper/shared/logging.rb +77 -0
  18. data/lib/terraform-wrapper/shared/runner.rb +59 -22
  19. data/lib/terraform-wrapper/shared/variables.rb +66 -0
  20. data/lib/terraform-wrapper/tasks/apply.rb +16 -14
  21. data/lib/terraform-wrapper/tasks/binary.rb +25 -21
  22. data/lib/terraform-wrapper/tasks/clean.rb +15 -11
  23. data/lib/terraform-wrapper/tasks/destroy.rb +16 -14
  24. data/lib/terraform-wrapper/tasks/init.rb +16 -14
  25. data/lib/terraform-wrapper/tasks/plan.rb +16 -14
  26. data/lib/terraform-wrapper/tasks/plandestroy.rb +16 -14
  27. data/lib/terraform-wrapper/tasks/validate.rb +7 -3
  28. data/lib/terraform-wrapper/version.rb +1 -1
  29. metadata +8 -3
  30. data/lib/terraform-wrapper/shared/identifiers.rb +0 -70
@@ -19,6 +19,10 @@ module TerraformWrapper
19
19
 
20
20
  class Binary < ::Rake::TaskLib
21
21
 
22
+ ###############################################################################
23
+
24
+ include TerraformWrapper::Shared::Logging
25
+
22
26
  ###############################################################################
23
27
 
24
28
  @binary
@@ -38,12 +42,12 @@ module TerraformWrapper
38
42
  def binary_task
39
43
  desc "Downloads and extracts the expected version of the Terraform binary if it is not already present."
40
44
  task :binary do |t, args|
41
- $logger.info("Checking Terraform binary for platform: #{@binary.platform}, version: #{@binary.version}")
45
+ logger.info("Checking Terraform binary for platform: #{@binary.platform}, version: #{@binary.version}")
42
46
 
43
47
  if not @binary.exists then
44
- $logger.info("Terraform binary not found. Preparing binary...")
48
+ logger.info("Terraform binary not found. Preparing binary...")
45
49
 
46
- raise "Failed to create binary directory: #{directory}" unless create_directory(directory: @binary.directory, purpose: "binaries")
50
+ logger.fatal("Failed to create binary directory: #{directory}") unless ::TerraformWrapper.create_directory(directory: @binary.directory, purpose: "binaries")
47
51
 
48
52
  archive_binary = "terraform"
49
53
  archive_file = "terraform_#{@binary.version}_#{@binary.platform}_amd64.zip"
@@ -65,11 +69,11 @@ module TerraformWrapper
65
69
  end
66
70
 
67
71
  if not @binary.executable then
68
- $logger.info("Terraform binary not executable. Setting permissions...")
72
+ logger.info("Terraform binary not executable. Setting permissions...")
69
73
  executable(path: @binary.path)
70
74
  end
71
75
 
72
- raise("Problem with checking the Terraform binary!") unless @binary.check
76
+ logger.fatal("Problem with checking the Terraform binary!") unless @binary.check
73
77
  end
74
78
  end
75
79
 
@@ -80,25 +84,25 @@ module TerraformWrapper
80
84
  ###############################################################################
81
85
 
82
86
  def download(path:, uri:)
83
- $logger.info("Downloading: #{uri}")
87
+ logger.info("Downloading: #{uri}")
84
88
 
85
89
  response = Net::HTTP.get_response(URI(uri))
86
90
 
87
- raise "Download request did not return HTTP status 200!" if response.code != "200"
88
- raise "Download response body is not permitted!" unless response.class.body_permitted?
89
- raise "Download response body is empty!" if response.body.nil?
91
+ logger.fatal("Download request did not return HTTP status 200!") if response.code != "200"
92
+ logger.fatal("Download response body is not permitted!") unless response.class.body_permitted?
93
+ logger.fatal("Download response body is empty!") if response.body.nil?
90
94
 
91
95
  open(path, "wb") { |file|
92
96
  file.write(response.body)
93
97
  }
94
98
 
95
- raise "Download failed!" unless File.file?(path)
99
+ logger.fatal("Download failed!") unless File.file?(path)
96
100
  end
97
101
 
98
102
  ###############################################################################
99
103
 
100
104
  def verify(file:, path:, sums:)
101
- $logger.info("Checking SHA256 for: #{file}")
105
+ logger.info("Checking SHA256 for: #{file}")
102
106
 
103
107
  result = false
104
108
 
@@ -110,25 +114,25 @@ module TerraformWrapper
110
114
  sum_file = fields["file"]
111
115
  sum_sha256 = fields["sum"]
112
116
  rescue
113
- $logger.warn("Unexpected data in sums file: #{sums}")
117
+ logger.warn("Unexpected data in sums file: #{sums}")
114
118
  next
115
119
  end
116
120
 
117
121
  if sum_file == file then
118
- $logger.info("Expected SHA256 sum: #{sum_sha256}")
119
- $logger.info("Actual SHA256 sum: #{sha256}")
122
+ logger.info("Expected SHA256 sum: #{sum_sha256}")
123
+ logger.info("Actual SHA256 sum: #{sha256}")
120
124
  result = (sum_sha256 == sha256)
121
125
  break
122
126
  end
123
127
  end
124
128
 
125
- raise "Error whilst verifying the SHA256 sum of the downloaded Terraform archive!" unless result
129
+ logger.fatal("Error whilst verifying the SHA256 sum of the downloaded Terraform archive!") unless result
126
130
  end
127
131
 
128
132
  ###############################################################################
129
133
 
130
134
  def extract(archive:, binary:, destination:)
131
- $logger.info("Extracting: #{archive}")
135
+ logger.info("Extracting: #{archive}")
132
136
 
133
137
  Zip::ZipFile.open(archive) do |zip|
134
138
  zip.each do |file|
@@ -136,15 +140,15 @@ module TerraformWrapper
136
140
  end
137
141
  end
138
142
 
139
- raise "Extraction of Terraform binary: #{binary}, from archive: #{archive} has failed!" unless File.file?(destination)
143
+ logger.fatal("Extraction of Terraform binary: #{binary}, from archive: #{archive} has failed!") unless File.file?(destination)
140
144
  end
141
145
 
142
146
  ###############################################################################
143
147
 
144
148
  def executable(path:)
145
- $logger.info("Making executable: #{path}")
149
+ logger.info("Making executable: #{path}")
146
150
  FileUtils.chmod("+x", path)
147
- raise "Setting executable bit on file: #{path} has failed!" unless File.executable?(path)
151
+ logger.fatal("Setting executable bit on file: #{path} has failed!") unless File.executable?(path)
148
152
  end
149
153
 
150
154
  ###############################################################################
@@ -152,12 +156,12 @@ module TerraformWrapper
152
156
  def clean(archive:, sums:)
153
157
  [archive, sums].each do |file|
154
158
  if File.file?(file)
155
- $logger.info("Removing file: #{file}")
159
+ logger.info("Removing file: #{file}")
156
160
 
157
161
  begin
158
162
  File.delete(file)
159
163
  rescue
160
- $logger.error("Failed to delete: #{file}, please remove manually.")
164
+ logger.error("Failed to delete: #{file}, please remove manually.")
161
165
  end
162
166
  end
163
167
  end
@@ -14,6 +14,10 @@ module TerraformWrapper
14
14
 
15
15
  class Clean < ::Rake::TaskLib
16
16
 
17
+ ###############################################################################
18
+
19
+ include TerraformWrapper::Shared::Logging
20
+
17
21
  ###############################################################################
18
22
 
19
23
  @@glob_directories = {
@@ -46,13 +50,13 @@ module TerraformWrapper
46
50
  ###############################################################################
47
51
 
48
52
  def clean_task
49
- desc "Cleans the Terraform workspace of any downloaded providers and modules."
53
+ desc "Cleans a Terraform infrastructure component workspace of any downloaded providers and modules."
50
54
  task :clean do |t, args|
51
- $logger.info("Cleaning Terraform component: #{@code.name}...")
55
+ logger.info("Cleaning Terraform component: #{@code.name}...")
52
56
 
53
57
  directory_counter = 0
54
58
  @@glob_directories.each do |key, value|
55
- $logger.info("Cleaning directories: #{key}")
59
+ logger.info("Cleaning directories: #{key}")
56
60
  directory_item_counter = 0
57
61
 
58
62
  directories = Dir.glob(File.join(@code.path, value))
@@ -62,12 +66,12 @@ module TerraformWrapper
62
66
  directory_counter += 1
63
67
  end
64
68
  end
65
- $logger.info("Cleaned: #{directory_item_counter.to_s} directories.")
69
+ logger.success("Cleaned: #{directory_item_counter.to_s} directories.")
66
70
  end
67
71
 
68
72
  file_counter = 0
69
73
  @@glob_files.each do |key, value|
70
- $logger.info("Cleaning files: #{key}")
74
+ logger.info("Cleaning files: #{key}")
71
75
  file_item_counter = 0
72
76
 
73
77
  files = Dir.glob(File.join(@code.path, value))
@@ -77,10 +81,10 @@ module TerraformWrapper
77
81
  file_counter += 1
78
82
  end
79
83
  end
80
- $logger.info("Cleaned: #{file_item_counter.to_s} files.")
84
+ logger.success("Cleaned: #{file_item_counter.to_s} files.")
81
85
  end
82
86
 
83
- $logger.info("Cleaned total: #{directory_counter.to_s} directories and: #{file_counter.to_s} files.")
87
+ logger.success("Cleaned total: #{directory_counter.to_s} directories and: #{file_counter.to_s} files.")
84
88
  end
85
89
  end
86
90
 
@@ -97,10 +101,10 @@ module TerraformWrapper
97
101
  begin
98
102
  FileUtils.remove_dir(directory)
99
103
  result = true
100
- $logger.info("Deleted directory: #{directory}")
104
+ logger.info("Deleted directory: #{directory}")
101
105
  rescue
102
106
  result = false
103
- $logger.warn("Failed to delete directory: #{directory}")
107
+ logger.warn("Failed to delete directory: #{directory}")
104
108
  end
105
109
  end
106
110
 
@@ -116,10 +120,10 @@ module TerraformWrapper
116
120
  begin
117
121
  File.delete(file)
118
122
  result = true
119
- $logger.info("Deleted file: #{file}")
123
+ logger.info("Deleted file: #{file}")
120
124
  rescue
121
125
  result = false
122
- $logger.warn("Failed to delete file: #{file}")
126
+ logger.warn("Failed to delete file: #{file}")
123
127
  end
124
128
  end
125
129
 
@@ -16,22 +16,20 @@ module TerraformWrapper
16
16
 
17
17
  ###############################################################################
18
18
 
19
- @backend
19
+ include TerraformWrapper::Shared::Logging
20
+
21
+ ###############################################################################
22
+
20
23
  @binary
21
24
  @code
22
- @configs
23
- @overrides
24
- @service
25
+ @options
25
26
 
26
27
  ###############################################################################
27
28
 
28
- def initialize(backend:, binary:, code:, configs:, overrides:, service:)
29
- @backend = backend
30
- @binary = binary
31
- @code = code
32
- @configs = configs
33
- @overrides = overrides
34
- @service = service
29
+ def initialize(binary:, code:, options:)
30
+ @binary = binary
31
+ @code = code
32
+ @options = options
35
33
 
36
34
  yield self if block_given?
37
35
 
@@ -41,13 +39,17 @@ module TerraformWrapper
41
39
  ###############################################################################
42
40
 
43
41
  def destroy_task
44
- desc "Destroys infrastructure with Terraform for a given configuration on a workspace."
42
+ desc "Destroys infrastructure with Terraform for a given configuration on an infrastructure component."
45
43
  task :destroy, [:config] => :binary do |t, args|
46
- $logger.info("Running Terraform destroy for service: #{@service}, component: #{@code.name}...")
44
+ options = @options.merge({"name" => args[:config]})
47
45
 
48
- config = TerraformWrapper::Shared::Config.new(backend: @backend, base: @configs, code: @code, name: args[:config], overrides: @overrides, service: @service)
46
+ logger.info("Processing configuration for Terraform destroy...")
47
+
48
+ config = TerraformWrapper::Shared::Config.new(code: @code, options: options)
49
49
  runner = TerraformWrapper::Shared::Runner.new(binary: @binary, code: @code)
50
50
 
51
+ logger.info("Running Terraform destroy for service: #{config.service}, component: #{@code.name}...")
52
+
51
53
  runner.init(config: config)
52
54
  runner.destroy
53
55
  end
@@ -16,22 +16,20 @@ module TerraformWrapper
16
16
 
17
17
  ###############################################################################
18
18
 
19
- @backend
19
+ include TerraformWrapper::Shared::Logging
20
+
21
+ ###############################################################################
22
+
20
23
  @binary
21
24
  @code
22
- @configs
23
- @overrides
24
- @service
25
+ @options
25
26
 
26
27
  ###############################################################################
27
28
 
28
- def initialize(backend:, binary:, code:, configs:, overrides:, service:)
29
- @backend = backend
30
- @binary = binary
31
- @code = code
32
- @configs = configs
33
- @overrides = overrides
34
- @service = service
29
+ def initialize(binary:, code:, options:)
30
+ @binary = binary
31
+ @code = code
32
+ @options = options
35
33
 
36
34
  yield self if block_given?
37
35
 
@@ -41,13 +39,17 @@ module TerraformWrapper
41
39
  ###############################################################################
42
40
 
43
41
  def init_task
44
- desc "Initialises the Terraform workspace and remote backend (if applicable)."
42
+ desc "Initialises the Terraform infrastructure component and state backend."
45
43
  task :init, [:config] => :binary do |t, args|
46
- $logger.info("Initialising Terraform for service: #{@service}, component: #{@code.name}...")
44
+ options = @options.merge({"name" => args[:config]})
47
45
 
48
- config = TerraformWrapper::Shared::Config.new(backend: @backend, base: @configs, code: @code, name: args[:config], overrides: @overrides, service: @service)
46
+ logger.info("Processing configuration for Terraform init...")
47
+
48
+ config = TerraformWrapper::Shared::Config.new(code: @code, options: options)
49
49
  runner = TerraformWrapper::Shared::Runner.new(binary: @binary, code: @code)
50
50
 
51
+ logger.info("Initialising Terraform for service: #{config.service}, component: #{@code.name}...")
52
+
51
53
  runner.init(config: config)
52
54
  end
53
55
  end
@@ -16,22 +16,20 @@ module TerraformWrapper
16
16
 
17
17
  ###############################################################################
18
18
 
19
- @backend
19
+ include TerraformWrapper::Shared::Logging
20
+
21
+ ###############################################################################
22
+
20
23
  @binary
21
24
  @code
22
- @configs
23
- @overrides
24
- @service
25
+ @options
25
26
 
26
27
  ###############################################################################
27
28
 
28
- def initialize(backend:, binary:, code:, configs:, overrides:, service:)
29
- @backend = backend
30
- @binary = binary
31
- @code = code
32
- @configs = configs
33
- @overrides = overrides
34
- @service = service
29
+ def initialize(binary:, code:, options:)
30
+ @binary = binary
31
+ @code = code
32
+ @options = options
35
33
 
36
34
  yield self if block_given?
37
35
 
@@ -41,13 +39,17 @@ module TerraformWrapper
41
39
  ###############################################################################
42
40
 
43
41
  def plan_task
44
- desc "Runs a Terraform plan for a given configuration on a workspace."
42
+ desc "Creates a Terraform plan for a given configuration on an infrastructure component."
45
43
  task :plan, [:config, :out] => :binary do |t, args|
46
- $logger.info("Running Terraform plan for service: #{@service}, component: #{@code.name}...")
44
+ options = @options.merge({"name" => args[:config]})
47
45
 
48
- config = TerraformWrapper::Shared::Config.new(backend: @backend, base: @configs, code: @code, name: args[:config], overrides: @overrides, service: @service)
46
+ logger.info("Processing configuration for Terraform plan...")
47
+
48
+ config = TerraformWrapper::Shared::Config.new(code: @code, options: options)
49
49
  runner = TerraformWrapper::Shared::Runner.new(binary: @binary, code: @code)
50
50
 
51
+ logger.info("Running Terraform plan for service: #{config.service}, component: #{@code.name}...")
52
+
51
53
  runner.init(config: config)
52
54
  runner.plan(file: args[:out])
53
55
  end
@@ -16,22 +16,20 @@ module TerraformWrapper
16
16
 
17
17
  ###############################################################################
18
18
 
19
- @backend
19
+ include TerraformWrapper::Shared::Logging
20
+
21
+ ###############################################################################
22
+
20
23
  @binary
21
24
  @code
22
- @configs
23
- @overrides
24
- @service
25
+ @options
25
26
 
26
27
  ###############################################################################
27
28
 
28
- def initialize(backend:, binary:, code:, configs:, overrides:, service:)
29
- @backend = backend
30
- @binary = binary
31
- @code = code
32
- @configs = configs
33
- @overrides = overrides
34
- @service = service
29
+ def initialize(binary:, code:, options:)
30
+ @binary = binary
31
+ @code = code
32
+ @options = options
35
33
 
36
34
  yield self if block_given?
37
35
 
@@ -41,13 +39,17 @@ module TerraformWrapper
41
39
  ###############################################################################
42
40
 
43
41
  def plan_destroy_task
44
- desc "Runs a Terraform destroy plan for a given configuration on a workspace."
42
+ desc "Creates a Terraform destroy plan for a given configuration on an infrastructure component."
45
43
  task :"plan-destroy", [:config, :out] => :binary do |t, args|
46
- $logger.info("Running Terraform destroy plan for service: #{@service}, component: #{@code.name}...")
44
+ options = @options.merge({"name" => args[:config]})
47
45
 
48
- config = TerraformWrapper::Shared::Config.new(backend: @backend, base: @configs, code: @code, name: args[:config], overrides: @overrides, service: @service)
46
+ logger.info("Processing configuration for Terraform destroy plan...")
47
+
48
+ config = TerraformWrapper::Shared::Config.new(code: @code, options: options)
49
49
  runner = TerraformWrapper::Shared::Runner.new(binary: @binary, code: @code)
50
50
 
51
+ logger.info("Running Terraform destroy plan for service: #{config.service}, component: #{@code.name}...")
52
+
51
53
  runner.init(config: config)
52
54
  runner.plan(destroy: true, file: args[:out])
53
55
  end
@@ -14,6 +14,10 @@ module TerraformWrapper
14
14
 
15
15
  class Validate < ::Rake::TaskLib
16
16
 
17
+ ###############################################################################
18
+
19
+ include TerraformWrapper::Shared::Logging
20
+
17
21
  ###############################################################################
18
22
 
19
23
  @binary
@@ -33,12 +37,12 @@ module TerraformWrapper
33
37
  ###############################################################################
34
38
 
35
39
  def validate_task
36
- desc "Validates the Terraform code within a workspace."
40
+ desc "Validates the Terraform code for an infrastructure component."
37
41
  task :validate => :binary do |t, args|
38
- $logger.info("Validating Terraform component: #{@code.name}...")
39
-
40
42
  runner = TerraformWrapper::Shared::Runner.new(binary: @binary, code: @code)
41
43
 
44
+ logger.info("Validating Terraform component: #{@code.name}...")
45
+
42
46
  runner.download
43
47
  runner.validate
44
48
  end