Last active
October 13, 2015 21:58
-
-
Save rbirnie/4261855 to your computer and use it in GitHub Desktop.
fact_importer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env ruby | |
# | |
# This scripts runs on remote puppetmasters that you wish to import their nodes facts into Foreman | |
# it uploads all of the new facts its encounter based on a control file which is stored in /tmp directory. | |
# This script can run in cron, e.g. once every minute | |
# if you run it on many puppetmasters at the same time, you might consider adding something like: | |
# sleep rand(10) # that not all PM hammers the DB at once. | |
# [email protected] | |
# note this requires ruby 1.8.7+ | |
# puppet config dir | |
puppetdir="/var/lib/puppet" | |
# URL where Foreman lives | |
url="https://foreman" | |
# Temp file keeping the last run time | |
stat_file = "/tmp/foreman_fact_import" | |
require 'fileutils' | |
require 'net/http' | |
require 'net/https' | |
require 'uri' | |
require 'thread' | |
last_run = File.exists?(stat_file) ? File.stat(stat_file).mtime.utc : Time.now - 365*60*60 | |
facts = Dir["#{puppetdir}/yaml/facts/*.yaml"] | |
def process(filename, last_run) | |
last_fact = File.stat(filename).mtime.utc | |
if last_fact > last_run | |
fact = File.read(filename) | |
puts "Importing #{filename}" | |
begin | |
uri = URI.parse(url) | |
http = Net::HTTP.new(uri.host, uri.port) | |
if uri.scheme == 'https' then | |
http.use_ssl = true | |
http.verify_mode = OpenSSL::SSL::VERIFY_NONE | |
end | |
req = Net::HTTP::Post.new("/fact_values/create?format=yml") | |
req.set_form_data({'facts' => fact}) | |
response = http.request(req) | |
rescue Exception => e | |
raise "Could not send facts to Foreman: #{e}" | |
end | |
end | |
end | |
queue = Queue.new | |
facts.each{|e| queue << e } | |
threads = [] | |
16.times do | |
threads << Thread.new do | |
while (e = queue.pop(true) rescue nil) | |
process(e, last_run) | |
end | |
end | |
end | |
threads.each {|t| t.join } | |
puts "All Threads Joined. Processing Done" | |
FileUtils.touch stat_file |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment