-
Notifications
You must be signed in to change notification settings - Fork 271
/
push_facts.rb
executable file
·46 lines (40 loc) · 1.44 KB
/
push_facts.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#! /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.
# ohadlevy@gmail.com
# puppet config dir
puppetdir="/var/lib/puppet"
# URL where Foreman lives
url="http://foreman"
# Temp file keeping the last run time
stat_file = "/tmp/foreman_fact_importer"
require 'fileutils'
require 'net/http'
require 'net/https'
require 'uri'
last_run = File.exists?(stat_file) ? File.stat(stat_file).mtime.utc : Time.now - 365*60*60
Dir["#{puppetdir}/yaml/facts/*.yaml"].each do |filename|
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
FileUtils.touch stat_file