rake_terraform 1.15.0 → 1.16.0.pre.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -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
@@ -0,0 +1,179 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rake'
4
+
5
+ module RakeTerraform
6
+ module TaskSets
7
+ # rubocop:disable Metrics/ClassLength
8
+
9
+ class Terraform
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 =
19
+ RakeDependencies::TaskSets::All.define(
20
+ task_set_opts, &configuration_block
21
+ )
22
+ end
23
+
24
+ def define_on(application)
25
+ @delegate.define_on(application)
26
+ self
27
+ end
28
+
29
+ def binary
30
+ @binary ||= File.join(path, binary_directory, binary_name)
31
+ end
32
+
33
+ private
34
+
35
+ # rubocop:disable Metrics/MethodLength
36
+
37
+ def task_set_opts
38
+ {
39
+ namespace: namespace,
40
+ dependency: dependency,
41
+ version: version,
42
+ path: path,
43
+ type: type,
44
+
45
+ os_ids: os_ids,
46
+
47
+ uri_template: uri_template,
48
+ file_name_template: file_name_template,
49
+
50
+ binary_directory: binary_directory,
51
+
52
+ needs_fetch: needs_fetch
53
+ }
54
+ end
55
+
56
+ # rubocop:enable Metrics/MethodLength
57
+
58
+ def logger
59
+ @logger ||=
60
+ @opts[:logger] ||
61
+ Logger.new($stderr,
62
+ level: Logger.const_get(ENV['TF_LOG'] || 'WARN'))
63
+ end
64
+
65
+ def namespace
66
+ @namespace ||= @opts[:namespace] || :terraform
67
+ end
68
+
69
+ def dependency
70
+ @dependency ||= 'terraform'
71
+ end
72
+
73
+ def version
74
+ @version ||= @opts[:version] || '0.10.3'
75
+ end
76
+
77
+ def path
78
+ @path ||= @opts[:path] ||
79
+ File.join(Dir.pwd, 'vendor', 'terraform')
80
+ end
81
+
82
+ def binary_directory
83
+ @binary_directory ||= 'bin'
84
+ end
85
+
86
+ def binary_name
87
+ @binary_name ||= 'terraform'
88
+ end
89
+
90
+ def type
91
+ @type ||= :zip
92
+ end
93
+
94
+ def os_ids
95
+ @os_ids ||= { mac: 'darwin', linux: 'linux' }
96
+ end
97
+
98
+ def uri_template
99
+ @uri_template ||=
100
+ 'https://releases.hashicorp.com/terraform/<%= @version %>/' \
101
+ 'terraform_<%= @version %>_<%= @os_id %>_amd64<%= @ext %>'
102
+ end
103
+
104
+ def file_name_template
105
+ @file_name_template ||=
106
+ 'terraform_<%= @version %>_<%= @os_id %>_amd64<%= @ext %>'
107
+ end
108
+
109
+ def needs_fetch
110
+ @needs_fetch ||= ->(_) { return !exists_with_correct_version?(binary) }
111
+ end
112
+
113
+ def exists_with_correct_version?(binary)
114
+ log_binary_location(binary)
115
+
116
+ exist?(binary) && correct_version?(binary)
117
+ end
118
+
119
+ def exist?(binary)
120
+ File.exist?(binary)
121
+ end
122
+
123
+ def correct_version?(binary)
124
+ result = StringIO.new
125
+ command = version_command(binary)
126
+
127
+ log_version_lookup(command)
128
+
129
+ command.execute(stdout: result)
130
+
131
+ log_version_information(result)
132
+ log_check_outcome(result)
133
+
134
+ contains_version_number?(result)
135
+ end
136
+
137
+ def version_command(binary)
138
+ Lino::CommandLineBuilder
139
+ .for_command(binary)
140
+ .with_flag('-version')
141
+ .build
142
+ end
143
+
144
+ def log_binary_location(binary)
145
+ logger.info("Terraform binary should be at: #{binary}")
146
+ end
147
+
148
+ def log_version_lookup(command)
149
+ logger.info(
150
+ 'Fetching terraform version information using command: ' \
151
+ "#{command}"
152
+ )
153
+ end
154
+
155
+ def log_version_information(result)
156
+ logger.info(
157
+ "Terraform version information is: \n#{result}"
158
+ )
159
+ end
160
+
161
+ def log_check_outcome(result)
162
+ logger.debug(
163
+ "Version: '#{version}' is in version line: " \
164
+ "'#{version_line(result)}'?: #{contains_version_number?(result)}"
165
+ )
166
+ end
167
+
168
+ def version_line(result)
169
+ result.string.lines.first
170
+ end
171
+
172
+ def contains_version_number?(result)
173
+ version_line(result) =~ /#{version}/
174
+ end
175
+ end
176
+
177
+ # rubocop:enable Metrics/ClassLength
178
+ end
179
+ end