Skip to content

Instantly share code, notes, and snippets.

@rottenbytes
Created June 21, 2012 08:35
Show Gist options
  • Save rottenbytes/2964601 to your computer and use it in GitHub Desktop.
Save rottenbytes/2964601 to your computer and use it in GitHub Desktop.
Add caching to @opscode/chef-rundeck
diff --git a/bin/chef-rundeck b/bin/chef-rundeck
index 5ea22d7..b665ca5 100755
--- a/bin/chef-rundeck
+++ b/bin/chef-rundeck
@@ -46,6 +46,12 @@ class ChefRundeckCLI
:long => "--port PORT",
:description => "The port to run on, default 9980",
:default => 9980
+
+ option :cache_file,
+ :short => "-f FILE",
+ :long => "--file FILE",
+ :description => "The cache file path",
+ :default => "/tmp/rundeck_chef_cache.xml"
end
cli = ChefRundeckCLI.new
@@ -54,6 +60,7 @@ cli.parse_options
ChefRundeck.config_file = cli.config[:config_file]
ChefRundeck.username = cli.config[:username]
ChefRundeck.web_ui_url = cli.config[:web_ui_url]
+ChefRundeck.cache_file = cli.config[:cache_file]
ChefRundeck.configure
ChefRundeck.run! :host => 'localhost', :port => cli.config[:port]
diff --git a/lib/chef-rundeck.rb b/lib/chef-rundeck.rb
index 7e95dc4..674003b 100644
--- a/lib/chef-rundeck.rb
+++ b/lib/chef-rundeck.rb
@@ -27,6 +27,7 @@ class ChefRundeck < Sinatra::Base
attr_accessor :config_file
attr_accessor :username
attr_accessor :web_ui_url
+ attr_accessor :cache_file
def configure
Chef::Config.from_file(ChefRundeck.config_file)
@@ -34,31 +35,58 @@ class ChefRundeck < Sinatra::Base
end
end
- get '/' do
- content_type 'text/xml'
- response = '<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE project PUBLIC "-//DTO Labs Inc.//DTD Resources Document 1.0//EN" "project.dtd"><project>'
+ def generate_xml
+ # get stuff
+ fp = open(ChefRundeck.cache_file,"w")
+ response = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><!DOCTYPE project PUBLIC \"-//DTO Labs Inc.//DTD Resources Document 1.0//EN\" \"project.dtd\">\n<project>\n"
+
Chef::Node.list(true).each do |node_array|
node = node_array[1]
#--
# Certain features in Rundeck require the osFamily value to be set to 'unix' to work appropriately. - SRK
#++
- os_family = node[:kernel][:os] =~ /windows/i ? 'windows' : 'unix'
- response << <<-EOH
+
+ begin
+ if node.name == node.fqdn then
+ os_family = node[:kernel][:os] =~ /windows/i ? 'windows' : 'unix'
+ tags = Array.new
+ tags << node.chef_environment
+ tags << node.run_list.roles
+ #tags="#{xml_escape([node.chef_environment, node.run_list.roles.join(',')].join(','))}"
+ response << <<-EOH
<node name="#{xml_escape(node[:fqdn])}"
- type="Node"
description="#{xml_escape(node.name)}"
osArch="#{xml_escape(node[:kernel][:machine])}"
osFamily="#{xml_escape(os_family)}"
osName="#{xml_escape(node[:platform])}"
osVersion="#{xml_escape(node[:platform_version])}"
- tags="#{xml_escape([node.chef_environment, node.run_list.roles.join(',')].join(','))}"
+ tags="#{xml_escape(tags.join(','))}"
username="#{xml_escape(ChefRundeck.username)}"
hostname="#{xml_escape(node[:fqdn])}"
editUrl="#{xml_escape(ChefRundeck.web_ui_url)}/nodes/#{xml_escape(node.name)}/edit"/>
EOH
+ else
+ #puts node.name + " has a diverging FQDN : " + node.fqdn
+ end
+ rescue Exception => e
+ #puts "oops " + node.name + " raised " + e.inspect
+ end
end
response << "</project>"
- response
+ fp.write(response)
+ fp.close
+ end
+
+ get '/' do
+ content_type 'text/xml'
+ unless File.exists?(ChefRundeck.cache_file)
+ generate_xml()
+ end
+
+ if Time.now.to_i - File.mtime(ChefRundeck.cache_file).to_i > 300 then
+ generate_xml()
+ end
+ File.read(ChefRundeck.cache_file)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment