terraspace 0.3.6 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (43) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +17 -0
  3. data/lib/templates/base/project/README.md +1 -1
  4. data/lib/terraspace/all/runner.rb +1 -0
  5. data/lib/terraspace/all/summary.rb +8 -1
  6. data/lib/terraspace/app.rb +9 -5
  7. data/lib/terraspace/builder.rb +10 -6
  8. data/lib/terraspace/cli.rb +10 -16
  9. data/lib/terraspace/cli/all.rb +6 -0
  10. data/lib/terraspace/cli/bundle.rb +2 -1
  11. data/lib/terraspace/cli/clean.rb +18 -6
  12. data/lib/terraspace/cli/clean/all.rb +18 -0
  13. data/lib/terraspace/cli/clean/base.rb +15 -0
  14. data/lib/terraspace/cli/clean/cache.rb +25 -0
  15. data/lib/terraspace/cli/{logs/tasks.rb → clean/logs.rb} +8 -9
  16. data/lib/terraspace/cli/help/clean/all.md +10 -0
  17. data/lib/terraspace/cli/help/clean/cache.md +12 -0
  18. data/lib/terraspace/cli/help/clean/logs.md +17 -0
  19. data/lib/terraspace/cli/help/{log.md → logs.md} +14 -14
  20. data/lib/terraspace/cli/init.rb +3 -7
  21. data/lib/terraspace/cli/logs.rb +105 -10
  22. data/lib/terraspace/cli/{log → logs}/concern.rb +1 -1
  23. data/lib/terraspace/cli/new/helper.rb +9 -2
  24. data/lib/terraspace/dependency/helper/output.rb +1 -1
  25. data/lib/terraspace/hooks/builder.rb +52 -0
  26. data/lib/terraspace/hooks/concern.rb +9 -0
  27. data/lib/terraspace/{terraform/hooks → hooks}/dsl.rb +3 -2
  28. data/lib/terraspace/hooks/runner.rb +23 -0
  29. data/lib/terraspace/mod.rb +11 -2
  30. data/lib/terraspace/plugin/summary/interface.rb +3 -1
  31. data/lib/terraspace/shell.rb +15 -10
  32. data/lib/terraspace/terraform/args/custom.rb +1 -1
  33. data/lib/terraspace/terraform/remote_state/output_proxy.rb +3 -3
  34. data/lib/terraspace/terraform/remote_state/{null_object.rb → unresolved.rb} +1 -1
  35. data/lib/terraspace/terraform/runner.rb +2 -7
  36. data/lib/terraspace/version.rb +1 -1
  37. data/spec/terraspace/{terraform/hooks → hooks}/builder_spec.rb +4 -5
  38. data/spec/terraspace/terraform/remote_state/output_proxy_spec.rb +3 -3
  39. data/terraspace.gemspec +1 -1
  40. metadata +20 -14
  41. data/lib/terraspace/cli/help/clean.md +0 -5
  42. data/lib/terraspace/cli/log.rb +0 -112
  43. data/lib/terraspace/terraform/hooks/builder.rb +0 -40
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 28bdb53824befd6a98263c63612db913c5496546ef751ed408500b4140ac57c4
4
- data.tar.gz: fe844bdeb2dacfbecfa954a912bfa4ef3e5b4e6265d2eea697d072d8533b2fd5
3
+ metadata.gz: 6b99a86e9b9b829fa0ac1b04a6774b02a43e67642219ea3f22aeb663869bfec6
4
+ data.tar.gz: f2b265d5e13a86949ddb753e991fc8784c77361909162123dfd7d06d0c1c3555
5
5
  SHA512:
6
- metadata.gz: 7e01f4b80088de2c7a3f3f1c8ac81a401b4a999cbb3fbc4af78651ee8e1788246dcb8b889172b94ea5ff7a7932a4874f27e225f66dedac2603a5752a20fb383c
7
- data.tar.gz: c0ca89b09e3d8ca3f36f6d2f513fedbaa29aad071af4c4c6e99c38438b8c2002582adaa49b2f7105becde5e2ac21785a7f5617b9c5fd1fd60ea25fbaadda7a54
6
+ metadata.gz: 7fd4d65aac675d439e5e23089db75f937e3f50c72158099d1088112db67ed97c93afef6c47e0555cdde5a6dfe3e175ede017fc9e509c5419128c53ee114de91d
7
+ data.tar.gz: 4ef8b0354c0e8dd14ccd074bc6ecd2cda96f80c846bfd3dc790dce4cf20f36efbe236020eff587555032ec1dd869ec405cde649a348e232e747ba7900ad79393
@@ -3,6 +3,23 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  This project *loosely tries* to adhere to [Semantic Versioning](http://semver.org/), even before v1.0.
5
5
 
6
+ ## [0.4.0]
7
+ * improve hooks: allow multiple hooks of same type, change path hooks/terraform.rb
8
+ * improve hooks: introduce terraspace-level as well as terraform-level hooks
9
+ * improve hooks: can take Ruby block or shell script
10
+ * improve auto init: reinit when module source changed, improve auto init: generalize retry check
11
+ * fix already_init? detection for case when stack does use module
12
+ * terraspace bundler options: can set any option now
13
+ * add `terraspace all init` command
14
+ * improve terraspace clean, prompt user and add `-y` option
15
+ * bundle: check if in terraspace project
16
+ * Rename NullObject to Unresolved
17
+ * improve error message when stacks not found. give `terraspace list` hint
18
+ * terraspace list. change default to `--type stack`
19
+ * change summary option to --details
20
+ * include terraspace version in generated Gemfile
21
+ * logs command to handle viewing, and clean logs to clean
22
+
6
23
  ## [0.3.6]
7
24
  * #44 improve logs management commands: `terraspace logs remove` and `terraspace logs truncate`
8
25
 
@@ -14,4 +14,4 @@ To deploy individual stacks:
14
14
 
15
15
  ## Terrafile
16
16
 
17
- To add more modules for use, you can add them to the [Terrafile](https://terraspace.cloud/docs/terrafile/).
17
+ To use more modules, add them to the [Terrafile](https://terraspace.cloud/docs/terrafile/).
@@ -105,6 +105,7 @@ module Terraspace::All
105
105
  set_log_path!(mod_name)
106
106
  name = command_map(@command)
107
107
  o = @options.merge(mod: mod_name, yes: true, build: false, input: false)
108
+ o.merge!(quiet: false) if @command == "init" # noisy so can filter and summarize output
108
109
  case @command
109
110
  when "up"
110
111
  Terraspace::CLI::Up.new(o).run
@@ -1,6 +1,6 @@
1
1
  module Terraspace::All
2
2
  class Summary
3
- include Terraspace::CLI::Log::Concern
3
+ include Terraspace::CLI::Logs::Concern
4
4
  include Terraspace::Util::Logging
5
5
 
6
6
  def initialize(data={})
@@ -35,6 +35,13 @@ module Terraspace::All
35
35
  end
36
36
  end
37
37
 
38
+ def init
39
+ @lines.select! do |line|
40
+ line.include?("successfully initialized") || # success
41
+ line.include?("Error: ") # error
42
+ end
43
+ end
44
+
38
45
  # Example 1:
39
46
  # [2020-09-07T14:45:14 #23340 terraspace plan b1]: No changes. Infrastructure is up-to-date.
40
47
  # Example 2:
@@ -9,6 +9,8 @@ module Terraspace
9
9
  end
10
10
 
11
11
  def defaults
12
+ ts_logger = Logger.new(ENV['TS_LOG_PATH'] || $stderr)
13
+
12
14
  config = ActiveSupport::OrderedOptions.new
13
15
  config.all = ActiveSupport::OrderedOptions.new
14
16
  config.all.concurrency = 5
@@ -21,14 +23,16 @@ module Terraspace
21
23
  config.build.cache_dir = ":CACHE_ROOT/:REGION/:ENV/:BUILD_DIR"
22
24
  config.build.cache_root = nil # defaults to /full/path/to/.terraspace-cache
23
25
  config.build.clean_cache = nil # defaults to /full/path/to/.terraspace-cache
26
+ config.bundle = ActiveSupport::OrderedOptions.new
27
+ config.bundle.logger = ts_logger
24
28
  config.cloud = ActiveSupport::OrderedOptions.new
25
29
  config.cloud.auto_sync = true
26
- config.cloud.working_dir_prefix = nil
27
30
  config.cloud.hostname = nil
28
31
  config.cloud.vars = ActiveSupport::OrderedOptions.new
29
32
  config.cloud.vars.overwrite = true
30
33
  config.cloud.vars.overwrite_sensitive = true
31
34
  config.cloud.vars.show_message = "create"
35
+ config.cloud.working_dir_prefix = nil
32
36
  config.cloud.workspace = ActiveSupport::OrderedOptions.new
33
37
  config.cloud.workspace.attrs = ActiveSupport::OrderedOptions.new
34
38
  config.hooks = Hooks.new
@@ -36,14 +40,14 @@ module Terraspace
36
40
  config.init.mode = "auto" # auto, never, always
37
41
  config.log = ActiveSupport::OrderedOptions.new
38
42
  config.log.root = Terraspace.log_root
39
- config.logger = Logger.new(ENV['TS_LOG_PATH'] || $stderr)
40
- config.logger.level = ENV['TS_LOG_LEVEL'] || :info
43
+ config.logger = ts_logger
41
44
  config.logger.formatter = Logger::Formatter.new
42
- config.test_framework = "rspec"
45
+ config.logger.level = ENV['TS_LOG_LEVEL'] || :info
43
46
  config.terraform = ActiveSupport::OrderedOptions.new
44
47
  config.terraform.plugin_cache = ActiveSupport::OrderedOptions.new
45
- config.terraform.plugin_cache.enabled = true
46
48
  config.terraform.plugin_cache.dir = ENV['TF_PLUGIN_CACHE_DIR'] || "#{Terraspace.tmp_root}/plugin_cache"
49
+ config.terraform.plugin_cache.enabled = true
50
+ config.test_framework = "rspec"
47
51
  config
48
52
  end
49
53
 
@@ -1,7 +1,8 @@
1
1
  module Terraspace
2
2
  class Builder < Terraspace::CLI::Base
3
- include Compiler::DirsConcern
4
3
  include Compiler::CommandsConcern
4
+ include Compiler::DirsConcern
5
+ include Hooks::Concern
5
6
 
6
7
  attr_reader :graph
7
8
 
@@ -14,11 +15,14 @@ module Terraspace
14
15
  placeholder_stack_message
15
16
  logger.info "Building #{build_dir}" unless @options[:quiet] # from terraspace all
16
17
 
17
- build_unresolved
18
- auto_create_backend
19
- batches = build_batches
20
- build_all
21
- logger.info "Built in #{build_dir}" unless @options[:quiet] # from terraspace all
18
+ batches = nil
19
+ run_hooks("terraspace.rb", "build") do
20
+ build_unresolved
21
+ auto_create_backend
22
+ batches = build_batches
23
+ build_all
24
+ logger.info "Built in #{build_dir}" unless @options[:quiet] # from terraspace all
25
+ end
22
26
  batches
23
27
  end
24
28
 
@@ -32,14 +32,14 @@ module Terraspace
32
32
  long_desc Help.text(:all)
33
33
  subcommand "all", All
34
34
 
35
+ desc "clean SUBCOMMAND", "clean subcommands"
36
+ long_desc Help.text(:clean)
37
+ subcommand "clean", Clean
38
+
35
39
  desc "cloud SUBCOMMAND", "cloud subcommands"
36
40
  long_desc Help.text(:cloud)
37
41
  subcommand "cloud", Cloud
38
42
 
39
- desc "logs SUBCOMMAND", "logs management subcommands"
40
- long_desc Help.text(:logs)
41
- subcommand "logs", Logs
42
-
43
43
  desc "new SUBCOMMAND", "new subcommands"
44
44
  long_desc Help.text(:new)
45
45
  subcommand "new", New
@@ -65,12 +65,6 @@ module Terraspace
65
65
  CheckSetup.new(options).run
66
66
  end
67
67
 
68
- desc "clean", "Removes .terraspace-cache dir."
69
- long_desc Help.text(:clean)
70
- def clean
71
- Clean.new(options).run
72
- end
73
-
74
68
  desc "console STACK", "Run console in built terraform project."
75
69
  long_desc Help.text(:console)
76
70
  instance_option.call
@@ -105,19 +99,19 @@ module Terraspace
105
99
 
106
100
  desc "list", "List stacks and modules."
107
101
  long_desc Help.text(:list)
108
- option :type, aliases: %w[t], desc: "Type: stack or module. Default all"
102
+ option :type, default: "stack", aliases: %w[t], desc: "Type: stack, module, or all"
109
103
  def list
110
104
  List.new(options).run
111
105
  end
112
106
 
113
- desc "log [ACTION] [STACK]", "View and tail logs."
114
- long_desc Help.text("log")
107
+ desc "logs [ACTION] [STACK]", "View and tail logs."
108
+ long_desc Help.text("logs")
115
109
  option :timestamps, aliases: %w[t], type: :boolean, desc: "Whether or not to show the leading timestamp. Defaults to timestamps for multiple logs, and no timestamp if a single log is specified. Note: In follow mode, timestamp always shown"
116
110
  option :follow, aliases: %w[f], type: :boolean, desc: "Follow the log in live tail fashion. Must specify a stack if using this option."
117
111
  option :limit, aliases: %w[n], default: 10, type: :numeric, desc: "Number of lines to limit showing. Only applies in no-follow mode."
118
112
  option :all, aliases: %w[a], type: :boolean, desc: "All mode turns off the limit. Defaults to all if a single log is specified. Only applies in no-follow mode."
119
- def log(action=nil, stack=nil)
120
- Log.new(@options.merge(action: action, stack: stack)).run
113
+ def logs(action=nil, stack=nil)
114
+ Logs.new(@options.merge(action: action, stack: stack)).run
121
115
  end
122
116
 
123
117
  desc "plan STACK", "Plan stack."
@@ -159,7 +153,7 @@ module Terraspace
159
153
  long_desc Help.text(:summary)
160
154
  option :mod, desc: "Module to build to generate a backend file for discovery. By default the last module is used. Usually, it wont matter."
161
155
  init_option.call
162
- option :short, aliases: %w[s], type: :boolean, desc: "Only show statefiles"
156
+ option :details, type: :boolean, desc: "Show details of the listed resources"
163
157
  def summary
164
158
  Summary.new(options).run
165
159
  end
@@ -18,6 +18,12 @@ class Terraspace::CLI
18
18
  Terraspace::All::Grapher.new(@options.merge(stacks: stacks)).run
19
19
  end
20
20
 
21
+ desc "init", "Init."
22
+ long_desc Help.text("all/init")
23
+ def init(*stacks)
24
+ Terraspace::All::Runner.new("init", @options.merge(stacks: stacks)).run
25
+ end
26
+
21
27
  desc "refresh", "Refresh all or multiple stacks."
22
28
  long_desc Help.text("all/refresh")
23
29
  def refresh(*stacks)
@@ -1,5 +1,4 @@
1
1
  require "terraspace-bundler"
2
- TerraspaceBundler.config.logger = Terraspace.logger
3
2
 
4
3
  class Terraspace::CLI
5
4
  class Bundle
@@ -8,6 +7,8 @@ class Terraspace::CLI
8
7
  end
9
8
 
10
9
  def run
10
+ Terraspace.check_project!
11
+ TerraspaceBundler.config.deep_merge!(Terraspace.config.bundle)
11
12
  TerraspaceBundler::CLI.start(args)
12
13
  end
13
14
 
@@ -1,12 +1,24 @@
1
1
  class Terraspace::CLI
2
- class Clean
3
- def initialize(options={})
4
- @options = options
2
+ class Clean < Terraspace::Command
3
+ class_option :yes, aliases: :y, type: :boolean, desc: "bypass are you sure prompt"
4
+
5
+ desc "all", "Runs all clean operations."
6
+ long_desc Help.text("clean/all")
7
+ def all
8
+ All.new(options).run
9
+ end
10
+
11
+ desc "cache", "Removes cache dirs."
12
+ long_desc Help.text("clean/cache")
13
+ def cache
14
+ Cache.new(options).run
5
15
  end
6
16
 
7
- def run
8
- FileUtils.rm_rf(Terraspace.cache_root)
9
- puts "Removed #{Terraspace::Util.pretty_path(Terraspace.cache_root)}"
17
+ desc "logs", "Removes or truncate logs."
18
+ long_desc Help.text("clean/logs")
19
+ option :truncate, aliases: :t, type: :boolean, desc: "Truncate instead of remove logs"
20
+ def logs
21
+ Logs.new(options).run
10
22
  end
11
23
  end
12
24
  end
@@ -0,0 +1,18 @@
1
+ class Terraspace::CLI::Clean
2
+ class All < Base
3
+ def run
4
+ are_you_sure?
5
+ o = @options.merge(yes: true) # override to avoid double prompt
6
+ Cache.new(o).run
7
+ Logs.new(o).run
8
+ end
9
+
10
+ def are_you_sure?
11
+ message = <<~EOL.chomp
12
+ Will remove Terraspace cache and logs.
13
+ Are you sure?
14
+ EOL
15
+ sure?(message) # from Util::Sure
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,15 @@
1
+ class Terraspace::CLI::Clean
2
+ class Base
3
+ include Terraspace::Util::Logging
4
+ include Terraspace::Util::Sure
5
+
6
+ def initialize(options={})
7
+ @options = options
8
+ Terraspace.check_project!
9
+ end
10
+
11
+ def pretty(path)
12
+ Terraspace::Util.pretty_path(path)
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,25 @@
1
+ class Terraspace::CLI::Clean
2
+ class Cache < Base
3
+ def run
4
+ Terraspace.check_project!
5
+ paths = [Terraspace.cache_root, Terraspace.tmp_root]
6
+ are_you_sure?(paths)
7
+ paths.each do |path|
8
+ FileUtils.rm_rf(path)
9
+ puts "Removed #{pretty(path)}"
10
+ end
11
+ end
12
+
13
+ def are_you_sure?(paths)
14
+ pretty_paths = paths.map { |p| " #{pretty(p)}" }.join("\n")
15
+ message = <<~EOL.chomp
16
+ Will remove these folders and all their files:
17
+
18
+ #{pretty_paths}
19
+
20
+ Are you sure?
21
+ EOL
22
+ sure?(message) # from Util::Sure
23
+ end
24
+ end
25
+ end
@@ -1,21 +1,19 @@
1
- class Terraspace::CLI::Logs
2
- class Tasks
3
- include Terraspace::Util::Sure
4
-
5
- def initialize(options={})
6
- @options = options
7
- Terraspace.check_project!
1
+ class Terraspace::CLI::Clean
2
+ class Logs < Base
3
+ def run
4
+ action = @options[:truncate] ? "truncate" : "remove"
5
+ are_you_sure?(action)
6
+ @options[:truncate] ? truncate : remove
7
+ logger.info "Logs #{action}d" # IE: Logs truncated or Logs removed
8
8
  end
9
9
 
10
10
  def truncate
11
- are_you_sure?("truncate")
12
11
  log_files.each do |path|
13
12
  File.open(path, "w").close # truncates files
14
13
  end
15
14
  end
16
15
 
17
16
  def remove
18
- are_you_sure?("remove")
19
17
  puts "Removing all files in #{pretty_log_root}/" unless @options[:mute]
20
18
  FileUtils.rm_rf(log_root)
21
19
  FileUtils.mkdir_p(log_root)
@@ -42,3 +40,4 @@ class Terraspace::CLI::Logs
42
40
  end
43
41
  end
44
42
  end
43
+
@@ -0,0 +1,10 @@
1
+ ## Examples
2
+
3
+ $ terraspace clean all
4
+ Will remove Terraspace cache and logs.
5
+ Are you sure? (y/N) y
6
+ Removed .terraspace-cache
7
+ Removed /tmp/terraspace
8
+ Removing all files in log/
9
+ Logs removed
10
+ $
@@ -0,0 +1,12 @@
1
+ ## Example
2
+
3
+ $ terraspace clean cache
4
+ Will remove these folders and all their files:
5
+
6
+ .terraspace-cache
7
+ /tmp/terraspace
8
+
9
+ Are you sure? (y/N)
10
+ Removed .terraspace-cache
11
+ Removed /tmp/terraspace
12
+ $
@@ -0,0 +1,17 @@
1
+ ## Examples
2
+
3
+ Remove logs completely:
4
+
5
+ $ terraspace clean logs
6
+ Will remove all the log files in log/ folder
7
+ Are you sure? (y/N) y
8
+ Removing all files in log/
9
+ Logs removed
10
+
11
+ Truncate logs. IE: Keeps the files but removes contents and zero bytes the files.
12
+
13
+ $ terraspace clean logs --truncate
14
+ Will truncate all the log files in log/ folder
15
+ Are you sure? (y/N) y
16
+ Logs truncated
17
+ $
@@ -4,7 +4,7 @@ The log commands will filter out the logs for the last ran terraspace command. I
4
4
 
5
5
  Follow all the logs as you're running `terraspace all up`:
6
6
 
7
- terraspace log -f
7
+ terraspace logs -f
8
8
 
9
9
  Note, Terraspace automatically checks every second for new logs and adds them to be followed.
10
10
 
@@ -12,18 +12,18 @@ Note, Terraspace automatically checks every second for new logs and adds them to
12
12
 
13
13
  View last 10 lines of each log file.
14
14
 
15
- terraspace log up network # view up log on specific stack
16
- terraspace log up # view all up logs
17
- terraspace log down # view all down logs
18
- terraspace log # view all logs: up, down, etc
15
+ terraspace logs up network # view up log on specific stack
16
+ terraspace logs up # view all up logs
17
+ terraspace logs down # view all down logs
18
+ terraspace logs # view all logs: up, down, etc
19
19
 
20
20
  By default, the log command shows the last 10 lines of the logs for each log file. You can use the `-n` option to adjust this.
21
21
 
22
- terraspace log -n 2 # view last 2 lines of all logs: up, down, etc
22
+ terraspace logs -n 2 # view last 2 lines of all logs: up, down, etc
23
23
 
24
24
  To show all logs, use the `-a` option.
25
25
 
26
- terraspace log up -a
26
+ terraspace logs up -a
27
27
 
28
28
  Note, if both an action and stack is specified, then it defaults to showing all logs. If you want not to show all logs, use `--no-all`.
29
29
 
@@ -31,18 +31,18 @@ Note, if both an action and stack is specified, then it defaults to showing all
31
31
 
32
32
  To tail logs, use the `-f` option.
33
33
 
34
- terraspace log up network -f # view up log on specific stack
35
- terraspace log up -f # view all up logs
36
- terraspace log down -f # view all down logs
37
- terraspace log -f # view all logs: up, down, etc
34
+ terraspace logs up network -f # view up log on specific stack
35
+ terraspace logs up -f # view all up logs
36
+ terraspace logs down -f # view all down logs
37
+ terraspace logs -f # view all logs: up, down, etc
38
38
 
39
39
  ## Timestamps
40
40
 
41
41
  The timestamps are shown by default when you are looking for multiple files. When you specify both the action and stack for a single log file, then timestamps are not shown.
42
42
 
43
- terraspace log up # timestamps will be shown in this case
44
- terraspace log up network # timestamps not be shown in this case
43
+ terraspace logs up # timestamps will be shown in this case
44
+ terraspace logs up network # timestamps not be shown in this case
45
45
 
46
46
  To show timestamps:
47
47
 
48
- terraspace up up network --timestamps
48
+ terraspace logs up network --timestamps