Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#
# Copyright 2011, James Turnbull
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
require 'net/http'
require 'rubygems'
require 'xmlsimple'
require 'yaml'
# Retrieve node name from arguments
hostname = ARGV[0]
# If no node name is passed exit with 1
if hostname == nil
puts "No node specified"
exit(1)
end
# Update this to the URL of your Yana server
YANA_URL = 'http://localhost:8080/yana/api/nodes'
# Retrieve the list of nodes and convert from XML
yana = XmlSimple.xml_in(Net::HTTP.get_response(URI.parse(YANA_URL)).body)
nodes = yana["node"]
# Identify if a node matches the hostname passed in from Puppet
node = nodes.detect {|n| n["hostname"].to_s == hostname }
# If no node exists then exit with 1
if node == nil
puts "No node #{hostname} found"
exit(1)
end
# Otherwise process the node and configure the output
# Declare our variables
output = {}
classes = []
parameters = {}
# Turn Yana tags into classes
node["tags"][0]["tag"].each do |t|
classes << t["name"].to_s
end
# Turn Yana attributes into parameters
node["attributes"][0]["attribute"].each do |a|
parameters[a["name"].to_s] = a["value"][0].to_str
end
# Turn some remaining Yana variables into parameters
parameters["osName"] = node["osName"][0]
parameters["osFamily"] = node["osFamily"][0]
parameters["description"] = node["description"][0]
# Build our output hash
output["classes"] = classes
output["parameters"] = parameters
# Return YAML hash
puts output.to_yaml
exit(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.