Last active
December 27, 2015 19:19
-
-
Save binford2k/7376203 to your computer and use it in GitHub Desktop.
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 | |
require 'yaml' | |
require 'syslog' | |
require 'net/https' | |
FOREMAN = 'http://foreman.server.org' | |
MASTER = 'puppet.master.org' | |
RAKEAPI = "ssh #{MASTER} /opt/puppet/bin/rake -f /opt/puppet/share/puppet-dashboard/Rakefile RAILS_ENV=production node:add['%name',,'%classes']" | |
GITROOT = '/var/repos' | |
event, host = ARGV | |
uri = URI.parse("#{FOREMAN}/#{host}") | |
http = Net::HTTP.new(uri.host, uri.port) | |
http.use_ssl = true | |
http.verify_mode = OpenSSL::SSL::VERIFY_NONE | |
ENV['HOME'] = '/root' | |
ENV['PATH'] = '/sbin:/usr/sbin:/bin:/usr/bin:/opt/puppet/bin' | |
begin | |
request = Net::HTTP::Get.new(uri.request_uri) | |
request['Accept'] = 'text/yaml' | |
encdata = YAML.load(http.request(request).body) | |
name = encdata['name'] | |
env = encdata['environment'] | |
classes = encdata['classes'].join(',') | |
command = RAKEAPI.gsub(/%\w+/, { '%name' => name, '%classes' => classes }) | |
system(command) | |
File.open ( "#{GITROOT}/host/#{name}.yaml", "w" ) do |f| | |
hieradata = { | |
'target_ipaddress' => encdata['parameters']['target_ipaddress'], # or wherever you want ze infos to come from | |
'target_hostname' => encdata['parameters']['target_hostname'], | |
} | |
f.write(hieradata.to_yaml) | |
} | |
# if you decide to allow this to deploy to a given environment | |
# if so, remember to change the push below to push that environment branch | |
#system("git --git-dir #{GITROOT} checkout #{env}") | |
system("git --git-dir #{GITROOT} add host/#{name}.yaml") | |
system("git --git-dir #{GITROOT} commit -m 'Provisioned new host: #{name}'") | |
# you may end up having to pass -f to force. I don't think so | |
system("git --git-dir #{GITROOT} push origin master") | |
rescue Exception => e | |
Syslog.open('Foreman Scraper', Syslog::LOG_PID | Syslog::LOG_CONS) do |syslog| | |
syslog.warning "Configuration error for '#{ARGV.first}': #{e.message}" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment