Created
September 25, 2018 19:59
-
-
Save logicminds/8993ae91698694ad46058dee5d9a51f7 to your computer and use it in GitHub Desktop.
Code to consume a a catalog form a puppet serverless catalog compiler
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
#!/usr/bin/env ruby | |
require 'puppet' | |
require 'json' | |
require 'tmpdir' | |
require 'net/http' | |
require 'uri' | |
require 'puppet/configurer' | |
def retrieve_catalog | |
uri = URI.parse("https://api.faastruby.io/my-workspace2/example") | |
request = Net::HTTP::Post.new(uri) | |
request.body = "code=file{'/tmp/papplytest': ensure => present}" | |
req_options = { | |
use_ssl: uri.scheme == "https", | |
} | |
response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http| | |
http.request(request) | |
end | |
if response.code == '200' | |
JSON.parse(response.body) | |
else | |
puts "Response was #{response.code}" | |
puts JSON.parse(response.body) | |
exit 1 | |
end | |
end | |
def create_plugin_files(plugins, modules_path) | |
plugins.each do |path, content| | |
file = File.join(modules_path, path) | |
FileUtils.mkdir_p(File.dirname(file)) | |
File.write(file, content) | |
end | |
end | |
def read_catalog(text) | |
format = Puppet::Resource::Catalog.default_format | |
begin | |
catalog = Puppet::Resource::Catalog.convert_from(format, text) | |
rescue StandardError => detail | |
raise Puppet::Error, format(_("Could not deserialize catalog from %{format}: %{detail}"), format: format, detail: detail), detail.backtrace | |
end | |
catalog.to_ral | |
end | |
def run | |
data = retrieve_catalog | |
catalog = data['catalog'] | |
plugins = data['plugins'] | |
Puppet.initialize_settings | |
Dir.mktmpdir do |dir| | |
modules_path = File.join(dir, 'modules') | |
Puppet.settings[:basemodulepath] = Array(modules_path) | |
create_plugin_files(plugins, modules_path) | |
env = Puppet.lookup(:environments).get(Puppet[:environment]) | |
Puppet.override(current_environment: env, loaders: Puppet::Pops::Loaders.new(env)) do | |
catalog = read_catalog(catalog.to_json) | |
configurer = Puppet::Configurer.new | |
data = configurer.run(catalog: catalog, pluginsync: false) | |
end | |
end | |
end | |
run |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment