Skip to content

Instantly share code, notes, and snippets.

@ccaum
Created March 15, 2011 03:22
Show Gist options
  • Save ccaum/870264 to your computer and use it in GitHub Desktop.
Save ccaum/870264 to your computer and use it in GitHub Desktop.
Confluence Puppet Doc Generator
require 'xmlrpc/client'
# A useful helper for running Confluence XML-RPC from Ruby. Takes care of
# adding the token to each method call (so you can call server.getSpaces()
# instead of server.getSpaces(token)). Also takes care of re-logging in
# if your login times out.
#
# Usage:
#
# server = Confluence::Server.new :server => 'http://confluence02.lightningsoure.com'
# server.login :username => 'username', :password => 'password'
# puts server.getSpaces()
#
# NOTE: The :server parameter for creating a Confluence::Server object is optional. If nothing if provided
# the default behavior is to connect to http:://confluence.lightningsource.com
module Confluence
class Server
def initialize(params = Hash.new)
unless params.keys.include? :server
raise "Parameter :server not found"
end
server_url = params[:server]
server_url += "/rpc/xmlrpc" unless server_url[-11..-1] == "/rpc/xmlrpc"
@server_url = server_url
server = XMLRPC::Client.new2(server_url)
@conf = server.proxy("confluence1")
@token = "12345"
end
def login(params = Hash.new)
@user = params[:username]
@pass = params[:password]
do_login()
end
def method_missing(method_name, *args)
begin
@conf.send(method_name, *([@token] + args))
rescue XMLRPC::FaultException => e
if (e.faultString.include?("InvalidSessionException"))
do_login
retry
else
raise e.faultString
end
end
end
def page_exists?(page)
getPageID(page) != nil
end
def storePage(page)
if page.is_a? Confluence::Page
if page_exists? page
newpage = Page.new
newpage.id = getPageID page
cpage = @conf.getPage(@token, page.space, page.title)
cpage.reject! { |k,v| k == 'permissions' }
cpage.each { |k,v| newpage.send( "#{k.to_sym}=", v) }
newpage.content = page.content
page = newpage
end
@conf.storePage(@token, page.to_h)
else
@conf.storePage(@token, page)
end
end
def getPageID(page)
id = nil
unless page.space.is_a? String
raise "No Space provided for page"
end
@conf.getPages(@token, page.space).each do |cpage|
if cpage['title'] == page.title
id = cpage['id'].to_i
end
end
id
end
private
def do_login()
begin
@token = @conf.login(@user, @pass)
rescue XMLRPC::FaultException => e
raise e.faultString
end
end
end
class Confluence::Page
attr_accessor :id, :space, :parentId, :title, :url, :version, :content, :created, :creator, :modified, :modifier, :homePage, :locks, :contentStatus, :current
def to_h
h = Hash.new
self.instance_variables.each do |var|
#Cut off the @ symobl on the instance variables
var = var[1..-1]
#Call the accessor method for the variable to get its value
h[var] = self.send(var.to_sym)
end
h
end
end
end
#!/usr/bin/ruby
require 'puppet'
require 'puppet/type'
require 'erb'
require 'lib/confluence4r'
include Confluence
confluence = Confluence::Server.new :server => 'http://confluence.domain.com'
confluence.login :username => 'puppet', :password => 'puppet_password'
Puppet::Type.loadall
Puppet::Type.eachtype{}.sort.each do |key,type|
erb = File.join(File.dirname(__FILE__), 'lib/type.erb')
page = Confluence::Page.new
page.space = 'puppet'
page.title = type.name.to_s
page.parentId = '6225986'
page.content = ERB.new( IO.read( erb )).result(binding)
confluence.storePage page
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment