From d122c213680b8c3ce3afa85a316d7849e72c5c48 Mon Sep 17 00:00:00 2001 From: Joseph Martin Date: Sun, 18 Aug 2013 09:17:00 -0500 Subject: [PATCH] Use Chef REST API --- bin/chef-rundeck | 20 ++++++++++++++++---- lib/chef-rundeck.rb | 25 ++++++++++++++++++------- 2 files changed, 34 insertions(+), 11 deletions(-) diff --git a/bin/chef-rundeck b/bin/chef-rundeck index 154bf2f..2890429 100755 --- a/bin/chef-rundeck +++ b/bin/chef-rundeck @@ -23,12 +23,22 @@ require 'mixlib/cli' class ChefRundeckCLI include Mixlib::CLI + + option :api_url, + :short => "-a API_URL", + :long => "--api-url API_URL", + :description => "The base URL of the Chef REST API" + + option :client_key, + :short => "-k KEYFILE", + :long => "--key-file KEYFILE", + :description => "The client.pem to sign requests" option :config_file, - :short => "-c CONFIG", - :long => "--config CONFIG", - :default => 'config.rb', - :description => "The Chef configuration file to use" + :short => "-c CONFIG", + :long => "--config CONFIG", + :default => 'config.rb', + :description => "The Chef configuration file to use" option :username, :short => "-u USERNAME", @@ -61,6 +71,8 @@ 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.api_url = cli.config[:api_url] +ChefRundeck.client_key = cli.config[:client_key] ChefRundeck.configure begin diff --git a/lib/chef-rundeck.rb b/lib/chef-rundeck.rb index 8b094f3..19cd234 100644 --- a/lib/chef-rundeck.rb +++ b/lib/chef-rundeck.rb @@ -18,6 +18,7 @@ require 'chef' require 'chef/node' require 'chef/mixin/xml_escape' +require 'chef/rest' class ChefRundeck < Sinatra::Base @@ -26,22 +27,32 @@ class ChefRundeck < Sinatra::Base class << self attr_accessor :config_file attr_accessor :username + attr_accessor :api_url attr_accessor :web_ui_url + attr_accessor :client_key def configure Chef::Config.from_file(ChefRundeck.config_file) - client = Chef::Client.new - client.run_ohai - client.register Chef::Log.level = Chef::Config[:log_level] + + unless ChefRundeck.api_url + ChefRundeck.api_url = Chef::Config[:chef_server_url] + end + + unless ChefRundeck.client_key + ChefRundeck.client_key = Chef::Config[:client_key] + end end end get '/' do content_type 'text/xml' response = '' - Chef::Node.list(true).each do |node_array| - node = node_array[1] + rest = Chef::REST.new(ChefRundeck.api_url, ChefRundeck.username, ChefRundeck.client_key) + nodes = rest.get_rest("/nodes/") + + nodes.keys.each do |node_name| + node = rest.get_rest("/nodes/#{node_name}") #-- # Certain features in Rundeck require the osFamily value to be set to 'unix' to work appropriately. - SRK #++ @@ -49,7 +60,7 @@ def configure response << <<-EOH + editUrl="#{xml_escape(ChefRundeck.web_ui_url)}/nodes/#{xml_escape(node_name)}/edit"/> EOH end response << ""