Skip to content

Commit

Permalink
Use Chef REST API
Browse files Browse the repository at this point in the history
  • Loading branch information
Joseph Martin committed Aug 18, 2013
1 parent 12a8c79 commit d122c21
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 11 deletions.
20 changes: 16 additions & 4 deletions bin/chef-rundeck
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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
Expand Down
25 changes: 18 additions & 7 deletions lib/chef-rundeck.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
require 'chef'
require 'chef/node'
require 'chef/mixin/xml_escape'
require 'chef/rest'

class ChefRundeck < Sinatra::Base

Expand All @@ -26,38 +27,48 @@ 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 = '<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE project PUBLIC "-//DTO Labs Inc.//DTD Resources Document 1.0//EN" "project.dtd"><project>'
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
#++
os_family = node[:kernel][:os] =~ /windows/i ? 'windows' : 'unix'
response << <<-EOH
<node name="#{xml_escape(node[:fqdn])}"
type="Node"
description="#{xml_escape(node.name)}"
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[:tags].join(','), node.run_list.roles.join(',')].join(','))}"
username="#{xml_escape(ChefRundeck.username)}"
hostname="#{xml_escape(node[:fqdn])}"
editUrl="#{xml_escape(ChefRundeck.web_ui_url)}/nodes/#{xml_escape(node.name)}/edit"/>
editUrl="#{xml_escape(ChefRundeck.web_ui_url)}/nodes/#{xml_escape(node_name)}/edit"/>
EOH
end
response << "</project>"
Expand Down

0 comments on commit d122c21

Please sign in to comment.