terraspace 0.3.4 → 0.4.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +33 -0
- data/README.md +56 -29
- data/lib/templates/base/project/README.md +1 -1
- data/lib/terraspace/all/preview.rb +1 -1
- data/lib/terraspace/all/runner.rb +1 -0
- data/lib/terraspace/all/summary.rb +8 -1
- data/lib/terraspace/app.rb +9 -5
- data/lib/terraspace/builder.rb +10 -6
- data/lib/terraspace/cli.rb +11 -16
- data/lib/terraspace/cli/all.rb +6 -0
- data/lib/terraspace/cli/bundle.rb +2 -1
- data/lib/terraspace/cli/clean.rb +18 -6
- data/lib/terraspace/cli/clean/all.rb +18 -0
- data/lib/terraspace/cli/clean/base.rb +15 -0
- data/lib/terraspace/cli/clean/cache.rb +25 -0
- data/lib/terraspace/cli/{logs/tasks.rb → clean/logs.rb} +16 -5
- data/lib/terraspace/cli/help/all/init.md +33 -0
- data/lib/terraspace/cli/help/clean/all.md +10 -0
- data/lib/terraspace/cli/help/clean/cache.md +12 -0
- data/lib/terraspace/cli/help/clean/logs.md +17 -0
- data/lib/terraspace/cli/help/logs.md +48 -0
- data/lib/terraspace/cli/init.rb +3 -7
- data/lib/terraspace/cli/list.rb +2 -1
- data/lib/terraspace/cli/logs.rb +106 -9
- data/lib/terraspace/cli/{log → logs}/concern.rb +2 -1
- data/lib/terraspace/cli/new/helper.rb +9 -2
- data/lib/terraspace/compiler/dependencies/helpers.rb +34 -0
- data/lib/terraspace/compiler/dsl/syntax/helpers/common.rb +0 -26
- data/lib/terraspace/compiler/dsl/syntax/tfvar.rb +1 -0
- data/lib/terraspace/compiler/erb/context.rb +1 -1
- data/lib/terraspace/compiler/erb/helpers.rb +6 -0
- data/lib/terraspace/dependency/helper/base.rb +7 -0
- data/lib/terraspace/dependency/helper/depends_on.rb +12 -0
- data/lib/terraspace/dependency/helper/output.rb +11 -0
- data/lib/terraspace/hooks/builder.rb +52 -0
- data/lib/terraspace/hooks/concern.rb +9 -0
- data/lib/terraspace/{terraform/hooks → hooks}/dsl.rb +3 -2
- data/lib/terraspace/hooks/runner.rb +23 -0
- data/lib/terraspace/mod.rb +11 -2
- data/lib/terraspace/plugin/summary/interface.rb +3 -1
- data/lib/terraspace/shell.rb +15 -10
- data/lib/terraspace/terraform/args/custom.rb +1 -1
- data/lib/terraspace/terraform/args/default.rb +9 -19
- data/lib/terraspace/terraform/remote_state/fetcher.rb +13 -4
- data/lib/terraspace/terraform/remote_state/marker/output.rb +3 -1
- data/lib/terraspace/terraform/remote_state/output_proxy.rb +18 -14
- data/lib/terraspace/terraform/remote_state/unresolved.rb +40 -0
- data/lib/terraspace/terraform/runner.rb +3 -8
- data/lib/terraspace/version.rb +1 -1
- data/spec/fixtures/dependencies/app/stacks/a1/tfvars/dev.tfvars +1 -0
- data/spec/fixtures/fetcher/c1.json +4 -0
- data/spec/terraspace/compiler/erb/render_spec.rb +15 -0
- data/spec/terraspace/dependency/helper/depends_on_spec.rb +27 -0
- data/spec/terraspace/dependency/helper/output_spec.rb +29 -0
- data/spec/terraspace/{terraform/hooks → hooks}/builder_spec.rb +4 -5
- data/spec/terraspace/terraform/remote_state/fetcher_spec.rb +108 -27
- data/spec/terraspace/terraform/remote_state/marker/output_spec.rb +36 -0
- data/spec/terraspace/terraform/remote_state/output_proxy_spec.rb +69 -0
- data/terraspace.gemspec +1 -1
- metadata +38 -13
- data/lib/terraspace/cli/help/clean.md +0 -5
- data/lib/terraspace/cli/help/log.md +0 -48
- data/lib/terraspace/cli/log.rb +0 -112
- data/lib/terraspace/terraform/hooks/builder.rb +0 -40
@@ -0,0 +1,12 @@
|
|
1
|
+
module Terraspace::Dependency::Helper
|
2
|
+
class DependsOn < Base
|
3
|
+
def result
|
4
|
+
if @mod.resolved # dependencies have been resolved
|
5
|
+
# Note: A generated line is not really needed. Dependencies are stored in memory. Added to assist users with debugging
|
6
|
+
"# #{@mod.name} depends on #{@identifier}" # raw String value
|
7
|
+
else
|
8
|
+
Terraspace::Terraform::RemoteState::Marker::Output.new(@mod, @identifier, @options).build # Returns OutputProxy which defaults to json
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
module Terraspace::Dependency::Helper
|
2
|
+
class Output < Base
|
3
|
+
def result
|
4
|
+
if @mod.resolved # dependencies have been resolved
|
5
|
+
Terraspace::Terraform::RemoteState::Fetcher.new(@mod, @identifier, @options).output # Returns OutputProxy which defaults to json
|
6
|
+
else
|
7
|
+
Terraspace::Terraform::RemoteState::Marker::Output.new(@mod, @identifier, @options).build # Returns OutputProxy => Unresolved
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
module Terraspace::Hooks
|
2
|
+
class Builder
|
3
|
+
extend Memoist
|
4
|
+
include Dsl
|
5
|
+
include DslEvaluator
|
6
|
+
include Terraspace::Util
|
7
|
+
|
8
|
+
# IE: dsl_file: config/hooks/terraform.rb
|
9
|
+
attr_accessor :name
|
10
|
+
def initialize(mod, dsl_file, name)
|
11
|
+
@mod, @dsl_file, @name = mod, dsl_file, name
|
12
|
+
@hooks = {before: {}, after: {}}
|
13
|
+
end
|
14
|
+
|
15
|
+
def build
|
16
|
+
return @hooks unless File.exist?(@dsl_file)
|
17
|
+
evaluate_file(@dsl_file)
|
18
|
+
@hooks.deep_stringify_keys!
|
19
|
+
end
|
20
|
+
memoize :build
|
21
|
+
|
22
|
+
def run_hooks
|
23
|
+
build
|
24
|
+
run_each_hook("before")
|
25
|
+
out = yield if block_given?
|
26
|
+
run_each_hook("after")
|
27
|
+
out
|
28
|
+
end
|
29
|
+
|
30
|
+
def run_each_hook(type)
|
31
|
+
hooks = @hooks.dig(type, @name) || []
|
32
|
+
hooks.each do |hook|
|
33
|
+
run_hook(type, hook)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def run_hook(type, hook)
|
38
|
+
return unless run?(hook)
|
39
|
+
|
40
|
+
command = File.basename(@dsl_file).sub('.rb','') # IE: kubes, kubectl, docker
|
41
|
+
id = "#{command} #{type} #{@name}"
|
42
|
+
label = " label: #{hook["label"]}" if hook["label"]
|
43
|
+
logger.info "Running #{id} hook.#{label}"
|
44
|
+
logger.debug "Hook options: #{hook}"
|
45
|
+
Runner.new(@mod, hook).run
|
46
|
+
end
|
47
|
+
|
48
|
+
def run?(hook)
|
49
|
+
!!hook["execute"]
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
module Terraspace::
|
1
|
+
module Terraspace::Hooks
|
2
2
|
module Dsl
|
3
3
|
def before(*commands, **props)
|
4
4
|
commands.each do |name|
|
@@ -13,7 +13,8 @@ module Terraspace::Terraform::Hooks
|
|
13
13
|
end
|
14
14
|
|
15
15
|
def each_hook(type, name, props={})
|
16
|
-
@hooks[type][name]
|
16
|
+
@hooks[type][name] ||= []
|
17
|
+
@hooks[type][name] << props
|
17
18
|
end
|
18
19
|
end
|
19
20
|
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module Terraspace::Hooks
|
2
|
+
class Runner
|
3
|
+
include Terraspace::Util
|
4
|
+
|
5
|
+
def initialize(mod, hook)
|
6
|
+
@mod, @hook = mod, hook
|
7
|
+
@execute = @hook["execute"]
|
8
|
+
end
|
9
|
+
|
10
|
+
def run
|
11
|
+
case @execute
|
12
|
+
when String
|
13
|
+
Terraspace::Shell.new(@mod, @execute, exit_on_fail: @hook["exit_on_fail"]).run
|
14
|
+
when -> (e) { e.respond_to?(:public_instance_methods) && e.public_instance_methods.include?(:call) }
|
15
|
+
@execute.new.call
|
16
|
+
when -> (e) { e.respond_to?(:call) }
|
17
|
+
@execute.call
|
18
|
+
else
|
19
|
+
logger.warn "WARN: execute option not set for hook: #{@hook.inspect}"
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
data/lib/terraspace/mod.rb
CHANGED
@@ -34,8 +34,17 @@ module Terraspace
|
|
34
34
|
Terraspace.check_project!
|
35
35
|
return if root
|
36
36
|
|
37
|
-
pretty_paths = paths.map { |p| Terraspace::Util.pretty_path(p) }
|
38
|
-
logger.error
|
37
|
+
pretty_paths = paths.map { |p| Terraspace::Util.pretty_path(p) }.join(", ")
|
38
|
+
logger.error <<~EOL
|
39
|
+
ERROR: Unable to find #{@name.color(:green)}. Searched paths:
|
40
|
+
|
41
|
+
#{pretty_paths}
|
42
|
+
|
43
|
+
To see available stacks, try running:
|
44
|
+
|
45
|
+
terraspace list
|
46
|
+
|
47
|
+
EOL
|
39
48
|
ENV['TS_TEST'] ? raise : exit(1)
|
40
49
|
end
|
41
50
|
|
@@ -64,7 +64,9 @@ module Terraspace::Plugin::Summary
|
|
64
64
|
|
65
65
|
def show_each(path)
|
66
66
|
data = JSON.load(IO.read(path))
|
67
|
+
return unless data # edge case: blank file
|
67
68
|
resources = data['resources']
|
69
|
+
return unless resources
|
68
70
|
remove_statefile(path) if resources && resources.size == 0 && !ENV['TS_KEEP_EMPTY_STATEFILES']
|
69
71
|
return unless resources && resources.size > 0
|
70
72
|
|
@@ -75,7 +77,7 @@ module Terraspace::Plugin::Summary
|
|
75
77
|
identifier = r['instances'].map do |i|
|
76
78
|
i['attributes']['name'] || i['attributes']['id']
|
77
79
|
end.join(',')
|
78
|
-
return
|
80
|
+
return unless @options[:details]
|
79
81
|
logger.info " #{r['type']} #{r['name']}: #{identifier}"
|
80
82
|
end
|
81
83
|
end
|
data/lib/terraspace/shell.rb
CHANGED
@@ -6,7 +6,8 @@ module Terraspace
|
|
6
6
|
|
7
7
|
def initialize(mod, command, options={})
|
8
8
|
@mod, @command, @options = mod, command, options
|
9
|
-
|
9
|
+
# error_messages holds aggregation of all error lines
|
10
|
+
@known_error, @error_messages = nil, ''
|
10
11
|
end
|
11
12
|
|
12
13
|
# requires @mod to be set
|
@@ -32,10 +33,9 @@ module Terraspace
|
|
32
33
|
Open3.popen3(env, @command, chdir: @mod.cache_dir) do |stdin, stdout, stderr, wait_thread|
|
33
34
|
mimic_terraform_input(stdin, stdout)
|
34
35
|
while err = stderr.gets
|
35
|
-
@
|
36
|
-
|
37
|
-
|
38
|
-
else
|
36
|
+
@error_messages << err # aggregate all error lines
|
37
|
+
@known_error ||= known_error_type(err)
|
38
|
+
unless @known_error
|
39
39
|
# Sometimes may print a "\e[31m\n" which like during dependencies fetcher init
|
40
40
|
# suppress it so dont get a bunch of annoying "newlines"
|
41
41
|
next if err == "\e[31m\n" && @options[:suppress_error_color]
|
@@ -49,8 +49,8 @@ module Terraspace
|
|
49
49
|
end
|
50
50
|
|
51
51
|
def known_error_type(err)
|
52
|
-
if
|
53
|
-
:
|
52
|
+
if reinit_required?(err)
|
53
|
+
:reinit_required
|
54
54
|
elsif bucket_not_found?(err)
|
55
55
|
:bucket_not_found
|
56
56
|
end
|
@@ -61,7 +61,12 @@ module Terraspace
|
|
61
61
|
err.include?("Failed to get existing workspaces")
|
62
62
|
end
|
63
63
|
|
64
|
-
def
|
64
|
+
def reinit_required?(err)
|
65
|
+
# Example error: https://gist.github.com/tongueroo/f7e0a44b64f0a2e533089b18f331c21e
|
66
|
+
squeezed = @error_messages.gsub("\n", ' ').squeeze(' ') # remove double whitespaces and newlines
|
67
|
+
general_check = squeezed.include?("terraform init") && squeezed.include?("Error:")
|
68
|
+
|
69
|
+
general_check ||
|
65
70
|
err.include?("reinitialization required") ||
|
66
71
|
err.include?("terraform init") ||
|
67
72
|
err.include?("require reinitialization")
|
@@ -71,9 +76,9 @@ module Terraspace
|
|
71
76
|
return if status == 0
|
72
77
|
|
73
78
|
exit_on_fail = @options[:exit_on_fail].nil? ? true : @options[:exit_on_fail]
|
74
|
-
if @
|
79
|
+
if @known_error == :reinit_required
|
75
80
|
raise InitRequiredError.new(@error_messages)
|
76
|
-
elsif @
|
81
|
+
elsif @known_error == :bucket_not_found
|
77
82
|
raise BucketNotFoundError.new(@error_messages)
|
78
83
|
elsif exit_on_fail
|
79
84
|
logger.error "Error running command: #{@command}".color(:red)
|
@@ -26,7 +26,7 @@ module Terraspace::Terraform::Args
|
|
26
26
|
args << var_files.map { |f| "-var-file #{Dir.pwd}/#{f}" }.join(' ')
|
27
27
|
end
|
28
28
|
|
29
|
-
args << input_option
|
29
|
+
args << input_option
|
30
30
|
|
31
31
|
# must be at the end
|
32
32
|
plan = @options[:plan]
|
@@ -45,27 +45,17 @@ module Terraspace::Terraform::Args
|
|
45
45
|
end
|
46
46
|
|
47
47
|
def input_option
|
48
|
-
option =
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
option = " -input=#{input}" # = sign required for apply when there's a plan at the end. so input=false works input false doesnt
|
55
|
-
end
|
56
|
-
option
|
48
|
+
option = if @options[:auto]
|
49
|
+
"false"
|
50
|
+
else
|
51
|
+
@options[:input] ? @options[:input] : "false"
|
52
|
+
end
|
53
|
+
" -input=#{option}"
|
57
54
|
end
|
58
55
|
|
59
56
|
def init_args
|
60
57
|
args = "-get"
|
61
|
-
|
62
|
-
args << " -input=false"
|
63
|
-
end
|
64
|
-
unless @options[:input].nil?
|
65
|
-
input = @options[:input] ? "true" : "false"
|
66
|
-
args << " -input=#{input}"
|
67
|
-
end
|
68
|
-
|
58
|
+
args << input_option
|
69
59
|
args << " -reconfigure" if @options[:reconfigure]
|
70
60
|
|
71
61
|
# must be at the end
|
@@ -86,7 +76,7 @@ module Terraspace::Terraform::Args
|
|
86
76
|
|
87
77
|
def plan_args
|
88
78
|
args = []
|
89
|
-
args << input_option
|
79
|
+
args << input_option
|
90
80
|
args << "-destroy" if @options[:destroy]
|
91
81
|
args << "-out #{expanded_out}" if @options[:out]
|
92
82
|
args
|
@@ -8,6 +8,7 @@ module Terraspace::Terraform::RemoteState
|
|
8
8
|
@parent, @identifier, @options = parent, identifier, options
|
9
9
|
child_name, @output_key = identifier.split('.')
|
10
10
|
@child = Terraspace::Mod.new(child_name)
|
11
|
+
@child.resolved = @parent.resolved
|
11
12
|
end
|
12
13
|
|
13
14
|
def run
|
@@ -16,16 +17,24 @@ module Terraspace::Terraform::RemoteState
|
|
16
17
|
load
|
17
18
|
end
|
18
19
|
|
20
|
+
# Returns OutputProxy
|
19
21
|
def output
|
20
22
|
run
|
21
23
|
if pull_success?
|
22
|
-
|
23
|
-
error = output_error(:key_not_found) unless @outputs.key?(@output_key)
|
24
|
-
OutputProxy.new(value, @options.merge(error: error))
|
24
|
+
pull_success_output
|
25
25
|
else
|
26
26
|
@error_type ||= :state_not_found # could be set to :bucket_not_found by bucket_not_found_error
|
27
27
|
error = output_error(@error_type)
|
28
|
-
OutputProxy.new(nil, @options.merge(error: error))
|
28
|
+
OutputProxy.new(@child, nil, @options.merge(error: error))
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def pull_success_output
|
33
|
+
if @outputs.key?(@output_key)
|
34
|
+
OutputProxy.new(@child, output_value, @options)
|
35
|
+
else
|
36
|
+
error = output_error(:key_not_found)
|
37
|
+
OutputProxy.new(@child, nil, @options.merge(error: error))
|
29
38
|
end
|
30
39
|
end
|
31
40
|
|
@@ -8,6 +8,7 @@ module Terraspace::Terraform::RemoteState::Marker
|
|
8
8
|
@child_name, @output_key = @identifier.split('.')
|
9
9
|
end
|
10
10
|
|
11
|
+
# Returns OutputProxy
|
11
12
|
def build
|
12
13
|
if valid?
|
13
14
|
Terraspace::Dependency::Registry.register(@parent_name, @child_name)
|
@@ -16,7 +17,7 @@ module Terraspace::Terraform::RemoteState::Marker
|
|
16
17
|
end
|
17
18
|
# MARKER for debugging. Only appears on 1st pass. Will not see unless changing Terraspace code for debugging.
|
18
19
|
marker = "MARKER:terraform_output('#{@identifier}')"
|
19
|
-
Terraspace::Terraform::RemoteState::OutputProxy.new(marker, @options)
|
20
|
+
Terraspace::Terraform::RemoteState::OutputProxy.new(@mod, marker, @options)
|
20
21
|
end
|
21
22
|
|
22
23
|
def valid?
|
@@ -26,6 +27,7 @@ module Terraspace::Terraform::RemoteState::Marker
|
|
26
27
|
def warning
|
27
28
|
logger.warn "WARN: The #{@child_name} stack does not exist".color(:yellow)
|
28
29
|
caller_line = caller.find { |l| l.include?('.tfvars') }
|
30
|
+
return unless caller_line # specs dont have a tfvars file
|
29
31
|
source_code = PrettyTracer.new(caller_line).source_code
|
30
32
|
logger.info source_code
|
31
33
|
end
|
@@ -1,29 +1,33 @@
|
|
1
1
|
module Terraspace::Terraform::RemoteState
|
2
2
|
class OutputProxy
|
3
3
|
# raw: can be anything: String, Array, Hash, etc
|
4
|
-
# options: original options passed by user
|
4
|
+
# options: original options passed by user from the output helper in tfvars
|
5
5
|
attr_reader :raw, :options
|
6
|
-
def initialize(raw, options={})
|
7
|
-
@raw, @options = raw, options
|
6
|
+
def initialize(mod, raw, options={})
|
7
|
+
@mod, @raw, @options = mod, raw, options
|
8
8
|
@format = @options[:format]
|
9
9
|
end
|
10
10
|
|
11
11
|
# Should always return a String
|
12
12
|
def to_s
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
13
|
+
if @mod.resolved
|
14
|
+
# Dont use Unresolved wrapper because Integer get changed to Strings.
|
15
|
+
# Want raw value to be used for the to_json call
|
16
|
+
value = @raw.nil? ? mock_or_error : @raw
|
17
|
+
value.to_json
|
18
|
+
else
|
19
|
+
Unresolved.new
|
18
20
|
end
|
19
21
|
end
|
20
22
|
|
21
|
-
def
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
23
|
+
def to_ruby
|
24
|
+
data = @raw.nil? ? mock_or_error : @raw
|
25
|
+
@mod.resolved ? data : Unresolved.new
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
def mock_or_error
|
30
|
+
@options[:mock] || @options[:error]
|
27
31
|
end
|
28
32
|
end
|
29
33
|
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module Terraspace::Terraform::RemoteState
|
2
|
+
class Unresolved
|
3
|
+
def to_a
|
4
|
+
[]
|
5
|
+
end
|
6
|
+
|
7
|
+
def to_ary
|
8
|
+
[]
|
9
|
+
end
|
10
|
+
|
11
|
+
def to_s
|
12
|
+
"(unresolved)" # always returned as part of first unresolved processing pass
|
13
|
+
end
|
14
|
+
alias_method :to_str, :to_s # ERB requires to_str
|
15
|
+
|
16
|
+
def to_f
|
17
|
+
0.0
|
18
|
+
end
|
19
|
+
|
20
|
+
def to_i
|
21
|
+
0
|
22
|
+
end
|
23
|
+
|
24
|
+
def nil?
|
25
|
+
true
|
26
|
+
end
|
27
|
+
|
28
|
+
def inspect
|
29
|
+
format("#<%s:0x%x>", self.class, object_id)
|
30
|
+
end
|
31
|
+
|
32
|
+
def method_missing(*_args, &_block)
|
33
|
+
self
|
34
|
+
end
|
35
|
+
|
36
|
+
def respond_to?(_message, _include_private = false)
|
37
|
+
true
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|