class Kubes::Hooks::Builder

Attributes

name[RW]

Public Class Methods

new(file, options={}) click to toggle source
# 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

Public Instance Methods

build() click to toggle source
# File lib/kubes/hooks/builder.rb, line 17
def build
  evaluate_file(@dsl_file)
  evaluate_plugin_hooks
  @hooks.deep_stringify_keys!
end
evaluate_plugin_hooks() click to toggle source
# 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) click to toggle source
# File lib/kubes/hooks/builder.rb, line 34
def hooks_class(klass)
  "#{klass}::Hooks".constantize # IE: KubesGoogle::Hooks
rescue NameError
end
run?(hook) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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_hooks() { || ... } click to toggle source
# 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