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.
@@ -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
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative 'tasks/validate'
2
4
  require_relative 'tasks/plan'
3
5
  require_relative 'tasks/provision'
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'rake_factory'
2
4
  require 'ruby_terraform'
3
5
  require 'ostruct'
@@ -7,16 +9,16 @@ module RakeTerraform
7
9
  module Tasks
8
10
  class Destroy < RakeFactory::Task
9
11
  default_name :destroy
10
- default_prerequisites RakeFactory::DynamicValue.new { |t|
12
+ default_prerequisites(RakeFactory::DynamicValue.new do |t|
11
13
  [t.ensure_task_name]
12
- }
13
- default_description RakeFactory::DynamicValue.new { |t|
14
+ end)
15
+ default_description(RakeFactory::DynamicValue.new do |t|
14
16
  "Destroy #{t.configuration_name} using terraform"
15
- }
17
+ end)
16
18
 
17
- parameter :configuration_name, :required => true
18
- parameter :source_directory, :required => true
19
- parameter :work_directory, :required => true
19
+ parameter :configuration_name, required: true
20
+ parameter :source_directory, required: true
21
+ parameter :work_directory, required: true
20
22
 
21
23
  parameter :backend_config
22
24
 
@@ -24,41 +26,51 @@ module RakeTerraform
24
26
  parameter :var_file
25
27
  parameter :state_file
26
28
 
27
- parameter :debug, :default => false
28
- parameter :no_color, :default => false
29
- parameter :no_backup, :default => false
29
+ parameter :debug, default: false
30
+ parameter :input, default: false
31
+ parameter :no_color, default: false
32
+ parameter :no_backup, default: false
30
33
 
31
34
  parameter :backup_file
32
35
 
33
- parameter :ensure_task_name, :default => :'terraform:ensure'
36
+ parameter :ensure_task_name, default: :'terraform:ensure'
37
+
38
+ # rubocop:disable Metrics/BlockLength
34
39
 
35
40
  action do |t|
36
41
  Colored2.disable! if t.no_color
37
42
 
43
+ module_directory =
44
+ File.join(FileUtils.pwd, t.source_directory)
38
45
  configuration_directory =
39
- File.join(t.work_directory, t.source_directory)
46
+ File.join(t.work_directory, t.source_directory)
40
47
 
41
- puts "Destroying #{t.configuration_name}".cyan
42
- RubyTerraform.clean(
43
- directory: configuration_directory)
48
+ Kernel.puts "Destroying #{t.configuration_name}".cyan
44
49
 
45
- mkdir_p File.dirname(configuration_directory)
46
- cp_r t.source_directory, configuration_directory
50
+ FileUtils.rm_rf(configuration_directory)
51
+ FileUtils.mkdir_p(configuration_directory)
47
52
 
48
- Dir.chdir(configuration_directory) do
49
- RubyTerraform.init(
50
- backend_config: t.backend_config,
51
- no_color: t.no_color)
52
- RubyTerraform.destroy(
53
- force: true,
54
- no_color: t.no_color,
55
- no_backup: t.no_backup,
56
- backup: t.backup_file,
57
- state: t.state_file,
58
- vars: t.vars,
59
- var_file: t.var_file)
60
- end
53
+ RubyTerraform.init(
54
+ chdir: configuration_directory,
55
+ from_module: module_directory,
56
+ backend_config: t.backend_config,
57
+ no_color: t.no_color,
58
+ input: t.input
59
+ )
60
+ RubyTerraform.destroy(
61
+ chdir: configuration_directory,
62
+ force: true,
63
+ input: t.input,
64
+ no_color: t.no_color,
65
+ no_backup: t.no_backup,
66
+ backup: t.backup_file,
67
+ state: t.state_file,
68
+ vars: t.vars,
69
+ var_file: t.var_file
70
+ )
61
71
  end
72
+
73
+ # rubocop:enable Metrics/BlockLength
62
74
  end
63
75
  end
64
76
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'rake_factory'
2
4
  require 'ruby_terraform'
3
5
  require 'ostruct'
@@ -7,53 +9,57 @@ module RakeTerraform
7
9
  module Tasks
8
10
  class Output < RakeFactory::Task
9
11
  default_name :output
10
- default_prerequisites RakeFactory::DynamicValue.new { |t|
12
+ default_prerequisites(RakeFactory::DynamicValue.new do |t|
11
13
  [t.ensure_task_name]
12
- }
13
- default_description RakeFactory::DynamicValue.new { |t|
14
+ end)
15
+ default_description(RakeFactory::DynamicValue.new do |t|
14
16
  "Output #{t.configuration_name} using terraform"
15
- }
17
+ end)
16
18
 
17
- parameter :configuration_name, :required => true
18
- parameter :source_directory, :required => true
19
- parameter :work_directory, :required => true
19
+ parameter :configuration_name, required: true
20
+ parameter :source_directory, required: true
21
+ parameter :work_directory, required: true
20
22
 
21
23
  parameter :backend_config
22
24
 
23
25
  parameter :state_file
24
26
 
25
- parameter :debug, :default => false
26
- parameter :no_color, :default => false
27
- parameter :no_print_output, :default => false
27
+ parameter :debug, default: false
28
+ parameter :input, default: false
29
+ parameter :no_color, default: false
30
+ parameter :no_print_output, default: false
28
31
 
29
- parameter :ensure_task_name, :default => :'terraform:ensure'
32
+ parameter :ensure_task_name, default: :'terraform:ensure'
30
33
 
31
34
  action do |t|
32
35
  Colored2.disable! if t.no_color
33
36
 
37
+ module_directory =
38
+ File.join(FileUtils.pwd, t.source_directory)
34
39
  configuration_directory =
35
- File.join(t.work_directory, t.source_directory)
36
-
37
- puts "Output of #{t.configuration_name}".cyan
38
- RubyTerraform.clean(
39
- directory: configuration_directory)
40
+ File.join(t.work_directory, t.source_directory)
40
41
 
41
- mkdir_p File.dirname(configuration_directory)
42
- cp_r t.source_directory, configuration_directory
42
+ Kernel.puts("Output of #{t.configuration_name}".cyan)
43
43
 
44
- Dir.chdir(configuration_directory) do
45
- RubyTerraform.init(
46
- backend_config: t.backend_config,
47
- no_color: t.no_color)
44
+ FileUtils.rm_rf(configuration_directory)
45
+ FileUtils.mkdir_p(configuration_directory)
48
46
 
49
- output = RubyTerraform.output(
50
- no_color: t.no_color,
51
- state: t.state_file)
47
+ RubyTerraform.init(
48
+ chdir: configuration_directory,
49
+ from_module: module_directory,
50
+ backend_config: t.backend_config,
51
+ no_color: t.no_color,
52
+ input: t.input
53
+ )
54
+ output = RubyTerraform.output(
55
+ chdir: configuration_directory,
56
+ no_color: t.no_color,
57
+ state: t.state_file
58
+ )
52
59
 
53
- puts output unless t.no_print_output
60
+ Kernel.puts(output) unless t.no_print_output
54
61
 
55
- output
56
- end
62
+ output
57
63
  end
58
64
  end
59
65
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'rake_factory'
2
4
  require 'ruby_terraform'
3
5
  require 'ostruct'
@@ -7,16 +9,16 @@ module RakeTerraform
7
9
  module Tasks
8
10
  class Plan < RakeFactory::Task
9
11
  default_name :plan
10
- default_prerequisites RakeFactory::DynamicValue.new { |t|
12
+ default_prerequisites(RakeFactory::DynamicValue.new do |t|
11
13
  [t.ensure_task_name]
12
- }
13
- default_description RakeFactory::DynamicValue.new { |t|
14
+ end)
15
+ default_description(RakeFactory::DynamicValue.new do |t|
14
16
  "Plan #{t.configuration_name} using terraform"
15
- }
17
+ end)
16
18
 
17
- parameter :configuration_name, :required => true
18
- parameter :source_directory, :required => true
19
- parameter :work_directory, :required => true
19
+ parameter :configuration_name, required: true
20
+ parameter :source_directory, required: true
21
+ parameter :work_directory, required: true
20
22
 
21
23
  parameter :backend_config
22
24
 
@@ -24,39 +26,45 @@ module RakeTerraform
24
26
  parameter :var_file
25
27
  parameter :state_file
26
28
 
27
- parameter :debug, :default => false
28
- parameter :no_color, :default => false
29
+ parameter :debug, default: false
30
+ parameter :input, default: false
31
+ parameter :no_color, default: false
29
32
 
30
33
  parameter :plan_file
31
- parameter :destroy, :default => false
34
+ parameter :destroy, default: false
32
35
 
33
- parameter :ensure_task_name, :default => :'terraform:ensure'
36
+ parameter :ensure_task_name, default: :'terraform:ensure'
34
37
 
35
38
  action do |t|
36
39
  Colored2.disable! if t.no_color
37
40
 
41
+ module_directory =
42
+ File.join(FileUtils.pwd, t.source_directory)
38
43
  configuration_directory =
39
- File.join(t.work_directory, t.source_directory)
40
-
41
- puts "Planning #{configuration_name}".cyan
42
- RubyTerraform.clean(
43
- directory: configuration_directory)
44
-
45
- mkdir_p File.dirname(configuration_directory)
46
- cp_r t.source_directory, configuration_directory
47
-
48
- Dir.chdir(configuration_directory) do
49
- RubyTerraform.init(
50
- backend_config: t.backend_config,
51
- no_color: t.no_color)
52
- RubyTerraform.plan(
53
- no_color: t.no_color,
54
- destroy: t.destroy,
55
- state: t.state_file,
56
- plan: t.plan_file,
57
- vars: t.vars,
58
- var_file: t.var_file)
59
- end
44
+ File.join(t.work_directory, t.source_directory)
45
+
46
+ Kernel.puts("Planning #{configuration_name}".cyan)
47
+
48
+ FileUtils.rm_rf(configuration_directory)
49
+ FileUtils.mkdir_p(configuration_directory)
50
+
51
+ RubyTerraform.init(
52
+ chdir: configuration_directory,
53
+ from_module: module_directory,
54
+ backend_config: t.backend_config,
55
+ no_color: t.no_color,
56
+ input: t.input
57
+ )
58
+ RubyTerraform.plan(
59
+ chdir: configuration_directory,
60
+ input: t.input,
61
+ no_color: t.no_color,
62
+ destroy: t.destroy,
63
+ state: t.state_file,
64
+ plan: t.plan_file,
65
+ vars: t.vars,
66
+ var_file: t.var_file
67
+ )
60
68
  end
61
69
  end
62
70
  end