Skip to content

Instantly share code, notes, and snippets.

@madAndroid
Created July 18, 2013 09:35
Show Gist options
  • Save madAndroid/6028037 to your computer and use it in GitHub Desktop.
Save madAndroid/6028037 to your computer and use it in GitHub Desktop.
# Copyright 2012-2013 The Scale Factory Limited. All rights reserved.
begin
require 'json'
require 'net/http'
rescue LoadError
retry if require 'rubygems'
raise
end
#################################################################################
# EC2 userdata fact finding
ec2_userdata = Facter.value('ec2_userdata')
if ec2_userdata
json = JSON.parse( ec2_userdata )
json.each do |key,val|
next unless key.start_with?('sf_')
Facter.add(key) do
has_weight(10)
setcode do
if json[key].respond_to?('join')
json[key].join(',')
else
val
end
end
end
end
end
#################################################################################
# JSON http response fact finding
if Facter.value('is_virtual') == 'true'
begin
## Check if we have provided a url for the http endpoint
if File.exists?( '/etc/sf_nodeless_url' )
url = File.open('/etc/sf_nodeless_url', 'rb') { |f| f.read }
uri = URI.parse( url )
http = Net::HTTP::new( uri.host, uri.port )
http.open_timeout = 3 # time out connects after 3s
request = Net::HTTP::Get.new( uri.request_uri )
response = http.request( request )
if response.code == "200"
json = JSON.parse( response.body )
json.each do |key,val|
next unless key.start_with?('sf_')
Facter.add(key) do
has_weight(10)
setcode do
if json[key].respond_to?('join')
json[key].join(',')
else
val
end
end
end
end
end
end
rescue Exception => e
# XXX Should we generate an error or log entry here? No other Facter
# code seems to do so, but it might be useful for debugging.
end
end
#################################################################################
# Fallback facts taken from config files. These have lower priority so get
# executed last.
Facter.add("sf_roles") do
has_weight(100)
setcode do
# Determine which config file to use. Use /etc/puppet/sf_roles.conf
# if that exists. If that doesn't exist, try /etc/sf_roles.conf.
roles_file = '/etc/sf_roles.conf';
if File.exists?( '/etc/puppet/sf_roles.conf' )
roles_file = '/etc/puppet/sf_roles.conf'
end
# Read roles from config file, one per line. Return
# then as a comma-separated list
if File.exists?( roles_file )
roles = []
File.open( roles_file, 'r') { |f|
f.each_line do |line|
roles << line.chomp
end
}
roles.join(',')
end
end
end
Facter.add("sf_environment") do
has_weight(100)
setcode do
# Determine which config file to use. Use /etc/puppet/sf_environment.conf
# if that exists. If that doesn't exist, try /etc/sf_location.conf.
environment_file = '/etc/sf_environment.conf';
if File.exists?( '/etc/puppet/sf_environment.conf' )
environment_file = '/etc/puppet/sf_environment.conf'
end
# Read environment from config file. There is only one.
if File.exists?( environment_file )
environment = nil
File.open( environment_file, 'r') { |f|
environment = f.readline.chomp
}
environment
end
end
end
Facter.add("sf_location") do
has_weight(100)
setcode do
# Determine which config file to use. Use /etc/puppet/sf_location.conf
# if that exists. If that doesn't exist, try /etc/sf_location.conf.
location_file = '/etc/sf_location.conf';
if File.exists?( '/etc/puppet/sf_location.conf' )
location_file = '/etc/puppet/sf_location.conf'
end
# Read location from config file. Again, there is only one.
if File.exists?( location_file )
location = nil
File.open( location_file, 'r') { |f|
location = f.readline.chomp
}
location
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment