Sha256: 7f566f8362069396322ab7d2ae1745f3ac97d63364443a5a6a5a850871c1c539

Contents?: true

Size: 1017 Bytes

Versions: 17

Compression:

Stored size: 1017 Bytes

Contents

require 'json'
require 'puppetfactory'

class Puppetfactory::Plugins::Hooks < Puppetfactory::Plugins
  attr_reader :weight

  def initialize(options)
    super(options)

    @weight = 1
    @path   = options[:hooks_path] || '/etc/puppetfactory/hooks'
  end

  def create(username)
    call_hooks(:create, username)
  end

  def delete(username)
    call_hooks(:delete, username)
  end

  private
  def call_hooks(hook_type, username)
    success = true
    # the .to_s allows us to accept strings or symbols
    Dir.glob("#{HOOKS_PATH}/#{hook_type.to_s}/*") do |hook|
      next unless File.file?(hook)
      next unless File.executable?(hook)

      begin
        output, status = Open3.capture2e(hook, username)
        raise "Execution error: #{output}" unless status.success?
        $logger.info output

      rescue => e
        $logger.error "Error running hook: #{hook}"
        $logger.error e.message
        success = false
      end
    end

    # only true if all hooks succeeded.
    success
  end

end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
puppetfactory-0.6.5 lib/puppetfactory/plugins/hooks.rb
puppetfactory-0.6.4 lib/puppetfactory/plugins/hooks.rb
puppetfactory-0.6.3 lib/puppetfactory/plugins/hooks.rb
puppetfactory-0.6.2 lib/puppetfactory/plugins/hooks.rb
puppetfactory-0.6.1 lib/puppetfactory/plugins/hooks.rb
puppetfactory-0.6.0 lib/puppetfactory/plugins/hooks.rb
puppetfactory-0.5.9 lib/puppetfactory/plugins/hooks.rb
puppetfactory-0.5.8 lib/puppetfactory/plugins/hooks.rb
puppetfactory-0.5.7 lib/puppetfactory/plugins/hooks.rb
puppetfactory-0.5.6 lib/puppetfactory/plugins/hooks.rb
puppetfactory-0.5.5 lib/puppetfactory/plugins/hooks.rb
puppetfactory-0.5.4 lib/puppetfactory/plugins/hooks.rb
puppetfactory-0.5.3 lib/puppetfactory/plugins/hooks.rb
puppetfactory-0.5.2 lib/puppetfactory/plugins/hooks.rb
puppetfactory-0.5.1 lib/puppetfactory/plugins/hooks.rb
puppetfactory-0.5.0 lib/puppetfactory/plugins/hooks.rb
puppetfactory-0.4.0 lib/puppetfactory/plugins/hooks.rb