tayu 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/bin/tayu +67 -0
  2. data/config/tayu.yml +6 -0
  3. data/lib/tayu.rb +122 -0
  4. metadata +80 -0
data/bin/tayu ADDED
@@ -0,0 +1,67 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # Copyright 2012, Adrian van Dongen
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+ #
17
+
18
+ $:.unshift(File.expand_path(File.join(File.dirname(__FILE__), "..", "lib")))
19
+
20
+ require 'yaml'
21
+ require 'optparse'
22
+ require 'tayu'
23
+
24
+ options = {}
25
+
26
+ optparse = OptionParser.new do |opts|
27
+ # Set a banner, displayed at the top
28
+ # of the help screen.
29
+ opts.banner = 'Usage: tayu [options] ...'
30
+
31
+ opts.separator ''
32
+ opts.separator 'Configuration options:'
33
+
34
+ options[:config_file] = File.join(File.expand_path(File.dirname(__FILE__)), "..", "config/tayu.yml")
35
+ opts.on( '-c', '--config FILE', 'Specify the location of your tayu configuration file') do |config_file|
36
+ options[:config_file] = config_file
37
+ end
38
+
39
+ options[:port] = 8144
40
+ opts.on( '-p', '--port PORT', 'The port to run tayu on') do |port|
41
+ options[:port] = port
42
+ end
43
+
44
+ opts.separator ""
45
+ opts.separator "Common options:"
46
+
47
+ opts.on_tail('-h', '--help', 'Display this screen' ) do
48
+ puts opts
49
+ exit
50
+ end
51
+ end
52
+
53
+ begin
54
+ optparse.parse!
55
+ config = YAML.load_file(options[:config_file])
56
+
57
+ Tayu.puppetdb_server = config[:puppetdb_server]
58
+ Tayu.puppetdb_port = config[:puppetdb_port]
59
+ Tayu.allowed_tags = config[:allowed_tags].split(' ')
60
+ Tayu.username = config[:username]
61
+ Tayu.run! :host => "localhost", :port => options[:port]
62
+ rescue OptionParser::InvalidArgument, OptionParser::InvalidOption, OptionParser::MissingArgument
63
+ puts $!.to_s
64
+ puts optparse
65
+ exit
66
+ end
67
+
data/config/tayu.yml ADDED
@@ -0,0 +1,6 @@
1
+ :puppetdb_server: localhost
2
+ :puppetdb_port: 8080
3
+ :username: rundeck
4
+ :allowed_tags:
5
+ location
6
+ updateschedule
data/lib/tayu.rb ADDED
@@ -0,0 +1,122 @@
1
+ #
2
+ # Copyright 2012, Adrian van Dongen
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ #
16
+
17
+ require 'sinatra'
18
+ require 'json'
19
+ require 'rest_client'
20
+ require 'builder/xchar'
21
+
22
+ class Tayu < Sinatra::Base
23
+
24
+ class << self
25
+ attr_accessor :puppetdb_server
26
+ attr_accessor :puppetdb_port
27
+ attr_accessor :allowed_tags
28
+ attr_accessor :username
29
+ end
30
+
31
+ ## convert to string and then to XML escaped text.
32
+ def xml_escape(input)
33
+ return input.to_s.to_xs
34
+ end
35
+
36
+ ## retrieve all nodes from puppetdb and return then as an array
37
+ def get_nodes
38
+ q = '["=", ["node", "active"], true]'
39
+
40
+ if ! q.is_a? String then
41
+ q=JSON[q]
42
+ end
43
+ params = {:query => q}
44
+ puts q
45
+ puts params
46
+
47
+ response_nodelist = RestClient.get"http://#{Tayu.puppetdb_server}:#{Tayu.puppetdb_port}/nodes", { :accept => :json, :params => params }
48
+ return JSON.parse(response_nodelist)
49
+ end
50
+
51
+ ## retrieve all facts for specified node and return them as an hash
52
+ def get_facts(nodename)
53
+ facts_puppetdb = Hash.new
54
+ response_facts = RestClient.get"http://#{Tayu.puppetdb_server}:#{Tayu.puppetdb_port}/facts/" + nodename, { :accept => :json}
55
+
56
+ # json string holds two object, we only need te facts object
57
+ json_object = JSON.parse(response_facts)
58
+ json_object.each do |key, value|
59
+ if key == 'facts'
60
+ facts_puppetdb = value
61
+ end
62
+ end
63
+ return facts_puppetdb
64
+ end
65
+
66
+ ## generate xml document body based on retrieved nodes and there facts
67
+ def generate_body
68
+ 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)
69
+
70
+ # for each node get the facts
71
+ get_nodes.each do |node|
72
+ facts = get_facts(node)
73
+ tags_config = Tayu.allowed_tags
74
+ tags_rundeck = Array.new
75
+ os_family = facts["kernel"] =~ /windows/i ? 'windows' : 'unix'
76
+
77
+ # check if value is empty if not add to array
78
+ tags_config.each do |tag|
79
+ if !facts[tag].nil?
80
+ tags_rundeck << facts[tag]
81
+ end
82
+ end
83
+
84
+ # xml body
85
+ xmlbody << <<-EOH
86
+ <node name="#{xml_escape(facts["fqdn"])}"
87
+ type="Node"
88
+ description="#{xml_escape(facts["fqdn"])}"
89
+ osArch="#{xml_escape(facts["kernel"])}"
90
+ osFamily="#{xml_escape(os_family)}"
91
+ osName="#{xml_escape(facts["operatingsystem"])}"
92
+ tags="#{xml_escape(tags_rundeck.join(','))}"
93
+ osVersion="#{xml_escape(facts["operatingsystemrelease"])}"
94
+ username="#{xml_escape(Tayu.username)}"
95
+ hostname="#{xml_escape(facts["fqdn"])}"/>
96
+ EOH
97
+ end
98
+
99
+ # close xml body and return results
100
+ xmlbody << "</project>"
101
+ return xmlbody
102
+ end
103
+
104
+ ## index page
105
+ get '/' do
106
+
107
+ # set document type to xml
108
+ response['Content-Type'] = 'text/xml'
109
+
110
+ # Generate xml body and display it
111
+ body = generate_body
112
+ body
113
+
114
+ end
115
+
116
+ ## 404 page
117
+ not_found do
118
+ status 404
119
+ 'page not found'
120
+ end
121
+
122
+ end
metadata ADDED
@@ -0,0 +1,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tayu
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Adrian van Dongen
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-19 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: sinatra
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: builder
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: 2.0.0
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: 2.0.0
46
+ description: Provides a resource endpoint for RunDeck from a PuppetDB server
47
+ email: sirhopcount@goodfellasonline.nl
48
+ executables:
49
+ - tayu
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - lib/tayu.rb
54
+ - config/tayu.yml
55
+ - bin/tayu
56
+ homepage: http://github.com/sirhopcount/tayu
57
+ licenses: []
58
+ post_install_message:
59
+ rdoc_options: []
60
+ require_paths:
61
+ - lib
62
+ required_ruby_version: !ruby/object:Gem::Requirement
63
+ none: false
64
+ requirements:
65
+ - - ! '>='
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ none: false
70
+ requirements:
71
+ - - ! '>='
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ requirements: []
75
+ rubyforge_project:
76
+ rubygems_version: 1.8.24
77
+ signing_key:
78
+ specification_version: 3
79
+ summary: Intergrates PuppetDB with Rundeck
80
+ test_files: []