|
| 1 | +#!/usr/bin/env ruby |
| 2 | + |
| 3 | +# ------------------------------------------------------------------------------------ |
| 4 | +# This is a script that demonstrates the use of the OctocatalogDiff::API::V1.catalog_diff |
| 5 | +# method to compare two catalogs. |
| 6 | +# |
| 7 | +# Run this with: |
| 8 | +# bundle exec examples/api/v1/catalog-diff-local-files.rb |
| 9 | +# |
| 10 | +# In this example, we'll compare two catalogs, based on branches from a git repository. |
| 11 | +# ------------------------------------------------------------------------------------ |
| 12 | + |
| 13 | +# Once you have installed the gem, you want this: |
| 14 | +# |
| 15 | +# require 'octocatalog-diff' |
| 16 | +# |
| 17 | +# To make the script run correctly from within the `examples` directory, this locates |
| 18 | +# the `octocatalog-diff` gem directly from this checkout. |
| 19 | +require_relative '../../../lib/octocatalog-diff' |
| 20 | + |
| 21 | +# Here are a few variables we'll use to compile this catalog. To ensure that this is a |
| 22 | +# working script, it will use resources from the test fixtures. You will need adjust these |
| 23 | +# to the actual values in your application. |
| 24 | +FACT_FILE = File.expand_path('../../../spec/octocatalog-diff/fixtures/facts/facts.yaml', File.dirname(__FILE__)) |
| 25 | +NODE = 'rspec-node.github.net'.freeze |
| 26 | +GIT_TARBALL = File.expand_path('../../../spec/octocatalog-diff/fixtures/git-repos/simple-repo.tar', File.dirname(__FILE__)) |
| 27 | +PUPPET_BINARY = File.expand_path('../../../script/puppet', File.dirname(__FILE__)) |
| 28 | + |
| 29 | +# Before we get started with the demo, I need to extract the tarball to create the git repo. |
| 30 | +# You won't be doing this in your own script, so as the great and powerful Oz says, "pay no |
| 31 | +# attention to the man behind the curtain." |
| 32 | +require 'fileutils' |
| 33 | +git_repo = Dir.mktmpdir |
| 34 | +at_exit { FileUtils.remove_entry_secure git_repo if File.directory?(git_repo) } |
| 35 | +system "tar -C '#{git_repo}' -xf '#{GIT_TARBALL}'" |
| 36 | + |
| 37 | +# Just FYI, let's show you the checked out git repo. |
| 38 | +puts 'Here is the directory containing the git repository.' |
| 39 | +puts '$ ls -lR' |
| 40 | +system "cd '#{git_repo}/git-repo' && ls -lR" |
| 41 | + |
| 42 | +puts '' |
| 43 | +puts '$ git branch' |
| 44 | +system "cd '#{git_repo}/git-repo' && git branch" |
| 45 | + |
| 46 | +# Set up a logger |
| 47 | +strio = StringIO.new |
| 48 | +logger = Logger.new strio |
| 49 | + |
| 50 | +# To get the catalog differences, call the octocatalog-diff API. |
| 51 | +puts '' |
| 52 | +puts 'Please wait a few seconds for the catalogs to be compiled...' |
| 53 | +result = OctocatalogDiff::API::V1.catalog_diff( |
| 54 | + basedir: File.join(git_repo, 'git-repo'), |
| 55 | + to_env: 'test-branch', |
| 56 | + from_env: 'master', |
| 57 | + fact_file: FACT_FILE, |
| 58 | + to_hiera_config: 'config/hiera.yaml', # Relative to the checkout, and only for the "to" catalog |
| 59 | + to_hiera_path: 'hieradata', # Relative to checkout, and only for the "to" catalog |
| 60 | + enc: 'config/enc.sh', # Relative to checkout |
| 61 | + bootstrap_script: 'script/bootstrap.sh', # Relative to checkout |
| 62 | + node: NODE, |
| 63 | + puppet_binary: PUPPET_BINARY, |
| 64 | + ignore: [ |
| 65 | + { type: Regexp.new('\AClass\z'), title: Regexp.new('.*') } # Ignore all type=Class resources |
| 66 | + ], |
| 67 | + logger: logger |
| 68 | +) |
| 69 | + |
| 70 | +# Print the log |
| 71 | +puts strio.string |
| 72 | + |
| 73 | +# The `catalog-diff-local-files.rb` example explores the data structures of the |
| 74 | +# return values. Here, simply report on the results. |
| 75 | + |
| 76 | +puts '' |
| 77 | +puts "The from-catalog has #{result.from.resources.size} resources" |
| 78 | +# In the catalog, each resource is a hash, and the keys are strings not symbols. |
| 79 | +result.from.resources.each do |resource| |
| 80 | + puts " #{resource['type']}[#{resource['title']}]" |
| 81 | +end |
| 82 | + |
| 83 | +puts "The to-catalog has #{result.to.resources.size} resources" |
| 84 | +result.to.resources.each do |resource| |
| 85 | + puts " #{resource['type']}[#{resource['title']}]" |
| 86 | +end |
| 87 | + |
| 88 | +puts "There are #{result.diffs.size} differences" |
| 89 | + |
| 90 | +result.diffs.each do |diff| |
| 91 | + if diff.addition? |
| 92 | + puts "Added a #{diff.type} resource called #{diff.title}!" |
| 93 | + elsif diff.removal? |
| 94 | + puts "Removed a #{diff.type} resource called #{diff.title}!" |
| 95 | + elsif diff.change? |
| 96 | + puts "Changed the #{diff.type} resource #{diff.title} attribute #{diff.structure.join('::')}" |
| 97 | + puts " from #{diff.old_value.inspect} to #{diff.new_value.inspect}" |
| 98 | + else |
| 99 | + puts 'This is a bug - each entry is an addition, removal, or change.' |
| 100 | + end |
| 101 | +end |
0 commit comments