Sha256: d9fafcc001b5096eaca0d0180bc5d53f9027e1cacb67e801feb3224e4b9537fd

Contents?: true

Size: 1.47 KB

Versions: 9

Compression:

Stored size: 1.47 KB

Contents

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

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

    def build
      return @hooks unless File.exist?(@dsl_file)
      evaluate_file(@dsl_file)
      @hooks.deep_stringify_keys!
    end
    memoize :build

    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  "Running #{id} hook.#{on}#{label}"
      logger.debug "Hook options: #{hook}"
      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

9 entries across 9 versions & 1 rubygems

Version Path
kubes-0.6.1 lib/kubes/hooks/builder.rb
kubes-0.6.0 lib/kubes/hooks/builder.rb
kubes-0.5.1 lib/kubes/hooks/builder.rb
kubes-0.5.0 lib/kubes/hooks/builder.rb
kubes-0.4.7 lib/kubes/hooks/builder.rb
kubes-0.4.6 lib/kubes/hooks/builder.rb
kubes-0.4.5 lib/kubes/hooks/builder.rb
kubes-0.4.4 lib/kubes/hooks/builder.rb
kubes-0.4.3 lib/kubes/hooks/builder.rb