Last active
May 14, 2018 20:05
-
-
Save Sharpie/da84e70331c29ee760504e63ea0d323a to your computer and use it in GitHub Desktop.
Ruby script for retrieving Puppet Catalogs
This file contains 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
#!/opt/puppetlabs/puppet/bin/ruby | |
# This script executes the front end of a `puppet agent` run. Specifically it: | |
# | |
# - loads settings | |
# - ensures a SSL certificate is presant | |
# - syncs plugins | |
# - retrieves a catalog | |
require 'puppet' | |
require 'puppet/face' | |
LOG_LEVEL = ARGV.find {|e| e.match('log_level')} | |
# Initialize Puppet settings. | |
# | |
# Any puppet.conf setting can be passed to this script and will | |
# be initialized via ARGV. | |
Puppet.initialize_settings(ARGV) | |
# Shift to agent run mode. | |
run_mode = Puppet::Util::RunMode[:agent] | |
app_defaults = Puppet::Settings.app_defaults_for_run_mode(run_mode) | |
app_defaults.merge!({:catalog_terminus => :rest, | |
:facts_terminus => :facter}) | |
Puppet.settings.initialize_app_defaults(app_defaults) | |
# :not_required indicates our environment mode --- we don't require a local | |
# copy of the environment to exist in order to retrieve a catalog. | |
Puppet::ApplicationSupport.push_application_context(run_mode, :not_required) | |
# Set up logging, ensure required directories exist, and set CA location. | |
Puppet::Util::Log.newdestination(:console) | |
# Default logging to --verbose unless set via --log_level | |
Puppet::Util::Log.level = :info if LOG_LEVEL.nil? | |
Puppet.settings.use(:main, :agent, :ssl) | |
Puppet::SSL::Host.ca_location = :remote | |
errmsg = nil | |
pool = nil | |
begin | |
# Ensure we have a signed certificate. If not, we will generate a keypair, | |
# submit a CSR and wait the amount of time set by waitforcert | |
# (default 2 minutes). | |
host = Puppet::SSL::Host.new | |
host.wait_for_cert(Puppet[:waitforcert]) | |
# Configure re-usable HTTP connections. | |
pool = Puppet::Network::HTTP::Pool.new(Puppet[:http_keepalive_timeout]) | |
Puppet.push_context({http_pool: pool}) | |
# Determine pluginsync environment by contacting the ENC. | |
configured_environment = Puppet[:environment] if Puppet.settings.set_by_config?(:environment) | |
node = Puppet::Node.indirection.find(Puppet[:node_name_value], | |
environment: Puppet::Node::Environment.remote(Puppet[:environment]), | |
configured_environment: configured_environment, | |
ignore_cache: true, | |
fail_on_404: false) | |
enc_environment = if (node && node.has_environment_instance?) | |
node.environment | |
elsif (node && node.environment_name) | |
Puppet::Node::Environment.remote(node.environment_name) | |
else | |
Puppet::Node::Environment.remote(Puppet[:environment]) | |
end | |
Puppet.push_context({current_environment: enc_environment}) | |
Puppet[:environment] = enc_environment.name | |
# Sync plugins. | |
Puppet::Face[:plugin, '0.0.1'].download | |
# Get catalog | |
catalog = Puppet::Face[:catalog, '0.0.1'].find | |
# Manipulate catalog and extract resources here. | |
#require 'pry' | |
#binding.pry | |
rescue => e | |
errmsg = ["ERROR #{e.class}: #{e.message}", | |
e.backtrace].join("\n\t") | |
ensure | |
pool.close unless pool.nil? | |
end | |
if errmsg.nil? | |
exit 0 | |
else | |
$stderr.puts(errmsg) | |
exit 1 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment