Skip to content

Instantly share code, notes, and snippets.

@nextrevision
Created April 20, 2015 20:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nextrevision/b3b58f8b5f729ac5468e to your computer and use it in GitHub Desktop.
Save nextrevision/b3b58f8b5f729ac5468e to your computer and use it in GitHub Desktop.
Compiles a puppet catalog without any puppet config files
# Requires automysqlbackup and stdlib module to be a child
# directory of the CWD
require "puppet"
require "json"
def run
manifest_path = './manifests/site.pp'
module_paths = ['../','./']
config_dir = nil
hiera_config = nil
verbose = 1
Puppet.initialize_settings
if config_dir
Puppet.settings.handlearg("--confdir", config_dir)
end
if verbose == 1
Puppet::Util::Log.newdestination(:console)
Puppet::Util::Log.level = :debug
end
Puppet.settings.handlearg("--config", ".")
Puppet.settings.handlearg("--config", ".")
Puppet.settings.handlearg("--manifest", manifest_path)
module_path = module_paths.join(":")
Puppet.settings.handlearg("--modulepath", module_path)
if hiera_config
raise ArgumentError, "[ERROR] hiera_config (#{hiera_config}) does not exist" if !FileTest.exist?(hiera_config)
Puppet.settings[:hiera_config] = hiera_config
end
nodename = get_nodename
facts_val = get_facts(nodename)
Puppet[:code] = "include automysqlbackup\n"
begin
compile_catalog(facts_val['hostname'], facts_val)
rescue => e
puts e.message
end
end
def get_nodename
Puppet[:certname]
end
def get_facts(node)
facts_val = {
'clientversion' => Puppet::PUPPETVERSION,
'environment' => 'production',
'hostname' => node.split('.').first,
'fqdn' => node,
'domain' => node.split('.', 2).last,
'clientcert' => node
}
facts_val
end
def compile_catalog(node_fqdn, facts = {})
hostname = node_fqdn.split('.').first
facts['hostname'] = hostname
node = Puppet::Node.new(hostname)
node.merge(facts)
Puppet::Parser::Compiler.compile(node)
end
catalog = run
json = catalog.to_data_hash.to_json
puts (JSON.pretty_generate JSON.parse(json))
vardir = Dir.mktmpdir
File.open("#{vardir}/catalog.json", "w").write(json)
File.open("#{vardir}/catalog.dot", "w").write(catalog.to_dot)
puts "# Wrote #{vardir}/catalog.{dot,json}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment