Skip to content

Commit 3cdfee0

Browse files
author
Kevin Paulisse
committed
Add examples
1 parent 271fabd commit 3cdfee0

File tree

2 files changed

+179
-0
lines changed

2 files changed

+179
-0
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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.
11+
# - The "from" catalog will be a catalog that has already been compiled, and exists as a JSON file.
12+
# We will use one of the JSON files from the spec fixtures.
13+
# - The "to" catalog will be compiled using Puppet from one of the spec fixtures.
14+
#
15+
# This example will NOT show integration with a git repository. The `catalog-diff-git-repo.rb` file in
16+
# this directory shows off those features.
17+
# ------------------------------------------------------------------------------------
18+
19+
# Once you have installed the gem, you want this:
20+
#
21+
# require 'octocatalog-diff'
22+
#
23+
# To make the script run correctly from within the `examples` directory, this locates
24+
# the `octocatalog-diff` gem directly from this checkout.
25+
require_relative '../../../lib/octocatalog-diff'
26+
27+
# Here are a few variables we'll use to compile this catalog. To ensure that this is a
28+
# working script, it will use resources from the test fixtures. You will need adjust these
29+
# to the actual values in your application.
30+
FACT_FILE = File.expand_path('../../../spec/octocatalog-diff/fixtures/facts/facts.yaml', File.dirname(__FILE__))
31+
HIERA_CONFIG = File.expand_path('../../../spec/octocatalog-diff/fixtures/repos/default/config/hiera.yaml', File.dirname(__FILE__))
32+
NODE = 'rspec-node.github.net'.freeze
33+
FROM_CATALOG = File.expand_path('../../../spec/octocatalog-diff/fixtures/catalogs/ignore-tags-new.json', File.dirname(__FILE__))
34+
PUPPET_REPO = File.expand_path('../../../spec/octocatalog-diff/fixtures/repos/ignore-tags-old', File.dirname(__FILE__))
35+
PUPPET_BINARY = File.expand_path('../../../script/puppet', File.dirname(__FILE__))
36+
37+
# To get the catalog differences, call the octocatalog-diff API.
38+
puts 'Please wait a few seconds for the catalogs to be compiled...'
39+
result = OctocatalogDiff::API::V1.catalog_diff(
40+
bootstrapped_to_dir: PUPPET_REPO,
41+
fact_file: FACT_FILE,
42+
from_catalog: FROM_CATALOG,
43+
hiera_config: HIERA_CONFIG,
44+
hiera_path: 'hieradata',
45+
node: NODE,
46+
puppet_binary: PUPPET_BINARY
47+
)
48+
49+
# We should get back a structure with 3 keys: `:diffs` will be an array of differences; `:from` will be the "from"
50+
# catalog (which in this case is taken directly from the JSON file), and `:to` will be the "to" catalog (which
51+
# in this case was compiled). We use 'Openstruct' so that you can treat it as a hash, or as an object with methods.
52+
puts "Object returned from OctocatalogDiff::API::V1.catalog_diff is: #{result.class}"
53+
54+
# Let's see what kind of objects we have. First, treating the result as a hash:
55+
puts "The keys are: #{result.to_h.keys.join(', ')}"
56+
[:diffs, :from, :to].each do |key|
57+
puts "result[:#{key}] is a(n) #{result[key].class}"
58+
end
59+
60+
# We can also use these as methods.
61+
[:diffs, :from, :to].each do |key|
62+
puts "result.#{key} is a(n) #{result.send(key).class}"
63+
end
64+
65+
# Let's inspect the :diffs array a bit.
66+
puts "The first element of result.diffs is a(n) #{result.send(:diffs).first.class}"
67+
puts "There are #{result[:diffs].size} diffs reported here"
68+
69+
# Let's print the first diff in JSON format
70+
d = result.diffs.first
71+
puts '--------------------------------------'
72+
puts 'The first diff is:'
73+
puts d.inspect
74+
75+
# Internally, the structure of the diff is an array
76+
puts '--------------------------------------'
77+
puts 'The raw object format of that diff is:'
78+
puts d.raw.inspect

0 commit comments

Comments
 (0)