terraspace 0.4.3 → 0.5.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (72) hide show
  1. checksums.yaml +4 -4
  2. data/.cody/aws/bin/build.sh +2 -2
  3. data/.cody/azurerm/bin/build.sh +2 -2
  4. data/.cody/google/bin/build.sh +2 -2
  5. data/.github/ISSUE_TEMPLATE.md +1 -1
  6. data/CHANGELOG.md +32 -0
  7. data/README.md +4 -3
  8. data/lib/templates/base/arg/terraform.rb.tt +3 -0
  9. data/lib/templates/base/helper/%name%_helper.rb.tt +2 -0
  10. data/lib/templates/base/hook/%kind%.rb.tt +7 -0
  11. data/lib/terraspace.rb +7 -4
  12. data/lib/terraspace/all/base.rb +0 -1
  13. data/lib/terraspace/app.rb +1 -0
  14. data/lib/terraspace/autoloader.rb +16 -1
  15. data/lib/terraspace/bundle.rb +6 -0
  16. data/lib/terraspace/cli/bundle.rb +0 -1
  17. data/lib/terraspace/cli/check_setup.rb +15 -2
  18. data/lib/terraspace/cli/clean/base.rb +0 -1
  19. data/lib/terraspace/cli/clean/cache.rb +0 -1
  20. data/lib/terraspace/cli/cloud.rb +2 -0
  21. data/lib/terraspace/cli/help/new/arg.md +19 -0
  22. data/lib/terraspace/cli/help/new/helper.md +39 -0
  23. data/lib/terraspace/cli/help/new/hook.md +25 -0
  24. data/lib/terraspace/cli/help/new/test.md +34 -0
  25. data/lib/terraspace/cli/new.rb +22 -16
  26. data/lib/terraspace/cli/new/arg.rb +62 -0
  27. data/lib/terraspace/cli/new/helper.rb +44 -12
  28. data/lib/terraspace/cli/new/helpers.rb +22 -0
  29. data/lib/terraspace/cli/new/helpers/plugin_gem.rb +25 -0
  30. data/lib/terraspace/cli/new/hook.rb +70 -0
  31. data/lib/terraspace/cli/new/module.rb +0 -11
  32. data/lib/terraspace/cli/new/plugin.rb +4 -4
  33. data/lib/terraspace/cli/new/plugin/helper.rb +1 -1
  34. data/lib/terraspace/cli/new/project.rb +16 -7
  35. data/lib/terraspace/cli/new/sequence.rb +3 -10
  36. data/lib/terraspace/cli/new/source/core.rb +1 -1
  37. data/lib/terraspace/cli/new/stack.rb +1 -12
  38. data/lib/terraspace/cli/new/test.rb +50 -0
  39. data/lib/terraspace/cli/summary.rb +0 -1
  40. data/lib/terraspace/command.rb +50 -0
  41. data/lib/terraspace/compiler/builder.rb +2 -0
  42. data/lib/terraspace/compiler/dsl/mod.rb +2 -0
  43. data/lib/terraspace/compiler/dsl/syntax/mod.rb +2 -0
  44. data/lib/terraspace/compiler/dsl/syntax/mod/backend.rb +1 -1
  45. data/lib/terraspace/compiler/erb/context.rb +2 -0
  46. data/lib/terraspace/compiler/helper_extender.rb +27 -0
  47. data/lib/terraspace/core.rb +0 -6
  48. data/lib/terraspace/ext/core/module.rb +16 -0
  49. data/lib/terraspace/hooks/builder.rb +6 -7
  50. data/lib/terraspace/hooks/concern.rb +2 -2
  51. data/lib/terraspace/logger.rb +6 -0
  52. data/lib/terraspace/mod.rb +0 -1
  53. data/lib/terraspace/plugin.rb +8 -4
  54. data/lib/terraspace/plugin/config/interface.rb +2 -2
  55. data/lib/terraspace/plugin/helper/interface.rb +31 -0
  56. data/lib/terraspace/shell.rb +5 -33
  57. data/lib/terraspace/shell/error.rb +46 -0
  58. data/lib/terraspace/terraform/args/custom.rb +2 -3
  59. data/lib/terraspace/terraform/runner.rb +5 -13
  60. data/lib/terraspace/terraform/runner/retryer.rb +69 -0
  61. data/lib/terraspace/version.rb +1 -1
  62. data/spec/terraspace/terraform/args/custom_spec.rb +6 -4
  63. data/terraspace.gemspec +4 -4
  64. metadata +29 -21
  65. data/lib/terraspace/cli/help/new/bootstrap_test.md +0 -8
  66. data/lib/terraspace/cli/help/new/module_test.md +0 -12
  67. data/lib/terraspace/cli/help/new/project_test.md +0 -8
  68. data/lib/terraspace/cli/new/helper/plugin_gem.rb +0 -12
  69. data/lib/terraspace/cli/new/test/base.rb +0 -17
  70. data/lib/terraspace/cli/new/test/bootstrap.rb +0 -18
  71. data/lib/terraspace/cli/new/test/module.rb +0 -15
  72. data/lib/terraspace/cli/new/test/project.rb +0 -15
@@ -8,7 +8,6 @@ module Terraspace::Terraform
8
8
  def initialize(name, options={})
9
9
  @name = name
10
10
  super(options)
11
- @retries = 1
12
11
  end
13
12
 
14
13
  def run
@@ -25,20 +24,13 @@ module Terraspace::Terraform
25
24
  run_hooks("terraform.rb", name) do
26
25
  Terraspace::Shell.new(@mod, command, @options.merge(env: custom.env_vars)).run
27
26
  end
28
- rescue Terraspace::InitRequiredError => e
29
- logger.info "Terraform reinitialization required detected. Will run `terraform init` and try again."
30
- logger.debug "Retry attempt: #{@retries}"
31
- logger.debug "#{e.class}"
32
- Runner.new("init", @options).run
33
- if @retries <= 3
34
- backoff = 2 ** @retries # 2, 4, 8
35
- logger.debug "Waiting #{backoff}s before retrying"
36
- sleep(backoff)
37
- @retries += 1
27
+ rescue Terraspace::SharedCacheError, Terraspace::InitRequiredError
28
+ @retryer ||= Retryer.new(@mod, @options, name, $!)
29
+ if @retryer.retry?
30
+ @retryer.run
38
31
  retry
39
32
  else
40
- logger.info "ERROR: #{e.message}"
41
- exit 1
33
+ exit(1)
42
34
  end
43
35
  end
44
36
 
@@ -0,0 +1,69 @@
1
+ class Terraspace::Terraform::Runner
2
+ class Retryer
3
+ include Terraspace::Util::Logging
4
+ include Terraspace::Util::Pretty
5
+
6
+ def initialize(mod, options, command_name, exception)
7
+ @mod, @options, @command_name, @exception = mod, options, command_name, exception
8
+ @retries = 1
9
+ end
10
+
11
+ def retry?
12
+ if @retries <= 3 && !@stop_retrying
13
+ true # will retry
14
+ else
15
+ logger.info "ERROR: #{@exception.message}"
16
+ false # will not retry
17
+ end
18
+ end
19
+
20
+ def run
21
+ backoff = 2 ** @retries # 2, 4, 8
22
+ logger.debug "Waiting #{backoff}s before retrying"
23
+ sleep(backoff)
24
+ @retries += 1
25
+
26
+ case @exception
27
+ when Terraspace::SharedCacheError
28
+ shared_cache_error
29
+ when Terraspace::InitRequiredError
30
+ init_required_error
31
+ end
32
+ end
33
+
34
+ def shared_cache_error
35
+ logger.info "Terraform Shared Cache error detected. Will purge caches and run `terraform init` to try again."
36
+ logger.debug "Retry attempt: #{@retries}"
37
+ logger.debug "#{@exception.class}"
38
+ logger.debug "#{@exception.message}"
39
+ if Terraspace.config.terraform.plugin_cache.purge_on_error # Purging the cache "fixes" this terraform bug
40
+ purge_caches
41
+ reinit
42
+ else
43
+ @stop_retrying = true
44
+ end
45
+ end
46
+
47
+ def init_required_error
48
+ logger.info "Terraform reinitialization required detected. Will run `terraform init` and try again."
49
+ logger.debug "Retry attempt: #{@retries}"
50
+ logger.debug "#{@exception.class}"
51
+ reinit
52
+ end
53
+
54
+ def reinit
55
+ Terraspace::Terraform::Runner.new("init", @options).run unless @command_name == "init"
56
+ end
57
+
58
+ def purge_caches
59
+ dir = "#{@mod.cache_dir}/.terraform"
60
+ logger.info "Purging #{pretty_path(dir)}"
61
+ FileUtils.rm_rf(dir)
62
+
63
+ dir = "#{Terraspace.config.terraform.plugin_cache.dir}"
64
+ logger.info "Purging #{pretty_path(dir)}"
65
+ FileUtils.rm_rf(dir)
66
+ FileUtils.mkdir_p(dir) # need /tmp/terraspace/plugin_cache dir to exist
67
+ end
68
+ end
69
+ end
@@ -1,3 +1,3 @@
1
1
  module Terraspace
2
- VERSION = "0.4.3"
2
+ VERSION = "0.5.3"
3
3
  end
@@ -10,17 +10,18 @@ describe Terraspace::Terraform::Args::Custom do
10
10
  let(:file) { fixture("terraform/args/single.rb") }
11
11
  let(:name) { "apply" }
12
12
  it "build creates the @commands structure" do
13
+ custom.evaluate_file(file)
13
14
  commands = custom.build
14
15
  expect(commands.keys).to include("apply")
15
16
  end
16
17
 
17
18
  it "args" do
18
- custom.build
19
+ custom.evaluate_file(file)
19
20
  expect(custom.args).to eq(["-lock-timeout=20m"])
20
21
  end
21
22
 
22
23
  it "var_files" do
23
- custom.build
24
+ custom.evaluate_file(file)
24
25
  allow(custom).to receive(:var_file_exist?).and_return(true)
25
26
  expect(custom.var_files).to eq(["-var-file=a.tfvars", "-var-file=b.tfvars"])
26
27
  end
@@ -30,17 +31,18 @@ describe Terraspace::Terraform::Args::Custom do
30
31
  let(:file) { fixture("terraform/args/multiple.rb") }
31
32
  let(:name) { "apply" }
32
33
  it "build creates the @commands structure" do
34
+ custom.evaluate_file(file)
33
35
  commands = custom.build
34
36
  expect(commands.keys).to include("apply")
35
37
  end
36
38
 
37
39
  it "args" do
38
- custom.build
40
+ custom.evaluate_file(file)
39
41
  expect(custom.args).to eq(["-lock-timeout=20m"])
40
42
  end
41
43
 
42
44
  it "var_files" do
43
- custom.build
45
+ custom.evaluate_file(file)
44
46
  allow(custom).to receive(:var_file_exist?).and_return(true)
45
47
  expect(custom.var_files).to eq([])
46
48
  end
@@ -34,10 +34,10 @@ Gem::Specification.new do |spec|
34
34
  spec.add_dependency "zeitwerk"
35
35
 
36
36
  # core baseline plugins
37
- spec.add_dependency "terraspace_plugin_aws", "~> 0.2.0"
38
- spec.add_dependency "terraspace_plugin_azurerm", "~> 0.2.0"
39
- spec.add_dependency "terraspace_plugin_google", "~> 0.2.0"
40
- spec.add_dependency "rspec-terraspace"
37
+ spec.add_dependency "terraspace_plugin_aws", "~> 0.3.0"
38
+ spec.add_dependency "terraspace_plugin_azurerm", "~> 0.3.0"
39
+ spec.add_dependency "terraspace_plugin_google", "~> 0.3.0"
40
+ spec.add_dependency "rspec-terraspace", "~> 0.2.0"
41
41
 
42
42
  spec.add_development_dependency "bundler"
43
43
  spec.add_development_dependency "byebug"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: terraspace
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.3
4
+ version: 0.5.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tung Nguyen
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-10-23 00:00:00.000000000 Z
11
+ date: 2020-11-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -212,56 +212,56 @@ dependencies:
212
212
  requirements:
213
213
  - - "~>"
214
214
  - !ruby/object:Gem::Version
215
- version: 0.2.0
215
+ version: 0.3.0
216
216
  type: :runtime
217
217
  prerelease: false
218
218
  version_requirements: !ruby/object:Gem::Requirement
219
219
  requirements:
220
220
  - - "~>"
221
221
  - !ruby/object:Gem::Version
222
- version: 0.2.0
222
+ version: 0.3.0
223
223
  - !ruby/object:Gem::Dependency
224
224
  name: terraspace_plugin_azurerm
225
225
  requirement: !ruby/object:Gem::Requirement
226
226
  requirements:
227
227
  - - "~>"
228
228
  - !ruby/object:Gem::Version
229
- version: 0.2.0
229
+ version: 0.3.0
230
230
  type: :runtime
231
231
  prerelease: false
232
232
  version_requirements: !ruby/object:Gem::Requirement
233
233
  requirements:
234
234
  - - "~>"
235
235
  - !ruby/object:Gem::Version
236
- version: 0.2.0
236
+ version: 0.3.0
237
237
  - !ruby/object:Gem::Dependency
238
238
  name: terraspace_plugin_google
239
239
  requirement: !ruby/object:Gem::Requirement
240
240
  requirements:
241
241
  - - "~>"
242
242
  - !ruby/object:Gem::Version
243
- version: 0.2.0
243
+ version: 0.3.0
244
244
  type: :runtime
245
245
  prerelease: false
246
246
  version_requirements: !ruby/object:Gem::Requirement
247
247
  requirements:
248
248
  - - "~>"
249
249
  - !ruby/object:Gem::Version
250
- version: 0.2.0
250
+ version: 0.3.0
251
251
  - !ruby/object:Gem::Dependency
252
252
  name: rspec-terraspace
253
253
  requirement: !ruby/object:Gem::Requirement
254
254
  requirements:
255
- - - ">="
255
+ - - "~>"
256
256
  - !ruby/object:Gem::Version
257
- version: '0'
257
+ version: 0.2.0
258
258
  type: :runtime
259
259
  prerelease: false
260
260
  version_requirements: !ruby/object:Gem::Requirement
261
261
  requirements:
262
- - - ">="
262
+ - - "~>"
263
263
  - !ruby/object:Gem::Version
264
- version: '0'
264
+ version: 0.2.0
265
265
  - !ruby/object:Gem::Dependency
266
266
  name: bundler
267
267
  requirement: !ruby/object:Gem::Requirement
@@ -385,7 +385,10 @@ files:
385
385
  - README.md
386
386
  - Rakefile
387
387
  - exe/terraspace
388
+ - lib/templates/base/arg/terraform.rb.tt
388
389
  - lib/templates/base/git_hook/hook.sh
390
+ - lib/templates/base/helper/%name%_helper.rb.tt
391
+ - lib/templates/base/hook/%kind%.rb.tt
389
392
  - lib/templates/base/project/.gitignore
390
393
  - lib/templates/base/project/Gemfile.tt
391
394
  - lib/templates/base/project/README.md
@@ -510,16 +513,17 @@ files:
510
513
  - lib/terraspace/cli/help/logs.md
511
514
  - lib/terraspace/cli/help/logs/remove.md
512
515
  - lib/terraspace/cli/help/logs/truncate.md
513
- - lib/terraspace/cli/help/new/bootstrap_test.md
516
+ - lib/terraspace/cli/help/new/arg.md
514
517
  - lib/terraspace/cli/help/new/example.md
515
518
  - lib/terraspace/cli/help/new/git_hook.md
519
+ - lib/terraspace/cli/help/new/helper.md
520
+ - lib/terraspace/cli/help/new/hook.md
516
521
  - lib/terraspace/cli/help/new/module.md
517
- - lib/terraspace/cli/help/new/module_test.md
518
522
  - lib/terraspace/cli/help/new/plugin.md
519
523
  - lib/terraspace/cli/help/new/project.md
520
- - lib/terraspace/cli/help/new/project_test.md
521
524
  - lib/terraspace/cli/help/new/shim.md
522
525
  - lib/terraspace/cli/help/new/stack.md
526
+ - lib/terraspace/cli/help/new/test.md
523
527
  - lib/terraspace/cli/help/output.md
524
528
  - lib/terraspace/cli/help/plan.md
525
529
  - lib/terraspace/cli/help/providers.md
@@ -536,9 +540,12 @@ files:
536
540
  - lib/terraspace/cli/logs.rb
537
541
  - lib/terraspace/cli/logs/concern.rb
538
542
  - lib/terraspace/cli/new.rb
543
+ - lib/terraspace/cli/new/arg.rb
539
544
  - lib/terraspace/cli/new/git_hook.rb
540
545
  - lib/terraspace/cli/new/helper.rb
541
- - lib/terraspace/cli/new/helper/plugin_gem.rb
546
+ - lib/terraspace/cli/new/helpers.rb
547
+ - lib/terraspace/cli/new/helpers/plugin_gem.rb
548
+ - lib/terraspace/cli/new/hook.rb
542
549
  - lib/terraspace/cli/new/module.rb
543
550
  - lib/terraspace/cli/new/plugin.rb
544
551
  - lib/terraspace/cli/new/plugin/helper.rb
@@ -549,10 +556,7 @@ files:
549
556
  - lib/terraspace/cli/new/source/plugin.rb
550
557
  - lib/terraspace/cli/new/source/test.rb
551
558
  - lib/terraspace/cli/new/stack.rb
552
- - lib/terraspace/cli/new/test/base.rb
553
- - lib/terraspace/cli/new/test/bootstrap.rb
554
- - lib/terraspace/cli/new/test/module.rb
555
- - lib/terraspace/cli/new/test/project.rb
559
+ - lib/terraspace/cli/new/test.rb
556
560
  - lib/terraspace/cli/seed.rb
557
561
  - lib/terraspace/cli/summary.rb
558
562
  - lib/terraspace/cli/test.rb
@@ -590,6 +594,7 @@ files:
590
594
  - lib/terraspace/compiler/erb/helpers.rb
591
595
  - lib/terraspace/compiler/erb/render.rb
592
596
  - lib/terraspace/compiler/expander.rb
597
+ - lib/terraspace/compiler/helper_extender.rb
593
598
  - lib/terraspace/compiler/strategy/abstract_base.rb
594
599
  - lib/terraspace/compiler/strategy/mod.rb
595
600
  - lib/terraspace/compiler/strategy/mod/base.rb
@@ -630,6 +635,7 @@ files:
630
635
  - lib/terraspace/plugin/expander/generic.rb
631
636
  - lib/terraspace/plugin/expander/interface.rb
632
637
  - lib/terraspace/plugin/finder.rb
638
+ - lib/terraspace/plugin/helper/interface.rb
633
639
  - lib/terraspace/plugin/infer_provider.rb
634
640
  - lib/terraspace/plugin/layer/interface.rb
635
641
  - lib/terraspace/plugin/meta.rb
@@ -639,6 +645,7 @@ files:
639
645
  - lib/terraspace/seeder/content.rb
640
646
  - lib/terraspace/seeder/where.rb
641
647
  - lib/terraspace/shell.rb
648
+ - lib/terraspace/shell/error.rb
642
649
  - lib/terraspace/terraform/api.rb
643
650
  - lib/terraspace/terraform/api/base.rb
644
651
  - lib/terraspace/terraform/api/client.rb
@@ -670,6 +677,7 @@ files:
670
677
  - lib/terraspace/terraform/remote_state/output_proxy.rb
671
678
  - lib/terraspace/terraform/remote_state/unresolved.rb
672
679
  - lib/terraspace/terraform/runner.rb
680
+ - lib/terraspace/terraform/runner/retryer.rb
673
681
  - lib/terraspace/tester.rb
674
682
  - lib/terraspace/tester/finder.rb
675
683
  - lib/terraspace/tester/meta.rb
@@ -826,7 +834,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
826
834
  - !ruby/object:Gem::Version
827
835
  version: '0'
828
836
  requirements: []
829
- rubygems_version: 3.1.2
837
+ rubygems_version: 3.1.4
830
838
  signing_key:
831
839
  specification_version: 4
832
840
  summary: 'Terraspace: The Terraspace Framework'
@@ -1,8 +0,0 @@
1
- ## Example
2
-
3
- $ terraspace new bootstrap_test
4
- => Creating test bootstrap structure
5
- exist
6
- create .rspec
7
- create spec/spec_helper.rb
8
- $
@@ -1,12 +0,0 @@
1
- ## Example
2
-
3
- $ terraspace new module_test example
4
- => Creating module test: example
5
- exist app/modules/example
6
- create app/modules/example/test/.rspec
7
- create app/modules/example/test/Gemfile
8
- create app/modules/example/test/spec/fixtures/stack/main.tf
9
- create app/modules/example/test/spec/fixtures/stack/outputs.tf
10
- create app/modules/example/test/spec/main_spec.rb
11
- create app/modules/example/test/spec/spec_helper.rb
12
- $
@@ -1,8 +0,0 @@
1
- ## Example
2
-
3
- $ terraspace new project_test demo
4
- => Creating project test: demo
5
- exist
6
- create spec/fixtures/tfvars/demo.tfvars
7
- create spec/stacks/demo/main_spec.rb
8
- $
@@ -1,12 +0,0 @@
1
- module Terraspace::CLI::New::Helper
2
- module PluginGem
3
- private
4
- def plugin_gem_name
5
- if @options[:plugin_gem]
6
- @options[:plugin_gem]
7
- else
8
- "terraspace_plugin_#{@options[:plugin]}"
9
- end
10
- end
11
- end
12
- end
@@ -1,17 +0,0 @@
1
- module Terraspace::CLI::New::Test
2
- class Base < Thor::Group
3
- include Thor::Actions
4
- include Terraspace::CLI::New::Helper
5
-
6
- # Reuse options form Sequence. Tried separting them out and it's not worth it.
7
- # It introduced too much duplication.
8
- Terraspace::CLI::New::Sequence.base_options.each { |args| class_option(*args) }
9
- Terraspace::CLI::New::Sequence.component_options.each { |args| class_option(*args) }
10
-
11
- private
12
- def test_template_source(template, type)
13
- source = Terraspace::CLI::New::Source::Test.new(self, @options)
14
- source.set_source_paths(template, type)
15
- end
16
- end
17
- end
@@ -1,18 +0,0 @@
1
- module Terraspace::CLI::New::Test
2
- class Bootstrap < Base
3
- def self.options
4
- [
5
- [:dir, default: ".", desc: "directory to write to"],
6
- ]
7
- end
8
-
9
- options.each { |args| class_option(*args) }
10
-
11
- def create
12
- return if @options[:test] == false
13
- puts "=> Creating test bootstrap structure"
14
- test_template_source(@options[:lang], "bootstrap")
15
- directory ".", options[:dir]
16
- end
17
- end
18
- end
@@ -1,15 +0,0 @@
1
- module Terraspace::CLI::New::Test
2
- class Module < Base
3
- argument :name
4
-
5
- def create
6
- return if @options[:test] == false
7
- test_template_source(@options[:lang], "module")
8
-
9
- puts "=> Creating module test: #{name}"
10
- dest = "app/modules/#{name}"
11
- dest = "#{@options[:project_name]}/#{dest}" if @options[:project_name]
12
- directory ".", dest
13
- end
14
- end
15
- end