Sha256: b54a530572e32ecedb99cf9f91bc41263edc191a7a0c0a72872d7a5b60d2befe

Contents?: true

Size: 1.74 KB

Versions: 1

Compression:

Stored size: 1.74 KB

Contents

require "web-puppet/version"
require 'rack'
require 'puppet'
require 'json'


module  WebPuppet
  class App
    def call env

      Puppet[:config] = "/etc/puppet/puppet.conf"
      Puppet.parse_config

      Puppet[:clientyamldir] = "$yamldir"
      Puppet::Node.indirection.terminus_class = :yaml

      nodes = Puppet::Node.indirection.search("*")

      data = {}
      nodes.each do |n|
        facts = Puppet::Node::Facts.indirection.find(n.name)
        tags = Puppet::Resource::Catalog.indirection.find(n.name).tags
        data[n.name] = {
          :facts => facts.values,
          :tags => tags
        }
      end

      response = Rack::Response.new
      response.header['Content-Type'] = 'application/json'
      response.write JSON.pretty_generate(data)
      response.finish
    end

    def add_auth(conf)
      application = Rack::Auth::Basic.new(self) do |username, password|
        stored_username = conf.get_value('username')
        username_check = stored_username ? stored_username == username : true
        password_check = conf.get_value('password') == password
        username_check && password_check
      end
      application.realm = 'Web Puppet'
      application
    end

    def self.run!(options)
      application = self.new

      daemonize = options[:daemonize]
      port = options[:port]

      if options[:config]
        conf = ParseConfig.new(options[:config])
        application = application.add_auth(conf) if conf.get_value('password')
        daemonize = conf.get_value('daemonize') ? conf.get_value('daemonize') == "true" : daemonize
        port = conf.get_value('port') ? conf.get_value('port') : port
      end

      Rack::Server.new(:app => application, :Port => port, :daemonize => daemonize).start

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
web-puppet-0.1.0 lib/web-puppet.rb