|
| 1 | +[](https://travis-ci.com/lingua-pupuli/puppetfile-resolver) |
| 2 | + |
| 3 | +# Puppetfile Resolver |
| 4 | + |
| 5 | +The [Puppetfile](https://puppet.com/docs/pe/latest/puppetfile.html) is used by Puppet to manage the collection of modules used by a Puppet master. The Puppetfile is then used by tools like [R10K](https://github.com/puppetlabs/r10k) and [Code Manager](https://puppet.com/docs/pe/latest/code_mgr_how_it_works.html#how-code-manager-works) to download and install the required modules. |
| 6 | + |
| 7 | +However, the Puppetfile is designed to have explicit dependencies, that is, **all** modules and **all** of the dependencies must be specified in Puppetfile. This is very different to formats like `Gemfile` (Ruby) or `package.json` (NodeJS) where dependencies are brought in as needed. |
| 8 | + |
| 9 | +Using explicit dependencies is great in a configuration management system like Puppet, but it puts the burden on updates onto the user. |
| 10 | + |
| 11 | +This library includes all of the code to parse a Puppetfile and then calculate a dependency graph to try and resolve all of the module dependencies and versions. The resolver can also restrict on Puppet version, for example, only Modules which support Puppet 6. |
| 12 | + |
| 13 | +**Note** This is still in active development. Git history may be re-written until an initial version is released |
| 14 | + |
| 15 | +## To Do |
| 16 | + |
| 17 | +- Could do with more tests |
| 18 | +- Add YARD documentation |
| 19 | + |
| 20 | +## Why a library and not a CLI tool? |
| 21 | + |
| 22 | +Due to all of the different needs of tool developers, this is offered as a library instead of full blown CLI tool. For example, the needs of a VSCode Extensions developer are very different than that of the Puppet Developer Kit developer. |
| 23 | + |
| 24 | +Therefore this is a library which is intended to be used by tool developers to create useful tools for users, and not really for direct use by users. |
| 25 | + |
| 26 | +Note that a CLI is included (`puppetfile-cli.rb`) only as an example of how to create a tool using this library. |
| 27 | + |
| 28 | +## Architecture |
| 29 | + |
| 30 | +``` text |
| 31 | + +-----------------+ +-----------------+ +-----------------+ |
| 32 | + | Forge Searcher | | Github Searcher | | Local Searcher | |
| 33 | + +-------+---------+ +--------+--------+ +-------+---------+ |
| 34 | + | | | |
| 35 | + +----------------------+--------------------+ |
| 36 | + | |
| 37 | + | |
| 38 | + V |
| 39 | + +--------+ +----------+ +-------------------+ |
| 40 | +-- Text --> | Parser | -- Document Model -+-> | Resolver | -- Dependency Graph -+-> | Resolution Result | |
| 41 | + +--------+ | +----------+ | +-------------------+ |
| 42 | + | | |
| 43 | + | | |
| 44 | + V V |
| 45 | + +-----------+ +------------+ |
| 46 | + | Document | | Resolution | |
| 47 | + | Validator | | Validator | |
| 48 | + +-----------+ +------------+ |
| 49 | +``` |
| 50 | + |
| 51 | +### Puppetfile Parser |
| 52 | + |
| 53 | +The parser converts the content of a Puppetfile into a document model (`PuppetfileResolver::Puppetfile`). |
| 54 | + |
| 55 | +Currently only one Parser is available, `R10KEval`, which uses the same parsing logic as the [R10K Parser](https://github.com/puppetlabs/r10k/blob/master/lib/r10k/puppetfile.rb). In the future other parsers may be added, such as a [Ruby AST based parser](https://github.com/puppetlabs/r10k/pull/885). |
| 56 | + |
| 57 | +### Puppetfile Document Validation |
| 58 | + |
| 59 | +Even though a Puppetfile can be parsed, doesn't mean it's valid. For example, defining a module twice. |
| 60 | + |
| 61 | +### Puppetfile Resolution |
| 62 | + |
| 63 | +Given a Puppetfile document model, the library can attempt to recursively resolve all of the modules and their dependencies. The resolver be default will not be strict, that is, missing dependencies will not throw an error, and will attempt to also be resolved. When in strict mode, any missing dependencies will throw errors. |
| 64 | + |
| 65 | +### Module Searchers |
| 66 | + |
| 67 | +The Puppetfile resolution needs information about all of the available modules and versions, and does this through calling various Specification Searchers. Currently Puppet Forge, Github and Local FileSystem searchers are implemented. Additional searchers could be added, for example GitLab or SVN. |
| 68 | + |
| 69 | +The result is a dependency graph listing all of the modules, dependencies and version information. |
| 70 | + |
| 71 | +### Resolution validation |
| 72 | + |
| 73 | +Even though a Puppetfile can be resolved, doesn't mean it is valid. For example, missing module dependencies are not considered valid. |
| 74 | + |
| 75 | +### Dependency Graph |
| 76 | + |
| 77 | +The resolver uses the [Molinillo](https://github.com/CocoaPods/Molinillo) ruby gem for dependency resolution. Molinillo is used in Bundler, among other gems, so it's well used and maintained project. |
| 78 | + |
| 79 | +### Example workflow |
| 80 | + |
| 81 | +1. Load the contents of a Puppetfile from disk |
| 82 | + |
| 83 | +2. Parse the Puppetfile into a document model |
| 84 | + |
| 85 | +3. Verify that the document model is valid |
| 86 | + |
| 87 | +4. Create a resolver object with the document model, and the required Puppet version (optional) |
| 88 | + |
| 89 | +5. Start the resolution |
| 90 | + |
| 91 | +6. Validate the resolution against the document model |
| 92 | + |
| 93 | +### Example usage |
| 94 | + |
| 95 | +``` ruby |
| 96 | +puppetfile_path = '/path/to/Puppetfile' |
| 97 | + |
| 98 | +# Parse the Puppetfile into an object model |
| 99 | +content = File.open(puppetfile_path, 'rb') { |f| f.read } |
| 100 | +require 'puppetfile-resolver/puppetfile/parser/r10k_eval' |
| 101 | +puppetfile = ::PuppetfileResolver::Puppetfile::Parser::R10KEval.parse(content) |
| 102 | + |
| 103 | +# Make sure the Puppetfile is valid |
| 104 | +unless puppetfile.valid? |
| 105 | + puts 'Puppetfile is not valid' |
| 106 | + puppetfile.validation_errors.each { |err| puts err } |
| 107 | + exit 1 |
| 108 | +end |
| 109 | + |
| 110 | +# Create the resolver |
| 111 | +# - Use the document we just parsed (puppetfile) |
| 112 | +# - Don't restrict by Puppet version (nil) |
| 113 | +resolver = PuppetfileResolver::Resolver.new(puppetfile, nil) |
| 114 | + |
| 115 | +# Configure the resolver |
| 116 | +cache = nil # Use the default inmemory cache |
| 117 | +ui = nil # Don't output any information |
| 118 | +module_paths = [] # List of paths to search for modules on the local filesystem |
| 119 | +allow_missing_modules = true # Allow missing dependencies to be resolved |
| 120 | +opts = { cache: cache, ui: ui, module_paths: module_paths, allow_missing_modules: allow_missing_modules } |
| 121 | + |
| 122 | +# Resolve |
| 123 | +result = resolver.resolve(opts) |
| 124 | + |
| 125 | +# Output resolution validation errors |
| 126 | +result.validation_errors.each { |err| puts "Resolution Validation Error: #{err}\n"} |
| 127 | +``` |
| 128 | + |
| 129 | +## Known issues |
| 130 | + |
| 131 | +- The Forge module searcher will only use the internet facing forge ([https://forge.puppet.com](https://forge.puppet.com/)). Self-hosted forges are not supported |
| 132 | + |
| 133 | +- The Git module searcher will only search public Github based modules. Private repositories or other VCS systems are not supported |
0 commit comments