Sha256: 22ce3126b31a100c08f0cdfe289262ad02c955f4075b1c6db925be255f4ebee8

Contents?: true

Size: 1.85 KB

Versions: 32

Compression:

Stored size: 1.85 KB

Contents

module Kubes::Hooks
  class Builder
    extend Memoist
    include Dsl
    include DslEvaluator
    include Kubes::Logging

    attr_accessor :name
    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

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

    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

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

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

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

    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

    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
  end
end

Version data entries

32 entries across 32 versions & 1 rubygems

Version Path
kubes-0.9.3 lib/kubes/hooks/builder.rb
kubes-0.9.2 lib/kubes/hooks/builder.rb
kubes-0.9.1 lib/kubes/hooks/builder.rb
kubes-0.9.0 lib/kubes/hooks/builder.rb
kubes-0.8.10 lib/kubes/hooks/builder.rb
kubes-0.8.9 lib/kubes/hooks/builder.rb
kubes-0.8.8 lib/kubes/hooks/builder.rb
kubes-0.8.7 lib/kubes/hooks/builder.rb
kubes-0.8.6 lib/kubes/hooks/builder.rb
kubes-0.8.5 lib/kubes/hooks/builder.rb
kubes-0.8.4 lib/kubes/hooks/builder.rb
kubes-0.8.3 lib/kubes/hooks/builder.rb
kubes-0.8.2 lib/kubes/hooks/builder.rb
kubes-0.8.1 lib/kubes/hooks/builder.rb
kubes-0.8.0 lib/kubes/hooks/builder.rb
kubes-0.7.10 lib/kubes/hooks/builder.rb
kubes-0.7.9 lib/kubes/hooks/builder.rb
kubes-0.7.8 lib/kubes/hooks/builder.rb
kubes-0.7.7 lib/kubes/hooks/builder.rb
kubes-0.7.6 lib/kubes/hooks/builder.rb