Class: Kubes::Hooks::Builder

Inherits:
Object
  • Object
show all
Extended by:
Memoist
Includes:
DslEvaluator, Dsl, Logging
Defined in:
lib/kubes/hooks/builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

#logger

Methods included from Dsl

#after, #before, #each_hook

Constructor Details

#initialize(file, options = {}) ⇒ Builder

Returns a new instance of Builder.



9
10
11
12
13
14
15
# File 'lib/kubes/hooks/builder.rb', line 9

def initialize(file, options={})
  @file, @options = file, options # IE: .kubes/config/hooks/kubectl.rb
  @dsl_file = "#{Kubes.root}/.kubes/config/hooks/#{@file}"
  @output_file = options[:file] # IE: .kubes/output/web/service.yaml
  @name = options[:name].to_s
  @hooks = {before: {}, after: {}}
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



8
9
10
# File 'lib/kubes/hooks/builder.rb', line 8

def name
  @name
end

Instance Method Details

#buildObject



17
18
19
20
21
# File 'lib/kubes/hooks/builder.rb', line 17

def build
  evaluate_file(@dsl_file)
  evaluate_plugin_hooks
  @hooks.deep_stringify_keys!
end

#evaluate_plugin_hooksObject



24
25
26
27
28
29
30
31
32
# File 'lib/kubes/hooks/builder.rb', line 24

def evaluate_plugin_hooks
  Kubes::Plugin.plugins.each do |klass|
    hooks_class = hooks_class(klass)
    next unless hooks_class
    plugin_hooks = hooks_class.new
    path = "#{plugin_hooks.path}/#{@file}"
    evaluate_file(path)
  end
end

#hooks_class(klass) ⇒ Object



34
35
36
37
# File 'lib/kubes/hooks/builder.rb', line 34

def hooks_class(klass)
  "#{klass}::Hooks".constantize # IE: KubesGoogle::Hooks
rescue NameError
end

#run?(hook) ⇒ Boolean

Returns:

  • (Boolean)


65
66
67
68
69
# File 'lib/kubes/hooks/builder.rb', line 65

def run?(hook)
  return false unless hook["execute"]
  return true  unless hook["on"]
  @output_file && @output_file.include?(hook["on"]) # output file is only passed
end

#run_each_hook(type) ⇒ Object



47
48
49
50
51
52
# File 'lib/kubes/hooks/builder.rb', line 47

def run_each_hook(type)
  hooks = @hooks.dig(type, @name.to_s) || []
  hooks.each do |hook|
    run_hook(type, hook)
  end
end

#run_hook(type, hook) ⇒ Object



54
55
56
57
58
59
60
61
62
63
# File 'lib/kubes/hooks/builder.rb', line 54

def run_hook(type, hook)
  return unless run?(hook)

  command = File.basename(@dsl_file).sub('.rb','') # IE: kubes, kubectl, docker
  id = "#{command} #{type} #{@name}"
  on = " on: #{hook["on"]}" if hook["on"]
  label = " label: #{hook["label"]}" if hook["label"]
  logger.info  "Hook: Running #{id} hook.#{on}#{label}"
  Runner.new(hook).run
end

#run_hooksObject



39
40
41
42
43
44
45
# File 'lib/kubes/hooks/builder.rb', line 39

def run_hooks
  build
  run_each_hook("before")
  out = yield if block_given?
  run_each_hook("after")
  out
end