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
@@ -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,39 +26,42 @@ 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 :no_color, default: false
31
+ parameter :no_backup, default: false
30
32
 
31
33
  parameter :backup_file
32
34
 
33
- parameter :ensure_task_name, :default => :'terraform:ensure'
35
+ parameter :ensure_task_name, default: :'terraform:ensure'
34
36
 
35
37
  action do |t|
36
38
  Colored2.disable! if t.no_color
37
39
 
38
40
  configuration_directory =
39
- File.join(t.work_directory, t.source_directory)
41
+ File.join(t.work_directory, t.source_directory)
40
42
 
41
- puts "Destroying #{t.configuration_name}".cyan
43
+ Kernel.puts "Destroying #{t.configuration_name}".cyan
42
44
  RubyTerraform.clean(
43
- directory: configuration_directory)
45
+ directory: configuration_directory
46
+ )
44
47
 
45
- mkdir_p File.dirname(configuration_directory)
46
- cp_r t.source_directory, configuration_directory
48
+ FileUtils.mkdir_p(File.dirname(configuration_directory))
49
+ FileUtils.cp_r(t.source_directory, configuration_directory)
47
50
 
48
51
  Dir.chdir(configuration_directory) do
49
52
  RubyTerraform.init(
50
- backend_config: t.backend_config,
51
- no_color: t.no_color)
53
+ backend_config: t.backend_config,
54
+ no_color: t.no_color
55
+ )
52
56
  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)
57
+ force: true,
58
+ no_color: t.no_color,
59
+ no_backup: t.no_backup,
60
+ backup: t.backup_file,
61
+ state: t.state_file,
62
+ vars: t.vars,
63
+ var_file: t.var_file
64
+ )
60
65
  end
61
66
  end
62
67
  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,50 +9,53 @@ 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 :no_color, default: false
29
+ parameter :no_print_output, default: false
28
30
 
29
- parameter :ensure_task_name, :default => :'terraform:ensure'
31
+ parameter :ensure_task_name, default: :'terraform:ensure'
30
32
 
31
33
  action do |t|
32
34
  Colored2.disable! if t.no_color
33
35
 
34
36
  configuration_directory =
35
- File.join(t.work_directory, t.source_directory)
37
+ File.join(t.work_directory, t.source_directory)
36
38
 
37
- puts "Output of #{t.configuration_name}".cyan
39
+ Kernel.puts("Output of #{t.configuration_name}".cyan)
38
40
  RubyTerraform.clean(
39
- directory: configuration_directory)
41
+ directory: configuration_directory
42
+ )
40
43
 
41
- mkdir_p File.dirname(configuration_directory)
42
- cp_r t.source_directory, configuration_directory
44
+ FileUtils.mkdir_p(File.dirname(configuration_directory))
45
+ FileUtils.cp_r(t.source_directory, configuration_directory)
43
46
 
44
47
  Dir.chdir(configuration_directory) do
45
48
  RubyTerraform.init(
46
- backend_config: t.backend_config,
47
- no_color: t.no_color)
49
+ backend_config: t.backend_config,
50
+ no_color: t.no_color
51
+ )
48
52
 
49
53
  output = RubyTerraform.output(
50
- no_color: t.no_color,
51
- state: t.state_file)
54
+ no_color: t.no_color,
55
+ state: t.state_file
56
+ )
52
57
 
53
- puts output unless t.no_print_output
58
+ Kernel.puts(output) unless t.no_print_output
54
59
 
55
60
  output
56
61
  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,38 +26,41 @@ 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 :no_color, default: false
29
31
 
30
32
  parameter :plan_file
31
- parameter :destroy, :default => false
33
+ parameter :destroy, default: false
32
34
 
33
- parameter :ensure_task_name, :default => :'terraform:ensure'
35
+ parameter :ensure_task_name, default: :'terraform:ensure'
34
36
 
35
37
  action do |t|
36
38
  Colored2.disable! if t.no_color
37
39
 
38
40
  configuration_directory =
39
- File.join(t.work_directory, t.source_directory)
41
+ File.join(t.work_directory, t.source_directory)
40
42
 
41
- puts "Planning #{configuration_name}".cyan
43
+ Kernel.puts("Planning #{configuration_name}".cyan)
42
44
  RubyTerraform.clean(
43
- directory: configuration_directory)
45
+ directory: configuration_directory
46
+ )
44
47
 
45
- mkdir_p File.dirname(configuration_directory)
46
- cp_r t.source_directory, configuration_directory
48
+ FileUtils.mkdir_p(File.dirname(configuration_directory))
49
+ FileUtils.cp_r(t.source_directory, configuration_directory)
47
50
 
48
51
  Dir.chdir(configuration_directory) do
49
52
  RubyTerraform.init(
50
- backend_config: t.backend_config,
51
- no_color: t.no_color)
53
+ backend_config: t.backend_config,
54
+ no_color: t.no_color
55
+ )
52
56
  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)
57
+ no_color: t.no_color,
58
+ destroy: t.destroy,
59
+ state: t.state_file,
60
+ plan: t.plan_file,
61
+ vars: t.vars,
62
+ var_file: t.var_file
63
+ )
59
64
  end
60
65
  end
61
66
  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 Provision < RakeFactory::Task
9
11
  default_name :provision
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
  "Provision #{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,40 +26,42 @@ 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 :no_color, default: false
31
+ parameter :no_backup, default: false
30
32
 
31
33
  parameter :backup_file
32
34
 
33
- parameter :ensure_task_name, :default => :'terraform:ensure'
35
+ parameter :ensure_task_name, default: :'terraform:ensure'
34
36
 
35
37
  action do |t|
36
38
  Colored2.disable! if t.no_color
37
39
 
38
40
  configuration_directory =
39
- File.join(t.work_directory, t.source_directory)
41
+ File.join(t.work_directory, t.source_directory)
40
42
 
41
- puts "Provisioning #{t.configuration_name}".cyan
43
+ Kernel.puts("Provisioning #{t.configuration_name}".cyan)
42
44
  RubyTerraform.clean(
43
- directory: configuration_directory)
44
-
45
- mkdir_p File.dirname(configuration_directory)
46
- cp_r t.source_directory, configuration_directory
45
+ directory: configuration_directory
46
+ )
47
47
 
48
+ FileUtils.mkdir_p(File.dirname(configuration_directory))
49
+ FileUtils.cp_r(t.source_directory, configuration_directory)
48
50
 
49
51
  Dir.chdir(configuration_directory) do
50
52
  RubyTerraform.init(
51
- backend_config: t.backend_config,
52
- no_color: t.no_color)
53
+ backend_config: t.backend_config,
54
+ no_color: t.no_color
55
+ )
53
56
  RubyTerraform.apply(
54
- auto_approve: true,
55
- no_color: t.no_color,
56
- no_backup: t.no_backup,
57
- backup: t.backup_file,
58
- state: t.state_file,
59
- vars: t.vars,
60
- var_file: t.var_file)
57
+ auto_approve: true,
58
+ no_color: t.no_color,
59
+ no_backup: t.no_backup,
60
+ backup: t.backup_file,
61
+ state: t.state_file,
62
+ vars: t.vars,
63
+ var_file: t.var_file
64
+ )
61
65
  end
62
66
  end
63
67
  end