rake_terraform 1.11.0 → 1.16.0.pre.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +2 -0
  3. data/Gemfile.lock +120 -32
  4. data/Rakefile +110 -37
  5. data/bin/console +4 -3
  6. data/lib/rake_terraform.rb +33 -114
  7. data/lib/rake_terraform/task_sets.rb +4 -0
  8. data/lib/rake_terraform/task_sets/all.rb +48 -40
  9. data/lib/rake_terraform/task_sets/provider.rb +168 -0
  10. data/lib/rake_terraform/task_sets/terraform.rb +179 -0
  11. data/lib/rake_terraform/tasks.rb +2 -0
  12. data/lib/rake_terraform/tasks/destroy.rb +30 -25
  13. data/lib/rake_terraform/tasks/output.rb +26 -21
  14. data/lib/rake_terraform/tasks/plan.rb +29 -24
  15. data/lib/rake_terraform/tasks/provision.rb +30 -26
  16. data/lib/rake_terraform/tasks/validate.rb +26 -21
  17. data/lib/rake_terraform/version.rb +3 -1
  18. data/rake_terraform.gemspec +36 -14
  19. metadata +145 -77
  20. data/.circleci/config.yml +0 -56
  21. data/.circleci/gpg.private.enc +0 -0
  22. data/.envrc +0 -5
  23. data/.git-crypt/.gitattributes +0 -4
  24. data/.git-crypt/keys/default/0/41D2606F66C3FF28874362B61A16916844CE9D82.gpg +0 -0
  25. data/.git-crypt/keys/default/0/AAE932B730C2BA697FFDFAE05DA390873E38358B.gpg +0 -0
  26. data/.gitattributes +0 -1
  27. data/.gitignore +0 -32
  28. data/.rspec +0 -2
  29. data/.ruby-version +0 -1
  30. data/CHANGELOG.md +0 -35
  31. data/config/secrets/.unlocked +0 -1
  32. data/config/secrets/ci/encryption.passphrase +0 -1
  33. data/config/secrets/ci/gpg.private +0 -58
  34. data/config/secrets/ci/gpg.public +0 -31
  35. data/config/secrets/ci/ssh.private +0 -51
  36. data/config/secrets/ci/ssh.public +0 -1
  37. data/config/secrets/circle_ci/config.yaml +0 -2
  38. data/config/secrets/github/config.yaml +0 -2
  39. data/config/secrets/rubygems/credentials +0 -2
  40. data/go +0 -56
  41. data/scripts/ci/common/configure-git.sh +0 -8
  42. data/scripts/ci/common/configure-rubygems.sh +0 -16
  43. data/scripts/ci/common/install-git-crypt.sh +0 -8
  44. data/scripts/ci/common/install-gpg-key.sh +0 -19
  45. data/scripts/ci/steps/prerelease.sh +0 -16
  46. data/scripts/ci/steps/release.sh +0 -18
  47. data/scripts/ci/steps/test.sh +0 -12
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'logger'
2
4
  require 'rake_dependencies'
3
5
  require 'ruby_terraform'
@@ -6,137 +8,54 @@ require 'rake_terraform/tasks'
6
8
  require 'rake_terraform/task_sets'
7
9
 
8
10
  module RakeTerraform
9
- include RubyTerraform
10
-
11
11
  def self.define_command_tasks(opts = {}, &block)
12
12
  RakeTerraform::TaskSets::All.define(opts, &block)
13
13
  end
14
14
 
15
15
  def self.define_installation_tasks(opts = {})
16
- logger = opts[:logger] ||
17
- Logger.new(STDERR, level: Logger.const_get(ENV['TF_LOG'] || 'WARN'))
18
-
19
- namespace = opts[:namespace] || :terraform
20
- version = opts[:version] || '0.10.3'
21
- path = opts[:path] || File.join(Dir.pwd, 'vendor', 'terraform')
22
- providers = opts[:providers] || []
23
-
24
- command_task_set_opts = {
25
- namespace: namespace,
26
- dependency: 'terraform',
27
- version: version,
28
- path: path,
29
- type: :zip,
30
-
31
- os_ids: {mac: 'darwin', linux: 'linux'},
32
-
33
- uri_template:
34
- 'https://releases.hashicorp.com/terraform/<%= @version %>/' +
35
- 'terraform_<%= @version %>_<%= @os_id %>_amd64<%= @ext %>',
36
- file_name_template:
37
- 'terraform_<%= @version %>_<%= @os_id %>_amd64<%= @ext %>',
38
-
39
- needs_fetch: lambda { |params|
40
- terraform_binary = File.join(
41
- params.path, params.binary_directory, 'terraform')
42
- version_string = StringIO.new
16
+ command_task_set = define_command_installation_tasks(opts)
17
+ provider_task_sets = define_providers_installation_tasks(opts)
43
18
 
44
- logger.info("Terraform binary should be at: #{terraform_binary}")
19
+ configure_ruby_terraform(command_task_set.binary)
20
+ wire_provider_ensure_tasks(opts)
45
21
 
46
- if File.exist?(terraform_binary)
47
- command_line = Lino::CommandLineBuilder
48
- .for_command(terraform_binary)
49
- .with_flag('-version')
50
- .build
51
-
52
- logger.info(
53
- 'Fetching terraform version information using command: ' +
54
- "#{command_line}")
55
-
56
- command_line.execute(stdout: version_string)
57
-
58
- logger.info(
59
- "Terraform version information is: \n#{version_string.string}")
60
-
61
- version_line = version_string.string.lines.first
62
- version_is_correct = version_line =~ /#{version}/
63
-
64
- logger.debug(
65
- "Version: '#{version}' is in version line: " +
66
- "'#{version_line}'?: #{version_is_correct}")
67
-
68
- return !version_is_correct
69
- end
22
+ [command_task_set.delegate, provider_task_sets.map(&:delegate)]
23
+ end
70
24
 
71
- return true
72
- }
73
- }
25
+ class << self
26
+ private
74
27
 
75
- RubyTerraform.configure do |c|
76
- c.binary = File.join(path, 'bin', 'terraform')
28
+ def define_command_installation_tasks(opts = {})
29
+ RakeTerraform::TaskSets::Terraform.define(opts)
77
30
  end
78
31
 
79
- command_task_set = RakeDependencies::TaskSets::All
80
- .define(command_task_set_opts)
81
-
82
- provider_task_sets = providers.map do |provider|
83
- dependency = "terraform-provider-#{provider[:name]}"
84
- provider_task_set_opts = {
85
- namespace: provider[:name],
86
- dependency: dependency,
87
- version: provider[:version],
88
- path: provider[:path] || File.join('vendor', dependency),
89
- type: :tar_gz,
90
-
91
- os_ids: {mac: 'darwin', linux: 'linux'},
92
-
93
- uri_template:
94
- "https://github.com/#{provider[:repository]}/releases/" +
95
- "download/<%= @version %>/" +
96
- "#{dependency}_v<%= @version %>_<%= @os_id %>" +
97
- "_amd64<%= @ext %>",
98
- file_name_template:
99
- "#{dependency}_v<%= @version %>_<%= @os_id %>_amd64<%= @ext %>",
100
-
101
- source_binary_name_template: dependency,
102
- target_binary_name_template: "#{dependency}_v<%= @version %>",
103
-
104
- installation_directory: "#{ENV['HOME']}/.terraform.d/plugins",
105
-
106
- needs_fetch: lambda { |params|
107
- provider_binary = File.join(
108
- params.path,
109
- params.binary_directory,
110
- "#{dependency}_v#{params.version}")
111
-
112
- logger.info(
113
- "Terraform provider binary for: #{provider.name} " +
114
- "should be at: #{provider_binary}")
115
-
116
- binary_exists = File.exists?(provider_binary)
117
-
118
- logger.debug("Provider file exists?: #{binary_exists}")
119
-
120
- !binary_exists
121
- }
122
- }
32
+ def define_provider_installation_tasks(opts = {})
33
+ RakeTerraform::TaskSets::Provider.define(opts)
34
+ end
123
35
 
124
- provider_task_set = RakeDependencies::TaskSets::All.new(
125
- provider_task_set_opts)
36
+ def define_providers_installation_tasks(opts = {})
37
+ namespace = opts[:namespace] || :terraform
38
+ providers = opts[:providers] || []
126
39
 
127
- Rake.application.in_namespace namespace do
128
- Rake.application.in_namespace :providers do
129
- provider_task_set.define_on(Rake.application)
130
- end
40
+ providers.map do |provider_opts|
41
+ define_provider_installation_tasks(
42
+ { parent_namespace: namespace }.merge(provider_opts)
43
+ )
131
44
  end
45
+ end
132
46
 
133
- provider_task_set
47
+ def configure_ruby_terraform(binary)
48
+ RubyTerraform.configure { |c| c.binary = binary }
134
49
  end
135
50
 
136
- Rake::Task["#{namespace}:ensure"]
137
- .enhance(providers.map { |provider|
138
- "#{namespace}:providers:#{provider[:name]}:ensure" })
51
+ def wire_provider_ensure_tasks(opts)
52
+ namespace = opts[:namespace] || :terraform
53
+ providers = opts[:providers] || []
139
54
 
140
- return command_task_set, provider_task_sets
55
+ Rake::Task["#{namespace}:ensure"]
56
+ .enhance(providers.map do |provider_opts|
57
+ "#{namespace}:providers:#{provider_opts[:name]}:ensure"
58
+ end)
59
+ end
141
60
  end
142
61
  end
@@ -1 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative 'task_sets/all'
4
+ require_relative 'task_sets/terraform'
5
+ require_relative 'task_sets/provider'
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'rake_factory'
2
4
 
3
5
  require_relative '../tasks'
@@ -5,9 +7,9 @@ require_relative '../tasks'
5
7
  module RakeTerraform
6
8
  module TaskSets
7
9
  class All < RakeFactory::TaskSet
8
- parameter :configuration_name, :required => true
9
- parameter :source_directory, :required => true
10
- parameter :work_directory, :required => true
10
+ parameter :configuration_name, required: true
11
+ parameter :source_directory, required: true
12
+ parameter :work_directory, required: true
11
13
 
12
14
  parameter :backend_config
13
15
 
@@ -15,73 +17,79 @@ module RakeTerraform
15
17
  parameter :var_file
16
18
  parameter :state_file
17
19
 
18
- parameter :debug, :default => false
20
+ parameter :debug, default: false
19
21
 
20
- parameter :no_color, :default => false
21
- parameter :no_backup, :default => false
22
- parameter :no_print_output, :default => false
22
+ parameter :no_color, default: false
23
+ parameter :no_backup, default: false
24
+ parameter :no_print_output, default: false
23
25
 
24
26
  parameter :backup_file
25
27
  parameter :plan_file
26
28
 
27
29
  parameter :argument_names
28
30
 
29
- parameter :ensure_task_name, :default => :'terraform:ensure'
31
+ parameter :ensure_task_name, default: :'terraform:ensure'
30
32
 
31
- parameter :validate_task_name, :default => :validate
33
+ parameter :validate_task_name, default: :validate
32
34
  parameter :validate_argument_names
33
35
 
34
- parameter :plan_task_name, :default => :plan
36
+ parameter :plan_task_name, default: :plan
35
37
  parameter :plan_argument_names
36
38
 
37
- parameter :provision_task_name, :default => :provision
39
+ parameter :provision_task_name, default: :provision
38
40
  parameter :provision_argument_names
39
41
 
40
- parameter :destroy_task_name, :default => :destroy
42
+ parameter :destroy_task_name, default: :destroy
41
43
  parameter :destroy_argument_names
42
44
 
43
- parameter :output_task_name, :default => :output
45
+ parameter :output_task_name, default: :output
44
46
  parameter :output_argument_names
45
47
 
46
- task Tasks::Validate,
47
- name: RakeFactory::DynamicValue.new { |ts|
48
- ts.validate_task_name
49
- },
50
- argument_names: RakeFactory::DynamicValue.new { |ts|
48
+ task Tasks::Validate, {
49
+ name: RakeFactory::DynamicValue.new do |ts|
50
+ ts.validate_task_name
51
+ end,
52
+ argument_names:
53
+ RakeFactory::DynamicValue.new do |ts|
51
54
  ts.validate_argument_names || ts.argument_names || []
52
- }
55
+ end
56
+ }
53
57
  task Tasks::Plan, {
54
- name: RakeFactory::DynamicValue.new { |ts|
55
- ts.plan_task_name
56
- },
57
- argument_names: RakeFactory::DynamicValue.new { |ts|
58
+ name: RakeFactory::DynamicValue.new do |ts|
59
+ ts.plan_task_name
60
+ end,
61
+ argument_names:
62
+ RakeFactory::DynamicValue.new do |ts|
58
63
  ts.plan_argument_names || ts.argument_names || []
59
- }
64
+ end
60
65
  }
61
66
  task Tasks::Provision, {
62
- name: RakeFactory::DynamicValue.new { |ts|
63
- ts.provision_task_name
64
- },
65
- argument_names: RakeFactory::DynamicValue.new { |ts|
67
+ name: RakeFactory::DynamicValue.new do |ts|
68
+ ts.provision_task_name
69
+ end,
70
+ argument_names:
71
+ RakeFactory::DynamicValue.new do |ts|
66
72
  ts.provision_argument_names || ts.argument_names || []
67
- }
73
+ end
68
74
  }
69
75
  task Tasks::Destroy, {
70
- name: RakeFactory::DynamicValue.new { |ts|
71
- ts.destroy_task_name
72
- },
73
- argument_names: RakeFactory::DynamicValue.new { |ts|
76
+ name: RakeFactory::DynamicValue.new do |ts|
77
+ ts.destroy_task_name
78
+ end,
79
+ argument_names:
80
+ RakeFactory::DynamicValue.new do |ts|
74
81
  ts.destroy_argument_names || ts.argument_names || []
75
- }
82
+ end
76
83
  }
77
84
  task Tasks::Output, {
78
- name: RakeFactory::DynamicValue.new { |ts|
79
- ts.output_task_name
80
- },
81
- argument_names: RakeFactory::DynamicValue.new { |ts|
85
+ name: RakeFactory::DynamicValue.new do |ts|
86
+ ts.output_task_name
87
+ end,
88
+ argument_names:
89
+ RakeFactory::DynamicValue.new do |ts|
82
90
  ts.output_argument_names || ts.argument_names || []
83
- }
91
+ end
84
92
  }
85
93
  end
86
94
  end
87
- end
95
+ end
@@ -0,0 +1,168 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rake'
4
+
5
+ module RakeTerraform
6
+ module TaskSets
7
+ # rubocop:disable Metrics/ClassLength
8
+
9
+ class Provider
10
+ def self.define(*args, &configuration_block)
11
+ new(*args, &configuration_block).define_on(Rake.application)
12
+ end
13
+
14
+ attr_reader :delegate
15
+
16
+ def initialize(*args, &configuration_block)
17
+ @opts = args[0]
18
+ @delegate = RakeDependencies::TaskSets::All.new(
19
+ task_set_opts, &configuration_block
20
+ )
21
+ end
22
+
23
+ def define_on(application)
24
+ Rake.application.in_namespace parent_namespace do
25
+ Rake.application.in_namespace :providers do
26
+ @delegate.define_on(application)
27
+ end
28
+ end
29
+ self
30
+ end
31
+
32
+ private
33
+
34
+ # rubocop:disable Metrics/MethodLength
35
+
36
+ def task_set_opts
37
+ {
38
+ namespace: namespace,
39
+ dependency: dependency,
40
+ version: version,
41
+ path: path,
42
+ type: type,
43
+
44
+ os_ids: os_ids,
45
+
46
+ uri_template: uri_template,
47
+ file_name_template: file_name_template,
48
+
49
+ source_binary_name_template: source_binary_name_template,
50
+ target_binary_name_template: target_binary_name_template,
51
+
52
+ installation_directory: installation_directory,
53
+
54
+ needs_fetch: needs_fetch
55
+ }
56
+ end
57
+
58
+ # rubocop:enable Metrics/MethodLength
59
+
60
+ def logger
61
+ @logger ||=
62
+ @opts[:logger] ||
63
+ Logger.new($stderr,
64
+ level: Logger.const_get(ENV['TF_LOG'] || 'WARN'))
65
+ end
66
+
67
+ def name
68
+ @name ||= @opts[:name]
69
+ end
70
+
71
+ def repository
72
+ @repository ||= @opts[:repository]
73
+ end
74
+
75
+ def parent_namespace
76
+ @parent_namespace ||= @opts[:parent_namespace] || :terraform
77
+ end
78
+
79
+ def namespace
80
+ @namespace ||= @opts[:namespace] || name
81
+ end
82
+
83
+ def dependency
84
+ @dependency ||= "terraform-provider-#{name}"
85
+ end
86
+
87
+ def version
88
+ @version ||= @opts[:version]
89
+ end
90
+
91
+ def path
92
+ @path ||= @opts[:path] || File.join('vendor', dependency)
93
+ end
94
+
95
+ def binary_directory
96
+ @binary_directory ||= 'bin'
97
+ end
98
+
99
+ def binary_name
100
+ @binary_name ||= "#{dependency}_v#{version}"
101
+ end
102
+
103
+ def binary
104
+ @binary ||= File.join(path, binary_directory, binary_name)
105
+ end
106
+
107
+ def type
108
+ @type ||= :tar_gz
109
+ end
110
+
111
+ def os_ids
112
+ @os_ids ||= { mac: 'darwin', linux: 'linux' }
113
+ end
114
+
115
+ def uri_template
116
+ @uri_template ||=
117
+ "https://github.com/#{repository}/releases/" \
118
+ 'download/<%= @version %>/' \
119
+ "#{dependency}_v<%= @version %>_<%= @os_id %>" \
120
+ '_amd64<%= @ext %>'
121
+ end
122
+
123
+ def file_name_template
124
+ @file_name_template ||=
125
+ "#{dependency}_v<%= @version %>_<%= @os_id %>_amd64<%= @ext %>"
126
+ end
127
+
128
+ def source_binary_name_template
129
+ @source_binary_name_template ||= dependency
130
+ end
131
+
132
+ def target_binary_name_template
133
+ @target_binary_name_template ||= "#{dependency}_v<%= @version %>"
134
+ end
135
+
136
+ def installation_directory
137
+ @installation_directory ||= "#{ENV['HOME']}/.terraform.d/plugins"
138
+ end
139
+
140
+ def needs_fetch
141
+ @needs_fetch ||= ->(_) { return !exist?(binary) }
142
+ end
143
+
144
+ def exist?(binary)
145
+ log_binary_location(binary)
146
+
147
+ result = File.exist?(binary)
148
+
149
+ log_check_outcome(result)
150
+
151
+ result
152
+ end
153
+
154
+ def log_binary_location(binary)
155
+ logger.info(
156
+ "Terraform provider binary for: #{name} " \
157
+ "should be at: #{binary}"
158
+ )
159
+ end
160
+
161
+ def log_check_outcome(result)
162
+ logger.debug("Provider file exists?: #{result}")
163
+ end
164
+ end
165
+
166
+ # rubocop:enable Metrics/ClassLength
167
+ end
168
+ end