rake_terraform 1.13.0 → 1.16.0.pre.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -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,81 @@ module RakeTerraform
15
17
  parameter :var_file
16
18
  parameter :state_file
17
19
 
18
- parameter :debug, :default => false
20
+ parameter :debug, default: false
21
+
22
+ parameter :input, default: false
19
23
 
20
- parameter :no_color, :default => false
21
- parameter :no_backup, :default => false
22
- parameter :no_print_output, :default => false
24
+ parameter :no_color, default: false
25
+ parameter :no_backup, default: false
26
+ parameter :no_print_output, default: false
23
27
 
24
28
  parameter :backup_file
25
29
  parameter :plan_file
26
30
 
27
31
  parameter :argument_names
28
32
 
29
- parameter :ensure_task_name, :default => :'terraform:ensure'
33
+ parameter :ensure_task_name, default: :'terraform:ensure'
30
34
 
31
- parameter :validate_task_name, :default => :validate
35
+ parameter :validate_task_name, default: :validate
32
36
  parameter :validate_argument_names
33
37
 
34
- parameter :plan_task_name, :default => :plan
38
+ parameter :plan_task_name, default: :plan
35
39
  parameter :plan_argument_names
36
40
 
37
- parameter :provision_task_name, :default => :provision
41
+ parameter :provision_task_name, default: :provision
38
42
  parameter :provision_argument_names
39
43
 
40
- parameter :destroy_task_name, :default => :destroy
44
+ parameter :destroy_task_name, default: :destroy
41
45
  parameter :destroy_argument_names
42
46
 
43
- parameter :output_task_name, :default => :output
47
+ parameter :output_task_name, default: :output
44
48
  parameter :output_argument_names
45
49
 
46
- task Tasks::Validate,
47
- name: RakeFactory::DynamicValue.new { |ts|
48
- ts.validate_task_name
49
- },
50
- argument_names: RakeFactory::DynamicValue.new { |ts|
50
+ task Tasks::Validate, {
51
+ name: RakeFactory::DynamicValue.new do |ts|
52
+ ts.validate_task_name
53
+ end,
54
+ argument_names:
55
+ RakeFactory::DynamicValue.new do |ts|
51
56
  ts.validate_argument_names || ts.argument_names || []
52
- }
57
+ end
58
+ }
53
59
  task Tasks::Plan, {
54
- name: RakeFactory::DynamicValue.new { |ts|
55
- ts.plan_task_name
56
- },
57
- argument_names: RakeFactory::DynamicValue.new { |ts|
60
+ name: RakeFactory::DynamicValue.new do |ts|
61
+ ts.plan_task_name
62
+ end,
63
+ argument_names:
64
+ RakeFactory::DynamicValue.new do |ts|
58
65
  ts.plan_argument_names || ts.argument_names || []
59
- }
66
+ end
60
67
  }
61
68
  task Tasks::Provision, {
62
- name: RakeFactory::DynamicValue.new { |ts|
63
- ts.provision_task_name
64
- },
65
- argument_names: RakeFactory::DynamicValue.new { |ts|
69
+ name: RakeFactory::DynamicValue.new do |ts|
70
+ ts.provision_task_name
71
+ end,
72
+ argument_names:
73
+ RakeFactory::DynamicValue.new do |ts|
66
74
  ts.provision_argument_names || ts.argument_names || []
67
- }
75
+ end
68
76
  }
69
77
  task Tasks::Destroy, {
70
- name: RakeFactory::DynamicValue.new { |ts|
71
- ts.destroy_task_name
72
- },
73
- argument_names: RakeFactory::DynamicValue.new { |ts|
78
+ name: RakeFactory::DynamicValue.new do |ts|
79
+ ts.destroy_task_name
80
+ end,
81
+ argument_names:
82
+ RakeFactory::DynamicValue.new do |ts|
74
83
  ts.destroy_argument_names || ts.argument_names || []
75
- }
84
+ end
76
85
  }
77
86
  task Tasks::Output, {
78
- name: RakeFactory::DynamicValue.new { |ts|
79
- ts.output_task_name
80
- },
81
- argument_names: RakeFactory::DynamicValue.new { |ts|
87
+ name: RakeFactory::DynamicValue.new do |ts|
88
+ ts.output_task_name
89
+ end,
90
+ argument_names:
91
+ RakeFactory::DynamicValue.new do |ts|
82
92
  ts.output_argument_names || ts.argument_names || []
83
- }
93
+ end
84
94
  }
85
95
  end
86
96
  end
87
- end
97
+ 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