terraspace 0.3.3 → 0.4.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.cody/aws/bin/build.sh +2 -0
- data/.cody/azurerm/bin/build.sh +2 -0
- data/.cody/google/bin/build.sh +2 -0
- data/CHANGELOG.md +39 -0
- data/README.md +1 -1
- data/lib/templates/base/project/README.md +1 -1
- data/lib/terraspace.rb +1 -0
- data/lib/terraspace/all/preview.rb +1 -1
- data/lib/terraspace/all/runner.rb +1 -0
- data/lib/terraspace/all/summary.rb +29 -2
- data/lib/terraspace/app.rb +9 -5
- data/lib/terraspace/builder.rb +10 -6
- data/lib/terraspace/cli.rb +33 -39
- data/lib/terraspace/cli/all.rb +15 -9
- 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/cloud.rb +3 -9
- data/lib/terraspace/cli/cloud/runs.rb +0 -2
- data/lib/terraspace/cli/help/all/down.md +32 -0
- data/lib/terraspace/cli/help/all/graph.md +21 -0
- data/lib/terraspace/cli/help/all/init.md +33 -0
- data/lib/terraspace/cli/help/all/output.md +22 -0
- data/lib/terraspace/cli/help/all/plan.md +25 -0
- data/lib/terraspace/cli/help/all/providers.md +21 -0
- data/lib/terraspace/cli/help/all/refresh.md +17 -0
- data/lib/terraspace/cli/help/all/show.md +21 -0
- data/lib/terraspace/cli/help/all/up.md +27 -0
- data/lib/terraspace/cli/help/all/validate.md +21 -0
- data/lib/terraspace/cli/help/build.md +6 -0
- data/lib/terraspace/cli/help/bundle.md +9 -5
- data/lib/terraspace/cli/help/check_setup.md +9 -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/cloud/destroy.md +16 -0
- data/lib/terraspace/cli/help/cloud/list.md +7 -0
- data/lib/terraspace/cli/help/cloud/runs/list.md +3 -3
- data/lib/terraspace/cli/help/cloud/runs/prune.md +1 -1
- data/lib/terraspace/cli/help/cloud/sync.md +26 -2
- data/lib/terraspace/cli/help/console.md +8 -0
- data/lib/terraspace/cli/help/down.md +26 -0
- data/lib/terraspace/cli/help/info.md +43 -0
- data/lib/terraspace/cli/help/init.md +37 -0
- data/lib/terraspace/cli/help/list.md +20 -0
- data/lib/terraspace/cli/help/logs.md +48 -0
- data/lib/terraspace/cli/help/logs/remove.md +5 -0
- data/lib/terraspace/cli/help/logs/truncate.md +5 -0
- data/lib/terraspace/cli/help/new/bootstrap_test.md +8 -0
- data/lib/terraspace/cli/help/new/example.md +8 -0
- data/lib/terraspace/cli/help/new/git_hook.md +6 -0
- data/lib/terraspace/cli/help/new/module.md +9 -0
- data/lib/terraspace/cli/help/new/module_test.md +12 -0
- data/lib/terraspace/cli/help/new/plugin.md +49 -0
- data/lib/terraspace/cli/help/new/project.md +40 -0
- data/lib/terraspace/cli/help/new/project_test.md +8 -0
- data/lib/terraspace/cli/help/new/shim.md +21 -0
- data/lib/terraspace/cli/help/new/stack.md +9 -0
- data/lib/terraspace/cli/help/output.md +6 -0
- data/lib/terraspace/cli/help/plan.md +29 -0
- data/lib/terraspace/cli/help/providers.md +18 -0
- data/lib/terraspace/cli/help/refresh.md +11 -0
- data/lib/terraspace/cli/help/seed.md +7 -0
- data/lib/terraspace/cli/help/show.md +36 -0
- data/lib/terraspace/cli/help/summary.md +11 -0
- data/lib/terraspace/cli/help/test.md +35 -0
- data/lib/terraspace/cli/help/up.md +30 -0
- data/lib/terraspace/cli/help/validate.md +9 -0
- data/lib/terraspace/cli/info.rb +4 -16
- 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 +1 -1
- data/lib/terraspace/cli/new.rb +18 -18
- data/lib/terraspace/cli/new/git_hook.rb +4 -1
- data/lib/terraspace/cli/new/helper.rb +9 -2
- data/lib/terraspace/cli/new/project.rb +1 -1
- data/lib/terraspace/cli/summary.rb +2 -2
- data/lib/terraspace/command.rb +1 -1
- 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/graph.rb +2 -1
- 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 +4 -2
- data/lib/terraspace/shell.rb +29 -14
- data/lib/terraspace/terraform/args/custom.rb +1 -1
- data/lib/terraspace/terraform/cloud/runs/lister.rb +0 -2
- data/lib/terraspace/terraform/cloud/syncer.rb +2 -2
- data/lib/terraspace/terraform/cloud/workspace.rb +0 -9
- 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 +2 -7
- 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/all/summary_spec.rb +1 -1
- 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 +78 -13
- data/lib/terraspace/cli/help/log.md +0 -46
- data/lib/terraspace/cli/help/update.md +0 -5
- data/lib/terraspace/cli/log.rb +0 -112
- data/lib/terraspace/terraform/hooks/builder.rb +0 -40
data/lib/terraspace/cli/logs.rb
CHANGED
@@ -1,15 +1,112 @@
|
|
1
|
+
require "eventmachine"
|
2
|
+
require "eventmachine-tail"
|
3
|
+
|
1
4
|
class Terraspace::CLI
|
2
|
-
class Logs <
|
3
|
-
|
4
|
-
|
5
|
-
def
|
6
|
-
|
5
|
+
class Logs < Base
|
6
|
+
include Concern
|
7
|
+
|
8
|
+
def initialize(options={})
|
9
|
+
super
|
10
|
+
@action, @stack = options[:action], options[:stack]
|
11
|
+
@action ||= '**'
|
12
|
+
@stack ||= '*'
|
13
|
+
end
|
14
|
+
|
15
|
+
def run
|
16
|
+
check_logs!
|
17
|
+
if @options[:follow]
|
18
|
+
follow_logs
|
19
|
+
else
|
20
|
+
all_log_paths.each { |path| show_log(path) }
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def follow_logs
|
25
|
+
glob_path = "#{Terraspace.log_root}/#{@action}/#{@stack}.log"
|
26
|
+
Dir.glob(glob_path).each do |path|
|
27
|
+
puts "Following #{pretty(path)}".color(:purple)
|
28
|
+
end
|
29
|
+
EventMachine.run do
|
30
|
+
interval = Integer(ENV['TS_LOG_GLOB_INTERNAL'] || 1)
|
31
|
+
EventMachine::FileGlobWatchTail.new(glob_path, nil, interval) do |filetail, line|
|
32
|
+
puts line # always show timestamp in follow mode
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def show_log(path)
|
38
|
+
report_log(path)
|
39
|
+
lines = readlines(path)
|
40
|
+
lines = apply_limit(lines)
|
41
|
+
lines.each do |line|
|
42
|
+
puts format(line)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def report_log(path)
|
47
|
+
pretty_path = pretty(path)
|
48
|
+
if File.exist?(path)
|
49
|
+
puts "Showing: #{pretty_path}".color(:purple)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def format(line)
|
54
|
+
if timestamps
|
55
|
+
line
|
56
|
+
else
|
57
|
+
line.sub(/.*\]: /,'')
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def all_log_paths
|
62
|
+
Dir.glob("#{Terraspace.log_root}/#{@action}/#{@stack}.log")
|
7
63
|
end
|
8
64
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
65
|
+
def check_logs!
|
66
|
+
return unless all_log_paths.empty?
|
67
|
+
puts "WARN: No logs found".color(:yellow)
|
68
|
+
end
|
69
|
+
|
70
|
+
# Only need to check if both action and stack are provided. Otherwise the Dir.globs are used to discover the files
|
71
|
+
def check_log!
|
72
|
+
return unless single_log?
|
73
|
+
path = "#{Terraspace.log_root}/#{@action}/#{@stack}.log"
|
74
|
+
return if File.exist?(path)
|
75
|
+
puts "ERROR: Log file was not found: #{pretty(path)}".color(:red)
|
76
|
+
exit 1
|
77
|
+
end
|
78
|
+
|
79
|
+
def single_log?
|
80
|
+
@action != '**' && @stack != '*'
|
81
|
+
end
|
82
|
+
|
83
|
+
def apply_limit(lines)
|
84
|
+
return lines if all
|
85
|
+
left = limit * -1
|
86
|
+
lines[left..-1] || []
|
87
|
+
end
|
88
|
+
|
89
|
+
def all
|
90
|
+
if single_log?
|
91
|
+
@options[:all].nil? ? true : @options[:all]
|
92
|
+
else # multiple
|
93
|
+
@options[:all].nil? ? false : @options[:all]
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
def limit
|
98
|
+
@options[:limit].nil? ? 10 : @options[:limit]
|
99
|
+
end
|
100
|
+
|
101
|
+
def timestamps
|
102
|
+
if single_log?
|
103
|
+
@options[:timestamps].nil? ? false : @options[:timestamps]
|
104
|
+
else
|
105
|
+
@options[:timestamps].nil? ? true : @options[:timestamps]
|
106
|
+
end
|
107
|
+
end
|
108
|
+
def pretty(path)
|
109
|
+
Terraspace::Util.pretty_path(path)
|
13
110
|
end
|
14
111
|
end
|
15
112
|
end
|
data/lib/terraspace/cli/new.rb
CHANGED
@@ -1,40 +1,40 @@
|
|
1
1
|
class Terraspace::CLI
|
2
2
|
class New < Terraspace::Command
|
3
|
-
long_desc Help.text(
|
3
|
+
long_desc Help.text("new/git_hook")
|
4
4
|
GitHook.cli_options.each { |args| option(*args) }
|
5
|
-
register(GitHook, "git_hook", "git_hook", "Generates new git hook")
|
5
|
+
register(GitHook, "git_hook", "git_hook", "Generates new git hook.")
|
6
6
|
|
7
|
-
long_desc Help.text(
|
7
|
+
long_desc Help.text("new/shim")
|
8
8
|
Shim.cli_options.each { |args| option(*args) }
|
9
|
-
register(Shim, "shim", "shim", "Generates terraspace shim")
|
9
|
+
register(Shim, "shim", "shim", "Generates terraspace shim.")
|
10
10
|
|
11
|
-
long_desc Help.text(
|
11
|
+
long_desc Help.text("new/module")
|
12
12
|
Module.base_options.each { |args| option(*args) }
|
13
13
|
Module.component_options.each { |args| option(*args) }
|
14
|
-
register(Module, "module", "module NAME", "Generates new module")
|
14
|
+
register(Module, "module", "module NAME", "Generates new module.")
|
15
15
|
|
16
|
-
long_desc Help.text(
|
16
|
+
long_desc Help.text("new/stack")
|
17
17
|
Stack.base_options.each { |args| option(*args) }
|
18
18
|
Stack.component_options.each { |args| option(*args) }
|
19
|
-
register(Stack, "stack", "stack NAME", "Generates new stack")
|
19
|
+
register(Stack, "stack", "stack NAME", "Generates new stack.")
|
20
20
|
|
21
|
-
long_desc Help.text(
|
21
|
+
long_desc Help.text("new/project")
|
22
22
|
Project.base_options.each { |args| option(*args) }
|
23
23
|
Project.project_options.each { |args| option(*args) }
|
24
|
-
register(Project, "project", "project NAME", "Generates new project")
|
24
|
+
register(Project, "project", "project NAME", "Generates new project.")
|
25
25
|
|
26
|
-
long_desc Help.text(
|
27
|
-
register(Test::Project, "project_test", "project_test NAME", "Generates new project test")
|
26
|
+
long_desc Help.text("new/project_test")
|
27
|
+
register(Test::Project, "project_test", "project_test NAME", "Generates new project test.")
|
28
28
|
|
29
|
-
long_desc Help.text(
|
30
|
-
register(Test::Module, "module_test", "module_test NAME", "Generates new module test")
|
29
|
+
long_desc Help.text("new/module_test")
|
30
|
+
register(Test::Module, "module_test", "module_test NAME", "Generates new module test.")
|
31
31
|
|
32
|
-
long_desc Help.text(
|
32
|
+
long_desc Help.text("new/bootstrap_test")
|
33
33
|
Test::Bootstrap.options.each { |args| option(*args) }
|
34
|
-
register(Test::Bootstrap, "bootstrap_test", "bootstrap_test", "Generates bootstrap test setup")
|
34
|
+
register(Test::Bootstrap, "bootstrap_test", "bootstrap_test", "Generates bootstrap test setup.")
|
35
35
|
|
36
|
-
long_desc Help.text(
|
36
|
+
long_desc Help.text("new/plugin")
|
37
37
|
Plugin.options.each { |args| option(*args) }
|
38
|
-
register(Plugin, "plugin", "plugin", "Generates plugin")
|
38
|
+
register(Plugin, "plugin", "plugin", "Generates plugin.")
|
39
39
|
end
|
40
40
|
end
|
@@ -15,7 +15,10 @@ class Terraspace::CLI::New
|
|
15
15
|
end
|
16
16
|
|
17
17
|
def create
|
18
|
-
|
18
|
+
unless File.exist?(".git")
|
19
|
+
puts "No .git folder found. Not creating git hook."
|
20
|
+
return
|
21
|
+
end
|
19
22
|
dest = ".git/hooks/#{options[:type]}"
|
20
23
|
template "hook.sh", dest
|
21
24
|
chmod dest, 0755
|
@@ -6,10 +6,17 @@ class Terraspace::CLI::New
|
|
6
6
|
def build_gemfile(*list)
|
7
7
|
lines = []
|
8
8
|
list.each do |name|
|
9
|
-
|
10
|
-
lines << line
|
9
|
+
lines << gem_line(name)
|
11
10
|
end
|
12
11
|
lines.join("\n")
|
13
12
|
end
|
13
|
+
|
14
|
+
def gem_line(name)
|
15
|
+
if name == "terraspace"
|
16
|
+
%Q|gem "#{name}", '~> #{Terraspace::VERSION}'|
|
17
|
+
else
|
18
|
+
%Q|gem "#{name}"|
|
19
|
+
end
|
20
|
+
end
|
14
21
|
end
|
15
22
|
end
|
@@ -12,7 +12,7 @@ class Terraspace::CLI
|
|
12
12
|
|
13
13
|
def run
|
14
14
|
Terraspace.check_project!
|
15
|
-
|
15
|
+
build_placeholder
|
16
16
|
puts "Summary of resources based on backend storage statefiles"
|
17
17
|
backend_expr = '.terraspace-cache/**/backend.*'
|
18
18
|
# Currently summary assumes backend are within the same bucket and key prefix
|
@@ -22,7 +22,7 @@ class Terraspace::CLI
|
|
22
22
|
|
23
23
|
# Grab the last module and build that.
|
24
24
|
# Assume the backend key has the same prefix
|
25
|
-
def
|
25
|
+
def build_placeholder
|
26
26
|
Build::Placeholder.new(@options).build
|
27
27
|
end
|
28
28
|
|
data/lib/terraspace/command.rb
CHANGED
@@ -0,0 +1,34 @@
|
|
1
|
+
module Terraspace::Compiler::Dependencies
|
2
|
+
# This is a separate module specifically because the DSL also has an output method.
|
3
|
+
# The module allows us to include dependency related methods only within tfvars context for the DSL.
|
4
|
+
#
|
5
|
+
# 1. Only include this module to DSL tfvars context.
|
6
|
+
# So the output method works in tfvars .rb files works.
|
7
|
+
# At the same time, the DSL usage of output also works for normal main.tf files.
|
8
|
+
# Passing specs prove this.
|
9
|
+
# 2. For ERB, there's currently only one ERB context. So this module is included in all contexts.
|
10
|
+
# The builder only processes dependencies from tfvars, so these helpers are only respected there.
|
11
|
+
#
|
12
|
+
# Where the module is included in the code:
|
13
|
+
#
|
14
|
+
# 1. lib/terraspace/compiler/dsl/syntax/tfvar.rb
|
15
|
+
# 2. lib/terraspace/compiler/erb/helpers.rb
|
16
|
+
#
|
17
|
+
module Helpers
|
18
|
+
def output(identifier, options={})
|
19
|
+
Terraspace::Dependency::Helper::Output.new(@mod, identifier, options).result
|
20
|
+
end
|
21
|
+
alias_method :terraform_output, :output # backwards compatibility
|
22
|
+
|
23
|
+
def depends_on(*child_names, **options)
|
24
|
+
child_names.flatten!
|
25
|
+
child_names.map do |child_name|
|
26
|
+
each_depends_on(child_name, options)
|
27
|
+
end.join("\n")
|
28
|
+
end
|
29
|
+
|
30
|
+
def each_depends_on(child_name, options={})
|
31
|
+
Terraspace::Dependency::Helper::DependsOn.new(@mod, child_name, options).result
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -1,8 +1,6 @@
|
|
1
1
|
module Terraspace::Compiler::Dsl::Syntax::Helpers
|
2
2
|
module Common
|
3
3
|
extend Memoist
|
4
|
-
Fetcher = Terraspace::Terraform::RemoteState::Fetcher
|
5
|
-
Marker = Terraspace::Terraform::RemoteState::Marker
|
6
4
|
Meta = Terraspace::Compiler::Dsl::Meta
|
7
5
|
|
8
6
|
def var
|
@@ -25,29 +23,5 @@ module Terraspace::Compiler::Dsl::Syntax::Helpers
|
|
25
23
|
command = ["terraspace"] + args
|
26
24
|
command.join(separator)
|
27
25
|
end
|
28
|
-
|
29
|
-
def terraform_output(identifier, options={})
|
30
|
-
if @mod.resolved # dependencies have been resolved
|
31
|
-
Fetcher.new(@mod, identifier, options).output
|
32
|
-
else
|
33
|
-
Marker::Output.new(@mod, identifier, options).build
|
34
|
-
end
|
35
|
-
end
|
36
|
-
|
37
|
-
def depends_on(*child_names, **options)
|
38
|
-
child_names.flatten!
|
39
|
-
child_names.map do |child_name|
|
40
|
-
each_depends_on(child_name, options)
|
41
|
-
end.join("\n")
|
42
|
-
end
|
43
|
-
|
44
|
-
def each_depends_on(child_name, options={})
|
45
|
-
if @mod.resolved # dependencies have been resolved
|
46
|
-
# Note: A generated line is not really needed. Dependencies are stored in memory. Added to assist users with debugging
|
47
|
-
"# #{@mod.name} depends on #{child_name}"
|
48
|
-
else
|
49
|
-
Marker::Output.new(@mod, child_name, options).build
|
50
|
-
end
|
51
|
-
end
|
52
26
|
end
|
53
27
|
end
|
@@ -20,8 +20,9 @@ module Terraspace::Dependency
|
|
20
20
|
@batches
|
21
21
|
end
|
22
22
|
|
23
|
+
# Only check when stacks option is pass. Edge case: There can be app/modules but no app/stacks yet
|
23
24
|
def check_empty_nodes!
|
24
|
-
return unless @nodes.empty?
|
25
|
+
return unless @nodes.empty? && @options[:stacks]
|
25
26
|
logger.error "ERROR: No stacks were found that match: #{@options[:stacks].join(' ')}".color(:red)
|
26
27
|
exit 1
|
27
28
|
end
|
@@ -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
|