Class: Tayu

Inherits:
Sinatra::Base
  • Object
show all
Defined in:
lib/tayu.rb

Class Attribute Summary collapse

Instance Method Summary collapse

Class Attribute Details

.allowed_tagsObject

Returns the value of attribute allowed_tags.



27
28
29
# File 'lib/tayu.rb', line 27

def allowed_tags
  @allowed_tags
end

.puppetdb_portObject

Returns the value of attribute puppetdb_port.



26
27
28
# File 'lib/tayu.rb', line 26

def puppetdb_port
  @puppetdb_port
end

.puppetdb_serverObject

Returns the value of attribute puppetdb_server.



25
26
27
# File 'lib/tayu.rb', line 25

def puppetdb_server
  @puppetdb_server
end

.usernameObject

Returns the value of attribute username.



28
29
30
# File 'lib/tayu.rb', line 28

def username
  @username
end

Instance Method Details

#/Object

index page



103
104
105
106
107
108
109
110
111
112
# File 'lib/tayu.rb', line 103

get '/' do

    # set document type to xml
    response['Content-Type'] = 'text/xml'

    # Generate xml body and display it
    body = generate_body
    body

end

#generate_bodyObject

generate xml document body based on retrieved nodes and there facts



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/tayu.rb', line 65

def generate_body
    xmlbody = %Q(<?xml version="1.0" encoding="UTF-8"?>\n<!DOCTYPE project PUBLIC "-//DTO Labs Inc.//DTD Resources Document 1.0//EN" "project.dtd">\n<project>\n)

    # for each node get the facts 
    get_nodes.each do |node|
        facts = get_facts(node)
        tags_config = Tayu.allowed_tags 
        tags_rundeck = Array.new 
        os_family = facts["kernel"] =~ /windows/i ? 'windows' : 'unix'

        # check if value is empty if not add to array
        tags_config.each do |tag|
            if !facts[tag].nil?
                tags_rundeck << facts[tag]
            end
        end

        # xml body
        xmlbody << <<-EOH
        <node name="#{xml_escape(facts["fqdn"])}"
            type="Node"
            description="#{xml_escape(facts["fqdn"])}"
            osArch="#{xml_escape(facts["kernel"])}"
            osFamily="#{xml_escape(os_family)}"
            osName="#{xml_escape(facts["operatingsystem"])}"
            tags="#{xml_escape(tags_rundeck.join(','))}"
            osVersion="#{xml_escape(facts["operatingsystemrelease"])}"
            username="#{xml_escape(Tayu.username)}"
            hostname="#{xml_escape(facts["fqdn"])}"/>
EOH
    end

    # close xml body and return results
    xmlbody << "</project>"
    return xmlbody
end

#get_facts(nodename) ⇒ Object

retrieve all facts for specified node and return them as an hash



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/tayu.rb', line 50

def get_facts(nodename)
    facts_puppetdb = Hash.new
    response_facts = RestClient.get"http://#{Tayu.puppetdb_server}:#{Tayu.puppetdb_port}/facts/" + nodename, { :accept => :json}

    # json string holds two object, we only need te facts object
    json_object = JSON.parse(response_facts)
    json_object.each do |key, value|
        if key == 'facts'
            facts_puppetdb = value
        end
    end 
    return facts_puppetdb
end

#get_nodesObject

retrieve all nodes from puppetdb and return then as an array



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/tayu.rb', line 37

def get_nodes
	q = '["=", ["node", "active"], true]'

	if ! q.is_a? String then
		q=JSON[q]
		end
	params = {:query => q}

    response_nodelist = RestClient.get"http://#{Tayu.puppetdb_server}:#{Tayu.puppetdb_port}/nodes",  { :accept => :json, :params => params }
    return JSON.parse(response_nodelist)
end

#xml_escape(input) ⇒ Object

convert to string and then to XML escaped text.



32
33
34
# File 'lib/tayu.rb', line 32

def xml_escape(input)
    return input.to_s.to_xs
end