Skip to content

Commit fe37f3e

Browse files
authored
Merge branch 'kpaulisse-release-0-7' into kpaulisse-spec-api-v1
2 parents d5bd021 + 1c2072f commit fe37f3e

15 files changed

Lines changed: 437 additions & 217 deletions

doc/dev/api/v1/calls/catalog.md

Lines changed: 100 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,111 @@
55
`catalog` returns an `OctocatalogDiff::Catalog` object built with the octocatalog-diff compiler, obtained from a Puppet server, or read in from a file. This is analogous to using the `--catalog-only` option with the octocatalog-diff command-line script.
66

77
```
8-
catalog_obj = OctocatalogDiff::API::V1.catalog(
9-
10-
)
8+
catalog_obj = OctocatalogDiff::API::V1.catalog(<Hash>)
9+
#=> OctocatalogDiff::API::V1::Catalog
1110
```
1211

13-
## Options
12+
The return value is an [`OctocatalogDiff::API::V1::Catalog`](/doc/dev/api/v1/objects/catalog.md) object.
13+
14+
For an example, see [catalog-builder-local-files.rb](/examples/api/v1/catalog-builder-local-files.rb).
1415

16+
## Parameters
1517

16-
**NOTE**: Additional options as described in the [options reference](/doc/optionsref.md) may also have an effect on catalog generation.
18+
The `catalog` method takes one argument, which is a Hash containing parameters.
1719

18-
## Return value
20+
The list of parameters here is not exhaustive. The `.catalog` method accepts most parameters described in [Configuration](/doc/configuration.md), [Building catalogs instead of diffing catalogs](/doc/advanced-catalog-only.md), and [Command line options reference](/doc/optionsref.md).
1921

20-
The return value is an [`OctocatalogDiff::API::V1::Catalog`](/doc/dev/api/v1/objects/catalog.md) object.
22+
It is also possible to use the parameters from [OctocatalogDiff::API::V1.config](/doc/dev/api/v1/calls/config.md) for the catalog compilation. Simply combine the hash returned by `.config` with any additional keys, and pass the merged hash to the `.catalog` method.
23+
24+
### Global parameters
25+
26+
#### `:logger` (Logger, Optional)
27+
28+
Debugging and informational messages will be logged to this object as the catalog is built.
29+
30+
If no Logger object is passed, these messages will be silently discarded.
31+
32+
#### `:node` (String, Required)
33+
34+
The node name whose catalog is to be compiled or obtained. This should be the fully qualified domain name that matches the node's name as seen in Puppet.
35+
36+
### Computed catalog parameters
37+
38+
#### `:basedir` (String, Optional)
39+
40+
Directory that contains a git repository with the Puppet code. Use in conjunction with `:to_branch` to specify the branch name that should be checked out.
41+
42+
If your Puppet code is not in a git repository, or you already have the branch checked out via some other process, use `:bootstrapped_to_dir` instead.
43+
44+
#### `:bootstrap_script` (String, Optional)
45+
46+
Path to a script to run after checking out the selected branch of Puppet code from the git repository. See [Bootstrapping your Puppet checkout](/doc/advanced-bootstrap.md) for details.
47+
48+
#### `:bootstrapped_to_dir` (String, Optional)
49+
50+
Directory that is already prepared ("bootstrapped") and can have Puppet run against its contents to compile the catalog.
51+
52+
Often this means that you have done the following to this directory:
53+
54+
- Checked out the necessary branch of Puppet code from your version control system
55+
- Installed any third party modules (e.g. with librarian-puppet or r10k)
56+
57+
#### `:enc` (String, Optional)
58+
59+
File path to your External Node Classifier.
60+
61+
See [Configuring octocatalog-diff to use ENC](/doc/configuration-enc.md) for details on using octocatalog-diff with an External Node Classifier.
62+
63+
#### `:fact_file` (String, Optional)
64+
65+
Path to the file that contains the facts for the node whose catalog you are going to compile.
66+
67+
Generally, must either specify the fact file, or [configure octocatalog-diff to use PuppetDB](/doc/configuration-puppetdb.md) to retrieve node facts.
68+
69+
#### `:hiera_config` (String, Optional)
70+
71+
Path to the Hiera configuration file (generally named `hiera.yaml`) for your Puppet installation. Please see [Configuring octocatalog-diff to use Hiera](/doc/configuration-hiera.md) for details on Hiera configuration.
72+
73+
#### `:hiera_path` (String, Optional)
74+
75+
Directory within your Puppet installation where Hiera data is stored. Please see [Configuring octocatalog-diff to use Hiera](/doc/configuration-hiera.md) for details on Hiera configuration.
76+
77+
If your Puppet setup is modeled after the [Puppet control repository template](https://github.com/puppetlabs/control-repo), the correct setting for `:hiera_path` is `'hieradata'`.
78+
79+
#### `:puppet_binary` (String, Required)
80+
81+
Path to the Puppet binary on your system.
82+
83+
Please refer to [Configuring octocatalog-diff to use Puppet](/doc/configuration-puppet.md) for details of connecting octocatalog-diff to your Puppet installation.
84+
85+
#### `:puppetdb_url` (String, Optional)
86+
87+
URL to PuppetDB. See [Configuring octocatalog-diff to use PuppetDB](/doc/configuration-puppetdb.md) for instructions on this setting, and other settings that may be needed in your environment.
88+
89+
#### `:to_branch` (String, Optional)
90+
91+
Branch name in git repository to use for Puppet code. This option must be used in conjunction with `:basedir` so that code can be located.
2192

2293
## Exceptions
94+
95+
The following exceptions may occur during the compilation of a catalog:
96+
97+
- `OctocatalogDiff::Errors::BootstrapError`
98+
99+
Bootstrapping failed.
100+
101+
- `OctocatalogDiff::Errors::CatalogError`
102+
103+
Catalog failed to compile. Please note that whenever possible, a `OctocatalogDiff::API::V1::Catalog` object is still constructed for a failed catalog, with `#valid?` returning false.
104+
105+
- `OctocatalogDiff::Errors::GitCheckoutError`
106+
107+
Git checkout failed.
108+
109+
- `OctocatalogDiff::Errors::PuppetVersionError`
110+
111+
The version of Puppet could not be determined, generally because the Puppet binary was not found, or does not respond as expected to `puppet version`.
112+
113+
- `OctocatalogDiff::Errors::ReferenceValidationError`
114+
115+
See [Catalog validation](/doc/advanced-catalog-validation.md).

doc/optionsref.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1092,4 +1092,12 @@ parameters are to be checked. (<a href="../lib/octocatalog-diff/cli/options/vali
10921092
</td>
10931093
</tr>
10941094

1095-
</table>
1095+
</table>
1096+
1097+
## Using these options in API calls
1098+
1099+
Most of these options can also be used when making calls to the [API](/doc/dev/api.md).
1100+
1101+
Generally, parameters for the API are named corresponding to the names of the command line parameters, with dashes (`-`) converted to underscores (`_`). For example, the command line option `--hiera-config` is passed to the API as the symbol `:hiera_config`.
1102+
1103+
Each of the options above has a link to the source file where it is declared, should you wish to review the specific parameter names and data structures that are being set.
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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]

lib/octocatalog-diff/catalog/computed.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,15 @@ def environment
8989
def cleanup_checkout_dir(checkout_dir, logger)
9090
return unless File.directory?(checkout_dir)
9191
logger.debug("Cleaning up temporary directory #{checkout_dir}")
92-
FileUtils.remove_entry_secure checkout_dir
92+
# Sometimes this seems to break when handling the recursive removal when running under
93+
# a parallel environment. Trap and ignore the errors here if we don't care about them.
94+
begin
95+
FileUtils.remove_entry_secure checkout_dir
96+
rescue Errno::ENOTEMPTY, Errno::ENOENT => exc
97+
# :nocov:
98+
logger.debug "cleanup_checkout_dir(#{checkout_dir}) logged #{exc.class} - this can be ignored"
99+
# :nocov:
100+
end
93101
end
94102

95103
# Private method: Bootstrap a directory

lib/octocatalog-diff/cli.rb

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# frozen_string_literal: true
22

3-
require_relative 'api/v1/catalog-compile'
4-
require_relative 'api/v1/catalog-diff'
3+
require_relative 'api/v1'
54
require_relative 'catalog-util/cached_master_directory'
65
require_relative 'errors'
76
require_relative 'util/catalogs'
@@ -111,8 +110,8 @@ def self.cli(argv = ARGV, logger = Logger.new(STDERR), opts = {})
111110
end
112111

113112
# Compile catalogs and do catalog-diff
114-
catalog_diff = OctocatalogDiff::API::V1::CatalogDiff.catalog_diff(options.merge(logger: logger))
115-
diffs = catalog_diff.diffs.map(&:raw)
113+
catalog_diff = OctocatalogDiff::API::V1.catalog_diff(options.merge(logger: logger))
114+
diffs = catalog_diff.diffs
116115

117116
# Display diffs
118117
printer_obj = OctocatalogDiff::Cli::Printer.new(options, logger)
@@ -159,19 +158,23 @@ def self.setup_logger(logger, options, argv_save)
159158
# Compile the catalog only
160159
def self.catalog_only(logger, options)
161160
opts = options.merge(logger: logger)
162-
to_catalog = OctocatalogDiff::API::V1::CatalogCompile.catalog(opts).raw
161+
to_catalog = OctocatalogDiff::API::V1.catalog(opts)
163162

164163
# If the catalog compilation failed, an exception would have been thrown. So if
165164
# we get here, the catalog succeeded. Dump the catalog to the appropriate place
166165
# and exit successfully.
167166
if options[:output_file]
168-
File.open(options[:output_file], 'w') { |f| f.write(to_catalog.catalog_json) }
167+
File.open(options[:output_file], 'w') { |f| f.write(to_catalog.to_json) }
169168
logger.info "Wrote catalog to #{options[:output_file]}"
170169
else
171-
puts to_catalog.catalog_json
170+
puts to_catalog.to_json
172171
end
173-
return [OctocatalogDiff::Catalog.new(backend: :noop), to_catalog] if options[:RETURN_DIFFS] # For integration testing
174-
EXITCODE_SUCCESS_NO_DIFFS
172+
173+
return EXITCODE_SUCCESS_NO_DIFFS unless options[:RETURN_DIFFS] # For integration testing
174+
# :nocov:
175+
dummy_catalog = OctocatalogDiff::API::V1::Catalog.new(OctocatalogDiff::Catalog.new(backend: :noop))
176+
[dummy_catalog, to_catalog]
177+
# :nocov:
175178
end
176179

177180
# --bootstrap-then-exit command

rake/templates/optionsref.erb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,11 @@ Usage: octocatalog-diff [command line options]
3434
</tr>
3535
<% end %>
3636
</table>
37+
38+
## Using these options in API calls
39+
40+
Most of these options can also be used when making calls to the [API](/doc/dev/api.md).
41+
42+
Generally, parameters for the API are named corresponding to the names of the command line parameters, with dashes (`-`) converted to underscores (`_`). For example, the command line option `--hiera-config` is passed to the API as the symbol `:hiera_config`.
43+
44+
Each of the options above has a link to the source file where it is declared, should you wish to review the specific parameter names and data structures that are being set.

0 commit comments

Comments
 (0)