|
| 1 | +#!/usr/bin/env ruby |
| 2 | + |
| 3 | +# ------------------------------------------------------------------------------------ |
| 4 | +# This is a script that demonstrates the use of the OctocatalogDiff::API::V1.catalog |
| 5 | +# method to build a catalog. |
| 6 | +# |
| 7 | +# Run this with: |
| 8 | +# bundle exec examples/api/v1/catalog-builder-local-files.rb |
| 9 | +# ------------------------------------------------------------------------------------ |
| 10 | + |
| 11 | +# Once you have installed the gem, you want this: |
| 12 | +# |
| 13 | +# require 'octocatalog-diff' |
| 14 | +# |
| 15 | +# To make the script run correctly from within the `examples` directory, this locates |
| 16 | +# the `octocatalog-diff` gem directly from this checkout. |
| 17 | +require_relative '../../../lib/octocatalog-diff' |
| 18 | + |
| 19 | +# Here are a few variables we'll use to compile this catalog. To ensure that this is a |
| 20 | +# working script, it will use resources from the test fixtures. You will need adjust these |
| 21 | +# to the actual values in your application. |
| 22 | + |
| 23 | +FACT_FILE = File.expand_path('../../../spec/octocatalog-diff/fixtures/facts/facts.yaml', File.dirname(__FILE__)) |
| 24 | +HIERA_CONFIG = File.expand_path('../../../spec/octocatalog-diff/fixtures/repos/default/config/hiera.yaml', File.dirname(__FILE__)) |
| 25 | +NODE = 'rspec-node.github.net'.freeze |
| 26 | +PUPPET_REPO = File.expand_path('../../../spec/octocatalog-diff/fixtures/repos/default', File.dirname(__FILE__)) |
| 27 | +PUPPET_BINARY = File.expand_path('../../../script/puppet', File.dirname(__FILE__)) |
| 28 | + |
| 29 | +# To compile this catalog, call the octocatalog-diff API. |
| 30 | +catalog = OctocatalogDiff::API::V1.catalog( |
| 31 | + bootstrapped_to_dir: PUPPET_REPO, |
| 32 | + fact_file: FACT_FILE, |
| 33 | + hiera_config: HIERA_CONFIG, |
| 34 | + hiera_path: 'hieradata', |
| 35 | + node: NODE, |
| 36 | + puppet_binary: PUPPET_BINARY |
| 37 | +) |
| 38 | + |
| 39 | +# Let's see what kind of an object we got... |
| 40 | +# #=> OctocatalogDiff::API::V1::Catalog |
| 41 | +puts "Object returned from OctocatalogDiff::API::V1.catalog is: #{catalog.class}" |
| 42 | + |
| 43 | +# If it's not valid, the error message will be available. |
| 44 | +unless catalog.valid? |
| 45 | + puts 'The catalog is not valid.' |
| 46 | + puts catalog.error_message |
| 47 | + exit 1 |
| 48 | +end |
| 49 | + |
| 50 | +# If it is valid, many other methods may be of interest. |
| 51 | +puts 'The catalog is valid.' |
| 52 | + |
| 53 | +# We can determine which backend was used to build it. With the arguments in this example, |
| 54 | +# the catalog was computed. |
| 55 | +# #=> OctocatalogDiff::Catalog::Computed |
| 56 | +puts "The catalog was built using #{catalog.builder}" |
| 57 | + |
| 58 | +# We can determine which directory was used as the temporary compilation directory. |
| 59 | +# This is only defined for the Computed backend. |
| 60 | +puts "Puppet was run in #{catalog.compilation_dir}" |
| 61 | + |
| 62 | +# We can report on which version of Puppet was run. This is only defined for the |
| 63 | +# Computed backend. |
| 64 | +puts "The version of Puppet used was #{catalog.puppet_version}" |
| 65 | + |
| 66 | +# We can get all resources in the catalog via the .resources method, which returns |
| 67 | +# an Array<Hash>. The Hash comes directly from the catalog structure. |
| 68 | +puts "There is/are #{catalog.resources.count} resource(s) in this catalog" |
| 69 | +catalog.resources.each do |resource| |
| 70 | + puts "- #{resource['type']} - #{resource['title']}" |
| 71 | +end |
| 72 | + |
| 73 | +# You can also locate a resource by its type and title. We'll choose an element at |
| 74 | +# random from the array. We will then locate it via the `.resource` method. |
| 75 | +selected_resource = catalog.resources.sample |
| 76 | +puts "Randomly selected type=#{selected_resource['type']} title=#{selected_resource['title']}" |
| 77 | + |
| 78 | +param = { type: selected_resource['type'], title: selected_resource['title'] } |
| 79 | +looked_up_resource = catalog.resource(param) |
| 80 | +puts "Looked up using catalog.resource: type=#{looked_up_resource['type']}, title=#{looked_up_resource['title']}" |
| 81 | + |
| 82 | +if selected_resource == looked_up_resource |
| 83 | + puts 'The resources are equal!' |
| 84 | +else |
| 85 | + # If this happens, it's a bug - please report it to us! |
| 86 | + puts 'The resources do not match!' |
| 87 | +end |
| 88 | + |
| 89 | +# If we want the JSON representation of the catalog, we can get that too. You'd |
| 90 | +# normally want to write this out to a file. We'll just print the first 80 characters. |
| 91 | +json_text = catalog.to_json |
| 92 | +puts "The JSON representation of the catalog is #{json_text.length} characters long" |
| 93 | +puts json_text[0..80] |
0 commit comments